package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.BarcodeListByInvoice;
import com.cim.idm.model.CheckMark;
import com.cim.idm.model.HoldAndReleaseDto;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.response.RespGenerator;
import com.cim.idm.service.AllocateStockInService;
import com.cim.idm.service.IMaterialShipService;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@RestController
@EnableAutoConfiguration
@RequestMapping("/api/Auxiliary")
@Slf4j
@Api(tags = "辅料管理")
public class AuxiliaryController {


    @Autowired
    private IMaterialShipService materialShipService;

    /**
     * 确认备货
     * @param in
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/auxiliaryAssign", method = RequestMethod.POST)
    public AjaxResult AuxiliaryAssign(@RequestBody JSONObject in ) throws Exception{
        String siteName = (String)in.get("SITENAME");
        String user = (String)in.get("USER");
        String shipRequestName = (String)in.get("SHIPREQUESTNAME");
        JSONArray boxListArray = in.getJSONArray("BOXLIST");
        List<LinkedHashMap<String,String>> boxList = new ArrayList<LinkedHashMap<String,String>>();
        if(null == boxListArray || boxListArray.size() <= 0) {
            return AjaxResult.me().setSuccess(true).setMessage("未获取到：" + shipRequestName + "的发货单据信息").setErrorCode(400);
        }
        // 遍历 JSONArray 并转换为 LinkedHashMap
        for (int i = 0; i < boxListArray.size(); i++) {
            JSONObject jsonObject = boxListArray.getJSONObject(i);
            LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            // 将每个字段添加到 LinkedHashMap 中
            for (String key : jsonObject.keySet()) {
                linkedHashMap.put(key, jsonObject.getString(key));
            }
            // 添加到列表中
            boxList.add(linkedHashMap);
        }
        EventInfo eventInfo = new EventInfoUtil().makeEventInfo("auxiliaryAssign", user, "auxiliaryAssign", "", "");
        return materialShipService.MaterialShipRequestAuxiliaryConfirmStocking(eventInfo,siteName,shipRequestName,user,boxList);
    }

    /**
     * 辅材出库
     * @param in
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/auxiliaryStockOut", method = RequestMethod.POST)
    public AjaxResult AuxiliaryStockOut(@RequestBody JSONObject in ) throws Exception{
        String siteName = (String)in.get("SITENAME");
        String user = (String)in.get("USER");
        String shipRequestName = (String)in.get("SHIPREQUESTNAME");
        String commitDate = (String)in.get("commitDate");
        JSONArray boxListArray = in.getJSONArray("BOXLIST");
        List<LinkedHashMap<String,String>> boxList = new ArrayList<LinkedHashMap<String,String>>();
        if(null == boxListArray || boxListArray.size() <= 0) {
            return AjaxResult.me().setSuccess(true).setMessage("未获取到：" + shipRequestName + "的发货单据信息").setErrorCode(400);
        }
        // 遍历 JSONArray 并转换为 LinkedHashMap
        for (int i = 0; i < boxListArray.size(); i++) {
            JSONObject jsonObject = boxListArray.getJSONObject(i);
            LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            // 将每个字段添加到 LinkedHashMap 中
            for (String key : jsonObject.keySet()) {
                linkedHashMap.put(key, jsonObject.getString(key));
            }
            // 添加到列表中
            boxList.add(linkedHashMap);
        }

        EventInfo eventInfo = new EventInfoUtil().makeEventInfo("auxiliaryStockOut", user, "auxiliaryStockOut", "", "");
        return materialShipService.MaterialShipRequestAuxiliaryStockOut(eventInfo,siteName,shipRequestName,user,boxList,commitDate);
    }

    /**
     * 取消确认备货
     * @param in
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/auxiliaryDessign", method = RequestMethod.POST)
    public AjaxResult AuxiliaryDessign(@RequestBody JSONObject in ) throws Exception{
        String siteName = (String)in.get("SITENAME");
        String user = (String)in.get("USER");
        String shipRequestName = (String)in.get("SHIPREQUESTNAME");
        if(StringUtils.isEmpty(shipRequestName)) {
            return AjaxResult.me().setSuccess(true).setMessage("单号不能为空").setErrorCode(400);
        }
        EventInfo eventInfo = new EventInfoUtil().makeEventInfo("auxiliaryDessign", user, "auxiliaryDessign", "", "");
        return materialShipService.MaterialShipRequestAuxiliaryCancelConfirmStocking(eventInfo,siteName,shipRequestName,user);
    }
}
