package com.steerinfo.ems.emscalpointvalue.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue;
import com.steerinfo.ems.emscalpointvalue.service.IEmsCalpointValueService;
import com.steerinfo.ems.trmcalpoint.model.TRmCalpoint;
import com.steerinfo.ems.trmcalpoint.service.ITRmCalpointService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

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

    @Autowired
    IEmsCalpointValueService emsCalpointValueService;
    @Autowired
    ITRmCalpointService tRmCalpointService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("emscalpointvalue:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
		// PageList<EmsCalpointValue> list =
		// emsCalpointValueService.queryForPage(parmas, pageNum, pageSize);
		// return success(list);
    	PageList<Map<String, Object>> list = emsCalpointValueService.getDateEditForPage(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("emscalpointvalue:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<EmsCalpointValue> list = emsCalpointValueService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据EmsCalpointValue对象创建")
    @ApiImplicitParam(name = "emsCalpointValue", value = "详细实体emsCalpointValue", required = true, dataType = "EmsCalpointValue")
    //@RequiresPermissions("emscalpointvalue:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute EmsCalpointValue model){
        EmsCalpointValue emsCalpointValue = emsCalpointValueService.add(model);
        return success(emsCalpointValue);
    }

    @ApiOperation(value="批量更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的emsCalpointValue信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "emsCalpointValue", value = "详细实体emsCalpointValue", required = true, dataType = "EmsCalpointValue")
    })
    //@RequiresPermissions("emscalpointvalue:update")
    @PutMapping(value = "/batchupdate", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@RequestBody List<Map<String,Object>> mapList){ // EmsCalpointValue[] model
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.HOUR_OF_DAY, 1);
    	cal.add(Calendar.DAY_OF_YEAR, -1);
    	int hour = cal.get(Calendar.HOUR_OF_DAY);
    	if(!JwtUtil.getUseridByToken().equalsIgnoreCase("admin")){
    		for (int i = 0; i< mapList.size(); i++) {
        		String clock = mapList.get(i).get("clock").toString();
        		if(hour>8 || !new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()).equals(clock)){
            		// return failed(null, "每天晚上11点到第二天上午8点为输入底码时间");
            	}
        	}
    	}
    	for (int i = 0; i< mapList.size(); i++) {
    		if(mapList.get(i).get("mendvalue") == null || mapList.get(i).get("mintvalue") ==null){
    			return failed(null, "期末值、最终值不能为空");
    		}
    		if(mapList.get(i).get("itemid") == null || mapList.get(i).get("clock") ==null){
    			return failed(null, "计量点编号、时间不能为空");
    		}
    	}
    	for (int i = 0; i< mapList.size(); i++) {
    		EmsCalpointValue model = new EmsCalpointValue();
    		String itemid = mapList.get(i).get("itemid").toString();
    		HashMap<String, Object> m = new HashMap<String, Object>();
            m.put("clock", mapList.get(i).get("clock").toString());
            m.put("itemid", itemid);
            model.setId(m);
            model.setMintvalue(new BigDecimal(mapList.get(i).get("mintvalue").toString()));
            model.setApportvalue(new BigDecimal(mapList.get(i).get("apportvalue").toString()));
            model.setMendvalue(new BigDecimal(mapList.get(i).get("mendvalue").toString()));
            model.setBz(mapList.get(i).get("bz")==null?"":mapList.get(i).get("bz").toString());
    		model.setOperUser(JwtUtil.getUseridByToken());
    		model.setOperDate(new Date());
    		emsCalpointValueService.modifyOrInsert(model);
    		// 修改计量点序号
    		TRmCalpoint tRmCalpoint = tRmCalpointService.getById(itemid);
    		tRmCalpoint.setBdesc2(mapList.get(i).get("xh").toString());
    		tRmCalpointService.modify(tRmCalpoint);
    	}
        return success();
    }
    
    @ApiOperation(value="查询人工录入编辑数据", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/getDateEditForPage")
    public RESTfulResult getDateEditForPage(@RequestParam HashMap<String, Object> parmas,Integer pageNum, Integer pageSize){
    	if(parmas.containsKey("locateid") && parmas.get("locateid") != null && !"".equals(parmas.get("locateid").toString())){
            String locateid = parmas.get("locateid").toString();
            if(locateid.indexOf(",") == -1){
                parmas.put("locateid", "and locateid = '" + locateid + "'");
            } else {
            	locateid = "and locateid in ('" + locateid.replaceAll(",", "','") + "')";
                parmas.put("locateid", locateid);
            }
        }
    	PageList<Map<String, Object>> list = emsCalpointValueService.getDateEditForPage(parmas, pageNum, pageSize);
		return success(list);
    }

}
