package com.njcn.device.pq.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.device.pq.pojo.bo.excel.LineMark;
import com.njcn.device.pq.pojo.param.TerminalMainQueryParam;
import com.njcn.device.pq.pojo.po.LineDetail;
import com.njcn.device.pq.pojo.vo.LineMarkVO;
import com.njcn.device.pq.service.LineMarkService;
import com.njcn.poi.util.PoiUtil;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * pqs
 * 监测点评分控制
 * @author cdf
 * @date 2022/5/10
 */
@Slf4j
@Api(tags = "监测点评分管理")
@RestController
@RequestMapping("/mark")
@RequiredArgsConstructor
public class LineMarkController extends BaseController {
    private final LineMarkService lineMarkService;



    /**
     * 批量导入监测点评分
     * @author cdf
     * @date 2022/5/10
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/batchExport")
    @ApiOperation("批量导入监测点评分")
    public HttpResult<String> batchExport(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response){
        String methodDescribe = getMethodDescribe("batchExport");
        String res = lineMarkService.batchExport(file,response);
        if(res.equals(CommonResponseEnum.SUCCESS.getMessage())){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else if(res.equals(CommonResponseEnum.FAIL.getMessage())) {
            return null;
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, res, methodDescribe);
        }
    }

    /**
     * 导出excel头模板
     * @author cdf
     * @date 2022/6/8
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/heardExport")
    @ApiOperation("导出excel头模板")
    public void heardExport(HttpServletResponse response){
        ExportParams exportParams = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,LineMark.class,new ArrayList<>());
        PoiUtil.exportFileByWorkbook(workbook,"",response);
    }


    /**
     * 获取监测点评分列表
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineMark")
    @ApiOperation("查询监测点评分列表")
    @ApiImplicitParam(name = "terminalMainQueryParam", value = "监测点评分参数体")
    HttpResult<IPage<LineMarkVO>> getLineMark(@RequestBody TerminalMainQueryParam terminalMainQueryParam){
        String methodDescribe = getMethodDescribe("getLineMark");
        IPage<LineMarkVO> resList = lineMarkService.getLineMark(terminalMainQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }


    /**
     * 获取监测点评分列表
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/exportLineMark")
    @ApiOperation("导出监测点评分列表")
    @ApiImplicitParam(name = "terminalMainQueryParam", value = "监测点评分参数体")
    void exportLineMark(@RequestBody TerminalMainQueryParam terminalMainQueryParam,HttpServletResponse response){
        List<LineMark> resList = lineMarkService.exportLineMark(terminalMainQueryParam);
        ExportParams exportParams = new ExportParams();

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,LineMark.class,resList);
        PoiUtil.exportFileByWorkbook(workbook,"监测点评分台账",response);
    }


    /**
     *
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/singleOperate")
    @ApiOperation("修改单个监测点等级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "监测点id", required = true),
            @ApiImplicitParam(name = "lineGrade", value = "监测点等级", required = true),
    })
    public HttpResult<Object> singleOperate(@RequestParam("id") String id, @RequestParam("lineGrade")String lineGrade){
        String methodDescribe = getMethodDescribe("singleOperate");
        boolean res = lineMarkService.singleOperate(id,lineGrade);
       if(res){
           return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
       }else {
           return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

       }
    }

    /**
     * 获取单个监测点评级
     *
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获取单个监测点评级")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    @GetMapping("getLineGradeById")
    public HttpResult<LineDetail> getLineGradeById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineGradeById");
        LineDetail lineLedger = lineMarkService.getLineGradeById(id);
        if (Objects.nonNull(lineLedger)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, lineLedger, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }



}
