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

import java.math.BigDecimal;
import java.util.List;
import net.lab1024.sa.admin.module.business.inventory.dao.InventoryDao;
import net.lab1024.sa.admin.module.business.inventory.domain.entity.InventoryEntity;
import net.lab1024.sa.admin.module.business.inventory.domain.form.InventoryAddForm;
import net.lab1024.sa.admin.module.business.inventory.domain.form.InventoryQueryForm;
import net.lab1024.sa.admin.module.business.inventory.domain.form.InventoryUpdateForm;
import net.lab1024.sa.admin.module.business.inventory.domain.vo.InventoryVO;
import net.lab1024.sa.admin.module.business.inventory.manager.InventoryManager;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存信息 Service
 *
 * @Author 霍锦
 * @Date 2024-12-06 17:24:53
 * @Copyright 友仓
 */

@Service
public class InventoryService {

    @Resource
    private InventoryDao inventoryDao;

    @Resource
    private InventoryManager inventoryManager;

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

    /**
     * 更新
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(InventoryUpdateForm updateForm) {
        InventoryEntity inventoryEntity = SmartBeanUtil.copy(updateForm, InventoryEntity.class);
        inventoryDao.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());
        }

        inventoryDao.deleteById(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();
    }
}
