package com.ruoyi.predict.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.predict.domain.*;
import com.ruoyi.predict.service.IBcrService;
import com.ruoyi.predict.service.IMvService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author hlf
 * @date 2022/10/17 14:51
 * 文件说明：机组测点管理
 */
@Api(value = "机组测点管理Controller", tags = {"机组测点管理操作接口"})
@Controller
@RequestMapping("/predict/api/umpmc")
public class UnitMeasuringPointManagementController extends BaseController {

    @Autowired
    private IMvService mvService;

    @Autowired
    private IBcrService bcrService;

    @ApiOperation("获取机组测点信息列表")
    @PostMapping("/getUnitMeasuringPointList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "windplantName(筛选传值windplantNo)", value = "所属场站"),
            @ApiImplicitParam(name = "wturName", value = "机组名称"),
            @ApiImplicitParam(name = "name", value = "测点代码"),
            @ApiImplicitParam(name = "ref", value = "描述"),
            @ApiImplicitParam(name = "ratio", value = "倍率"),
            @ApiImplicitParam(name = "unifiedPoint", value = "统一测点"),
            @ApiImplicitParam(name = "type", value = "区分(1mv 2bcr)"),
    })
    public TableDataInfo getUnitMeasuringPointList(UnitMeasuringPoint unitMeasuringPoint) {
        startPage();
        List<UnitMeasuringPoint> list = new ArrayList<>();
        Mv mv = new Mv();
        mv.setWindplantNo(unitMeasuringPoint.getWindplantNo());
        List<Mv> mvList = mvService.selectMvListIsNot0(mv);
        for (Mv m : mvList) {
            UnitMeasuringPoint obj = new UnitMeasuringPoint();
            obj.setPointNo(m.getMvNo());
            obj.setWindplantName(m.getWindplantName());
            obj.setWturName(m.getWturName());
            obj.setName(m.getName());
            obj.setRef(m.getRef());
            obj.setRatio(m.getCoeff());
            obj.setUnifiedPoint(m.getUnifiedPoint());
            obj.setType(1);
            list.add(obj);
        }
        Bcr bcr = new Bcr();
        bcr.setWindplantNo(unitMeasuringPoint.getWindplantNo());
        List<Bcr> bcrList = bcrService.selectBcrListIsNot0(bcr);
        for (Bcr b : bcrList) {
            UnitMeasuringPoint obj = new UnitMeasuringPoint();
            obj.setPointNo(b.getBcrNo());
            obj.setWindplantName(b.getWindplantName());
            obj.setWturName(b.getWturName());
            obj.setName(b.getName());
            obj.setRef(b.getRef());
            obj.setRatio(b.getIncval());
            obj.setUnifiedPoint(b.getUnifiedPoint());
            obj.setType(2);
            list.add(obj);
        }
        return getDataTable(list);
    }

    @ApiOperation("批量删除机组测点信息")
    @PostMapping("/deleteUnitMeasuringPoint")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mvPointNos", value = "批量MV表机组测点编号", required = true),
            @ApiImplicitParam(name = "bcrPointNos", value = "批量BCR表机组测点编号", required = true),
    })
    public AjaxResult deleteUnitMeasuringPoint(@RequestParam(value = "mvPointNos") String mvPointNos,
                                               @RequestParam(value = "bcrPointNos") String bcrPointNos) {
        if (!"".equals(mvPointNos)) {
            String[] ids = mvPointNos.split(",");
            for (String id : ids) {
                Mv mv = mvService.selectMvById(Convert.toLong(id));
                if (ObjectUtil.isNotNull(mv)) {
                    mvService.deleteMvById(Convert.toLong(id));
                }
            }
        }
        if (!"".equals(bcrPointNos)) {
            String[] ids = bcrPointNos.split(",");
            for (String id : ids) {
                Bcr bcr = bcrService.selectBcrById(Convert.toLong(id));
                if (ObjectUtil.isNotNull(bcr)) {
                    bcrService.deleteBcrById(Convert.toLong(id));
                }
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("机组测点管理信息导入")
    @PostMapping("/unitMeasuringPointImport")
    @ResponseBody
    public AjaxResult unitMeasuringPointImport(MultipartFile file, boolean updateSupport) throws Exception {
        //判断是否为excel文件
        if (!"xlsx".equals(Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1)) && !"xls".equals(Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1))) {
            return error("仅允许导入“xlsx或xls”格式文件!");
        }
        ExcelUtil<UnitMeasuringPoint> util = new ExcelUtil<UnitMeasuringPoint>(UnitMeasuringPoint.class);
        List<UnitMeasuringPoint> unitMeasuringPointList = util.importExcel(file.getInputStream());
        List<Mv> mvList = new ArrayList<>();
        List<Bcr> bcrList = new ArrayList<>();
        if (unitMeasuringPointList.size() > 0) {
            //统一测点为1、2、3、4、5的总次数
            int mv1 = 0;
            int mv2 = 0;
            int mv3 = 0;
            int mv4 = 0;
            int mv5 = 0;
            //统一测点为500001、500002、500003、500004、500005、500006、500007、500008的总次数
            int bcr500001 = 0;
            int bcr500002 = 0;
            int bcr500003 = 0;
            int bcr500004 = 0;
            int bcr500005 = 0;
            int bcr500006 = 0;
            int bcr500007 = 0;
            int bcr500008 = 0;
            for (UnitMeasuringPoint obj : unitMeasuringPointList) {
                long unifiedPoint = obj.getUnifiedPoint();
                if (unifiedPoint >= 1 && unifiedPoint <= 5) {//mv表的范围
                    Mv mv = new Mv();
                    long mvNo;
                    long measurementtypeNo;
                    if (unifiedPoint == 1) {
                        mvNo = 1 + mv1 * 10;
                        measurementtypeNo = 9;
                        mv1++;
                    } else if (unifiedPoint == 2) {
                        mvNo = 2 + mv2 * 10;
                        measurementtypeNo = 1;
                        mv2++;
                    } else if (unifiedPoint == 3) {
                        mvNo = 3 + mv3 * 10;
                        measurementtypeNo = 11;
                        mv3++;
                    } else if (unifiedPoint == 4) {
                        mvNo = 4 + mv4 * 10;
                        measurementtypeNo = 8;
                        mv4++;
                    } else {
                        mvNo = 5 + mv5 * 10;
                        measurementtypeNo = 10;
                        mv5++;
                    }
                    mv.setMvNo(mvNo);
                    mv.setWindplantNo(obj.getWindplantNo());
                    mv.setWturNo(obj.getWturNo());
                    mv.setName(obj.getName());
                    mv.setRef(obj.getRef());
                    mv.setCoeff(obj.getRatio());
                    mv.setMeasurementtypeNo(measurementtypeNo);
                    mv.setUnifiedPoint(obj.getUnifiedPoint());
                    mvList.add(mv);
                } else {
                    Bcr bcr = new Bcr();
                    long bcrNo = 0;
                    if (unifiedPoint == 500001) {
                        bcrNo = 1 + bcr500001 * 10;
                        bcr500001++;
                    } else if (unifiedPoint == 500002) {
                        bcrNo = 2 + bcr500002 * 10;
                        bcr500002++;
                    } else if (unifiedPoint == 500003) {
                        bcrNo = 3 + bcr500003 * 10;
                        bcr500003++;
                    } else if (unifiedPoint == 500004) {
                        bcrNo = 4 + bcr500004 * 10;
                        bcr500004++;
                    } else if (unifiedPoint == 500005) {
                        bcrNo = 5 + bcr500005 * 10;
                        bcr500005++;
                    } else if (unifiedPoint == 500006) {
                        bcrNo = 6 + bcr500006 * 10;
                        bcr500006++;
                    } else if (unifiedPoint == 500007) {
                        bcrNo = 7 + bcr500007 * 10;
                        bcr500007++;
                    } else if (unifiedPoint == 500008) {
                        bcrNo = 8 + bcr500008 * 10;
                        bcr500008++;
                    }
                    bcr.setBcrNo(bcrNo);
                    bcr.setWindplantNo(obj.getWindplantNo());
                    bcr.setWturNo(obj.getWturNo());
                    bcr.setName(obj.getName());
                    bcr.setRef(obj.getRef());
                    bcr.setIncval(obj.getRatio());
                    bcr.setBcrType((long) 0);
                    bcr.setUnifiedPoint(obj.getUnifiedPoint());
                    bcrList.add(bcr);
                }
            }
            if (mvList.size() > 0) {
                mvService.insertMvList(mvList);
            }
            if (bcrList.size() > 0) {
                bcrService.insertBcrList(bcrList);
            }
        }
        return AjaxResult.success("正在导入中...");
    }

    @ApiOperation("机组测点管理信息导出")
    @GetMapping("/unitMeasuringPointExport")
    @ResponseBody
    public void unitMeasuringPointExport(HttpServletResponse response, UnitMeasuringPoint unitMeasuringPoint, String idsMv, String idsBcr) throws IOException {
        List<UnitMeasuringPoint> list = new ArrayList<>();
        if (!"".equals(idsMv)) {//mv表
            String[] pointNos = Convert.toStrArray(idsMv);
            for (String pointNo : pointNos) {
                Mv mv = mvService.selectMvById(Convert.toLong(pointNo));
                if (ObjectUtil.isNotNull(mv)) {
                    UnitMeasuringPoint obj = new UnitMeasuringPoint();
                    obj.setWindplantNo(mv.getWindplantNo());
                    obj.setWturNo(mv.getWturNo());
                    obj.setName(mv.getName());
                    obj.setRef(mv.getRef());
                    obj.setUnifiedPoint(mv.getUnifiedPoint());
                    list.add(obj);
                }
            }
        } else if (!"".equals(idsBcr)) {//bcr表
            String[] pointNos = Convert.toStrArray(idsBcr);
            for (String pointNo : pointNos) {
                Bcr bcr = bcrService.selectBcrById(Convert.toLong(pointNo));
                if (ObjectUtil.isNotNull(bcr)) {
                    UnitMeasuringPoint obj = new UnitMeasuringPoint();
                    obj.setWindplantNo(bcr.getWindplantNo());
                    obj.setWturNo(bcr.getWturNo());
                    obj.setName(bcr.getName());
                    obj.setRef(bcr.getRef());
                    obj.setUnifiedPoint(bcr.getUnifiedPoint());
                    list.add(obj);
                }
            }
        } else {
            Mv mv = new Mv();
            mv.setWindplantNo(unitMeasuringPoint.getWindplantNo());
            List<Mv> mvList = mvService.selectMvListIsNot0(mv);
            for (Mv m : mvList) {
                UnitMeasuringPoint obj = new UnitMeasuringPoint();
                obj.setWindplantNo(m.getWindplantNo());
                obj.setWturNo(m.getWturNo());
                obj.setName(m.getName());
                obj.setRef(m.getRef());
                obj.setUnifiedPoint(m.getUnifiedPoint());
                list.add(obj);
            }
            Bcr bcr = new Bcr();
            bcr.setWindplantNo(unitMeasuringPoint.getWindplantNo());
            List<Bcr> bcrList = bcrService.selectBcrListIsNot0(bcr);
            for (Bcr b : bcrList) {
                UnitMeasuringPoint obj = new UnitMeasuringPoint();
                obj.setWindplantNo(b.getWindplantNo());
                obj.setWturNo(b.getWturNo());
                obj.setName(b.getName());
                obj.setRef(b.getRef());
                obj.setUnifiedPoint(b.getUnifiedPoint());
                list.add(obj);
            }
        }
        ExcelUtil<UnitMeasuringPoint> util = new ExcelUtil<UnitMeasuringPoint>(UnitMeasuringPoint.class);
        util.exportExcel(response, list, "机组测点管理信息数据");
    }
}
