package com.gyee.frame.controller.singleanalysis;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.custom.SingleAnalysisVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.singleanalysis.SingleAnalysisService;
import com.gyee.frame.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/singleanalysis")
@Api(value = "单机性能分析" ,tags="单机性能分析")
public class SingleAnalysisController {

    @Resource
    private SingleAnalysisService singleAnalysisService;

    /**
     * 单机信息总览首页
     * @param tablepar
     * @param wpId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisMain")
    @ResponseBody
    @ApiOperation(value = "查询单机性能分析首页", notes = "查询单机性能分析首页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页对象", required = true, dataType = "Tablepar", paramType = "query"),
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult singleanalysisMain(Tablepar tablepar, String wpId, String year, String month) throws Exception {

        List<SingleAnalysisVo> vos =new ArrayList<>();

        if (StringUtils.notEmp(wpId) && StringUtils.notEmp(year) && StringUtils.notEmp(month)) {

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND,0);

            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            Date endDate = cal.getTime();

            vos =singleAnalysisService.SingleAnalysisList(tablepar, wpId,beginDate,endDate);

        }

        if (null!=vos) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        }

    }

    /**
     * 单机信息总览子页面
     * @param wtId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisSub")
    @ResponseBody
    @ApiOperation(value = "查询单机性能分析子页面", notes = "查询单机性能分析子页面")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult singleanalysisSub(String wtId, String year, String month) throws Exception {

        Map<String,Object> map=new HashMap<>();

        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month)) {

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND,0);

            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            Date endDate = cal.getTime();


            SingleAnalysisVo  byzb =singleAnalysisService.SingleAnalysisListByWtId(wtId,beginDate,endDate);


            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.add(Calendar.YEAR, -1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            beginDate = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            endDate = cal.getTime();


            SingleAnalysisVo  tqzb =singleAnalysisService.SingleAnalysisListByWtId(wtId,beginDate,endDate);

            SingleAnalysisVo  tbzb =singleAnalysisService.SingleAnalysisListByWtId(byzb,tqzb);


            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            beginDate = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            endDate = cal.getTime();


            SingleAnalysisVo  hqzb =singleAnalysisService.SingleAnalysisListByWtId(wtId,beginDate,endDate);
            SingleAnalysisVo  hbzb =singleAnalysisService.SingleAnalysisListByWtId(byzb,hqzb);



            map.put("byzb",byzb);//本月指标
            map.put("tqzb",tqzb);//全年同期
            map.put("tbzb",tbzb);//同比指标
            map.put("hqzb",hqzb);//上月同期
            map.put("hbzb",hbzb);//环比指标
        }

        if (null!=map) {
            return AjaxResult.successData(AjaxStatus.success.code, map);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, map);
        }

    }


    /**
     * 单机信息总览图表接口(3个)
     * @param wtId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisChart")
    @ResponseBody
    @ApiOperation(value = "单机信息总览图表接口(3个) 发电量和风速、五项损失、静风频率和待机时间", notes = "单机信息总览图表接口(3个) 发电量和风速、五项损失、静风频率和待机时间")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult singleanalysisChart(String wtId, String year, String month) throws Exception {

        Map<String,List<SingleAnalysisVo>> map =new HashMap<>();

        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month)) {

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND,0);

            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            Date endDate = cal.getTime();

            map =singleAnalysisService.SingleAnalysisListByWtIdDetiml(wtId,beginDate,endDate);

        }

//        map.put("ff",fdlfsls);//发电量和风速
//        map.put("ws",wsls);//五项损失，绑定五个
//        map.put("jd",jfpldjsjls);//静风频率和待机时间

        if (null!=map) {
            return AjaxResult.successData(AjaxStatus.success.code, map);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, map);
        }

    }


}
