package com.njcn.system.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.DeptConfigParam;
import com.njcn.system.pojo.vo.DeptConfigVO;
import com.njcn.system.service.DeptMapService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date: 2022/08/04 10:01
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/deptMap")
@Api(tags = "部门与地图关联配置")
@AllArgsConstructor
public class DeptMapController extends BaseController {

    private final DeptMapService deptMapService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getDeptMapConfig")
    @ApiOperation("查询地图与部门关联配置列表")
    public HttpResult<List<DeptConfigVO>> getDeptMapConfig() {
        String methodDescribe = getMethodDescribe("getDeptMapConfig");
        LogUtil.njcnDebug(log, "{}", methodDescribe, methodDescribe);
        List<DeptConfigVO> result = deptMapService.getDeptMapConfig();
        if (Objects.isNull(result)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getDeptMapConfigById")
    @ApiOperation("根据id查询地图与部门关联配置")
    @ApiImplicitParam(name = "id", value = "参数id", required = true)
    public HttpResult<DeptConfigVO> getDeptMapConfigById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getDeptMapConfigById");
        LogUtil.njcnDebug(log, "{}", methodDescribe, id);
        DeptConfigVO result = deptMapService.getDeptMapConfigById(id);
        if (Objects.isNull(result)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getConfigByDeptId")
    @ApiOperation("根据部门id查询地图关联配置")
    @ApiImplicitParam(name = "deptId", value = "部门id", required = true)
    public HttpResult<DeptConfigVO> getConfigByDeptId(@RequestParam("deptId") String deptId) {
        String methodDescribe = getMethodDescribe("getConfigByDeptId");
        LogUtil.njcnDebug(log, "{}", methodDescribe, deptId);
        DeptConfigVO result = deptMapService.getConfigByDeptId(deptId);
        if (Objects.isNull(result)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/addDeptMapConfig")
    @ApiOperation("新增地图与部门关联配置")
    @ApiImplicitParam(name = "deptConfigParam", value = "新增配置实体", required = true)
    public HttpResult<Object> addDeptMapConfig(@RequestBody @Validated DeptConfigParam deptConfigParam) {
        String methodDescribe = getMethodDescribe("addDeptMapConfig");
        LogUtil.njcnDebug(log, "{}", methodDescribe, deptConfigParam);
        boolean res = deptMapService.addDeptMapConfig(deptConfigParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateDeptMapConfigById")
    @ApiOperation("修改地图与部门关联配置")
    @ApiImplicitParam(name = "deptConfigUpdateParam", value = "修改配置实体", required = true)
    public HttpResult<Object> updateDeptMapConfigById(@RequestBody @Validated DeptConfigParam.DeptConfigUpdateParam deptConfigUpdateParam) {
        String methodDescribe = getMethodDescribe("updateDeptMapConfigById");
        LogUtil.njcnDebug(log, "{}", methodDescribe, deptConfigUpdateParam);
        boolean res = deptMapService.updateDeptMapConfigById(deptConfigUpdateParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.ID_NOT_EXIST);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @GetMapping("/removeDeptMapConfigById")
    @ApiOperation("根据id删除地图与部门关联配置")
    @ApiImplicitParam(name = "id", value = "参数id", required = true)
    public HttpResult<Object> removeDeptMapConfigById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("removeDeptMapConfigById");
        LogUtil.njcnDebug(log, "{}", methodDescribe, id);
        boolean res = deptMapService.removeDeptMapConfigById(id);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.ID_NOT_EXIST);
        }
    }


}
