package com.ho.user.controller;

import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.PageVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.user.entity.SysDict;
import com.ho.user.api.entity.SysSubDict;
import com.ho.user.service.DictService;
import com.ho.user.vo.req.*;
import com.ho.user.vo.resp.DictRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author fancl
 * @desc: 字典Controller
 * @date 2022/8/18
 */
@RestController
@RequestMapping(ContextConstant.SYS + "dict")
@Api(tags = "系统模块-字典管理")
public class DictController {

    @Resource
    DictService dictService;


    @GetMapping("type/{type}")
    @ApiOperation(value = "查询字典类型接口(根据类型)")
    //@RequiresPermissions("sys:dept:add")
    // @LogAnnotation(title = "字典管理", action = "根据type查询字典类型")
    public DataResult<List<SysDict>> detailInfo(@PathVariable("type") String type) {
        DataResult<List<SysDict>> result = DataResult.success();
        result.setData(dictService.selectDictListByType(type));
        return result;
    }

    //
    @PostMapping("typeList")
    @ApiOperation(value = "查询字典列表接口(根据类型)")
    // @LogAnnotation(title = "字典管理",action = "根据类型查询字典")
    public DataResult<DictRespVo> dictList(@RequestBody @Valid DictSearchReqVO vo,HttpServletRequest request) {
        String lang =request.getHeader(CommonConstant.LANG);
        DataResult<DictRespVo> result = DataResult.success();
        DictTypeAndLabelReqVO typeAndLabelReqVO = new DictTypeAndLabelReqVO();
        typeAndLabelReqVO.setType(vo.getType());
        typeAndLabelReqVO.setLang(lang);
        result.setData(dictService.selectDict(typeAndLabelReqVO));
        return result;
    }

/*    @PostMapping("label")
    @ApiOperation(value = "查询字典列表接口(根据类型和标签)")
    @LogAnnotation(title = "字典管理",action = "根据类型和标签查询字典")
    public DataResult<DictRespVo> dictLabelList(@RequestBody @Valid DictTypeAndLabelReqVO vo) {
        DataResult<DictRespVo> result = DataResult.success();
        result.setData(dictService.selectDict(vo));
        return result;
    }*/

    @PostMapping("all/page")
    @ApiOperation(value = "分页查询字典列表接口")
    public DataResult<PageVO<DictRespVo>> dictList(@RequestBody @Valid DictPageReqVO vo) {
        DataResult<PageVO<DictRespVo>> result = DataResult.success();
        PageVO<DictRespVo> list = dictService.all(vo);
        result.setData(list);
        return result;
    }

    @PostMapping("typeAll")
    @ApiOperation(value = "查询所有字典列表接口")
    public DataResult<List<DictRespVo>> typeList(HttpServletRequest request) {
        String lang =request.getHeader(CommonConstant.LANG);
        DataResult<List<DictRespVo>> result = DataResult.success();
        result.setData(dictService.selectDictType(lang));
        return result;
    }

    @PostMapping("type")
    @ApiOperation(value = "新增字典类型接口")
    @LogAnnotation(title = "字典管理", action = "新增字典类型")
    public DataResult<SysDict> addDictType(@RequestBody @Valid DictAddTypeVO vo) {
        DataResult<SysDict> result = DataResult.success();
        //先判断是否有相同的type的值
        SysDict dict = dictService.selectDictByType(vo);
        if (dict != null) {
            throw new BusinessException(BaseResponseCode.DICT_ALREADY_EXISTS);
        }
        result.setData(dictService.addTypeDict(vo));
        return result;
    }


