package com.cim.idm.service.Impl;

import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.HoldAndReleaseDto;
import com.cim.idm.service.HoldAndReleaseService;
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.SetEventInfo;
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 HoldAndReleaseServiceImpl implements HoldAndReleaseService {
    /**
     * @param holdAndReleaseDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void hold(HoldAndReleaseDto holdAndReleaseDto) {
        log.debug("HoldAndReleaseServiceImpl hold {}", holdAndReleaseDto);
        SetEventInfo setEventInfo = new SetEventInfo();
        EventInfo eventInfo = null;

        String userId = holdAndReleaseDto.getUserId();
        List<HoldAndReleaseDto.HoldAndReleaseDtoBuilder> boxList = holdAndReleaseDto.getBoxList();
        List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey>();
        for (HoldAndReleaseDto.HoldAndReleaseDtoBuilder holdAndReleaseDtoBuilder : boxList) {
            String barCode = holdAndReleaseDtoBuilder.getBarCode();
            String siteName = holdAndReleaseDtoBuilder.getSiteName();
            MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, barCode);
            mpKeyList.add(materialPackingKey);
        }
        String flag = holdAndReleaseDto.getFlag();
        //判断标志冻结和解冻走不同的方法
        switch(flag) {
            case "Y": //冻结
                eventInfo = EventInfoUtil.makeEventInfo("Hold", userId, "冻结库存");
                break;
            case "N"://解冻
                eventInfo = EventInfoUtil.makeEventInfo("Release", userId, "解冻库存");
                break;
            default:
                break;
        }
        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("holdState", flag);
        bindMap.put("lastHoldUser", "Y".equals(flag) ? userId : "");
        setEventInfo.setUserColumns(bindMap);
        MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, eventInfo, setEventInfo);

    }
}
