package com.kxs.store.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.store.api.model.KxsWarehouseStock;
import com.kxs.store.biz.constant.enums.StoreStatusEnum;
import com.kxs.store.biz.mapper.KxsWarehouseStockLogMapper;
import com.kxs.store.api.model.KxsWarehouseStockLog;
import com.kxs.store.biz.service.KxsWarehouseStockLogService;
import org.springframework.stereotype.Service;

/**
 * 客小爽仓库库存变动记录表(KxsWarehouseStockLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsWarehouseStockLogService")
public class KxsWarehouseStockLogServiceImpl extends ServiceImpl<KxsWarehouseStockLogMapper, KxsWarehouseStockLog> implements KxsWarehouseStockLogService {

    @Override
    public void addKxsWarehouseStockLog(Integer warehouseId, Integer brandId, KxsWarehouseStock warehouseStock, String kind, int changeType, int count) {
        KxsWarehouseStockLog kxsWarehouseStockLog = new KxsWarehouseStockLog();
        kxsWarehouseStockLog.setWarehouseId(warehouseId);
        kxsWarehouseStockLog.setBrandId(brandId);
        kxsWarehouseStockLog.setChangeNum(count);
        kxsWarehouseStockLog.setChangeType(changeType);
        kxsWarehouseStockLog.setKind(kind);

        //入库
        if(changeType == StoreStatusEnum.STOCK_PUSH.getType()){
            kxsWarehouseStockLog.setBeforeStockNum(warehouseStock.getStockNum() - count);
            kxsWarehouseStockLog.setBeforeStockTotal(warehouseStock.getStockTotal() - count);
        }
        //出库
        if(changeType == StoreStatusEnum.STOCK_OUT.getType()){
            kxsWarehouseStockLog.setBeforeStockNum(warehouseStock.getStockNum() + count);
            kxsWarehouseStockLog.setBeforeDeliveryNum(warehouseStock.getDeliveryNum() - count);
        }
        //调拨
        if(changeType == StoreStatusEnum.STOCK__TSF.getType()){
            kxsWarehouseStockLog.setBeforeStockNum(kind.equals(KindTypeEnum.ADD.getType()) ? warehouseStock.getStockNum() - count : warehouseStock.getStockNum() + count);
        }

        kxsWarehouseStockLog.setAfterStockNum(warehouseStock.getStockNum());
        kxsWarehouseStockLog.setAfterDeliveryNum(warehouseStock.getDeliveryNum());
        kxsWarehouseStockLog.setAfterStockTotal(warehouseStock.getStockTotal());
        baseMapper.insert(kxsWarehouseStockLog);
    }
}

