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 org.springframework.beans.factory.annotation.Autowired;
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 java.util.*;

@Controller
@RequestMapping("/singleanalysis")
public class SingleAnalysisController {

    @Autowired
    private SingleAnalysisService singleAnalysisService;

    /**
     * 单机信息总览首页
     * @param tablepar
     * @param wpId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisMain")
    @ResponseBody
    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.emptyresultset.code, vos);
        }

    }

    /**
     * 单机信息总览子页面
     * @param wtId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisSub")
    @ResponseBody
    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.emptyresultset.code, map);
        }

    }


    /**
     * 单机信息总览图表接口(3个)
     * @param wtId
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    @PostMapping("/singleanalysisChart")
    @ResponseBody
    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.emptyresultset.code, map);
        }

    }


}
