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.*;
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/1/17 16:31
 * 文件说明：机组管理
 */
@Api(value = "机组管理Controller", tags = {"机组管理操作接口"})
@Controller
@RequestMapping("/predict/api/cmc")
public class CrewManagementController extends BaseController {

    @Autowired
    private IWturService wturService;

    @Autowired
    private IWfProjectCwWtgService wfProjectCwWtgService;

    @Autowired
    private IWtgTypeService wtgTypeService;

    @Autowired
    private IWtgTypeTemperatureDefService wtgTypeTemperatureDefService;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IAreaConfigService areaConfigService;

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IWindplantprojectService windplantprojectService;

    @Autowired
    private ICollectorwireprojectService collectorwireprojectService;

    @ApiOperation("获取机组信息列表")
    @PostMapping("/getWturList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "windplantName(筛选传值windplantNo)", value = "所属场站"),
            @ApiImplicitParam(name = "projectName(筛选传值projectNo)", value = "所属期次"),
            @ApiImplicitParam(name = "collectorwireName(筛选传值collectorwireNo)", value = "所属集电线"),
            @ApiImplicitParam(name = "wtgIssue", value = "机组代码"),
            @ApiImplicitParam(name = "name", value = "机组名称"),
            @ApiImplicitParam(name = "typeName", value = "机组类型"),
            @ApiImplicitParam(name = "longtitude", value = "机组经度"),
            @ApiImplicitParam(name = "latitude", value = "机组纬度"),
            @ApiImplicitParam(name = "elevation", value = "海拔高度"),
            @ApiImplicitParam(name = "ratedPower", value = "装机容量MW"),
            @ApiImplicitParam(name = "cutInWspeed", value = "切入风速"),
            @ApiImplicitParam(name = "cutOutWspeed", value = "切出风速"),
            @ApiImplicitParam(name = "ratedWpeed", value = "额定风速"),
            @ApiImplicitParam(name = "lTemperature", value = "低温切出"),
            @ApiImplicitParam(name = "hTemperature", value = "高温切出"),
    })
    public TableDataInfo getWturList(Wtur wtur) {
        startPage();
        List<Wtur> list = wturService.selectWturList(wtur);
        return getDataTable(list);
    }

    @ApiOperation("批量删除机组信息")
    @PostMapping("/deleteWtur")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wturNos", value = "批量机组编号", required = true),
    })
    public AjaxResult deleteWtur(@RequestParam(value = "wturNos") String wturNos) {
        String[] ids = wturNos.split(",");
        for (String id : ids) {
            Wtur wtur = wturService.selectWturById(Convert.toLong(id));
            if (ObjectUtil.isNotNull(wtur)) {
                if (wtur.getRatedPower() != null) {
                    WfProjectCwWtg wfProjectCwWtg = wfProjectCwWtgService.selectWfProjectCwWtgById(wtur.getWturNo());
                    //减去当前数据下的风机的总装机容量
                    Collectorwireproject collectorwireproject = collectorwireprojectService.selectCollectorwireproject(wfProjectCwWtg.getWindplantNo(), wfProjectCwWtg.getProjectNo(), wfProjectCwWtg.getCollectorwireNo());
                    if (ObjectUtil.isNotNull(collectorwireproject)) {
                        if (collectorwireproject.getWireCapacity() != null) {
                            collectorwireproject.setWireCapacity(collectorwireproject.getWireCapacity() - wtur.getRatedPower());
                            collectorwireprojectService.updateCollectorwireproject(collectorwireproject);
                        }
                    }
                    Windplantproject windplantproject = windplantprojectService.selectWindplantprojectByWpNoProjectNo(wfProjectCwWtg.getWindplantNo(), wfProjectCwWtg.getProjectNo());
                    if (ObjectUtil.isNotNull(windplantproject)) {
                        if (windplantproject.getProjectCapacity() != null) {
                            windplantproject.setProjectCapacity(windplantproject.getProjectCapacity() - wtur.getRatedPower());
                            windplantprojectService.updateWindplantproject(windplantproject);
                        }
                    }
                    Windplant windplant = windplantService.selectWindplantById(wfProjectCwWtg.getWindplantNo());
                    if (ObjectUtil.isNotNull(windplant)) {
                        if (windplant.getInstallPower() != null) {
                            windplant.setInstallPower(windplant.getInstallPower() - wtur.getRatedPower());
                            windplantService.updateWindplant(windplant);
                        }
                    }
                    AreaConfig areaConfig = areaConfigService.selectAreaConfigByRtuNo(wfProjectCwWtg.getWindplantNo());
                    if (ObjectUtil.isNotNull(areaConfig)) {
                        Area area = areaService.selectAreaById(areaConfig.getAreaNo());
                        if (ObjectUtil.isNotNull(area)) {
                            if (area.getInstallPower() != null) {
                                area.setInstallPower(area.getInstallPower() - wtur.getRatedPower());
                                areaService.updateArea(area);
                            }
                        }
                    }
                }
                wfProjectCwWtgService.deleteWfProjectCwWtgById(wtur.getWturNo());
                wtgTypeService.deleteWtgTypeById(wtur.getWtgType());
                wtgTypeTemperatureDefService.deleteWtgTypeTemperatureDefById(wtur.getWtgType());
            }
        }
        wturService.deleteWturByIds(wturNos);
        return AjaxResult.success();
    }

    @ApiOperation("机组管理信息导入")
    @PostMapping("/wturImport")
    @ResponseBody
    public AjaxResult wturImport(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<Wtur> util = new ExcelUtil<Wtur>(Wtur.class);
        List<Wtur> wturList = util.importExcel(file.getInputStream());
        for (int i = 0; i < wturList.size(); i++) {
            Wtur wtur = wturList.get(i);
            Long wturNo;
            if (i == 0) {
                if (wturService.selectNewData() == null) {
                    wturNo = Convert.toLong(1);
                } else {
                    wturNo = wturService.selectNewData() + (i + 1);
                }
            } else {
                wturNo = wturList.get(i - 1).getWturNo() + 1;
            }
            wtur.setWturNo(wturNo);
            Long typeNo;
            if (i == 0) {
                if (wtgTypeService.selectNewData() == null) {
                    typeNo = Convert.toLong(1);
                } else {
                    typeNo = wtgTypeService.selectNewData() + (i + 1);
                }
            } else {
                typeNo = wturList.get(i - 1).getWtgType() + 1;
            }
            wtur.setWtgType(typeNo);
            WfProjectCwWtg wfProjectCwWtg = new WfProjectCwWtg();
            wfProjectCwWtg.setWindplantNo(wtur.getWindplantNo());
            wfProjectCwWtg.setProjectNo(wtur.getProjectNo());
            wfProjectCwWtg.setCollectorwireNo(wtur.getCollectorwireNo());
            wfProjectCwWtg.setWtgNo(wturNo);
            wfProjectCwWtg.setWtgIssue(wtur.getWtgIssue());
            wfProjectCwWtgService.insertWfProjectCwWtg(wfProjectCwWtg);
            WtgType wtgType = new WtgType();
            wtgType.setTypeNo(typeNo);
            wtgType.setTypeName(wtur.getTypeName());
            wtgTypeService.insertWtgType(wtgType);
            WtgTypeTemperatureDef wtgTypeTemperatureDef = new WtgTypeTemperatureDef();
            wtgTypeTemperatureDef.setWtgType(typeNo);
            wtgTypeTemperatureDef.setlTemperature(wtur.getlTemperature());
            wtgTypeTemperatureDef.sethTemperature(wtur.gethTemperature());
            wtgTypeTemperatureDefService.insertWtgTypeTemperatureDef(wtgTypeTemperatureDef);
            /*装机容量累加到集电线、期次、场站、区域*/
            Collectorwireproject collectorwireproject = collectorwireprojectService.selectCollectorwireproject(wtur.getWindplantNo(), wtur.getProjectNo(), wtur.getCollectorwireNo());
            if (ObjectUtil.isNotNull(collectorwireproject)) {
                if (collectorwireproject.getWireCapacity() != null) {
                    collectorwireproject.setWireCapacity(collectorwireproject.getWireCapacity() + wtur.getRatedPower());
                    collectorwireprojectService.updateCollectorwireproject(collectorwireproject);
                }
            }
            Windplantproject windplantproject = windplantprojectService.selectWindplantprojectByWpNoProjectNo(wtur.getWindplantNo(), wtur.getProjectNo());
            if (ObjectUtil.isNotNull(windplantproject)) {
                if (windplantproject.getProjectCapacity() != null) {
                    windplantproject.setProjectCapacity(windplantproject.getProjectCapacity() + wtur.getRatedPower());
                    windplantprojectService.updateWindplantproject(windplantproject);
                }
            }
            Windplant windplant = windplantService.selectWindplantById(wtur.getWindplantNo());
            if (ObjectUtil.isNotNull(windplant)) {
                if (windplant.getInstallPower() != null) {
                    windplant.setInstallPower(windplant.getInstallPower() + wtur.getRatedPower());
                    windplantService.updateWindplant(windplant);
                }
            }
            AreaConfig areaConfig = areaConfigService.selectAreaConfigByRtuNo(wtur.getWindplantNo());
            if (ObjectUtil.isNotNull(areaConfig)) {
                Area area = areaService.selectAreaById(areaConfig.getAreaNo());
                if (ObjectUtil.isNotNull(area)) {
                    if (area.getInstallPower() != null) {
                        area.setInstallPower(area.getInstallPower() + wtur.getRatedPower());
                        areaService.updateArea(area);
                    }
                }
            }
        }
        wturService.insertWturList(wturList);
        return AjaxResult.success();
    }

    @ApiOperation("机组管理信息导出")
    @GetMapping("/wturExport")
    @ResponseBody
    public void wturExport(HttpServletResponse response, Wtur wtur, String ids) throws IOException {
        List<Wtur> list = new ArrayList<>();
        if (!"".equals(ids)) {
            String[] wturNos = Convert.toStrArray(ids);
            for (String wturNo : wturNos) {
                list.add(wturService.selectWturById(Convert.toLong(wturNo)));
            }
        } else {
            list.addAll(wturService.selectWturList(wtur));
        }
        ExcelUtil<Wtur> util = new ExcelUtil<Wtur>(Wtur.class);
        util.exportExcel(response, list, "机组管理信息数据");
    }
}
