package com.steerinfo.ems.tcm0318.controller;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.ifmesemswatermonitor.service.IIfMesEmsWatermonitorService;
import com.steerinfo.ems.tcm0318.model.TCm0318;
import com.steerinfo.ems.tcm0318.service.ITCm0318Service;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TCm0318 RESTful接口：
 * @author 王金涛
 * @version 1.0-SNAPSHORT 2019-10-16 11:40
 * 类描述
 * 修订历史：
 * 日期：2019-10-16
 * 作者：王金涛
 * 参考：
 * 描述：TCm0318 RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/tcm0318s")
public class TCm0318Controller extends BaseRESTfulController {

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

    @Autowired
    ITCm0318Service tCm0318Service;
    @Autowired
    private IIfMesEmsWatermonitorService ifMesEmsWatermonitorService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0318:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0318> list = tCm0318Service.queryForPage(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("tcm0318: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<TCm0318> list = tCm0318Service.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("tcm0318:view")
    @GetMapping(value = "/datacomparison/")
    public RESTfulResult list3(@RequestParam HashMap<String, Object> params,Integer pageNum, Integer pageSize){
    	if(params.get("monitordate") == null || params.get("monitordate").toString().isEmpty()){
            return failed(null, "对比日期不能为空！");
        } else if(params.get("monitordatee") == null || params.get("monitordatee").toString().isEmpty()){
            return failed(null, "被对比日期不能为空！");
        }
        else {
            List<Map<String, Object>> list = tCm0318Service.datacomparison(params);
            return success(list);
        }
    }

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

    @ApiOperation(value="水质监测数据录入", notes="根据url的id来指定更新对象，并根据传过来的tCm0318信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tCm0318", value = "详细实体tCm0318", required = true, dataType = "TCm0318")
    })
    //@RequiresPermissions("tcm0318:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody TCm0318 model) {
        if(tCm0318Service.getById(id) == null){
            return failed(null, "对应检测数据不存在！");
        }
        if(model.getTimegranid() == null || "".equals(model.getTimegranid())){
            return failed(null, "时间粒度不能为空！");
        }
        if(model.getMonitordate() == null || "".equals(model.getMonitordate())){
            model.setMonitordate(DateUtils.getCurrentTime("yyyy-MM-dd"));
        }
        if(model.getDatadate() == null || "".equals(model.getDatadate())){
            model.setDatadate(DateUtils.getCurrentTime("HH:mm:ss"));
        }
        if("DAY".equals(model.getTimegranid()) && !model.getMonitordate().substring(0,10).equals(tCm0318Service.getById(id).getMonitordate())){
            return failed(null, "检测日期错误！");
        }
        if("MONTH".equals(model.getTimegranid()) && !model.getMonitordate().substring(0,7).equals(tCm0318Service.getById(id).getMonitordate().substring(0,7))){
            return failed(null, "检测日期错误！");
        }
        LOGGER.info("水质监测数据录入表修改前的数据是" + tCm0318Service.getById(id).toString());
        model.setRecordid(id);
        TCm0318 tCm0318 = tCm0318Service.modify(model);
        LOGGER.info("水质监测数据录入表修改后的数据是" + tCm0318.toString());
        return success(tCm0318);
    }
    /**

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


     public void delete(TCm0312 model){
     Map<String, Object> parameters = new HashMap();
     parameters.put("planid",model.getRecordid() +model.getSitem());
     tCm0318Service.deleteprevious(parameters);
     }
     @ApiOperation(value="获取列表", notes="分页模糊查询")
     @ApiImplicitParams({
     @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
     @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
     })
     //@RequiresPermissions("tcm0318:view")
     @GetMapping(value = "/like/")
     public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
     PageList<TCm0318> list = tCm0318Service.queryLikeForPage(parmas, pageNum, pageSize);
     return success(list);
     }

     @ApiOperation(value="创建", notes="根据TCm0318对象创建")
     @ApiImplicitParam(name = "tCm0318", value = "详细实体tCm0318", required = true, dataType = "TCm0318")
     //@RequiresPermissions("tcm0318:create")
     @PostMapping(value = "/")
     public RESTfulResult add(@RequestBody TCm0318 model){
     TCm0318 tCm0318 = tCm0318Service.add(model);
     return success(tCm0318);
     }

     @ApiOperation(value="删除", notes="根据用户id数组集获得用户集合信息")
     @ApiImplicitParam(name="Ids", value="用户ID数组集", required=true, paramType="query" ,allowMultiple=true, dataType = "Long")
     @DeleteMapping(value = "/")
     public RESTfulResult delete(@RequestBody List<String> Ids){
     if (Ids.size() == 1) {
     tCm0318Service.delete(Ids.get(0));
     } else {
     tCm0318Service.delete(Ids);
     }
     return success();
     }

     @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
     @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
     //@RequiresPermissions("tcm0318:view")
     @GetMapping(value = "/{id}")
     public RESTfulResult get(@PathVariable String id){
     TCm0318 tCm0318 = tCm0318Service.getById(id);
     return success(tCm0318);
     }
     */
}
