package com.ruoyi.predict.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSONObject;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hlf
 * @date 2022/1/21 10:46
 * 文件说明：系统设置公共方法
 */
@Api(value = "系统设置公共方法Controller", tags = {"系统设置公共方法操作接口"})
@Controller
@RequestMapping("/predict/api/configureShare")
public class ConfigureShareController extends BaseController {

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IPvPowerStationService pvPowerStationService;

    @Autowired
    private IWindplantprojectService windplantprojectService;

    @Autowired
    private ICollectorwireprojectService collectorwireprojectService;

    @Autowired
    private IEnumMenuItemService enumMenuItemService;

    @Autowired
    private IWturService wturService;

    @Autowired
    private IWfProjectCwWtgService wfProjectCwWtgService;

    @ApiOperation("获取所有区域信息")
    @GetMapping("/getAreaAll")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaNo", value = "区域编号"),
            @ApiImplicitParam(name = "pathname", value = "区域名称"),
    })
    public AjaxResult getAreaAll() {
        JSONObject json = new JSONObject();
        List<Area> list = areaService.selectAreaList(new Area());
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("功率-根据区域编号获取场站信息")
    @GetMapping("/getGlStationsByAreaNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaNo", value = "区域编号", required = true),
            @ApiImplicitParam(name = "stationNo", value = "场站编号"),
            @ApiImplicitParam(name = "stationName", value = "场站名称"),
    })
    public AjaxResult getGlStationsByAreaNo(@RequestParam(value = "areaNo") String areaNo) {
        JSONObject json = new JSONObject();
        List<StationInformation> list = new ArrayList<>();
        Windplant windplant = new Windplant();
        windplant.setAreaNo(Convert.toLong(areaNo));
        windplant.setGlFlag((long) 1);
        List<Windplant> windplantList = windplantService.selectWindplantList(windplant);
        for (Windplant wp : windplantList) {
            StationInformation si = new StationInformation();
            si.setNo(wp.getWindplantNo());
            si.setStationName(wp.getName());
            si.setStationCode(wp.getPathname());
            list.add(si);
        }
        PvPowerStation pvPowerStation = new PvPowerStation();
        pvPowerStation.setAreaNo(Convert.toLong(areaNo));
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(pvPowerStation);
        for (PvPowerStation pps : pvPowerStationList) {
            StationInformation si = new StationInformation();
            si.setNo(pps.getPvStationNo());
            si.setStationName(pps.getName());
            si.setStationCode(pps.getRef());
            list.add(si);
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("电量-根据区域编号获取场站信息")
    @GetMapping("/getDlStationsByAreaNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaNo", value = "区域编号", required = true),
            @ApiImplicitParam(name = "stationNo", value = "场站编号"),
            @ApiImplicitParam(name = "stationName", value = "场站名称"),
    })
    public AjaxResult getDlStationsByAreaNo(@RequestParam(value = "areaNo") String areaNo) {
        JSONObject json = new JSONObject();
        List<StationInformation> list = new ArrayList<>();
        Windplant windplant = new Windplant();
        windplant.setAreaNo(Convert.toLong(areaNo));
        windplant.setDlFlag((long) 1);
        List<Windplant> windplantList = windplantService.selectWindplantList(windplant);
        for (Windplant wp : windplantList) {
            StationInformation si = new StationInformation();
            si.setNo(wp.getWindplantNo());
            si.setStationName(wp.getName());
            si.setStationCode(wp.getPathname());
            list.add(si);
        }
        PvPowerStation pvPowerStation = new PvPowerStation();
        pvPowerStation.setAreaNo(Convert.toLong(areaNo));
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(pvPowerStation);
        for (PvPowerStation pps : pvPowerStationList) {
            StationInformation si = new StationInformation();
            si.setNo(pps.getPvStationNo());
            si.setStationName(pps.getName());
            si.setStationCode(pps.getRef());
            list.add(si);
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据场站编号获取期次信息")
    @GetMapping("/getWindplantprojectByStationNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号"),
            @ApiImplicitParam(name = "projectName", value = "期次名称"),
    })
    public AjaxResult getWindplantprojectByStationNo(@RequestParam(value = "stationNo") String stationNo) {
        JSONObject json = new JSONObject();
        List<Windplantproject> list = windplantprojectService.selectWindplantprojectByWindplantNo(Convert.toLong(stationNo));
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据期次编号获取集电线信息")
    @GetMapping("/getCollectorwireprojectByProjectNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "collectorwireNo", value = "集电线编号"),
            @ApiImplicitParam(name = "wireName", value = "集电线名称"),
    })
    public AjaxResult getCollectorwireprojectByProjectNo(@RequestParam(value = "stationNo") String stationNo,
                                                         @RequestParam(value = "projectNo") String projectNo) {
        JSONObject json = new JSONObject();
        List<Collectorwireproject> list = collectorwireprojectService.selectCollectorwireprojectByProjectNo(Convert.toLong(stationNo), Convert.toLong(projectNo));
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据场站编号获取风机信息")
    @GetMapping("/getWturByStationNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
            @ApiImplicitParam(name = "wturNo", value = "风机编号"),
            @ApiImplicitParam(name = "name", value = "风机名称"),
    })
    public AjaxResult getWturByStationNo(@RequestParam(value = "stationNo") String stationNo) {
        JSONObject json = new JSONObject();
        List<Wtur> list = wturService.selectWturListByWindplantNo(Convert.toLong(stationNo));
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据场站期次编号获取风机信息")
    @GetMapping("/getWturByWindplantprojectNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "wtgNo", value = "风机编号"),
            @ApiImplicitParam(name = "wtgName", value = "风机名称"),
    })
    public AjaxResult getWturByWindplantprojectNo(@RequestParam(value = "stationNo") String stationNo,
                                                  @RequestParam(value = "projectNo") String projectNo) {
        JSONObject json = new JSONObject();
        List<WfProjectCwWtg> list = wfProjectCwWtgService.selectWfProjectCwWtgByPojectNo(Convert.toLong(stationNo), Convert.toLong(projectNo));
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据场站期次集电线编号获取风机信息")
    @GetMapping("/getWturByCollectorwireprojectNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "collectorwireNo", value = "集电线编号", required = true),
            @ApiImplicitParam(name = "wtgNo", value = "风机编号"),
            @ApiImplicitParam(name = "wtgName", value = "风机名称"),
    })
    public AjaxResult getWturByCollectorwireprojectNo(@RequestParam(value = "stationNo") String stationNo,
                                                      @RequestParam(value = "projectNo") String projectNo,
                                                      @RequestParam(value = "collectorwireNo") String collectorwireNo) {
        JSONObject json = new JSONObject();
        List<WfProjectCwWtg> list = wfProjectCwWtgService.selectWfProjectCwWtgByCollectorwireNo(Convert.toLong(stationNo), Convert.toLong(projectNo), Convert.toLong(collectorwireNo));
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据风机编号获取风机信息")
    @GetMapping("/getWturByWturNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wturNo", value = "风机编号", required = true),
            @ApiImplicitParam(name = "name", value = "风机名称"),
    })
    public AjaxResult getWturByWturNo(@RequestParam(value = "wturNo") String wturNo) {
        JSONObject json = new JSONObject();
        Wtur wtur = wturService.selectWturById(Convert.toLong(wturNo));
        json.put("wtur", wtur);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据菜单编号获取菜单项列表")
    @GetMapping("/getEnumerateMenu")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuNo", value = "菜单编号", required = true),
            @ApiImplicitParam(name = "itemName", value = "菜单项名称"),
            @ApiImplicitParam(name = "itemVal", value = "菜单项值"),
    })
    public AjaxResult getEnumerateMenu(@RequestParam(value = "menuNo") String menuNo) {
        JSONObject json = new JSONObject();
        json.put("list", enumMenuItemService.selectEnumMenuItemByMenuNoList(Convert.toLong(menuNo)));
        return AjaxResult.success(json);
    }

    @ApiOperation("下载模板")
    @GetMapping("/dexcel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateName", value = "模板名称", required = true),
    })
    public void dexcel(HttpServletResponse response, HttpServletRequest request, String templateName) {
        String filePath = RuoYiConfig.getDownloadPath();
        InputStream inputStream = null;
        try {
            File file = new File(filePath + templateName + ".xlsx");
            inputStream = new FileInputStream(file);
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String((RandomUtil.randomNumbers(18) + ".xlsx").getBytes()));
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            //response.setContentType("application/x-xls");
            response.setContentType("application/octet-stream; charset=utf-8");
            response.addHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
            response.addHeader("Access-Control-Expose-Headers", "token, uid, Content-Disposition");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Access-Control-Allow-Credentials", "true");
            toClient.write(buffer);
            toClient.flush();
            //关闭，即下载
            toClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
