package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.DictDto;
import com.zcxk.entity.Dict;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.DictService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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;
import java.util.stream.Collectors;

/**
 * 数据字典(Dict)控制层
 *
 * @author yjy
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/dict")
@Api(tags = "字典管理")
public class DictController {
    /**
     * 服务对象
     */
    @Autowired
    private DictService dictService;

    /**
     * 通过主键查询单条数据
     *
     * @param dict 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询单个数据字典配置列表")
    public AjaxMessage<Dict> selectOne(
            @ApiParam(value = "数据字典配置", required = true) @RequestBody Dict dict) {
        Dict result = dictService.selectById(dict.getId());

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增一条数据
     *
     * @param dict 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入数据字典配置列表")
    public AjaxMessage<Integer> insert(@ApiParam(value = "数据字典配置", required = true) @RequestBody Dict dict) {
        if(dictService.countByCodePid(dict)>0)
        {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_DICTCODE_ERROR);
        }
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            dict.setCreateBy(currentUser.getUsername());
        }
        dictService.insert(dict);
        int result =dict.getId();//返回新增数据的id
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param dict 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "修改数据字典配置列表")
    public AjaxMessage<Integer> update(@ApiParam(value = "数据字典配置", required = true) @RequestBody Dict dict) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            dict.setUpdateBy(currentUser.getUsername());
        }
        int result = dictService.update(dict);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }

    /**
     * 删除一条数据
     *
     * @param ids
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除数据字典配置列表")
    public AjaxMessage<Integer> delete(@ApiParam(value = "字典ids", required = true) @RequestBody Long[] ids) {
        int result = dictService.deleteById(ids);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询数据字典配置列表")
    public AjaxMessage<Pagination<Dict>> selectPage(@ApiParam(value = "页数", required = true) @RequestParam Integer pageNum,
                                                    @ApiParam(value = "条数", required = true) @RequestParam Integer pageSize,
                                                    @ApiParam(value = "查询关键字", required = false)@RequestParam(required = false) String keyWords,
                                                    @ApiParam(value = "父配置ID", required = false)@RequestParam(required = false) Integer parentDictId) {
        Dict dict = new Dict();
        dict.setDictCode(keyWords);
        dict.setParentDictId(parentDictId);
        IPage<Dict> iPage = new Page<>(pageNum, pageSize);
        iPage = dictService.selectPage(dict, iPage);
        Pagination<Dict> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    /**
     * 通过父配置id查询数据
     *
     * @param dict 参数对象
     * @return 数据列表
     */
    @RequestMapping(value = "selectList", method = RequestMethod.POST)
    @ApiOperation(value = "查询父配置字典数据")
    public AjaxMessage<List<Dict>> selectList(
            @ApiParam(value = "父配置ID和查询条件", required = true) @RequestBody Dict dict) {
        List<Dict> result = dictService.selectList(dict);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 根据备注查询字典列表
     *
     * @param remark 备注
     * @return 数据列表
     */
    @RequestMapping(value = "selectParentList", method = RequestMethod.GET)
    @ApiOperation(value = "根据备注查询字典列表")
    public AjaxMessage<List<DictDto>> selectParentList(
            @ApiParam(value = "备注", required = true)@RequestParam String remark) {
        Dict dict=new Dict();
        dict.setRemark(remark);
        List<DictDto> result = dictService.selectParentList(dict);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 通过父配置id查询数据
     *
     * @param
     * @return 数据列表
     */
    @RequestMapping(value = "selectListByPCodes", method = RequestMethod.GET)
    @ApiOperation(value = "查询父配置字典数据")
    public Map<String,String> selectListByPCodes(
            @ApiParam(value = "父字典编码.以逗号隔开", required = true)@RequestParam String parentDictCodes) {
        Map<String,String> result=new HashMap<>();
        for(String parentDictCode:parentDictCodes.split(",")){
            Dict dict=new Dict();
            dict.setParentDictCode(parentDictCode);
            result.putAll(dictService.selectList(dict).stream().collect(Collectors.toMap(Dict::getDictCode, Dict::getDictValue)));
        }
        return result;
    }
}