/**
 * Project Name:eis-jxyj-shelf-common
 * File Name:StoreServiceImpl.java
 * Package Name:com.prolog.eis.service.store.impl
 * Date:2022年11月1日下午2:23:41
 * Copyright (c) 2022, bluemobi All Rights Reserved.
 *
*/

package com.prolog.eis.service.shelves.impl;

import com.prolog.eis.constants.base.BaseConstant;
import com.prolog.eis.dao.out.ContainerInfoDetailMapper;
import com.prolog.eis.dao.out.ContainerInfoMapper;
import com.prolog.eis.dao.shelves.PalletInfoMapper;
import com.prolog.eis.model.container.ContainerInfo;
import com.prolog.eis.model.container.ContainerInfoDetail;
import com.prolog.eis.service.out.ContainerStoreChangeService;
import com.prolog.eis.service.shelves.StoreService;
import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.MapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

;

/**
 * Description: <br/>
 * Date: 2022年11月1日 下午2:23:41 <br/>
 * 
 * @version
 * @see
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private ContainerInfoDetailMapper containerInfoDetailMapper;

    @Resource
    private ContainerInfoMapper containerInfoMapper;

    @Resource
    private ContainerStoreChangeService containerStoreChangeService;

    @Resource
    private PalletInfoMapper palletInfoMapper;

    private static final String TASK_TYPE = "taskType";

    private static final String CONTAINER_NO = "containerNo";

    private static final String CONTAINER_SUB_NO = "containerSubNo";

    private static final String BIZ_PROPER_ID = "bizPropertyId";

    private static final String AREA_TYPE = "areaType";

    private static final String UPDATE_TIME = "updateTime";

    @Override
    public boolean storeChange(String containerNo, String containerSubNo, GoodsInfoVO vo, BigDecimal qty,
                               String remark, int taskType, String taskDetailId, Integer areaType) {
        List<ContainerInfoDetail> containerStores = containerInfoDetailMapper.findByMap(
                MapUtils.put(CONTAINER_NO, containerNo).put(CONTAINER_SUB_NO, containerSubNo)
                        .put("itemId",vo.getGoodsId()).put("lotId",vo.getLotId()).put("ownerId",vo.getOwnerId()).put("packId",vo.getPackId()).put(AREA_TYPE, areaType).getMap(),
                ContainerInfoDetail.class);

        if (CollectionUtils.isEmpty(containerStores) || containerStores.size() > 1) {
            throw new BizException(
                    String.format("容器%s货位%s库存不存在或数据大于1条，bizPropertyId：%s", containerNo, containerSubNo, vo.getProperty()));
        }
        containerInfoDetailMapper.updateContainerStore(containerNo, containerSubNo, vo.getGoodsId(),vo.getOwnerId(),vo.getLotId(),vo.getPackId(), qty, areaType);

        // 先改后查，记录修改后的库存
        List<ContainerInfoDetail> newContainerStores = containerInfoDetailMapper.findByMap(
                MapUtils.put(CONTAINER_NO, containerNo).put(CONTAINER_SUB_NO, containerSubNo)
                        .put("itemId",vo.getGoodsId()).put("lotId",vo.getLotId()).put("ownerId",vo.getOwnerId()).put("packId",vo.getPackId()).put(AREA_TYPE, areaType).getMap(),
                ContainerInfoDetail.class);

        if (CollectionUtils.isEmpty(containerStores) || containerStores.size() > 1) {
            throw new BizException(String.format("2容器%s货位%s库存不存在或数据大于1条，bizPropertyId：%s", containerNo, containerSubNo,
                    vo.getProperty()));
        }
        ContainerInfoDetail newContainerStore = newContainerStores.get(0);

        containerStoreChangeService.saveContainerStoreChange(containerNo, containerSubNo, vo, taskType,
                newContainerStore.getQty(), qty, remark, taskDetailId);

        if (newContainerStore.getQty().compareTo(BigDecimal.ZERO) == 0) {

            containerInfoDetailMapper.deleteByMap(
                    MapUtils.put(CONTAINER_NO, containerNo).put(CONTAINER_SUB_NO, containerSubNo)
                            .put("itemId",vo.getGoodsId()).put("lotId",vo.getLotId()).put("ownerId",vo.getOwnerId()).put("packId",vo.getPackId()).put(AREA_TYPE, areaType).getMap(),
                    ContainerInfoDetail.class);

            long containerNoStoreNum = containerInfoDetailMapper.findCountByMap(
                    MapUtils.put(CONTAINER_NO, containerNo).put(AREA_TYPE, areaType).getMap(),
                    ContainerInfoDetail.class);
            if (containerNoStoreNum == 0) {
                containerInfoMapper.deleteByMap(
                        MapUtils.put(CONTAINER_NO, containerNo).put(AREA_TYPE, areaType).getMap(), ContainerInfo.class);
                // 货架库存没有了--是--》变更货架类型为空货架
                palletInfoMapper.updatePalletTypeByPalletNo(containerNo, BaseConstant.PALLET_TYPE_EMPTY_0);
            }

            return true;
        } else if (newContainerStore.getQty().compareTo(BigDecimal.ZERO) < 0) {
            throw new BizException(String.format("容器{%s}库存减少异常,扣减数量{%d}", containerNo, qty));
        }

        return false;
    }

}
