package com.steerinfo.ems.trmworkprocmaterialvalue.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.ems.formula.service.IFormulaService;
import com.steerinfo.ems.ifmesemsswapfile.service.IIfMesEmsSwapfileService;
import com.steerinfo.ems.trmworkprocmaterial.mapper.TRmWorkprocMaterialMapper;
import com.steerinfo.ems.trmworkprocmaterial.model.TRmWorkprocMaterial;
import com.steerinfo.ems.trmworkprocmaterialvalue.mapper.TRmWorkprocMaterialValueMapper;
import com.steerinfo.ems.trmworkprocmaterialvalue.model.TRmWorkprocMaterialValue;
import com.steerinfo.ems.trmworkprocmaterialvalue.service.ITRmWorkprocMaterialValueService;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    ITRmWorkprocMaterialValueService tRmWorkprocMaterialValueService;
    
    @Autowired
    private TRmWorkprocMaterialMapper tRmWorkprocMaterialMapper;
    
    @Autowired
    IIfMesEmsSwapfileService ifMesEmsSwapfileService;
    
    @Autowired
    IFormulaService formulaService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("trmworkprocmaterialvalue:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TRmWorkprocMaterialValue> list = tRmWorkprocMaterialValueService.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("trmworkprocmaterialvalue:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TRmWorkprocMaterialValue> list = tRmWorkprocMaterialValueService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据TRmWorkprocMaterialValue对象创建")
    @ApiImplicitParam(name = "tRmWorkprocMaterialValue", value = "详细实体tRmWorkprocMaterialValue", required = true, dataType = "TRmWorkprocMaterialValue")
    //@RequiresPermissions("trmworkprocmaterialvalue:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute TRmWorkprocMaterialValue model){
        TRmWorkprocMaterialValue tRmWorkprocMaterialValue = tRmWorkprocMaterialValueService.add(model);
        return success(tRmWorkprocMaterialValue);
    }
    
    @ApiOperation(value="获取详细信息", notes="根据主键(itemid, clock, timegranid)来指定对象")
    //@RequiresPermissions("trmworkprocmaterialvalue:view")
    @PostMapping(value = "/info", produces  = "application/json;charset=UTF-8")
    public RESTfulResult get(@RequestBody TRmWorkprocMaterialValue model){
    	if(model.getClock() == null || model.getClock().isEmpty()){
    		return failed(null, "请传入时间");
    	}
    	if(model.getTimegranid() == null || model.getTimegranid().isEmpty()){
    		return failed(null, "请传入时间粒度");
    	}
    	if(model.getItemid() == null || model.getItemid().isEmpty()){
    		return failed(null, "请传入项目编号");
    	}
    	HashMap<String, Object> m = new HashMap<String, Object>();
    	m.put("clock", model.getClock());
    	m.put("timegranid", model.getTimegranid());
    	m.put("itemid", model.getItemid());
        model.setId(m);
        TRmWorkprocMaterialValue tRmWorkprocMaterialValue = tRmWorkprocMaterialValueService.getById(m);
        return success(tRmWorkprocMaterialValue);
    }

    @ApiOperation(value="更新详细信息", notes="根据主键(itemid, clock, timegranid)来指定更新对象，并根据传过来的tRmWorkprocMaterialValue信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tRmWorkprocMaterialValue", value = "详细实体tRmWorkprocMaterialValue", required = true, dataType = "TRmWorkprocMaterialValue")
    })
    //@RequiresPermissions("trmworkprocmaterialvalue:update")
    @PutMapping(value = "/updateForUKey/", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@RequestBody TRmWorkprocMaterialValue model){
    	if (model == null) {
    		return failed(null, "请传入主键参数");
    	}
    	if (model.getItemid()== null || model.getItemid().isEmpty()) {
    		return failed(null, "请传入主键参数itemid");
    	}
    	if (model.getClock() == null || model.getClock().isEmpty()) {
    		return failed(null, "请传入主键参数clock");
    	}
    	if (model.getTimegranid() == null || model.getTimegranid().isEmpty()) {
    		return failed(null, "请传入主键参数timegranid");
    	}
        TRmWorkprocMaterialValue tRmWorkprocMaterialValue = tRmWorkprocMaterialValueService.updateForUKey(model);
        return success(tRmWorkprocMaterialValue);
    }
    
    @ApiOperation(value="更新详细信息", notes="根据主键(itemid, clock, timegranid)来指定更新对象，并根据传过来的tRmWorkprocMaterialValue信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tRmWorkprocMaterialValue", value = "详细实体tRmWorkprocMaterialValue", required = true, dataType = "TRmWorkprocMaterialValue")
    })
    //@RequiresPermissions("trmworkprocmaterialvalue:update")
    @PutMapping(value = "/batchupdate/", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@RequestBody TRmWorkprocMaterialValue[] models){
    	for(int i = 0; i < models.length; i++){
    		TRmWorkprocMaterialValue model = models[i];
	    	if (model == null) {
	    		return failed(null, "请传入主键参数");
	    	}
	    	if (model.getItemid()== null || model.getItemid().isEmpty()) {
	    		return failed(null, "请传入主键参数itemid");
	    	}
	    	if (model.getClock() == null || model.getClock().isEmpty()) {
	    		return failed(null, "请传入主键参数clock");
	    	}
	    	if (model.getTimegranid() == null || model.getTimegranid().isEmpty()) {
	    		return failed(null, "请传入主键参数timegranid");
	    	}
	        TRmWorkprocMaterialValue tRmWorkprocMaterialValue = tRmWorkprocMaterialValueService.updateForUKey(model);
    	}
    	return success();
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("trmworkprocmaterialvalue:delete")
    @DeleteMapping(value = "/delete", produces  = "application/json;charset=UTF-8")
    public RESTfulResult delete(@RequestBody TRmWorkprocMaterialValue model){
    	if(model.getClock() == null || model.getClock().isEmpty()){
    		return failed(null, "请传入时间");
    	}
    	if(model.getTimegranid() == null || model.getTimegranid().isEmpty()){
    		return failed(null, "请传入时间粒度");
    	}
    	if(model.getItemid() == null || model.getItemid().isEmpty()){
    		return failed(null, "请传入项目编号");
    	}
    	HashMap<String, Object> m = new HashMap<String, Object>();
    	m.put("clock", model.getClock());
    	m.put("timegranid", model.getTimegranid());
    	m.put("itemid", model.getItemid());
        model.setId(m);
        tRmWorkprocMaterialValueService.delete(m);
        return success();
    }
    
    @ApiOperation(value="获得工序与原材料数据维护数据", notes="能源平衡管理-工序与原材料数据维护页面，分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/getWorkprocMaterialDetailForPage")
    public RESTfulResult getWorkprocMaterialDetailForPage(@RequestParam HashMap<String, Object> parmas,Integer pageNum, Integer pageSize){
    	if(parmas.containsKey("materialid") && parmas.get("materialid") != null && !"".equals(parmas.get("materialid").toString())){
            String materialid = parmas.get("materialid").toString();
            if(materialid.indexOf(",") == -1){
                parmas.put("materialid", "and materialid = '" + materialid + "'");
            } else {
            	materialid = "and materialid in ('" + materialid.replaceAll(",", "','") + "')";
                parmas.put("materialid", materialid);
            }
        }
    	
    	if(parmas.containsKey("workprocid") && parmas.get("workprocid") != null && !"".equals(parmas.get("workprocid").toString())){
            String workprocid = parmas.get("workprocid").toString();
            if(workprocid.indexOf(",") == -1){
                parmas.put("workprocid", "and workprocid = '" + workprocid + "'");
            } else {
            	workprocid = "and workprocid in ('" + workprocid.replaceAll(",", "','") + "')";
                parmas.put("workprocid", workprocid);
            }
        }
    	
    	if(parmas.containsKey("startDate") && parmas.get("startDate") != null && !"".equals(parmas.get("startDate").toString()) &&
        	parmas.containsKey("endDate") && parmas.get("endDate") != null && !"".equals(parmas.get("endDate").toString())){
        	String begin = parmas.get("startDate").toString();
        	String end = parmas.get("endDate").toString();
        		
        	parmas.put("clock", "and clock >='" + begin + "' and clock <= '" + end + "'");
        }
    	
    	PageList<Map<String, Object>> list = tRmWorkprocMaterialValueService.getWorkprocMaterialDetailForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="立即获取MES原材料信息", notes="将IF_MES_EMS_SWAPFILE中未读取的数据进行处理，将数据保存至T_RM_WORKPROC_MATERIAL_VALUE")
    @GetMapping(value = "/synchronousData")
    public RESTfulResult synchronousData(){
        tRmWorkprocMaterialValueService.synchronousData();
        return success();
    }
   
    @GetMapping("/bbb")
    public RESTfulResult getState() {
    	tRmWorkprocMaterialValueService.setDate(new Date());
    	return success();
    }
    
    @ApiOperation(value="重新计算或生成项目数据-(月、年)")
    @GetMapping(value = "/restatWmv")
    public RESTfulResult restatWmv(@RequestParam HashMap<String, Object> parmas, String clock, String clocke, String timegranid){
		if (clock==null || clock.isEmpty()) {
    		return failed(null, "请传入起始时间");
    	}
		if (timegranid==null || timegranid.isEmpty()) {
    		return failed(null, "请传入时间粒度");
    	}
		ifMesEmsSwapfileService.synchronousMesAnEmsData();
		tRmWorkprocMaterialValueService.synchronousData();
		tRmWorkprocMaterialValueService.synchronousData4();
		tRmWorkprocMaterialValueService.synchronousData5();
		parmas.put("useflag", "1");
		// 查询需要统计的计量点信息
		List<TRmWorkprocMaterial> l = tRmWorkprocMaterialMapper.selectByParameters(parmas);
		String res = "";
		for(int i = 0; i<l.size(); i++){
			TRmWorkprocMaterial t = l.get(i);
			res = formulaService.reItemVal(t.getItemid(),clock,timegranid,clocke);
		}
		if(!res.isEmpty()){
			return failed(null, res);
		}
        return success();
    }
    
    @ApiOperation(value="批量重新计算", notes="")
    @ApiImplicitParam(name = "TRmWorkprocMaterialValue", value = "详细实体TRmWorkprocMaterialValue", required = true, dataType = "TRmWorkprocMaterialValue")
    @PutMapping(value = "/batchCompute", produces  = "application/json;charset=UTF-8")
    public RESTfulResult batchCompute(@RequestBody TRmWorkprocMaterialValue[] model){
    	if(model.length<1){
    		return failed(null, "请勾选数据");
    	}
    	for (int i = 0; i< model.length; i++) {
    		TRmWorkprocMaterialValue tv = model[i];
    		tRmWorkprocMaterialValueService.restatData(tv.getItemid(), tv.getClock(), tv.getTimegranid());
    	}
    	return success();
    }
}
