package com.wms.service.serviceImplements;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.constants.enums.TaskType;
import com.wms.entity.app.dto.TaskRecordDto;
import com.wms.entity.table.TaskRecord;
import com.wms.mapper.TaskRecordMapper;
import com.wms.service.TaskRecordService;
import com.wms.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.wms.constants.WmsConstants.MYSQL_JSON_CI;


@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TaskRecordServiceImplements extends ServiceImpl<TaskRecordMapper, TaskRecord> implements TaskRecordService {
    private final TaskRecordMapper taskRecordMapper;

    @Override
    public List<TaskRecord> selectInTaskRecord(TaskRecord query) {
        query.setTaskType(TaskType.IN.getCode());// 设置为入库
        return selectTaskRecord(query);
    }

    @Override
    public List<TaskRecord> selectOutTaskRecord(TaskRecord query) {
        query.setTaskType(TaskType.OUT.getCode());// 设置为出库
        return selectTaskRecord(query);
    }

    @Override
    public List<TaskRecord> selectInventoryTaskRecord(TaskRecord query) {
        query.setTaskType(TaskType.INVENTORY.getCode());// 设置为盘点
        return selectTaskRecord(query);
    }

    @Override
    public List<TaskRecord> selectTaskRecord(TaskRecord query) {
        LambdaQueryWrapper<TaskRecord> queryWrapper = new LambdaQueryWrapper<TaskRecord>()
                .eq(TaskRecord::getTaskType, query.getTaskType())
                .like(StringUtils.isNotEmpty(query.getVehicleId()), TaskRecord::getVehicleId, query.getVehicleId());
        if (query.getGoodsRelated() != null) {
            queryWrapper
                    // 物料编号
                    .apply(StringUtils.isNotEmpty(query.getGoodsRelated().getGoodsId()), "goods_related ->> '$.goodsId' like concat('%', {0}, '%')" + MYSQL_JSON_CI, query.getGoodsRelated().getGoodsId())
                    // 物料名称/描述
                    .apply(StringUtils.isNotEmpty(query.getGoodsRelated().getGoodsName()), "goods_related ->> '$.goodsName' like concat('%', {0}, '%')" + MYSQL_JSON_CI, query.getGoodsRelated().getGoodsName());
        }
        return super.list(queryWrapper);
    }
}
