package com.njcn.product.system.elequality.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;

import com.njcn.product.system.elequality.pojo.dto.EpdDTO;
import com.njcn.product.system.elequality.pojo.param.EleEpdPqdParam;
import com.njcn.product.system.elequality.pojo.po.EleEpdPqd;
import com.njcn.product.system.elequality.pojo.vo.EleEpdPqdListVO;
import com.njcn.product.system.elequality.pojo.vo.EleEpdPqdVO;
import com.njcn.product.system.elequality.pojo.vo.EleEpdTreeVO;
import com.njcn.product.system.elequality.service.IEleEpdPqdService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.asn1.x509.Targets;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xuyang
 * @since 2023-05-24
 */
@Slf4j
@RestController
@RequestMapping("/csDictData")
@RequiredArgsConstructor
@Api(tags = "指标数据字典")
@Validated
public class EleEpdPqdController extends BaseController {

    private final IEleEpdPqdService eleEpdPqdService;

    @PostMapping("/addByModel")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据模板录入字典数据")
    @ApiImplicitParam(name = "eleEpdPqdParam", value = "模板的字典数据", required = true)
    @ApiIgnore
    public HttpResult<String> addByModel(@RequestBody @Validated List<EleEpdPqdParam> eleEpdPqdParam){
        log.info("根据模板录入字典数据");
        String methodDescribe = getMethodDescribe("addByModel");
        LogUtil.njcnDebug(log, "{}，模板当前解析字典数据为:", methodDescribe);
        eleEpdPqdService.saveData(eleEpdPqdParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @PostMapping("/add")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("新增字典数据")
    @ApiImplicitParam(name = "eleEpdPqdParam", value = "字典数据", required = true)
    public HttpResult<EleEpdPqd> add(@RequestBody @Validated EleEpdPqdParam eleEpdPqdParam){
        log.info("录入字典数据");
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，模板当前解析字典数据为:", methodDescribe);
        EleEpdPqd eleEpdPqd = eleEpdPqdService.add(eleEpdPqdParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqd, methodDescribe);
    }

    @PostMapping("/delete")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("删除字典数据")
    @ApiImplicitParam(name = "id", value = "字典数据id", required = true)
    public HttpResult<String> delete(@RequestParam String id){
        log.info("删除字典数据");
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，字典id为:", methodDescribe);
        eleEpdPqdService.delete(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @PostMapping("/update")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更新字典数据")
    @ApiImplicitParam(name = "updateParam", value = "字典数据", required = true)
    public HttpResult<String> update(@RequestBody @Validated EleEpdPqdParam.EleEpdPqdUpdateParam updateParam){
        log.info("更新字典数据");
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典数据为:", updateParam);
        eleEpdPqdService.update(updateParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询模板列表分页")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<EleEpdPqdVO>> getList(@RequestBody @Validated EleEpdPqdParam.EleEpdPqdQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("getList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<EleEpdPqdVO> list = eleEpdPqdService.eleEpdPqdList(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/dictMarkByDataType")
    @ApiOperation("字典数据组装唯一标识")
    @ApiImplicitParam(name = "dataType", value = "数据模型", required = true)
    @ApiIgnore
    public HttpResult<List<EleEpdPqd>> dictMarkByDataType(@RequestParam("dataType") @Validated String dataType) {
        String methodDescribe = getMethodDescribe("dictMarkByDataType");
        List<EleEpdPqd> list = eleEpdPqdService.dictMarkByDataType(dataType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/addEvt")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("事件录入字典数据")
    @ApiImplicitParam(name = "eleEpdPqdParam", value = "模板的字典数据", required = true)
    @ApiIgnore
    public HttpResult<Map<String,String>> addEvt(@RequestBody @Validated List<EleEpdPqdParam> eleEpdPqdParam){
        log.info("根据模板录入字典数据");
        String methodDescribe = getMethodDescribe("addEvt");
        LogUtil.njcnDebug(log, "{}，模板当前解析字典数据为:", methodDescribe);
        Map<String,String> map = eleEpdPqdService.saveEvt(eleEpdPqdParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, map, methodDescribe);
    }

    @PostMapping("/selectById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id查询字典数据")
    @ApiImplicitParam(name = "id", value = "字典id", required = true)
    public HttpResult<EleEpdPqd> selectById(@RequestParam("id") @Validated String id){
        log.info("根据id查询字典数据");
        String methodDescribe = getMethodDescribe("selectById");
        LogUtil.njcnDebug(log, "{}，根据id查询字典数据:", methodDescribe);
        EleEpdPqd eleEpdPqd = eleEpdPqdService.selectById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqd, methodDescribe);
    }
    @PostMapping("/selectAll")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询指标库，按datatype分类")
    public HttpResult<List<EleEpdPqdListVO>> selectAll(){
        log.info("查询指标库，按datatype分类");
        String methodDescribe = getMethodDescribe("selectAll");
        LogUtil.njcnDebug(log, "{}，查询指标库，按datatype分类:", methodDescribe);
        List<EleEpdPqdListVO> eleEpdPqds = eleEpdPqdService.selectAll();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqds, methodDescribe);
    }

    @PostMapping("/selectByIds")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据集合查询字典数据")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true)
    public HttpResult<List<EleEpdPqd>> selectByIds(@RequestBody @Validated List<String> ids){
        String methodDescribe = getMethodDescribe("selectByIds");
        LogUtil.njcnDebug(log, "{}，根据集合查询字典数据:", methodDescribe);
        List<EleEpdPqd> eleEpdPqds = eleEpdPqdService.selectByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqds, methodDescribe);
    }

    @PostMapping("/selectByClassId")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据数据分类查询字典数据")
    public HttpResult<List<EleEpdPqd>> selectByClassId(@RequestParam("classId") @Validated String classId){
        String methodDescribe = getMethodDescribe("selectByClassId");
        LogUtil.njcnDebug(log, "{}，根据数据分类查询字典数据:", methodDescribe);
        List<EleEpdPqd> eleEpdPqds = eleEpdPqdService.selectByClassId(classId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqds, methodDescribe);
    }

    @PostMapping("/judgeExist")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("校验字典是否存在")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "指标名称", required = true),
        @ApiImplicitParam(name = "dataType", value = "数据模型", required = true)
    })
    public HttpResult<List<EleEpdPqd>> judgeExist(@RequestParam("name") @Validated String name, @RequestParam("dataType") @Validated String dataType){
        String methodDescribe = getMethodDescribe("judgeExist");
        LogUtil.njcnDebug(log, "{}，校验字典是否存在:", methodDescribe);
        List<EleEpdPqd> list = eleEpdPqdService.judgeExist(name,dataType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/findByParam")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据条件查询字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "指标名称", required = true),
            @ApiImplicitParam(name = "dataType", value = "数据模型", required = true),
            @ApiImplicitParam(name = "phase", value = "相别", required = true)
    })
    public HttpResult<EleEpdPqd> findByParam(@RequestParam("name") @Validated String name, @RequestParam("dataType") @Validated String dataType, @RequestParam("phase") @Validated String phase){
        String methodDescribe = getMethodDescribe("findByParam");
        LogUtil.njcnDebug(log, "{}，根据条件查询字典数据:", methodDescribe);
        EleEpdPqd eleEpdPqd = eleEpdPqdService.findByParam(name,dataType,phase);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eleEpdPqd, methodDescribe);
    }

    @PostMapping("/findAll")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询所有字典数据")
    @ApiIgnore
    public HttpResult<List<EpdDTO>> findAll(){
        String methodDescribe = getMethodDescribe("findAll");
        List<EpdDTO> list = eleEpdPqdService.findAll();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @PostMapping("/findByName")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据名称查询字典信息")
    @ApiImplicitParam(name = "name", value = "指标名称", required = true)
    @ApiIgnore
    public HttpResult<EleEpdPqd> findByName(@RequestParam("name") @Validated String name){
        String methodDescribe = getMethodDescribe("findByName");
        EleEpdPqd po = eleEpdPqdService.lambdaQuery().eq(EleEpdPqd::getName,name).one();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, po, methodDescribe);
    }

    @PostMapping("/findListByShowName")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据名称查询字典信息集合")
    @ApiImplicitParam(name = "name", value = "指标名称", required = true)
    @ApiIgnore
    public HttpResult<List<EleEpdPqd>> findListByShowName(@RequestParam("name") @Validated String name){
        String methodDescribe = getMethodDescribe("findListByShowName");
        List<EleEpdPqd> po = eleEpdPqdService.lambdaQuery().eq(EleEpdPqd::getShowName,name).list();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, po, methodDescribe);
    }

    /**
     * 获取电能质量指标模板树
     * @author cdf
     * @date 2022/8/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/eleEpdChooseTree")
    @ApiOperation("获取电能质量指标模板树")
    public HttpResult<List<EleEpdTreeVO>> eleEpdChooseTree(){
        String methodDescribe = getMethodDescribe("eleEpdChooseTree");
        List<EleEpdTreeVO> res = eleEpdPqdService.eleEpdChooseTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }

}

