package com.cim.idm.service.Impl;

import com.cim.idm.dao.ToSapDao;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.BarcodeListByInvoice;
import com.cim.idm.service.MoveTransformService;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.MaterialPackingAssignShipRequestInfo;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MoveTransformServiceImpl implements MoveTransformService {

    @Autowired
    private ToSAPServiceImpl toSAPServiceImpl;
    @Autowired
    private ToSapDao toSapDao;


    /**
     * 绑定解绑形态转换单与库存关系
     *
     * @param barcodeListByInvoice
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String AssignOrDessign(BarcodeListByInvoice barcodeListByInvoice) throws Exception {
        log.info("MoveTransformService AssignOrDessign {}", barcodeListByInvoice);
        List<BarcodeListByInvoice.Barcode> boxList = barcodeListByInvoice.getBoxList();
        String userId = barcodeListByInvoice.getUserId();
        String opType = barcodeListByInvoice.getOpType();
        String siteName = barcodeListByInvoice.getSiteName();
        String shipRequestName = barcodeListByInvoice.getShipRequestName();
        String shipRequestDetailName = barcodeListByInvoice.getShipRequestDetailName();
        List<MaterialPackingKey> mpkList = new ArrayList<>();
        for (BarcodeListByInvoice.Barcode barcode : boxList) {
            String materialPackingName = barcode.getMaterialPackingName();
            siteName = barcode.getSiteName();
            MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
            mpkList.add(materialPackingKey);
            com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking materialPacking1 =
                    MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
            if (materialPacking1 == null) {
                throw new Exception("物料不存在");
            }
            String shipRequestName2 = materialPacking1.getShipRequestName();
            if (shipRequestName2 != null && !shipRequestName2.equals("")) {
                if (!shipRequestName.equals(shipRequestName2)) {
                    throw new Exception("物料已被其他单据占用");
                }
            }
        }
        if (opType == null || opType.equals("")) {
            throw new Exception("请选择操作类型");
        }
        if (userId == null || userId.equals("")) {
            throw new Exception("请选择操作人");
        }
        MaterialPackingAssignShipRequestInfo assignShipRequestInfo = new MaterialPackingAssignShipRequestInfo();
        //判断是解绑还是绑定类型
        switch(opType) {
            case "Assign":
            EventInfo eventInfo = EventInfoUtil.makeEventInfo("Assign", userId, "绑定单据");
            assignShipRequestInfo.setShipRequestName(shipRequestName);
            assignShipRequestInfo
                    .setShipRequestDetailName(shipRequestDetailName);
            MaterialPackingServiceProxy.getMaterialPackingService()
                    .assignShipRequest(mpkList, eventInfo,
                            assignShipRequestInfo);
                break;
                case "Dessign":
                    EventInfo eventInfo1 = EventInfoUtil.makeEventInfo("Dessign", userId, "解绑单据");
                    assignShipRequestInfo.setShipRequestName("");
                    assignShipRequestInfo
                            .setShipRequestDetailName("");
                    MaterialPackingServiceProxy.getMaterialPackingService()
                            .assignShipRequest(mpkList, eventInfo1,
                                    assignShipRequestInfo);
                    break;
                default:
                    break;
        }
        return "";

    }

    /**
     * 形态转换提交
     *
     * @param barcodeListByInvoice
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String MoveTransform(BarcodeListByInvoice barcodeListByInvoice) throws Exception {
        log.info("MoveTransformService MoveTransform {}", barcodeListByInvoice);
        String siteName = barcodeListByInvoice.getSiteName();
        String user = barcodeListByInvoice.getUserId();
//        String erpLocation = barcodeListByInvoice.getErpLocation();
//        String locationName = barcodeListByInvoice.getLocationName();
        String shipRequestName = barcodeListByInvoice.getShipRequestName();
        //提交SAP过账
        String undoId = toSAPServiceImpl.ChangeStateStockOut(shipRequestName, siteName,
                barcodeListByInvoice.getUserId());
        //更新库存状态和仓库
        List<MaterialPacking> boxListByShipRequestName = toSapDao.getBoxListByShipRequestName(shipRequestName);
        toSapDao.updateMaterialCode(boxListByShipRequestName, undoId);
        //更新库存状态和仓库
        List<MaterialPackingKey> mpKeyList = new ArrayList<>();
        for (MaterialPacking box : boxListByShipRequestName) {
            mpKeyList.add(new MaterialPackingKey("SDK", box.getMaterialPackingName()));
        }
        EventInfo eventInfo = EventInfoUtil.makeEventInfo("moveTransform", user, "形态转换");
        SetEventInfo setEventInfo = new SetEventInfo();
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("stockState", "Stocked");
        hashMap.put("shipRequestName", "");
        hashMap.put("shipRequestDetailName", "");
        setEventInfo.setUserColumns(hashMap);
        MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, eventInfo, setEventInfo);

        //保存物料凭证
        toSapDao.saveUnDoInfo(boxListByShipRequestName, undoId, "");
        //记录出入库流水
        return "";
    }
}
