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

import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.entity.ItemKeyEntity;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.form.ItemKeyAddForm;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.form.ItemKeyUpdateForm;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.manager.ItemKeyManager;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.service.ItemKeyQueryService;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.service.ItemKeyService;
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.util.List;

@Service
public class ItemKeyServiceImpl implements ItemKeyService {

    @Resource
    private ItemKeyManager itemKeyManager;

    @Resource
    private ItemKeyQueryService itemKeyQueryService;

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(ItemKeyAddForm addForm) {
        ItemKeyEntity itemKeyEntity = SmartBeanUtil.copy(addForm, ItemKeyEntity.class);
        itemKeyManager.save(itemKeyEntity);
        return ResponseDTO.ok();
    }

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

        itemKeyManager.batchDelete(idList);
        return ResponseDTO.ok();
    }

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

        itemKeyManager.deleteById(itemKeyId);
        return ResponseDTO.ok();
    }

    /**
     * 单一创建itemKey
     *
     * @param orderNumber 工单号
     * @param itemId      物料
     * @param propC1      批次
     * @return ItemKeyEntity
     */
    @Transactional(rollbackFor = Exception.class)
    public ItemKeyEntity createItemKey(String orderNumber, Long itemId, String propC1) {
        ItemKeyEntity itemKey = itemKeyQueryService.queryItemKey(orderNumber, itemId, propC1);
        if (itemKey != null) {
            return itemKey;
        }
        itemKey = ItemKeyEntity.builder()
                .orderNumber(orderNumber)
                .itemId(itemId)
                .propC1(propC1)
                .createUserId(SmartRequestUtil.getRequestUser().getUserId())
                .createUserName(SmartRequestUtil.getRequestUser().getUserName())
                .build();
        itemKeyManager.save(itemKey);
        return itemKey;
    }
}
