package com.sckw.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.web.response.HttpResult;
import com.sckw.system.model.SysDict;
import com.sckw.system.model.SysDictType;
import com.sckw.system.model.vo.req.QueryDictTypePageReqVo;
import com.sckw.system.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典
 *
 * @author zk
 * @date 2023-05-30
 */
@RestController
@RequestMapping("/sysDict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * @param params {type:字典类型、name：类型名称}
     * @return
     * @description 新增
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/addType")
    public HttpResult addType(@RequestBody SysDictType params) {
        sysDictService.addType(params);
        return HttpResult.ok();
    }

    /**
     * @param params {id：主键ID、type:字典类型、name：类型名称}
     * @return
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/updateType")
    public HttpResult updateType(@RequestBody SysDictType params) {
        sysDictService.updateType(params);
        return HttpResult.ok();
    }

    /**
     * @param params {pageNum：页数、pageSize：每页条数、。。。}
     * @return
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/select")
    public HttpResult findPage(@RequestBody HashMap params) {
        // 设置分页参数
        PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        List<SysDict> list = sysDictService.findPage(params);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @param id 主键ID
     * @return
     * @description 根据主键查询
     * @author zk
     * @date 2023/5/30
     **/
    @GetMapping("/detail")
    public HttpResult selectByKey(Long id) {
        SysDict sysDict = sysDictService.selectByKey(id);
        return HttpResult.ok(sysDict);
    }

    /**
     * @param params {type:字典类型}
     * @return
     * @description 查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/findList")
    public HttpResult findList(@RequestBody HashMap params) {
        return HttpResult.ok(sysDictService.findList(params));
    }

    /**
     * @param params {type:字典类型}
     * @return HttpResult
     * @description 查询字典树
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/findDictTree")
    public HttpResult findList(@RequestBody SysDict params) {
        return HttpResult.ok(sysDictService.findDictTree(params.getType(), params.getValue()));
    }

    /**
     * @param params {type:字典类型、value：字典值、label：字典名称、description：描述、sort排序序号}
     * @return
     * @description 新增
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/add")
    public HttpResult add(@RequestBody SysDict params) {
        sysDictService.add(params);
        return HttpResult.ok();
    }

    /**
     * @param params {id：主键ID、type:字典类型、value：字典值、label：字典名称、description：描述、sort排序序号}
     * @return
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/update")
    public HttpResult update(@RequestBody SysDict params) {
        sysDictService.update(params);
        return HttpResult.ok();
    }

    /**
     * @param ids {ids：主键ID（多个以逗号隔开）}
     * @return
     * @description 更新
     * @author zk
     * @date 2023/5/30
     **/
    @DeleteMapping("/dels")
    public HttpResult del(@RequestParam String ids) {
        sysDictService.deleteByKey(ids);
        return HttpResult.ok();
    }

    /**
     * @param reqVo 入参
     * @desc: 分页查询字典类型
     * @author: czh
     * @date: 2023/7/6
     */
    @PostMapping("queryDictTypePage")
    public HttpResult queryDictTypePage(@RequestBody QueryDictTypePageReqVo reqVo) {
        return HttpResult.ok(sysDictService.queryDictTypePage(reqVo));
    }

    /**
     * @param reqVo 入参
     * @desc: 分页查询字典类型
     * @author: czh
     * @date: 2023/7/6
     */
    @PostMapping("queryDictType")
    public HttpResult queryDictType(@RequestBody QueryDictTypePageReqVo reqVo) {
        return HttpResult.ok(sysDictService.queryDictType(reqVo));
    }

}
