package com.njcn.harmonic.controller.report;

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.harmonic.pojo.param.ReportSearchParam;
import com.njcn.harmonic.pojo.param.ReportTemplateParam;
import com.njcn.harmonic.pojo.po.ExcelRptTemp;
import com.njcn.harmonic.common.pojo.vo.ReportTemplateVO;
import com.njcn.harmonic.pojo.vo.ReportTreeVO;
import com.njcn.harmonic.pojo.vo.SysDeptTempVO;
import com.njcn.harmonic.service.report.CustomReportService;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * pqs
 * 用户自定义报表
 * @author cdf
 * @date 2022/8/15
 */

@Slf4j
@RestController
@RequestMapping("/customReport")
@Api(tags = "用户自定义报表")
@AllArgsConstructor
public class CustomReportController extends BaseController {

    private final CustomReportService customReportService;


    /**
     * 替换报表数据并返回
     * @author qijian
     * @date 2022/10/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getCustomReport")
    @ApiOperation("获取报表")
    @ApiImplicitParam(name = "reportSearchParam", value = "查询体", required = false)
    public void getCustomReport(@RequestBody ReportSearchParam reportSearchParam, HttpServletResponse response) {
        customReportService.getCustomReport(reportSearchParam,response);
    }


    /**
     * 新增自定义报表模板
     * @author cdf
     * @date 2022/10/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType=OperateType.ADD)
    @PostMapping("/addTemplate")
    @ApiOperation("新增自定义报表模板")
    public HttpResult<Boolean> addCustomReportTemplate(@Validated ReportTemplateParam reportTemplateParam){
        String methodDescribe = getMethodDescribe("addCustomReportTemplate");
        boolean res = customReportService.addCustomReportTemplate(reportTemplateParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 查询所有模板
     * @author qijian
     * @date 2022/10/14
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTemplateList")
    @ApiOperation("查询报表模板")
    @ApiImplicitParam(name = "reportSearchParam", value = "实体参数", required = false)
    public HttpResult<List<ReportTemplateVO>> getTemplateList(@RequestBody ReportSearchParam reportSearchParam){
        String methodDescribe = getMethodDescribe("getTemplateList");
        List<ReportTemplateVO> list = customReportService.getTemplateList(reportSearchParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 根据部门查询模板
     * @author qijian
     * @date 2022/10/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getTemplateByDept")
    @ApiOperation("根据部门查询模板")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public HttpResult<List<ReportTemplateVO>> getTemplateByDept(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("getTemplateList");
        List<ReportTemplateVO> list = customReportService.getTemplateByDept(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 删除模板
     * @author qijian
     * @date 2022/10/18
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType=OperateType.DELETE)
    @PostMapping("/delTemplate")
    @ApiOperation("删除报表模板")
    @ApiImplicitParam(name = "reportSearchParam", value = "实体参数", required = false)
    public HttpResult<Boolean> delTemplate(@RequestBody ReportSearchParam reportSearchParam){
        String methodDescribe = getMethodDescribe("delTemplate");
        boolean res = customReportService.delTemplate(reportSearchParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 根据id查询模板详情
     * @author qijian
     * @date 2022/10/14
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getCustomReportTemplateById")
    @ApiOperation("根据id查询模板详情")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public HttpResult<ExcelRptTemp> getCustomReportTemplateById(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("getCustomReportTemplateById");
        ExcelRptTemp excelRptTemp = customReportService.getCustomReportTemplateById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, excelRptTemp, methodDescribe);
    }

    /**
     * 根据id回显模板
     * @author qijian
     * @date 2022/10/14
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/viewCustomReportTemplateById")
    @ApiOperation("根据id查询模板详情")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public void viewCustomReportTemplateById(@RequestParam("id") String id,HttpServletResponse response){
        customReportService.viewCustomReportTemplateById(id,response);
    }

    /**
     * 修改自定义报表模板
     * @author qijian
     * @date 2022/10/18
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType=OperateType.UPDATE)
    @PostMapping("/updateTemplate")
    @ApiOperation("修改自定义报表模板")
    public HttpResult<Boolean> updateCustomReportTemplate(@Validated ReportTemplateParam.UpdateReportTemplateParam reportTemplateParam){
        String methodDescribe = getMethodDescribe("updateCustomReportTemplate");
        boolean res = customReportService.updateCustomReportTemplate(reportTemplateParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 绑定/解绑模板数据
     * @author qijian
     * @date 2022/10/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType=OperateType.UPDATE)
    @PostMapping("/updateBindTemplate")
    @ApiOperation("绑定/解绑模板数据")
    @ApiImplicitParam(name = "reportSearchParams", value = "实体参数", required = false)
    public HttpResult<Boolean> updateBindTemplate(@RequestBody List<ReportSearchParam> reportSearchParams){
        String methodDescribe = getMethodDescribe("updateBindTemplate");
        boolean res = customReportService.updateBindTemplate(reportSearchParams);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 根据模板ID查询数据
     * @author qijian
     * @date 2022/10/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getDataByTempId")
    @ApiOperation("根据模板ID查询数据")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public HttpResult<List<SysDeptTempVO>> getDataByTempId(@RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("getDataByTempId");
        List<SysDeptTempVO> list = customReportService.getDataByTempId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 修改激活状态
     * @author qijian
     * @date 2022/10/17
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType=OperateType.UPDATE)
    @PostMapping("/updateTemplateActive")
    @ApiOperation("修改激活状态")
    @ApiImplicitParam(name = "reportSearchParam", value = "实体参数", required = false)
    public HttpResult<Page<ExcelRptTemp>> updateTemplateActive(@RequestBody ReportSearchParam reportSearchParam){
        String methodDescribe = getMethodDescribe("updateTemplateActive");
        boolean res = customReportService.updateStatus(reportSearchParam);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 获取报表模板树
     * @author cdf
     * @date 2022/8/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/reportChooseTree")
    @ApiOperation("获取报表模板树")
    public HttpResult<List<ReportTreeVO>> reportChooseTree(){
        String methodDescribe = getMethodDescribe("reportChooseTree");
        List<ReportTreeVO> res = customReportService.reportChooseTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }




    /**
     * 获取报表指标是否合格模板树
     * @author cdf
     * @date 2023/10/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/targetLimitChooseTree")
    @ApiOperation("获取报表指标是否合格模板树")
    public HttpResult<List<ReportTreeVO>> targetLimitChooseTree(){
        String methodDescribe = getMethodDescribe("targetLimitChooseTree");
        List<ReportTreeVO> res = customReportService.targetLimitChooseTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }


    /**
     * 获取报表台账模板树
     * @author cdf
     * @date 2023/10/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/terminalChooseTree")
    @ApiOperation("获取报表台账模板树")
    public HttpResult<List<ReportTreeVO>> terminalChooseTree(){
        String methodDescribe = getMethodDescribe("terminalChooseTree");
        List<ReportTreeVO> res = customReportService.terminalChooseTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }



}
