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.DingTalkSender;
import com.cim.idm.utils.DingTalkUtils;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmspackage.material.MaterialServiceProxy;
import com.cim.idm.wmspackage.material.management.data.MaterialSpecKey;
import com.cim.idm.wmspackage.material.management.info.SetSpecEventInfo;
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.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@EnableAutoConfiguration
@RequestMapping("/api/materialMaint")
public class MaterialBasicRoleController {
	
	@RequestMapping(value = "/editMaterial", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> editMaterial(@RequestBody Map<String,Object> param){
		
		String siteName = (String)param.get("SITENAME");
		String materialspecname = (String)param.get("MATERIALSPECNAME");
		Number expirationday_t = (Number)param.get("EXPIRATIONDAY_T");
		String is_con_temp_t = (String)param.get("IS_CON_TEMP_T");
		String maturationflag_t = (String)param.get("MATURATIONFLAG_T");
		String maturationtime_t = (String)param.get("MATURATIONTIME_T");
		String funit_t = (String)param.get("FUNIT_T");
		String unit_rates_t = (String)param.get("UNIT_RATES_T");
		String iqcflag_t = (String)param.get("IQCFLAG_T");
		String inspection_cycle_t = (String)param.get("INSPECTION_CYCLE_T");
		String user = (String)param.get("user");
		String is_check = (String)param.get("IS_CHECK");

//		String sql = "UPDATE MATERIALSPEC " +
//                "SET EXPIRATIONDAY_T = ?, IS_CON_TEMP_T = ?, MATURATIONFLAG_T = ?, " +
//                "MATURATIONTIME_T = ?, FUNIT_T = ?, UNIT_RATES_T = ?, IQCFLAG_T = ?, INSPECTION_CYCLE_T = ? " +
//                "WHERE MATERIALSPECNAME = ? AND SITENAME = ?";
//
//		Object[] args = new Object[]{expirationday_t, is_con_temp_t, maturationflag_t, maturationtime_t,
//				funit_t, unit_rates_t, iqcflag_t, inspection_cycle_t,materialspecname, siteName};

//		IDMFrameServiceProxy.getSqlTemplate().update(sql, args);

		MaterialSpecKey key = new MaterialSpecKey();
		key.setSiteName(siteName);
		key.setMaterialSpecName(materialspecname);

		SetSpecEventInfo info = new SetSpecEventInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("EXPIRATIONDAY_T", expirationday_t);
		bindMap.put("IS_CON_TEMP_T", is_con_temp_t);
		bindMap.put("MATURATIONFLAG_T", maturationflag_t);
		bindMap.put("MATURATIONTIME_T", maturationtime_t);
		bindMap.put("FUNIT_T", funit_t);
		bindMap.put("UNIT_RATES_T", unit_rates_t);
		bindMap.put("IQCFLAG_T", iqcflag_t);
		bindMap.put("INSPECTION_CYCLE_T", inspection_cycle_t);
		bindMap.put("IS_CHECK", is_check);
		
		info.setUserColumns(bindMap);
		
		EventInfo eventInfo = new EventInfoUtil().makeEventInfo("EditMaterial", user, "EditMaterial");
		eventInfo.setEventName("ModifyMaterialSpec");
		
		MaterialServiceProxy.getMaterialSpecService().setEvent(key, eventInfo, info);

		String message = materialspecname + " 已经修改，待审核" ;
		String accessToken = null;
		try {
			accessToken = DingTalkUtils.getAccessToken();
		} catch (IOException e) {
			System.out.println("获取accessToken异常: " + e.getMessage());
		}
		String agentId = "3206304704"; // 如果API调用不需要，则忽略此参数
		String chatId = "";
		String getIDSql = "SELECT m.MESSAGEID  FROM MESSAGEPUSHDETAIL m WHERE m.DEPARTMENT = 'test'";
		List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(getIDSql);
		for(Map<String,Object> map : queryForList) {
			chatId = chatId + map.get("MESSAGEID").toString() + ",";
		}
		String dingId = chatId.substring(0, chatId.length()-1);

		try {
			//钉钉推送
			DingTalkSender.sendTextMessage(accessToken, agentId, dingId, dingId, message);
		} catch (IOException e) {
			System.out.println("钉钉推送异常: " + e.getMessage());
			e.printStackTrace();
		}
		
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;
        
	}
	
	@RequestMapping(value = "/checkMaterial", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> checkMaterial(@RequestBody Map<String,Object> param){
		
		String siteName = (String)param.get("SITENAME");
		String materialspecname = (String)param.get("MATERIALSPECNAME");
		Number expirationday_t = (Number)param.get("EXPIRATIONDAY_T");
		String is_con_temp_t = (String)param.get("IS_CON_TEMP_T");
		String maturationflag_t = (String)param.get("MATURATIONFLAG_T");
		String maturationtime_t = (String)param.get("MATURATIONTIME_T");
		String funit_t = (String)param.get("FUNIT_T");
		String unit_rates_t = (String)param.get("UNIT_RATES_T");
		String iqcflag_t = (String)param.get("IQCFLAG_T");
		String inspection_cycle_t = (String)param.get("INSPECTION_CYCLE_T");
		String user = (String)param.get("user");
		String is_check = (String)param.get("IS_CHECK");
		
		MaterialSpecKey key = new MaterialSpecKey();
		key.setSiteName(siteName);
		key.setMaterialSpecName(materialspecname);
		
		SetSpecEventInfo info = new SetSpecEventInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("EXPIRATIONDAY_T", expirationday_t);
		bindMap.put("IS_CON_TEMP", is_con_temp_t);
		bindMap.put("MATURATIONFLAG", maturationflag_t);
		bindMap.put("MATURATIONTIME", maturationtime_t);
		bindMap.put("FUNIT", funit_t);
		bindMap.put("UNIT_RATES", unit_rates_t);
		bindMap.put("IQCFLAG", iqcflag_t);
		bindMap.put("INSPECTION_CYCLE", inspection_cycle_t);
		bindMap.put("IS_CHECK", is_check);
		
		info.setUserColumns(bindMap);
		
		EventInfo eventInfo = new EventInfoUtil().makeEventInfo("CheckMaterial", user, "CheckMaterial");
		eventInfo.setEventName("CheckMaterialSpec");
		
		MaterialServiceProxy.getMaterialSpecService().setEvent(key, eventInfo, info);
		
		BaseResponse returnOK = RespGenerator.returnOK(param);
        return returnOK;
        
	}

}