package cn.common.api;


import cn.common.common.anon.ApiLog;
import cn.common.interaction.req.dict.BusinessDictAddReq;
import cn.common.interaction.req.dict.BusinessDictPageReq;
import cn.common.interaction.req.dict.BusinessDictUpdateReq;
import cn.common.interaction.resp.base.ApiResponse;
import cn.common.interaction.resp.dict.PlatformDictResp;
import cn.common.interaction.resp.page.Pagination;
import cn.common.service.PlatformDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @projectName health-platform-api
 * @Description: 系统字典前端控制器
 * @date 2019-08-28
 */
@RestController
@RequestMapping("api/v1/platformDict")
public class PlatformDictController extends BaseApiController {

    @Resource
    private PlatformDictService platformDictService;


    /**
     * 分页查询系统业务字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param pageReq 分页查询系统业务字典Req
     * @return ApiResponse
     */
    @PostMapping("/queryByPage")
    @RequiresPermissions("businessDictionary:itemList")
    @ApiLog(value = "分页查询系统业务字典信息")
    public ApiResponse<Pagination<PlatformDictResp>> queryByPage(@RequestBody @Valid
                                                                               BusinessDictPageReq pageReq){
        return apiResponse(platformDictService.queryByPage(pageReq));
    }

    /**
     * 新增系统字典参数
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param addReq 新增系统字典参数请求Req
     * @return ApiResponse
     */
    @PostMapping("/addItem")
    @RequiresPermissions("businessDictionary:addItem")
    @ApiLog(value = "新增系统字典参数")
    public ApiResponse addItem(@RequestBody @Valid BusinessDictAddReq addReq){
        platformDictService.addItem(addReq);
        return apiResponse();
    }

    /**
     * 系统字典更新
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param updateReq 系统字典更新Req
     * @return ApiResponse
     */
    @PostMapping("/updateItem")
    @RequiresPermissions("businessDictionary:updateItem")
    @ApiLog(value = "系统字典更新")
    public ApiResponse updateItem(@RequestBody @Valid BusinessDictUpdateReq updateReq){
        platformDictService.updateItem(updateReq);
        return apiResponse();
    }

    /**
     * 根据系统业务字典主键ID系统字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2020-07-07
     * @param mainIdList 系统字典主键ID集合
     * @return ApiResponse
     */
    @DeleteMapping("/batchDeleteItem/{mainIdList}")
    @RequiresPermissions("businessDictionary:batchDelete")
    @ApiLog(value = "根据系统业务字典主键ID系统字典信息")
    public ApiResponse deleteByMainId(@PathVariable(value = "mainIdList") String mainIdList){
        platformDictService.deleteByMainId(Arrays.asList(mainIdList.split(",")));
        return apiResponse();
    }

    /**
     * 根据字典分类查询字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/1/21
     * @param dictType 字段类型
     * @return List
     */
    @GetMapping(value = "/queryDictionaryByType")
    public ApiResponse<List<PlatformDictResp>> queryDictionaryByType(@RequestParam(name = "dictType") String dictType){
        return apiResponse(platformDictService.queryDictionaryByType(dictType));
    }

    /**
     * 根据字典值模糊查询字典信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/1/21
     * @param dictValue 字段值
     * @return List
     */
    @GetMapping(value = "/queryDictionaryByValue")
    public ApiResponse<List<PlatformDictResp>> queryDictionaryByValue(@RequestParam(name = "dictValue") String dictValue){
        return apiResponse(platformDictService.queryDictionaryByValue(dictValue));
    }

}
