package com.cim.idm.service.Impl;

import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.AssignAndDessignDto;
import com.cim.idm.model.MaterialPacking;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.service.AssignAndDessignService;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmspackage.invoice.MaterialReceiveRequestServiceProxy;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
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.MaterialPackingDeassignShipRequestInfo;
import com.cim.idm.wmspackage.receiverequestdetail.ReceiveRequestDetailServiceProxy;
import lombok.extern.slf4j.Slf4j;
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 AssignAndDessignServiceImpl implements AssignAndDessignService {
    /**
     * @param AssignAndDessignDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Object> AssignAndDessign(AssignAndDessignDto AssignAndDessignDto) throws Exception {
        log.info("AssignAndDessignServiceImpl.AssignAndDessign {}", AssignAndDessignDto);
        //获取单号和行号
        String opType = AssignAndDessignDto.getOpType();
        List<MaterialPacking> boxList = AssignAndDessignDto.getBoxList();
        String userId = AssignAndDessignDto.getUserId();
        String receiveRequestName = AssignAndDessignDto.getReceiveRequestName();
        String receiveRequestDetailName = AssignAndDessignDto.getReceiveRequestDetailName();
        if (boxList == null || boxList.size() == 0) {
           throw new Exception("请选择要绑定的物料");
        }
        //循环判断Box是否已被其他单据占用
        for (MaterialPacking materialPacking : boxList) {
            String materialPackingName = materialPacking.getMaterialPackingName();
            String siteName = materialPacking.getSiteName();
            if (materialPackingName == null || materialPackingName.equals("")) {
                throw new Exception("请选择要绑定的物料");
            }
            MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
            com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking materialPacking1 = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
            if (materialPacking1 == null) {
                throw new Exception("物料不存在");
            }
            String shipRequestName = materialPacking1.getShipRequestName();
            if (shipRequestName != null && !shipRequestName.equals("")) {
                if (!shipRequestName.equals(receiveRequestName)) {
                    throw new Exception("物料已被其他单据占用");
                }
            }
        }
        if (opType == null || opType.equals("")) {
            throw new Exception("请选择操作类型");
        }
        if (userId == null || userId.equals("")) {
            throw new Exception("请选择操作人");
        }
        MaterialPackingAssignShipRequestInfo assignShipRequestInfo = new MaterialPackingAssignShipRequestInfo();
        List<MaterialPackingKey> materialPackingKeyList = new ArrayList<MaterialPackingKey>() ;
        for (MaterialPacking materialPacking : boxList) {
            String materialPackingName = materialPacking.getMaterialPackingName();
            String siteName = materialPacking.getSiteName();
            if (materialPackingName == null || materialPackingName.equals("")) {
                throw new Exception("请选择要绑定的物料");
            }
            MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
            materialPackingKeyList.add(materialPackingKey);

        }
        //枚举两个方法组分别执行以下的Case
        switch (opType) {
            case "Assign":
                EventInfo eventInfo = EventInfoUtil.makeEventInfo("Assign", userId, "绑定单据");
                assignShipRequestInfo.setShipRequestName(receiveRequestName);
                assignShipRequestInfo
                        .setShipRequestDetailName(receiveRequestDetailName);
//                Map<String, Object> bindMap = new HashMap<String, Object>();
//                assignShipRequestInfo.setUserColumns(bindMap);
                MaterialPackingServiceProxy.getMaterialPackingService()
                        .assignShipRequest(materialPackingKeyList, eventInfo,
                                assignShipRequestInfo);
                break;
            case "Dessign":
                EventInfo eventInfo1 = EventInfoUtil.makeEventInfo("Dessign", userId, "解绑单据");
                assignShipRequestInfo.setShipRequestName("");
                assignShipRequestInfo
                        .setShipRequestDetailName("");
                MaterialPackingServiceProxy.getMaterialPackingService()
                        .assignShipRequest(materialPackingKeyList, eventInfo1,
                                assignShipRequestInfo);
                break;
            default:
                break;
        }
        return null;
    }
}
