package net.lab1024.sa.admin.module.business.wms.task.service;

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

import net.lab1024.sa.admin.module.business.wms.base.location.domain.entity.LocationEntity;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.entity.StockEntity;
import net.lab1024.sa.admin.module.business.wms.task.dao.TaskDao;
import net.lab1024.sa.admin.module.business.wms.task.domain.entity.TaskEntity;
import net.lab1024.sa.admin.module.business.wms.task.domain.form.TaskAddForm;
import net.lab1024.sa.admin.module.business.wms.task.domain.form.TaskQueryForm;
import net.lab1024.sa.admin.module.business.wms.task.domain.form.TaskUpdateForm;
import net.lab1024.sa.admin.module.business.wms.task.domain.vo.TaskVO;
import net.lab1024.sa.admin.module.business.wms.task.manager.TaskManager;
import net.lab1024.sa.admin.module.business.wms.base.location.domain.entity.LocationEntity;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.entity.StockEntity;
import net.lab1024.sa.admin.module.business.wms.task.domain.entity.TaskEntity;
import net.lab1024.sa.admin.module.business.wms.task.manager.TaskManager;
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;

/**
 * Task任务 Service
 *
 * @Author 霍锦
 * @Date 2024-12-03 17:08:30
 * @Copyright 友仓
 */

@Service
public class TaskService {

    @Resource
    private TaskDao taskDao;

    @Resource
    private TaskManager taskManager;

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(TaskAddForm addForm) {
        TaskEntity taskEntity = SmartBeanUtil.copy(addForm, TaskEntity.class);
        taskDao.insert(taskEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(TaskUpdateForm updateForm) {
        TaskEntity taskEntity = SmartBeanUtil.copy(updateForm, TaskEntity.class);
        taskDao.updateById(taskEntity);
        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());
        }

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

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

        taskDao.deleteById(taskId);
        return ResponseDTO.ok();
    }

    /**
     * 生成Task任务
     *
     * @param orderNumber  工单号
     * @param status       任务状态
     * @param taskType     任务类型
     * @param planQty      分配数量
     * @param itemKeyId    物料
     * @param asnDetailId  入库明细
     * @param pickDetailId 出库明细
     * @param srcStock     原容器
     * @param dstStock     目标容器
     * @param srcLocation  原库位
     * @param dstLocation  目标库位
     * @param inventoryId  库存ID
     * @param agvTaskId    AGV任务ID
     * @return TaskEntity
     */
    public TaskEntity createTask(String orderNumber, String status, String taskType, BigDecimal planQty, Long itemKeyId, Long asnDetailId, Long pickDetailId, StockEntity srcStock, StockEntity dstStock, LocationEntity srcLocation, LocationEntity dstLocation, Long inventoryId, Long agvTaskId) {
        return TaskEntity.builder()
                .billCode(orderNumber)
                .status(status)
                .taskType(taskType)
                .planQty(planQty)
                .moveQty(BigDecimal.ZERO)
                .itemKeyId(itemKeyId)
                .asnDetailId(asnDetailId)
                .pickDetailId(pickDetailId)
                .srcStockId(srcStock == null ? null : srcStock.getStockId())
                .srcStockCode(srcStock == null ? null : srcStock.getStockCode())
                .dstStockId(dstStock == null ? null : dstStock.getStockId())
                .dstStockCode(dstStock == null ? null : dstStock.getStockCode())
                .srcLocationId(srcLocation == null ? null : srcLocation.getLocationId())
                .srcLocationCode(srcLocation == null ? null : srcLocation.getLocationCode())
                .dstLocationId(dstLocation == null ? null : dstLocation.getLocationId())
                .dstLocationCode(dstLocation == null ? null : dstLocation.getLocationCode())
                .inventoryId(inventoryId)
                .agvTaskId(agvTaskId)
                .beSkip(0L)
                .createUserId(SmartRequestUtil.getRequestUser().getUserId())
                .createUserName(SmartRequestUtil.getRequestUser().getUserName())
                .build();
    }
}
