package com.cim.idm.controller;

import com.alibaba.fastjson.JSONObject;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import io.swagger.annotations.Api;
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;

@Api("RMA接口")
@RestController
@RequestMapping("/websocketMessage")
@EnableAutoConfiguration
public class WebsocketMessageController {

	@RequestMapping(value = "/getReceiveMessage", method = RequestMethod.POST)
	public AjaxResult getReceiveMessage(@RequestBody JSONObject in) throws CustomException {
		String userId = in.getString("userId");

		String sql ="SELECT\n" +
				"\t'1' RECEIVEREQUESTDETAILNAME,\n" +
				"\tSUM(m.REQUESTQUANTITY) REQUESTQUANTITY,\n" +
				"\tSUM(m.RECEIVEDQUANTITY) RECEIVEDQUANTITY,\n" +
				"\tSUM(m.REQUESTQUANTITY - m.RECEIVEDQUANTITY) AS MINUSQTY,\n" +
				"\tTO_CHAR(m2.ORDERDATE,'HH24:MI:SS') ORDERDATE,\n" +
				"\tm3.DESC_CN ,\n" +
				"\tm.MATERIALUNIT ,\n" +
				"\tm.SDK_ID ,\n" +
				"\tss.SPECNAME ,\n" +
				"\tm.PHASE ,\n" +
				"\tm.MATERIALSPECNAME,\n" +
				"\tm.SITENAME,\n" +
				"\tm.RECEIVEREQUESTNAME,\n" +
				"\tbe.LOCATION_USER,\n" +
				"\tbe.ERPLOCATIONNAME\n" +
				"FROM\n" +
				"\tMATERIALRECEIVEREQUESTDETAIL m\n" +
				"\tLEFT JOIN MATERIALRECEIVEREQUEST m2 ON\n" +
				"\tm.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME\n" +
				"\tLEFT JOIN MATERIALSPEC m3 ON\n" +
				"\tm.MATERIALSPECNAME = m3.MATERIALSPECNAME\n" +
				"\tLEFT JOIN SDK_SPEC ss ON\n" +
				"\tm.SDK_ID = ss.SDK_ID\n" +
				"\tLEFT JOIN SUPPLIER S ON\n" +
				"\tm2.SUPPLIERNO = S.SUPPLIERNO\n" +
				"\tLEFT JOIN MATERIALUNIT MM ON\n" +
				"\tMM.FNUMBER = m.MATERIALUNIT\n" +
				"\tLEFT JOIN BS_ERPLOCATION be ON\n" +
				"\tm2.ERPLOCATION = be.ERPLOCATIONNAME\n" +
				"\tLEFT JOIN ORG o ON\n" +
				"\to.ORGNO = m2.STOCKORGNO\n" +
				"\tLEFT JOIN STORAGESPEC sss ON\n" +
				"\tm.LOCATIONNAME = sss.STORAGENAME\n" +
				"\tWHERE \n" +
				"\tORDERDATE >= SYSDATE - 3\n" +
				"\tAND be.LOCATION_USER = :LOCATION_USER\n" +
				"\tGROUP BY\n" +
				"\tm3.DESC_CN ,\n" +
				"\tm.MATERIALUNIT ,\n" +
				"\tm.SDK_ID ,\n" +
				"\tss.SPECNAME ,\n" +
				"\tm.PHASE ,\n" +
				"\tm.MATERIALSPECNAME,\n" +
				"\tm.SITENAME,\n" +
				"\tm.RECEIVEREQUESTNAME,\n" +
				"\tbe.LOCATION_USER,\n" +
				"\tbe.ERPLOCATIONNAME,\n" +
				"\tm2.ORDERDATE\n" +
				"\tORDER BY m2.ORDERDATE DESC";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("LOCATION_USER", userId);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);
	}

	@RequestMapping(value = "/getMessageSum", method = RequestMethod.POST)
	public AjaxResult getMessageSum(@RequestBody JSONObject in) throws CustomException {
		String userId = in.getString("userId");

		String sql ="SELECT\n" +
				"\tCOUNT(*) SUM\n" +
				"FROM\n" +
				"\tMATERIALRECEIVEREQUESTDETAIL m\n" +
				"\tLEFT JOIN MATERIALRECEIVEREQUEST m2 ON\n" +
				"\tm.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME\n" +
				"\tLEFT JOIN MATERIALSPEC m3 ON\n" +
				"\tm.MATERIALSPECNAME = m3.MATERIALSPECNAME\n" +
				"\tLEFT JOIN SDK_SPEC ss ON\n" +
				"\tm.SDK_ID = ss.SDK_ID\n" +
				"\tLEFT JOIN SUPPLIER S ON\n" +
				"\tm2.SUPPLIERNO = S.SUPPLIERNO\n" +
				"\tLEFT JOIN MATERIALUNIT MM ON\n" +
				"\tMM.FNUMBER = m.MATERIALUNIT\n" +
				"\tLEFT JOIN BS_ERPLOCATION be ON\n" +
				"\tm2.ERPLOCATION = be.ERPLOCATIONNAME\n" +
				"\tLEFT JOIN ORG o ON\n" +
				"\to.ORGNO = m2.STOCKORGNO\n" +
				"\tLEFT JOIN STORAGESPEC sss ON\n" +
				"\tm.LOCATIONNAME = sss.STORAGENAME\n" +
				"\tWHERE \n" +
				"\tORDERDATE >= SYSDATE - 3\n" +
				"\tAND be.LOCATION_USER = :LOCATION_USER";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("LOCATION_USER", userId);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		String sum = list.get(0).get("SUM").toString();
		return AjaxResult.me().setResultObj(sum);
	}
}
