package net.lab1024.sa.admin.module.business.wms.inventory.inventory.service.impl;

import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.dao.InventoryDao;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.entity.InventoryEntity;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.form.InventoryAddForm;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.form.InventoryUpdateForm;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.manager.InventoryManager;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.service.InventoryService;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class InventoryServiceImpl implements InventoryService {

    @Resource
    private InventoryManager inventoryManager;

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(InventoryAddForm addForm) {
        InventoryEntity inventoryEntity = SmartBeanUtil.copy(addForm, InventoryEntity.class);
        inventoryManager.save(inventoryEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(InventoryUpdateForm updateForm) {
        InventoryEntity inventoryEntity = SmartBeanUtil.copy(updateForm, InventoryEntity.class);
        inventoryManager.updateById(inventoryEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }

        inventoryManager.removeBatchByIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long inventoryId) {
        if (null == inventoryId) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }

        inventoryManager.removeById(inventoryId);
        return ResponseDTO.ok();
    }

    public InventoryEntity createInventory(Long itemKeyId, Long locationId, Long stockId, BigDecimal quantity) {
        return InventoryEntity.builder()
                .itemKeyId(itemKeyId)
                .locationId(locationId)
                .stockId(stockId)
                .quantity(quantity)
                .queuedQuantity(BigDecimal.ZERO)
                .createUserId(SmartRequestUtil.getRequestUser().getUserId())
                .createUserName(SmartRequestUtil.getRequestUser().getUserName())
                .build();
    }
}
