package com.codvision.base.controller.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.entity.SysDict;
import com.codvision.base.entity.SysDictItem;
import com.codvision.base.entity.vo.SysDictItemVO;
import com.codvision.base.entity.vo.SysDictVO;
import com.codvision.base.service.ISysDictItemService;
import com.codvision.base.service.ISysDictService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 系统字典 管理模块
 *
 * @author lingee
 * @date 2023-11-09
 */
@RestController
@Api(tags = "平台管理端-系统字典管理")
@RequestMapping("/v1/manager/sysDict")
public class ManagerSysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ISysDictItemService sysDictItemService;

    @PostMapping("/findAll")
    @ApiOperation(value = "字典列表", notes = "字典列表接口")
    @RequiresPermissions("sys_dict_list")
    public ResponseEntity<IPage<SysDictVO>> findAll(
            @ApiParam(value = "关键字(dictType,description)") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否系统内置（0:否, 1:是）") @RequestParam(required = false) Integer systemFlag,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        ResponseEntity<IPage<SysDictVO>> responseEntity = new ResponseEntity<>();

        SysDict queryBean = new SysDict();
        queryBean.setKeyword(keyword);
        queryBean.setSystemFlag(systemFlag);
        QueryPage<SysDict> queryPage = new QueryPage<>(queryBean, page, size);

        IPage<SysDictVO> pageResult = sysDictService.getPageResult(queryPage);
        return responseEntity.data(pageResult);
    }


    @PostMapping("/addDict")
    @ApiOperation(value = "添加字典", notes = "添加字典接口")
    @RequiresPermissions("sys_dict_add")
    public ResponseEntity<String> addDict(@Valid @RequestBody SysDict sysDict) {
        sysDictService.addDict(sysDict);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/updateDict")
    @ApiOperation(value = "更新字典", notes = "更新字典接口")
    @RequiresPermissions("sys_dict_edit")
    public ResponseEntity<String> updateDict(@RequestBody SysDict sysDict) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (null == sysDict.getId()) {
            throw new BusinessException(30002);
        }
        sysDictService.updateDict(sysDict);
        return responseEntity.ok();
    }

    @PostMapping("/removeDict")
    @ApiOperation(value = "删除字典", notes = "删除字典接口")
    @RequiresPermissions("sys_dict_del")
    public ResponseEntity<String> removeDict(@RequestParam(value = "id") Integer id) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        sysDictService.removeDict(id);
        return responseEntity.ok();
    }

    @PostMapping("/getItemList")
    @ApiOperation(value = "字典项列表", notes = "字典项列表接口")
    @RequiresPermissions("sys_dict_item_list")
    public ResponseEntity<IPage<SysDictItemVO>> getListByDictId(@RequestParam(required = false) Integer dictId,
                                                                @RequestParam(required = false) String label,
                                                                @RequestParam(required = false) String itemValue,
                                                                @RequestParam(required = false) String dictType,
                                                                @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                                @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        SysDictItem queryBean = new SysDictItem();
        queryBean.setDictId(dictId);
        queryBean.setLabel(label);
        queryBean.setItemValue(itemValue);
        queryBean.setDictType(dictType);

        QueryPage<SysDictItem> queryPage = new QueryPage<>(queryBean, page, size);
        IPage<SysDictItemVO> pageResult = sysDictItemService.getPageResult(queryPage);

        return new ResponseEntity<IPage<SysDictItemVO>>().ok().data(pageResult);
    }

    @PostMapping("/addDictItem")
    @ApiOperation(value = "添加字典项", notes = "添加字典项接口")
    @RequiresPermissions("sys_dict_item_add")
    public ResponseEntity<SysDictItem> addDictItem(@Validated @RequestBody SysDictItem sysDictItem) {
        return new ResponseEntity<SysDictItem>().data(sysDictItemService.addDictItem(sysDictItem));
    }

    @PostMapping("/updateDictItem")
    @ApiOperation(value = "更新字典项", notes = "更新字典项接口")
    @RequiresPermissions("sys_dict_item_edit")
    public ResponseEntity<SysDictItem> updateDictItem(@RequestBody SysDictItem sysDictItem) {
        ResponseEntity<SysDictItem> responseEntity = new ResponseEntity<>();
        if (null == sysDictItem.getId()) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(sysDictItemService.updateDictItem(sysDictItem));
    }

    @PostMapping("/removeDictItem")
    @ApiOperation(value = "删除字典项", notes = "删除字典项接口")
    @RequiresPermissions("sys_dict_item_del")
    public ResponseEntity<String> removeDictItem(@RequestParam(value = "id") Integer id) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        sysDictItemService.removeDictItem(id);
        return responseEntity.ok();
    }
}
