package com.sifang.modules.monitor.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.monitor.dao.MonitorCollectDataDao;
import com.sifang.modules.monitor.service.MonitorCollectDataService;
import com.sifang.modules.monitor.vo.ThreeDaysPowerVo;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import com.sifang.modules.youkua.service.NhPowerStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/monitor/collect/data")
public class MonitorCollectDataController extends BaseController {

    @Autowired
    private MonitorCollectDataService collectDataService;

    @Autowired
    private CustomerEntryService entryService;

    @Autowired
    private NhPowerStationService nhPowerStationService;

    @Autowired
    private MonitorCollectDataDao collectDataDao;

    @RequestMapping("toviewPowerGenerationPage")
    public String toviewPowerGenerationPage(String id, Model model) {
        ThreeDaysPowerVo vo = nhPowerStationService.getThreeDaysPower(id);
        CustomerEntry entry = entryService.getById(id);
        if (entry.getFirstGenerationTime()!=null){
            vo.setFirstGenerate(DateUtils.formatDate(entry.getFirstGenerationTime(), "yyyy-MM-dd"));
        }
//        model.addAttribute("data", collectDataService.getDataInThreeDays(id));
        model.addAttribute("actualCapacity", entry.getActualCapacity().divide(BigDecimal.valueOf(1000)));
        model.addAttribute("data", vo);
        return "modules/monitor/viewPowerGenerationPage";
    }

    @RequestMapping("toViewMonitorPage")
    public String toViewMonitorPage(String id, Model model) {
        CustomerEntry entry = entryService.getById(id);
        model.addAttribute("id", id);
        model.addAttribute("customerEntry", entry);
        model.addAttribute("actualCapacity", entry.getActualCapacity().divide(BigDecimal.valueOf(1000)));//将单位转为kwh
//        model.addAttribute("dataVo", collectDataService.overviewStaticData(id));
//        model.addAttribute("curData", collectDataService.currentYearGeneration(id, "year"));
        model.addAttribute("powerStationVo",nhPowerStationService.getStationRealTimeInfo(id));
        model.addAttribute("weather", collectDataService.findWeatherByCity(entry.getCity()).getData());
        //获取日期和星期
        model.addAttribute("year", DateUtils.getYear());
        model.addAttribute("month", DateUtils.getMonth());
        model.addAttribute("day", DateUtils.getDay());
        model.addAttribute("week", DateUtils.getWeek());
        Map<String, String> map = new HashMap<>();
        map.put("id",id);
        List<Record> list = collectDataDao.selectEquipmentInfoList(null,map);
        model.addAttribute("equipmentInfoList", list);
        return "modules/monitor/viewMonitorPage";
    }

    /**
     * 发电趋势
     */
    @ResponseBody
    @RequestMapping(value = "powerGenerationTrends")
    public Result powerGenerationTrends(String entryId, String type, String time) {
//        Map<String, Object> result = collectDataService.powerGenerationTrends(entryId, type, time);
        if ("0".equals(type)){
            type="day";
        }else if("1".equals(type)){
            type = "month";
        }else if("2".equals(type)){
            type = "year";
        }
        Map<String, Object> result = nhPowerStationService.getPowerGenerationTrends(entryId, type, time);
        return Result.success(result);
    }

    @RequestMapping("toEquipmentInformationPage")
    public String toEquipmentInformationPage(String id, Model model) {
        model.addAttribute("id", id);
        return "modules/monitor/equipmentInformation";
    }

    @ResponseBody
    @RequestMapping(value = "/equipmentInfoList")
    public Object equipmentInfoList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        collectDataService.equipmentInfoList(page,map);
        return page.renderJson();
    }
}
