package com.steerinfo.ems.trmtransfereactvalue.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.trmtransfereactvalue.mapper.TRmTransfereActValueMapper;
import com.steerinfo.ems.trmtransfereactvalue.model.TRmTransfereActValue;
import com.steerinfo.ems.trmtransfereactvalue.service.ITRmTransfereActValueService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
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.ParseException;
import java.text.SimpleDateFormat;

/**
 * TRmTransfereActValue RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-03-11 03:26
 * 类描述
 * 修订历史：
 * 日期：2020-03-11
 * 作者：generator
 * 参考：
 * 描述：TRmTransfereActValue RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/trmtransfereactvalues")
public class TRmTransfereActValueController extends BaseRESTfulController {
	
	private static final Logger logger = LoggerFactory.getLogger(TRmTransfereActValueController.class);

	@Autowired
    ITRmTransfereActValueService tRmTransfereActValueService;
	
	@Autowired
    private TRmTransfereActValueMapper tRmTransfereActValueMapper;

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("trmtransfereactvalue:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap<String, Object> parmas,Integer pageNum, Integer pageSize){
        PageList<TRmTransfereActValue> list = tRmTransfereActValueService.queryLikeForPage(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("trmtransfereactvalue:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap<String, Object> parmas,Integer pageNum, Integer pageSize){
    	if(parmas.get("clock")==null || parmas.get("clocke")==null){
    		return failed(null, "请选择时间");
    	}
        PageList<Map<String, Object>> list = tRmTransfereActValueService.getDateForPage(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")
    })
    @GetMapping(value = "/getDateEditForPage")
    public RESTfulResult getDateEditForPage(@RequestParam HashMap<String, Object> parmas,Integer pageNum, Integer pageSize){
    	if(parmas.get("networkid")==null){
    		return failed(null, "请选择工序-站所");
    	}
    	if(parmas.get("clock")==null || parmas.get("clocke")==null){
    		return failed(null, "请选择生成时间");
    	}
    	try {
			Date start = new SimpleDateFormat("yyyy-MM-dd").parse(parmas.get("clock").toString());
			Date end = new SimpleDateFormat("yyyy-MM-dd").parse(parmas.get("clocke").toString());
			if (((end.getTime()-start.getTime()) / (24 * 60 * 60 * 1000)) > 366){
				return failed(null, "生成时间跨度不得超过1年");
			}
		} catch (ParseException e) {
			return failed(null, "时间格式错误");
		}
    	PageList<Map<String, Object>> list = tRmTransfereActValueService.getDateEditForPage(parmas, pageNum, pageSize);
    	if(list == null) {
    		return failed(null, "日期格式有误");
    	}
    	if(list.getList().size()<1){
    		return failed(null, "相同日期数据已经生成，无法再次生成");
    	}
		return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据TRmTransfereActValue对象创建")
    @ApiImplicitParam(name = "tRmTransfereActValue", value = "详细实体tRmTransfereActValue", required = true, dataType = "TRmTransfereActValue")
    //@RequiresPermissions("trmtransfereactvalue:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute TRmTransfereActValue model){
        TRmTransfereActValue tRmTransfereActValue = tRmTransfereActValueService.add(model);
        return success(tRmTransfereActValue);
    }

    @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("trmtransfereactvalue:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@RequestBody TRmTransfereActValue model){
    	if(model.getLocationid() == null || model.getLocationid().isEmpty()){
    		return failed(null, "请传入位置");
    	}
    	if(model.getClock() == null || model.getClock().isEmpty()){
    		return failed(null, "请传入时间");
    	}
        TRmTransfereActValue tRmTransfereActValue = tRmTransfereActValueService.getById(model.getId());
        return success(tRmTransfereActValue);
    }

    @ApiOperation(value="更新数据", notes="根据传过来的tRmTransfereActValue信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tRmTransfereActValue", value = "详细实体tRmTransfereActValue", required = true, dataType = "TRmTransfereActValue")
    })
    //@RequiresPermissions("trmtransfereactvalue:update")
    @PutMapping(value = "/batchupdate", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@RequestBody TRmTransfereActValue[] models){
    	String failmsg = "";
        for (int i = 0; i< models.length; i++) {
        	TRmTransfereActValue model = models[i];
        	if(model.getId()!=null && !model.getId().isEmpty()){
        		if(tRmTransfereActValueMapper.selectByPrimaryKey(model.getId()).getState().equals("2")){
        			failmsg += model.getId() + ",";
        		}
        	}
        }
        if(!failmsg.isEmpty()){
        	return failed(null, "序号 [" + failmsg.substring(0, failmsg.length()-1) + "] 已审核，不允许修改");
        }
    	for (int i = 0; i< models.length; i++) {
    		TRmTransfereActValue model = models[i];
    		if(model.getClock() == null || model.getClocke() == null || model.getClock().isEmpty() || model.getClocke().isEmpty()){
    			return failed(null, "数据请传入时间");
        	}
        	if(model.getLocationid() == null || model.getLocationid().isEmpty()){
        		return failed(null, "数据请传入位置");
        	}
        	if(model.getApportvalue() == null || model.getRealvalue() == null){
        		return failed(null, "请填写完整数据");
        	}
        	if(model.getItemid() == null || model.getItemid() == null){
        		return failed(null, "缺少计量点信息");
        	}
        	// 修改数据
        	tRmTransfereActValueService.updateOrInsert(model);
    	}
    	return success();
    }

    @ApiOperation(value="删除", notes="批量删除")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("trmtransfereactvalue:delete")
    @DeleteMapping(value = "/delete", produces  = "application/json;charset=UTF-8")
    public RESTfulResult delete(@RequestBody TRmTransfereActValue[] models){
    	String userId = JwtUtil.getUseridByToken();
    	if(models.length >= 1) {
             for (int i = 0; i < models.length; i++) {
            	 TRmTransfereActValue m = models[i];
            	 if(m==null || m.getId() ==null || m.getId().isEmpty()){
            		 return failed(null, "请传入数据ID");
            	 }
            	 TRmTransfereActValue tv = tRmTransfereActValueService.getById(m.getId());
            	 if (tv == null){
            		 return failed(null, "数据已经删除，请重新查询数据");
            	 }
            	 if (tv.getState().equalsIgnoreCase("2")) {
            		 return failed(null, "已经审核的数据不能删除,序号" + tv.getLocationid());
            	 }
            	 logger.info(userId + "删除，clock：" + tv.getClock() + ",clocke" + tv.getClocke());
             }
    	} else {
    		return failed(null, "请输入要删除的数据");
    	}
    	for (int i = 0; i< models.length; i++) {
        	tRmTransfereActValueService.delete(models[i].getId());
    	}
        return success();
    }
    
    @ApiOperation(value="审核、取消审核数据", notes="根据传过来的tRmTransfereActValue信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tRmTransfereActValue", value = "详细实体tRmTransfereActValue", required = true, dataType = "TRmTransfereActValue")
    })
    @PutMapping(value = "/batchcheck2", produces  = "application/json;charset=UTF-8")
    public RESTfulResult batchcheck2(@RequestBody TRmTransfereActValue[] models){
    	for (int i = 0; i< models.length; i++) {
    		TRmTransfereActValue model = models[i];
    		if(model.getId()==null || model.getId().isEmpty() || model.getState()==null || model.getState().isEmpty()){
    			return failed(null, "审核数据错误");
        	}
    		TRmTransfereActValue t = tRmTransfereActValueService.getById(model.getId());
    		if(t==null || t.getApportvalue() == null || t.getRealvalue() == null){
    			return failed(null, "审核数据错误~");
    		}
    		t.setState(model.getState());
    		tRmTransfereActValueService.checkValue(t);
    	}
        return success();
    }
}
