package com.steerinfo.ems.tcm0348.controller;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.tcm0315.model.TCm0315;
import com.steerinfo.ems.tcm0348.model.TCm0348;
import com.steerinfo.ems.tcm0348.service.ITCm0348Service;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TCm0348 RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2019-10-28 09:32
 * 类描述
 * 修订历史：
 * 日期：2019-10-28
 * 作者：generator
 * 参考：
 * 描述：TCm0348 RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/tcm0348s")
public class TCm0348Controller extends BaseRESTfulController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TCm0348Controller.class);

    @Autowired
    ITCm0348Service tCm0348Service;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0348:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0348> list = tCm0348Service.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="动力监测数据录入", notes="根据url的id来指定更新对象，并根据传过来的tCm0348信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tCm0348", value = "详细实体tCm0348", required = true, dataType = "TCm0348")
    })
    //@RequiresPermissions("tcm0348:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody TCm0348 model) {
        if(model.getMonitordate() != null && !"".equals(model.getMonitordate()) && model.getMonitordate().indexOf(model.getInsertTime()) == -1){
            return failed(null, "监测日期错误，不能修改");
        }
        else if(model.getDatadate() != null && !"".equals(model.getDatadate()) && model.getDatadate().indexOf(model.getInsertTime()) == -1){
            return failed(null, "监测时间错误，不能修改");
        }else {
            LOGGER.info("动力监测数据录入表修改前的数据是" + tCm0348Service.getById(id).toString());
            model.setRecordid(id);
            TCm0348 tCm0348 = tCm0348Service.modify(model);
            LOGGER.info("动力监测数据录入表修改后的数据是" + tCm0348.toString());
            return success(tCm0348);
        }
    }

    @ApiOperation(value="动力监测数据查询界面查询", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0348:view")
    @GetMapping(value = "/dataquery/")
    public RESTfulResult list2(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        if(parmas.get("monitordate") != null && parmas.get("monitordatee") != null && parmas.get("monitordate").toString().compareTo(parmas.get("monitordatee").toString()) > 0){
            return failed(null, "检测日期出现错误，开始时间不能大于结束时间");
        }
        else{
            PageList<TCm0348> list = tCm0348Service.dataQuery(parmas, pageNum, pageSize);
            return success(list);
        }
    }

    @ApiOperation(value="动力历史数据对比分析界面查询", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0348:view")
    @GetMapping(value = "/datacomparison/")
    public RESTfulResult list3(@RequestParam HashMap params,Integer pageNum, Integer pageSize){
        if(params.get("timegranid").toString() == null || "".equals(params.get("timegranid"))){
            return failed(null, "时间粒度不能为空！");
        }
        else if(params.get("monitordate").toString() == null || "".equals(params.get("monitordate"))){
            return failed(null, "对比日期不能为空！");
        }
        else if(params.get("monitordatee").toString() == null || "".equals(params.get("monitordatee"))){
            return failed(null, "被对比日期不能为空！");
        }
        else {
            PageList<TCm0348> list = tCm0348Service.datacomparison(params, pageNum, pageSize);
            return success(list);
        }
    }

    @ApiOperation(value="动力质量考核指标统计界面查询", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0348:view")
    @GetMapping(value = "/qualityassessment/")
    public RESTfulResult list4(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0348> list = tCm0348Service.qualityassessment(parmas, pageNum, pageSize);
        return success(list);
    }

    public void add(TCm0315 tCm0315) {
        List<TCm0348> list = new ArrayList<>();
        List<String> times = DateUtils.getMonitortime(tCm0315.getTimegranid(), tCm0315.getCreatyear(), tCm0315.getCreatyear() + "-01-01");
        for(int j = 1;j <= Long.parseLong(tCm0315.getDetectionf());j++) {
            for (int k = 0; k < times.size(); k++) {
                TCm0348 tCm0348 = new TCm0348();
                tCm0348.setBatchno(String.valueOf(j));
                tCm0348.setSitem(tCm0315.getSitem());
                tCm0348.setUnit(tCm0315.getUnit());
                tCm0348.setPlanid(tCm0315.getRecordid() +tCm0315.getSitem());
                tCm0348.setPowerdetecdepartment(tCm0315.getPowerdetecdepartment());
                tCm0348.setPowermonitorname(tCm0315.getPowermonitorname());
                tCm0348.setPowerpowersystemname(tCm0315.getPowersystemname());
                tCm0348.setTimegranid(tCm0315.getTimegranid());
                tCm0348.setInsertTime(times.get(k));
                tCm0348.setMonitordate(times.get(k));
                list.add(tCm0348);
            }
        }
        tCm0348Service.batchAdd(list);
    }
    public void update(TCm0315 before, TCm0315 tCm0315){
        Map<String, Object> parameters = new HashMap();
        List<TCm0348> list = new ArrayList<>();
        //时间粒度,系统名称,监测部门,监测点,检测项目,制定年度其中一项发生变化
        if(!before.getSitem().equals(tCm0315.getSitem()) || !before.getPowersystemname().equals(tCm0315.getPowersystemname()) || !before.getPowerdetecdepartment().equals(tCm0315.getPowerdetecdepartment()) || !before.getPowermonitorname().equals(tCm0315.getPowermonitorname()) || !before.getTimegranid().equals(tCm0315.getTimegranid()) || !before.getCreatyear().equals(tCm0315.getCreatyear())){
            parameters.put("planid", tCm0315.getRecordid() + tCm0315.getSitem());
            String time = tCm0348Service.deleteprevious(parameters) == null ? tCm0315.getCreatyear() + "-01-01" : tCm0348Service.deleteprevious(parameters);
            List<String> times = DateUtils.getMonitortime(tCm0315.getTimegranid(), tCm0315.getCreatyear(), time);
            for (int i = 1; i <= Integer.parseInt(tCm0315.getDetectionf()); i++) {
                for (int j = 0; j < times.size(); j++) {
                    TCm0348 tCm0348 = new TCm0348();
                    tCm0348.setBatchno(String.valueOf(i));
                    tCm0348.setInsertTime(times.get(j));
                    tCm0348.setMonitordate(times.get(j));
                    tCm0348.setPlanid(tCm0315.getRecordid() +tCm0315.getSitem());
                    tCm0348.setUnit(tCm0315.getUnit());
                    tCm0348.setTimegranid(tCm0315.getTimegranid());
                    tCm0348.setSitem(tCm0315.getSitem());
                    tCm0348.setPowerdetecdepartment(tCm0315.getPowerdetecdepartment());
                    tCm0348.setPowermonitorname(tCm0315.getPowermonitorname());
                    tCm0348.setPowerpowersystemname(tCm0315.getPowersystemname());
                    list.add(tCm0348);
                }
            }
            tCm0348Service.batchAdd(list);
        }
        else {    //根据监测频次修改数据录入表
            List<String> times2 = DateUtils.getMonitortime(tCm0315.getTimegranid(), tCm0315.getCreatyear(), tCm0315.getCreatyear() + "-01-01");
            if(tCm0315.getDetectionf().compareTo(before.getDetectionf()) > 0){
                for(int i = Integer.parseInt(before.getDetectionf()) + 1;i <= Integer.parseInt(tCm0315.getDetectionf());i++){
                    for (int j = 0;j < times2.size();j++){
                        TCm0348 newtCm0348 = new TCm0348();
                        newtCm0348.setBatchno(String.valueOf(i));
                        newtCm0348.setInsertTime(times2.get(j));
                        newtCm0348.setMonitordate(times2.get(j));
                        newtCm0348.setPlanid(tCm0315.getRecordid() +tCm0315.getSitem());
                        newtCm0348.setUnit(tCm0315.getUnit());
                        newtCm0348.setTimegranid(tCm0315.getTimegranid());
                        newtCm0348.setSitem(tCm0315.getSitem());
                        newtCm0348.setPowerdetecdepartment(tCm0315.getPowerdetecdepartment());
                        newtCm0348.setPowermonitorname(tCm0315.getPowermonitorname());
                        newtCm0348.setPowerpowersystemname(tCm0315.getPowersystemname());
                        list.add(newtCm0348);
                    }
                }
                tCm0348Service.batchAdd(list);
            }
            else {
                parameters.put("planid", tCm0315.getRecordid() + tCm0315.getSitem());
                parameters.put("lowerpici",tCm0315.getDetectionf());
                parameters.put("upperpici",before.getDetectionf());
                tCm0348Service.deleteprevious(parameters);
            }
        }
    }
    public void delete(TCm0315 model){
        Map<String, Object> parameters = new HashMap();
        parameters.put("planid",model.getRecordid() +model.getSitem());
        tCm0348Service.deleteprevious(parameters);
    }
}
