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.Bcr;
import com.ruoyi.predict.domain.Mv;
import com.ruoyi.predict.domain.StationPowerMeasurementPoint;
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/19 11:38
 * 文件说明：场站功率测点管理
 */
@Api(value = "场站功率测点管理Controller", tags = {"场站功率测点管理操作接口"})
@Controller
@RequestMapping("/predict/api/spmpmc")
public class StationPowerMeasurementPointManagementController extends BaseController {

    @Autowired
    private IMvService mvService;

    @Autowired
    private IBcrService bcrService;

    @ApiOperation("获取场站功率测点信息列表")
    @PostMapping("/getStationPowerMeasurementPointList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "windplantName(筛选传值windplantNo)", 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 getStationPowerMeasurementPointList(StationPowerMeasurementPoint stationPowerMeasurementPoint) {
        startPage();
        List<StationPowerMeasurementPoint> list = new ArrayList<>();
        Mv mv = new Mv();
        mv.setWindplantNo(stationPowerMeasurementPoint.getWindplantNo());
        mv.setWturNo((long) 0);
        List<Mv> mvList = mvService.selectMvList(mv);
        for (Mv m : mvList) {
            StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
            obj.setPointNo(m.getMvNo());
            obj.setWindplantName(m.getWindplantName());
            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(stationPowerMeasurementPoint.getWindplantNo());
        bcr.setWturNo((long) 0);
        List<Bcr> bcrList = bcrService.selectBcrList(bcr);
        for (Bcr b : bcrList) {
            StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
            obj.setPointNo(b.getBcrNo());
            obj.setWindplantName(b.getWindplantName());
            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("/deleteStationPowerMeasurementPoint")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mvPointNos", value = "批量MV表机组测点编号", required = true),
            @ApiImplicitParam(name = "bcrPointNos", value = "批量BCR表机组测点编号", required = true),
    })
    public AjaxResult deleteStationPowerMeasurementPoint(@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("/stationPowerMeasurementPointImport")
    @ResponseBody
    public AjaxResult stationPowerMeasurementPointImport(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<StationPowerMeasurementPoint> util = new ExcelUtil<StationPowerMeasurementPoint>(StationPowerMeasurementPoint.class);
        List<StationPowerMeasurementPoint> stationPowerMeasurementPointList = util.importExcel(file.getInputStream());
        List<Mv> mvList = new ArrayList<>();
        List<Bcr> bcrList = new ArrayList<>();
        if (stationPowerMeasurementPointList.size() > 0) {
            // 统一测点为
            // 100001
            // 101001
            // 110001、110002、110003、110004、110005、110006
            // 200001、200002、200003、200004、200005、200006、200007、200008、200009
            // 300001、300002、300003的总次数
            int mv1 = 0;
            int mv2 = 0;
            int mv3 = 0;
            int mv4 = 0;
            int mv5 = 0;
            int mv6 = 0;
            int mv7 = 0;
            int mv8 = 0;
            int mv9 = 0;
            int mv10 = 0;
            int mv11 = 0;
            int mv12 = 0;
            int mv13 = 0;
            int mv14 = 0;
            int mv15 = 0;
            int mv16 = 0;
            int mv17 = 0;
            int mv18 = 0;
            int mv19 = 0;
            int mv20 = 0;
            // 统一测点为
            // 600001、600002、600003、600004、600005、600006、600007、600008、600009
            // 700001、700002、700003、700004、700005、700006、700007、700008、700009
            // 800001、800002、800003、800004
            // 的总次数
            int bcr600001 = 0;
            int bcr600002 = 0;
            int bcr600003 = 0;
            int bcr600004 = 0;
            int bcr600005 = 0;
            int bcr600006 = 0;
            int bcr600007 = 0;
            int bcr600008 = 0;
            int bcr600009 = 0;
            int bcr700001 = 0;
            int bcr700002 = 0;
            int bcr700003 = 0;
            int bcr700004 = 0;
            int bcr700005 = 0;
            int bcr700006 = 0;
            int bcr700007 = 0;
            int bcr700008 = 0;
            int bcr700009 = 0;
            int bcr800001 = 0;
            int bcr800002 = 0;
            int bcr800003 = 0;
            int bcr800004 = 0;
            for (StationPowerMeasurementPoint obj : stationPowerMeasurementPointList) {
                long unifiedPoint = obj.getUnifiedPoint();
                if (unifiedPoint >= 100001 && unifiedPoint <= 300003) {//mv表的范围
                    Mv mv = new Mv();
                    long mvNo = 0;
                    long measurementtypeNo;
                    if (unifiedPoint == 100001) {
                        mvNo = 100001 + mv1;
                        measurementtypeNo = 1;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv1++;
                    } else if (unifiedPoint == 101001) {
                        mvNo = 101001 + mv2;
                        measurementtypeNo = 1;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv2++;
                    } else if (unifiedPoint == 110001) {
                        mvNo = 110001 + mv3 * 10;
                        measurementtypeNo = 1;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv3++;
                    } else if (unifiedPoint == 110002) {
                        mvNo = 110002 + mv4 * 10;
                        measurementtypeNo = 63;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv4++;
                    } else if (unifiedPoint == 110003) {
                        mvNo = 110003 + mv5 * 10;
                        measurementtypeNo = 63;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv5++;
                    } else if (unifiedPoint == 110004) {
                        mvNo = 110004 + mv6 * 10;
                        measurementtypeNo = 63;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv6++;
                    } else if (unifiedPoint == 110005) {
                        mvNo = 110005 + mv7 * 10;
                        measurementtypeNo = 22;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv7++;
                    } else if (unifiedPoint == 110006) {
                        mvNo = 110006 + mv8 * 10;
                        measurementtypeNo = 21;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv8++;
                    } else if (unifiedPoint == 200001) {
                        mvNo = 200001 + mv9 * 10;
                        measurementtypeNo = 65;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv9++;
                    } else if (unifiedPoint == 200002) {
                        mvNo = 200002 + mv10 * 10;
                        measurementtypeNo = 45;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv10++;
                    } else if (unifiedPoint == 200003) {
                        mvNo = 200003 + mv11 * 10;
                        measurementtypeNo = 42;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv11++;
                    } else if (unifiedPoint == 200004) {
                        mvNo = 200004 + mv12 * 10;
                        measurementtypeNo = 46;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv12++;
                    } else if (unifiedPoint == 200005) {
                        mvNo = 200005 + mv13 * 10;
                        measurementtypeNo = 44;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv13++;
                    } else if (unifiedPoint == 200006) {
                        mvNo = 200006 + mv14 * 10;
                        measurementtypeNo = 48;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv14++;
                    } else if (unifiedPoint == 200007) {
                        mvNo = 200007 + mv15 * 10;
                        measurementtypeNo = 49;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv15++;
                    } else if (unifiedPoint == 200008) {
                        mvNo = 200008 + mv16 * 10;
                        measurementtypeNo = 50;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv16++;
                    } else if (unifiedPoint == 200009) {
                        mvNo = 200009 + mv17 * 10;
                        measurementtypeNo = 51;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv17++;
                    } else if (unifiedPoint == 300001) {
                        mvNo = 300001 + mv18 * 10;
                        measurementtypeNo = 17;
                        mv.setMeasurementtypeNo(measurementtypeNo);
                        mv18++;
                    } else if (unifiedPoint == 300002) {
                        mvNo = 300002 + mv19 * 10;
                        mv19++;
                    } else if (unifiedPoint == 300003) {
                        mvNo = 300003 + mv20 * 10;
                        mv20++;
                    }
                    mv.setMvNo(mvNo);
                    mv.setWindplantNo(obj.getWindplantNo());
                    mv.setWturNo((long) 0);
                    mv.setName(obj.getName());
                    mv.setRef(obj.getRef());
                    mv.setCoeff(obj.getRatio());
                    mv.setUnifiedPoint(obj.getUnifiedPoint());
                    mvList.add(mv);
                } else {
                    Bcr bcr = new Bcr();
                    long bcrNo = 0;
                    if (unifiedPoint == 600001) {
                        bcrNo = 100001 + bcr600001 * 10;
                        bcr600001++;
                    } else if (unifiedPoint == 600002) {
                        bcrNo = 100002 + bcr600002 * 10;
                        bcr600002++;
                    } else if (unifiedPoint == 600003) {
                        bcrNo = 100003 + bcr600003 * 10;
                        bcr600003++;
                    } else if (unifiedPoint == 600004) {
                        bcrNo = 100004 + bcr600004 * 10;
                        bcr600004++;
                    } else if (unifiedPoint == 600005) {
                        bcrNo = 100005 + bcr600005 * 10;
                        bcr600005++;
                    } else if (unifiedPoint == 600006) {
                        bcrNo = 100006 + bcr600006 * 10;
                        bcr600006++;
                    } else if (unifiedPoint == 600007) {
                        bcrNo = 100007 + bcr600007 * 10;
                        bcr600007++;
                    } else if (unifiedPoint == 600008) {
                        bcrNo = 100008 + bcr600008 * 10;
                        bcr600008++;
                    } else if (unifiedPoint == 600009) {
                        bcrNo = 100009 + bcr600009 * 10;
                        bcr600009++;
                    } else if (unifiedPoint == 700001) {
                        bcrNo = 200001 + bcr700001 * 10;
                        bcr700001++;
                    } else if (unifiedPoint == 700002) {
                        bcrNo = 200002 + bcr700002 * 10;
                        bcr700002++;
                    } else if (unifiedPoint == 700003) {
                        bcrNo = 200003 + bcr700003 * 10;
                        bcr700003++;
                    } else if (unifiedPoint == 700004) {
                        bcrNo = 200004 + bcr700004 * 10;
                        bcr700004++;
                    } else if (unifiedPoint == 700005) {
                        bcrNo = 200005 + bcr700005 * 10;
                        bcr700005++;
                    } else if (unifiedPoint == 700006) {
                        bcrNo = 200006 + bcr700006 * 10;
                        bcr700006++;
                    } else if (unifiedPoint == 700007) {
                        bcrNo = 200007 + bcr700007 * 10;
                        bcr700007++;
                    } else if (unifiedPoint == 700008) {
                        bcrNo = 200008 + bcr700008 * 10;
                        bcr700008++;
                    } else if (unifiedPoint == 700009) {
                        bcrNo = 200009 + bcr700009 * 10;
                        bcr700009++;
                    } else if (unifiedPoint == 800001) {
                        bcrNo = 300001 + bcr800001 * 10;
                        bcr800001++;
                    } else if (unifiedPoint == 800002) {
                        bcrNo = 300002 + bcr800002 * 10;
                        bcr800002++;
                    } else if (unifiedPoint == 800003) {
                        bcrNo = 300003 + bcr800003 * 10;
                        bcr800003++;
                    } else if (unifiedPoint == 800004) {
                        bcrNo = 300004 + bcr800004 * 10;
                        bcr800004++;
                    }
                    bcr.setBcrNo(bcrNo);
                    bcr.setWindplantNo(obj.getWindplantNo());
                    bcr.setWturNo((long) 0);
                    bcr.setName(obj.getName());
                    bcr.setRef(obj.getRef());
                    bcr.setIncval(obj.getRatio());
                    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("/stationPowerMeasurementPointExport")
    @ResponseBody
    public void stationPowerMeasurementPointExport(HttpServletResponse response, StationPowerMeasurementPoint unitMeasuringPoint, String idsMv, String idsBcr) throws IOException {
        List<StationPowerMeasurementPoint> 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)) {
                    StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
                    obj.setWindplantNo(mv.getWindplantNo());
                    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)) {
                    StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
                    obj.setWindplantNo(bcr.getWindplantNo());
                    obj.setName(bcr.getName());
                    obj.setRef(bcr.getRef());
                    obj.setUnifiedPoint(bcr.getUnifiedPoint());
                    list.add(obj);
                }
            }
        } else {
            Mv mv = new Mv();
            mv.setWindplantNo(unitMeasuringPoint.getWindplantNo());
            mv.setWturNo((long) 0);
            List<Mv> mvList = mvService.selectMvList(mv);
            for (Mv m : mvList) {
                StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
                obj.setWindplantNo(m.getWindplantNo());
                obj.setName(m.getName());
                obj.setRef(m.getRef());
                obj.setUnifiedPoint(m.getUnifiedPoint());
                list.add(obj);
            }
            Bcr bcr = new Bcr();
            bcr.setWindplantNo(unitMeasuringPoint.getWindplantNo());
            bcr.setWturNo((long) 0);
            List<Bcr> bcrList = bcrService.selectBcrList(bcr);
            for (Bcr b : bcrList) {
                StationPowerMeasurementPoint obj = new StationPowerMeasurementPoint();
                obj.setWindplantNo(b.getWindplantNo());
                obj.setName(b.getName());
                obj.setRef(b.getRef());
                obj.setUnifiedPoint(b.getUnifiedPoint());
                list.add(obj);
            }
        }
        ExcelUtil<StationPowerMeasurementPoint> util = new ExcelUtil<StationPowerMeasurementPoint>(StationPowerMeasurementPoint.class);
        util.exportExcel(response, list, "场站功率测点管理信息数据");
    }
}
