package com.cim.idm.controller;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.response.RespGenerator;
import com.cim.idm.utils.EventInfoUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.index.qual.SameLen;
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.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Api("无订单无批次缓存仓")
@RestController
@RequestMapping("/stockIn")
@EnableAutoConfiguration
@Slf4j
public class CacheWareController {

	@RequestMapping(value = "/addNChargeNOrder", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> editMaterial(@RequestBody Map<String,Object> params){
		log.info("CacheWareController addNChargeNOrder", params);
		String sitename = (String)params.get("SITENAME");
		String userid = (String)params.get("USERID");
		String materialspecname = (String)params.get("MATERIALSPECNAME");
		LocalDateTime makedatet = LocalDateTime.now();
		Timestamp makedate = Timestamp.valueOf(makedatet);

		String materialquantity = (String)params.get("MATERIALQUANTITY");
		String erpfactory = (String)params.get("ERPFACTORY");
		String unit = (String)params.get("UNIT");
		String truegg = (String)params.get("TRUEGG");
		String remark = (String)params.get("REMARK");
		String is_sure = (String)params.get("IS_SURE");


		String sql = "INSERT INTO MATERIALCACHE (SITENAME,USERID,MATERIALSPECNAME, MAKEDATE,MATERIALQUANTITY,ERPFACTORY,UNIT,TRUEGG,REMARK,IS_SURE) \r\n" +
				"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		Object[] args = new Object[]{sitename, userid, materialspecname,   makedate, materialquantity,
				erpfactory, unit, truegg, remark, is_sure };
		IDMFrameServiceProxy.getSqlTemplate().update(sql, args);
		EventInfo eventInfo = new EventInfoUtil().makeEventInfo("addNChargeNOrder", userid, "addNChargeNOrder");

		BaseResponse returnOK = RespGenerator.returnOK(params);
		return returnOK;
	}

	@RequestMapping(value = "/checkNChargeNOrder", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> checkNChargeNOrder(@RequestBody Map<String,Object> params) throws ParseException {
		
		String sitename = (String)params.get("SITENAME");
		String userid = (String)params.get("USERID");
		String materialspecname = (String)params.get("MATERIALSPECNAME");
		String makedatet = (String) params.get("MAKEDATE");
		Timestamp makedate = Timestamp.valueOf(makedatet);
//		 @TODO 原代码异常：Timestamp makedate = Timestamp.valueOf(makedatet);
//		Timestamp makedate = Timestamp.from(
//				LocalDateTime.parse(makedatet, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
//						.atZone(ZoneOffset.UTC).toInstant());
		String is_sure = (String)params.get("IS_SURE");

		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("SITENAME", sitename);
		bindMap.put("MATERIALSPECNAME", materialspecname);
		bindMap.put("MAKEDATE", makedate);
		bindMap.put("IS_SURE", is_sure);
		
		String sql = "UPDATE MATERIALCACHE E SET E.IS_SURE = :IS_SURE WHERE E.MATERIALSPECNAME = :MATERIALSPECNAME AND SITENAME = :SITENAME AND MAKEDATE = :MAKEDATE";
		IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap);

		EventInfo eventInfo = new EventInfoUtil().makeEventInfo("checkNChargeNOrder", userid, "checkNChargeNOrder");

		BaseResponse returnOK = RespGenerator.returnOK(params);
        return returnOK;

	}
}
