package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppConveyTaskMapper;
import org.wcs.mapper.intf.AppConveyTaskService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyTask.QueryConveyTaskReq;
import org.wcs.model.po.app.AppConveyTask;
import org.wcs.utils.AppStringUtils;

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

/**
 * 箱式线任务表操作类
 */
@Service
public class AppConveyTaskDao extends ServiceImpl<AppConveyTaskMapper, AppConveyTask> implements AppConveyTaskService {

    /**
     * 查询箱式线任务
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 结果
     */
    @Override
    public Tuple2<Long, List<AppConveyTask>> queryConveyTask(QueryConveyTaskReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        Integer plcTaskId = null;
        if(AppStringUtils.isNotEmpty(request.getPlcTaskId())) {
            plcTaskId = AppStringUtils.forceToInt(request.getPlcTaskId());
        }
        LambdaQueryWrapper<AppConveyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getTaskId()), AppConveyTask::getTaskId, request.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getVehicleNo()), AppConveyTask::getVehicleNo, request.getVehicleNo());
        queryWrapper.eq(request.getTaskStatus() != null, AppConveyTask::getTaskStatus, request.getTaskStatus());
        queryWrapper.eq(request.getTaskType() != null, AppConveyTask::getTaskType, request.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocation()), AppConveyTask::getLocation, request.getLocation());
        queryWrapper.eq(plcTaskId != null, AppConveyTask::getPlcId, plcTaskId);
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getArriveLocation()), AppConveyTask::getArriveLocation, request.getArriveLocation());
        if(request.getCreateTimeRange() != null && request.getCreateTimeRange().length == 2) {
            queryWrapper.between(request.getCreateTimeRange()[0] != null && request.getCreateTimeRange()[1] != null, AppConveyTask::getCreateTime, request.getCreateTimeRange()[0], request.getCreateTimeRange()[1]);
        }
        if(request.getArriveTimeRange() != null && request.getArriveTimeRange().length == 2) {
            queryWrapper.between(request.getArriveTimeRange()[0] != null && request.getArriveTimeRange()[1] != null, AppConveyTask::getArriveTime, request.getArriveTimeRange()[0], request.getArriveTimeRange()[1]);
        }
        queryWrapper.orderByDesc(AppConveyTask::getCreateTime);
        Page<AppConveyTask> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppConveyTask> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加箱式线任务
     * @param appConveyTask 添加参数
     * @return 添加结果
     */
    @Override
    public int insertConveyTask(AppConveyTask appConveyTask) {
        if(appConveyTask == null) {
            return 0;
        }
        appConveyTask.setCreateTime(LocalDateTime.now());
        appConveyTask.setUpdateTime(LocalDateTime.now());
        try {
            return super.baseMapper.insert(appConveyTask);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据任务ID查询箱式线任务
     * @param taskId 任务ID
     * @return 箱式线任务列表
     */
    @Override
    public List<AppConveyTask> queryByTaskId(String taskId) {
        LambdaQueryWrapper<AppConveyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppConveyTask::getTaskId, taskId);
        queryWrapper.orderByAsc(AppConveyTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 更新箱式线任务数据，为null的不更新
     * @param appConveyTask 更新参数
     * @return 更新结果
     */
    @Override
    public int updateConveyTask(AppConveyTask appConveyTask) {
        if(appConveyTask == null) {
            return 0;
        }
        LambdaUpdateWrapper<AppConveyTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppConveyTask::getTaskId, appConveyTask.getTaskId());
        updateWrapper.set(AppConveyTask::getUpdateTime, LocalDateTime.now())
                .set(appConveyTask.getTaskGroup() != null, AppConveyTask::getTaskGroup, appConveyTask.getTaskGroup())
                .set(appConveyTask.getTaskStatus() != null, AppConveyTask::getTaskStatus, appConveyTask.getTaskStatus())
                .set(appConveyTask.getTaskType() != null, AppConveyTask::getTaskType, appConveyTask.getTaskType())
                .set(appConveyTask.getOrderId() != null, AppConveyTask::getOrderId, appConveyTask.getOrderId())
                .set(appConveyTask.getVehicleNo() != null, AppConveyTask::getVehicleNo, appConveyTask.getVehicleNo())
                .set(appConveyTask.getLocation() != null, AppConveyTask::getLocation, appConveyTask.getLocation())
                .set(appConveyTask.getSize() != null, AppConveyTask::getSize, appConveyTask.getSize())
                .set(appConveyTask.getWeight() != null, AppConveyTask::getWeight, appConveyTask.getWeight())
                .set(appConveyTask.getLength() != null, AppConveyTask::getLength, appConveyTask.getLength())
                .set(appConveyTask.getWidth() != null, AppConveyTask::getWidth, appConveyTask.getWidth())
                .set(appConveyTask.getHeight() != null, AppConveyTask::getHeight, appConveyTask.getHeight())
                .set(appConveyTask.getArriveLocation() != null, AppConveyTask::getArriveLocation, appConveyTask.getArriveLocation())
                .set(appConveyTask.getArriveTime() != null, AppConveyTask::getArriveTime, appConveyTask.getArriveTime())
                .set(appConveyTask.getCompleteTime() != null, AppConveyTask::getCompleteTime, appConveyTask.getCompleteTime())
                .set(appConveyTask.getRemark() != null, AppConveyTask::getRemark, appConveyTask.getRemark())
                .set(appConveyTask.getTaskSource() != null, AppConveyTask::getTaskSource, appConveyTask.getTaskSource())
                .set(appConveyTask.getCreatePerson() != null, AppConveyTask::getCreatePerson, appConveyTask.getCreatePerson())
                .set(appConveyTask.getPlcId() != null, AppConveyTask::getPlcId, appConveyTask.getPlcId());
        try {
            return baseMapper.update(updateWrapper);
        } catch (Exception e) {
            return 0;
        }


    }
}
