package cn.pluss.platform.controller.dict;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.exception.MsgException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

@Controller
@RequiredArgsConstructor
@RequestMapping("dict")
public class DictController extends BaseModelController<DictService, Dict> {

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index() {
        return "dict/dict";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detailDict/{id}", "/detailDict"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            Dict dictOne = baseService.getById(id);
            request.setAttribute("entity", dictOne);
        }
        return "dict/detailDict";
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<Dict> saveOrUpdate(@RequestBody Dict dict) {
        Calendar calendar=Calendar.getInstance();
        if (dict.getId() != null){
            dict.setUpdateTime(calendar.getTime());
            UpdateWrapper<Dict> dictUpdateWrapper=new UpdateWrapper<>();
            dictUpdateWrapper.eq("id",dict.getId());
            baseService.update(dict,dictUpdateWrapper);
        }else {
            dict.setCreateTime(calendar.getTime());
            dict.setUpdateTime(calendar.getTime());
            baseService.save(dict);
        }
        return ResultGenerator.genSuccessResult("保存成功", dict);
    }

    @PostMapping("/delete")
    public Result<Dict> del(@RequestBody Dict dict) {
        baseService.removeById(dict.getId());
        return ResultGenerator.genSuccessResult("删除成功", dict);
    }

    @GetMapping("/tree")
    @ResponseBody
    public Result<List<Dict>> tree() {
        List<Dict> result = baseService.tree();
        return ResultGenerator.genSuccessResult(result);
    }

    @GetMapping("/getGroup/{code}")
    @ResponseBody
    public Result<List<Dict>> getGroupByCode(@PathVariable(name = "code") String code) {
        Dict dict = new Dict().setCode(code);
        dict = baseService.getOne(new QueryWrapper<>(dict));

        if (dict == null) {
            throw new MsgException("对应code的字典项不存在");
        }

        Dict queryParent = (Dict) new Dict().setPId(dict.getId());
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(queryParent);
        queryWrapper.orderBy(true,true, "sort");
        List<Dict> list = baseService.list(queryWrapper);

        return ResultGenerator.genSuccessResult(list);
    }
}
