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

import java.time.LocalDateTime;
import java.util.List;

import net.lab1024.sa.admin.module.business.itemKey.dao.ItemKeyDao;
import net.lab1024.sa.admin.module.business.itemKey.domain.entity.ItemKeyEntity;
import net.lab1024.sa.admin.module.business.itemKey.domain.form.ItemKeyAddForm;
import net.lab1024.sa.admin.module.business.itemKey.domain.form.ItemKeyQueryForm;
import net.lab1024.sa.admin.module.business.itemKey.domain.form.ItemKeyUpdateForm;
import net.lab1024.sa.admin.module.business.itemKey.domain.vo.ItemKeyVO;
import net.lab1024.sa.admin.module.business.itemKey.manager.ItemKeyManager;
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-05 21:38:40
 * @Copyright 友仓
 */

@Service
public class ItemKeyService {

    @Resource
    private ItemKeyDao itemKeyDao;

    @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);
        itemKeyDao.insert(itemKeyEntity);
        return ResponseDTO.ok();
    }

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

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

        itemKeyDao.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();
        itemKeyDao.insert(itemKey);
        return itemKey;
    }
}
