package com.njcn.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.EventTemplateParam;
import com.njcn.system.pojo.po.EventTemplate;
import com.njcn.system.pojo.vo.EventReportDictVO;
import com.njcn.system.pojo.vo.EventTemplateVO;
import com.njcn.system.service.IEventTemplateService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * pqs
 *
 * @author hany
 * @date 2022/9/7
 */
@Slf4j
@RestController
@RequestMapping("/EventTemplate")
@Api(tags = "报告模板管理")
@RequiredArgsConstructor
@Validated
public class EventTemplateController extends BaseController{

    private final IEventTemplateService iEventTemplateService;


    /**
     * 查询所有模板
     * @author hany
     * @date 2022/09/09
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getList")
    @ApiOperation("查询所有模板")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<EventTemplateVO>> getList(@RequestBody @Validated EventTemplateParam.EventTemplateQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("getList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<EventTemplateVO> res = iEventTemplateService.getList(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }

    /**
     * 根据id查询模板
     * @author hany
     * @date 2022/09/26
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getTemplateById")
    @ApiOperation("查询模板数据")
    @ApiImplicitParam(name = "id",value = "模板id",required = true)
    public HttpResult<EventTemplate> getTemplateById(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("getTemplateById");
        EventTemplate eventTemplate = iEventTemplateService.getTemplateById(id);
        if (Objects.nonNull(eventTemplate)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eventTemplate, methodDescribe);
        }
        else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
    }

    /**
     * 新增模板
     * @author hany
     * @date 2022/09/09
     */
    @PostMapping("/add")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("新增模板")
    @ApiImplicitParam(name = "eventTemplateParam", value = "模板实体", required = true)
    public HttpResult<Object> add(@RequestBody @Validated EventTemplateParam eventTemplateParam){
        String methodDescribe = getMethodDescribe("add");
        boolean result = iEventTemplateService.add(eventTemplateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 修改模板
     * @author hany
     * @date 2022/09/08
     */
    @PostMapping("/update")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.UPDATE)
    @ApiOperation("修改模板")
    @ApiImplicitParam(name = "eventUpdateParam", value = "模板更新实体", required = true)
    public HttpResult<Object> update(@RequestBody @Validated EventTemplateParam.EventTemplateUpdateParam eventUpdateParam){
        String methodDescribe = getMethodDescribe("update");
        boolean result = iEventTemplateService.update(eventUpdateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 删除模板
     * @param ids 暂态报表模板id
     * @author hany
     * @date 2022/09/08
     */
    @PostMapping("/delete")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除模板")
    @ApiImplicitParam(name = "ids", value = "模板索引", required = true)
    public HttpResult<Object> delete(@RequestBody List<String> ids){
        String methodDescribe = getMethodDescribe("delete");
        boolean result = iEventTemplateService.delete(ids);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 根据模板id查询关系
     * @param ids id集合
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectRelevance")
    @ApiOperation("根据模板id查询关系")
    @ApiImplicitParam(name = "ids", value = "角色索引", required = true)
    public HttpResult<Object> selectRelevance(@RequestBody @Validated List<String> ids) {
        String methodDescribe = getMethodDescribe("selectRelevance");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, ids);
        boolean result = iEventTemplateService.selectRelevance(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_EXIST, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_UNEXIST, null, methodDescribe);
        }
    }



    /**
     * 根据模板id查询关系
     * @param id id
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectReleation")
    @ApiOperation("根据模板id查询关系")
    @ApiImplicitParam(name = "id", value = "角色索引", required = true)
    public HttpResult<List<EventReportDictVO>> selectRelation(@RequestParam(required = false,name = "id")  String id) {
        String methodDescribe = getMethodDescribe("selectRelation");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, id);
        List<EventReportDictVO> res = new ArrayList<>();
        if (StrUtil.isNotBlank(id)) {
            res.addAll(iEventTemplateService.selectRelation(id)) ;
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);

    }
}
