package com.izouma.nineth.web;

import cn.hutool.core.collection.CollUtil;
import com.izouma.nineth.domain.Setting;
import com.izouma.nineth.service.CacheService;
import com.izouma.nineth.service.SettingService;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.SettingRepo;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/setting")
@AllArgsConstructor
public class SettingController extends BaseController {
    private SettingService settingService;
    private SettingRepo    settingRepo;
    private CacheService   cacheService;

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Setting save(@RequestBody Setting record) {
        if (record.getId() != null) {
            Setting orig = settingRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            orig = settingRepo.save(orig);
            cacheService.clearSettingList(orig.getFlag());
            return orig;
        }

        if (record.getParent() == null) {
            record.setFlag(settingRepo.nextSort());
        }
        record = settingRepo.save(record);
        cacheService.clearSettingList(record.getFlag());
        return record;
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<Setting> all(@RequestBody PageQuery pageQuery) {
        return settingService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public Setting get(@PathVariable Long id) {
        return settingRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        settingRepo.softDelete(id);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Setting> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @PostMapping("/allList")
    public List<Setting> allList() {
        return settingService.getTree(settingRepo.findAll());
    }

    @Cacheable(value = "settingList", key = "#flag")
    @PostMapping("/byFlag")
    public List<Setting> byFlag(int flag) {
        List<Setting> tree = settingService.getTree(settingRepo.findAllByFlag(flag));
        if (CollUtil.isEmpty(tree)) {
            return null;
        }
        return tree.get(0).getChildren();
    }

}