    @PostMapping
    @ApiOperation(value = "新增字典接口")
    @LogAnnotation(title = "字典管理", action = "新增字典")
    //@RequiresPermissions("sys:dict:add")
    public DataResult<SysSubDict> addDict(@RequestBody @Valid DictAddReqVO vo) {
        DataResult<SysSubDict> result = DataResult.success();
        //先判断是否有相同value的值
        SysSubDict subDict = dictService.selectSubDictByTV(vo.getType(), vo.getLabel());
        if (subDict != null) {
            throw new BusinessException(BaseResponseCode.SUB_DICT_ALREADY_EXISTS);
        }
        //先判断 键 值 是否有相同的值
        SysSubDict sysSubDict = new SysSubDict();
        sysSubDict.setLabel(vo.getLabel());
        sysSubDict.setType(vo.getType());
        //sysSubDict.setId(null);
        List<SysSubDict> subDictList = dictService.selectSubDictByKey(sysSubDict);
        if (!subDictList.isEmpty()) {
            throw new BusinessException(BaseResponseCode.DICT_EXISTENCE_EXISTENCE_KEY_VALUE);
        }
        //判断排序
        SysSubDict subDictSort = dictService.selectSubDictBySort(vo.getType(), vo.getSort());
        if (subDictSort != null) {
            throw new BusinessException(BaseResponseCode.SORT_CODE_BEEN_USED_PLEASE_CORRECT_SORT);
        }
        result.setData(dictService.addSubDict(vo));
        return result;
    }


/*    @DeleteMapping("id/{id}")
    @ApiOperation(value = "删除字典类型接口根据id")
    @LogAnnotation(title = "字典管理", action = "根据id删除字典类型")
    //@RequiresPermissions("sys:dict:deleted")
    public DataResult deletedById(@PathVariable("id") Integer id) {
        dictService.deletedById(id);
        return DataResult.success();
    }*/

/*    @DeleteMapping("type/{type}")
    @ApiOperation(value = "删除字典接口根据类型")
    @LogAnnotation(title = "字典管理", action = "根据类型删除字典")
    public DataResult deletedByType(@PathVariable("type") String type) {
        dictService.deletedByType(type);
        return DataResult.success();
    }*/

    @DeleteMapping("sub/{id}")
    @ApiOperation(value = "删除字典根据id")
    @LogAnnotation(title = "字典管理", action = "根据id删除字典")
    public DataResult deletedSubById(@PathVariable("id") Integer id) {
        dictService.deletedSubById(id);
        return DataResult.success();
    }


    @PutMapping("type")
    @ApiOperation(value = "更新字典类型接口")
    @LogAnnotation(title = "字典管理", action = "更新字典类型")
    //@RequiresPermissions("sys:dict:update")
    public DataResult updateDict(@RequestBody @Valid DictUpdateReqVO vo) {
        //提前判断是否数据库中存在相同的type值
        DictAddTypeVO dictAddTypeVo = new DictAddTypeVO();
        dictAddTypeVo.setType(vo.getType());
        dictAddTypeVo.setTypeName(vo.getTypeName());
        SysDict sysDict = dictService.selectDictByTypeAndName(dictAddTypeVo);
        if (sysDict != null) {
            throw new BusinessException(BaseResponseCode.DICT_ALREADY_EXISTS);
        }
        dictService.updateDict(vo);
        return DataResult.success();
    }

    @PutMapping("sub")
    @ApiOperation(value = "更新字典接口")
    @LogAnnotation(title = "字典管理", action = "更新字典")
    //@RequiresPermissions("sys:dict:update")
    public DataResult updateSubDict(@RequestBody @Valid DictSubUpdateReqVO vo) {
        //先判断 键 值 是否有相同的值
        SysSubDict sysSubDict = new SysSubDict();
        sysSubDict.setLabel(vo.getLabel());
        sysSubDict.setType(vo.getType());
        sysSubDict.setId(vo.getId());
        List<SysSubDict> subDictList = dictService.selectSubDictByKey(sysSubDict);
        if (!subDictList.isEmpty()) {
            throw new BusinessException(BaseResponseCode.DICT_EXISTENCE_EXISTENCE_KEY_VALUE);
        }
        dictService.updateSubDict(vo);
        return DataResult.success();
    }

}
