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.model.MoveTransformDetailDto;
import com.cim.idm.model.MoveTransformDto;
import com.cim.idm.service.Impl.MoveTransformServiceImpl;
import com.cim.idm.utils.AjaxResult;
import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("形态转换接口")
@RestController
@RequestMapping("/api/MoveTransform")
@EnableAutoConfiguration
public class MoveTransformController {
	
	 private static Log log = LogFactory.getLog(MoveTransformController.class);
	 
	 @Autowired
	 private MoveTransformServiceImpl moveTransformServiceImpl;
	 
	@RequestMapping(value = "/MoveTransform", method = RequestMethod.POST)
	public AjaxResult MoveTransform(@RequestBody JSONObject in ) throws Exception{

		log.info(in);

		MoveTransformDto move = JSON.toJavaObject(in, MoveTransformDto.class);
		
		String user = move.getUser();//作业员
		String commitDate = move.getCommitDate();
		List<MoveTransformDetailDto> from = move.getFrom();//转换前
		List<MoveTransformDetailDto> to = move.getTo();//转换后
		
		//转换前信息调用其他出接口,并更新库存信息
		
		//转换后调用其他入接口,并更新库存信息
		boolean moveTransformOut = moveTransformServiceImpl.MoveTransformOut(from, to, user,commitDate);
		if (moveTransformOut != true) {
			return AjaxResult.me().setErrorCode(500).setMessage("审核失败");
		} else {
			//更新形态转换结果
			String sql = " UPDATE IC_TRANSFORM_H T SET T.RECEIVEREQUESTSTATE = 'Completed' "
					+ "WHERE T.RECEIVEREQUESTNAME  = :RECEIVEREQUESTNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("RECEIVEREQUESTNAME", from.get(0).getRECEIVEREQUESTNAME());
			IDMFrameServiceProxy.getSqlTemplate().update(sql, hashMap);


			// 查询当前标签
			List<Map<String, Object>> args = new ArrayList<Map<String, Object>>();
			for (MoveTransformDetailDto map : to) {

				String sqlPack = "SELECT * FROM MATERIALPACKING m \n" +
						"WHERE CHARGE = :CHARGE AND MATERIALSPECNAME = :MATERIALSPECNAME AND RECEIVEREQUESTNAME=:RECEIVEREQUESTNAME";
				Map<String, Object> hashMapPack = new HashMap<String,Object> ();
				hashMapPack.put("CHARGE", map.getCHARGE());
				hashMapPack.put("MATERIALSPECNAME", map.getMATERIALSPECNAME());
				hashMapPack.put("RECEIVEREQUESTNAME", map.getRECEIVEREQUESTNAME());
				List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlPack, hashMapPack);
				args.add(queryForList.get(0));
			}
			return AjaxResult.me().setResultObj(args);
		}
		
		

	}
}
