package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.CheckPlan;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.EventInfoUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.cim.idm.service.Impl.CheckPlanServiceImpl;

@Api("盘点接口")
@RestController
@RequestMapping("/checkPlan")
@EnableAutoConfiguration
public class CheckPlanController {
	@Autowired
	private CheckPlanServiceImpl CheckPlanServiceImpl;

	@RequestMapping(value = "/PDACreateCheckRecord", method = RequestMethod.POST)
	public AjaxResult getCheckPlanList(@RequestBody JSONObject in ) throws Exception{

		CheckPlan CheckPlan = JSON.toJavaObject(in, CheckPlan.class);
		String siteName = CheckPlan.getSiteName();
		String materialPackingName = CheckPlan.getMaterialPackingName();
		String user = CheckPlan.getUser();
		String checkPlanName = CheckPlan.getCheckPlanName();
		//String planDate = CheckPlan.getPlanDate();
		String actlocation = CheckPlan.getACTLOCATION();
		String erplocation = CheckPlan.getERPLOCATION();
		String checktimekey = CheckPlan.getCHECKTIMEKEY();
		String sql = "SELECT m.MATERIALPACKINGNAME,m.ERPFACTORY ,m.ERPLOCATION ,m.LOCATIONNAME,m.MATERIALQUANTITY "
				+ "FROM MATERIALPACKING m "
				+ "WHERE m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME AND m.SITENAME = :SITENAME AND STOCKSTATE = 'Stocked' ";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SITENAME",siteName); 
		bindMap.put("MATERIALPACKINGNAME",materialPackingName); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
//			throw new GlobalException("未找到对应的库存数据！");
			return AjaxResult.me().setSuccess(false).setResultObj(null).setMessage("未找到对应的库存数据！");
		}
		//获取盘点单的库位和数量
		String sql2 = "SELECT * "
				+ "FROM CHECKPLAN c "
				+ "WHERE c.CHECKPLANNAME = :CHECKPLANNAME AND c.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("CHECKPLANNAME", checkPlanName);
		hashMap.put("MATERIALPACKINGNAME", materialPackingName);
		List<Map<String,Object>> list2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql2, hashMap);
		
		String sql3 = "SELECT * "
				+ "FROM CHECKPLAN c "
				+ "WHERE c.CHECKPLANNAME = :CHECKPLANNAME ";
		
		List<Map<String,Object>> list3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql3, hashMap);
		if(Objects.isNull(list3) || list3.isEmpty()) {
//			throw new GlobalException("未找到对应的盘点计划！");
			return AjaxResult.me().setSuccess(false).setResultObj(null).setMessage("未找到对应的盘点计划！");
		}
		//拆分Box
		String[] split = materialPackingName.split("\\|");
		String syslocation="";
		if(list2.size()>0) 
		{
			syslocation =  list2.get(0).get("SYSLOCATION") == null ? "" : list2.get(0).get("SYSLOCATION").toString();
		}
		else 
		{
			syslocation =  list.get(0).get("LOCATIONNAME") == null ? "" : list.get(0).get("LOCATIONNAME").toString();
		}
		
		String MATERIALQUANTITY =  list.get(0).get("MATERIALQUANTITY") == null ? "" : list.get(0).get("MATERIALQUANTITY").toString();
		String planDate = list3.get(0).get("PLANDATE") == null ? "" : list3.get(0).get("PLANDATE").toString();
		String qty = split[7];
		EventInfo eventInfo = new EventInfoUtil().makeEventInfo("PDACreateCheckRecord", user, "PDACreateCheckRecord", "", "");
		AjaxResult pdaCreateCheckRecord1 = CheckPlanServiceImpl.PDACreateCheckRecord1(eventInfo, planDate, checkPlanName, materialPackingName, actlocation, syslocation, 
				siteName, erplocation, MATERIALQUANTITY, qty, checktimekey, "Checked", "PDACheck", split[0], "", "", "");
		if(pdaCreateCheckRecord1.getErrorCode() == 1) {
			return AjaxResult.me().setSuccess(true).setResultObj(list);
		} else {
			return AjaxResult.me().setSuccess(false).setMessage("操作失败 已盘点");
		}
	}
}
