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 com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppEtagTaskMapper;
import org.wcs.mapper.intf.AppEtagTaskService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagTask.EtagTaskQueryReq;
import org.wcs.model.po.app.AppEtagTagInfo;
import org.wcs.model.po.app.AppEtagTagLocation;
import org.wcs.model.po.app.AppEtagTask;
import org.wcs.utils.AppStringUtils;

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

/**
 * 电子标签任务数据访问层实现类
 */
@Slf4j
@Service
public class AppEtagTaskDao extends ServiceImpl<AppEtagTaskMapper, AppEtagTask> implements AppEtagTaskService {

    /**
     * 查询电子标签任务
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Tuple2<Long, List<AppEtagTask>> queryEtagTask(EtagTaskQueryReq request, Integer pageIndex, Integer pageSize) {
        if(request == null || pageIndex == null || pageSize == null || pageIndex < 0 || pageSize < 0) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getTaskId() != null, AppEtagTask::getTaskId, request.getTaskId());
        queryWrapper.eq(request.getTaskStatus() != null, AppEtagTask::getTaskStatus, request.getTaskStatus());
        queryWrapper.eq(request.getTaskType() != null, AppEtagTask::getTaskType, request.getTaskType());
        queryWrapper.like(request.getTaskGroup() != null, AppEtagTask::getTaskGroup, request.getTaskGroup());
        queryWrapper.like(request.getGoodsId() != null, AppEtagTask::getGoodsId, request.getGoodsId());
        queryWrapper.like(request.getVehicleNo() != null, AppEtagTask::getVehicleNo, request.getVehicleNo());
        if(request.getCreateTime() != null && request.getCreateTime().size() == 2) {
            queryWrapper.between(request.getCreateTime().get(0) != null && request.getCreateTime().get(1) != null, AppEtagTask::getCreateTime, request.getCreateTime().get(0), request.getCreateTime().get(1));
        }
        queryWrapper.orderByAsc(AppEtagTask::getCreateTime);
        Page<AppEtagTask> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppEtagTask> etagTaskPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(etagTaskPage.getTotal(), etagTaskPage.getRecords());
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 新增电子标签任务
     * @param appEtagTask 新增参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppEtagTask appEtagTask) {
        if(appEtagTask == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        appEtagTask.setCreateTime(LocalDateTime.now());
        try {
            return new DataBaseActionResult<>(baseMapper.insert(appEtagTask));
        } catch (Exception e) {
            log.error("新增电子标签任务失败", e);
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 批量新增电子标签任务
     * @param appEtagTaskList 新增参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> insert(List<AppEtagTask> appEtagTaskList) {
        if(appEtagTaskList == null || appEtagTaskList.isEmpty()) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        appEtagTaskList.forEach(appEtagTask -> appEtagTask.setCreateTime(LocalDateTime.now()));
        try {
            boolean saveBatch = super.saveBatch(appEtagTaskList);
            return new DataBaseActionResult<>(saveBatch ? appEtagTaskList.size() : 0);
        } catch (Exception e) {
            log.error("批量新增电子标签任务失败", e);
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 修改电子标签任务
     * @param appEtagTask 修改参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> updateIgnoreNull(AppEtagTask appEtagTask) {
        if(appEtagTask == null) {
            return new DataBaseActionResult<>(0, new Exception("参数错误"));
        }
        try {
            LambdaUpdateWrapper<AppEtagTask> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(AppEtagTask::getTaskId, appEtagTask.getTaskId());
            updateWrapper.set(appEtagTask.getTaskGroup() != null, AppEtagTask::getTaskGroup, appEtagTask.getTaskGroup());
            updateWrapper.set(appEtagTask.getTaskType() != null, AppEtagTask::getTaskType, appEtagTask.getTaskType());
            updateWrapper.set(appEtagTask.getVehicleNo() != null, AppEtagTask::getVehicleNo, appEtagTask.getVehicleNo());
            updateWrapper.set(appEtagTask.getOrderId() != null, AppEtagTask::getOrderId, appEtagTask.getOrderId());
            updateWrapper.set(appEtagTask.getLocation() != null, AppEtagTask::getLocation, appEtagTask.getLocation());
            updateWrapper.set(appEtagTask.getGoodsId() != null, AppEtagTask::getGoodsId, appEtagTask.getGoodsId());
            updateWrapper.set(appEtagTask.getGoodsName() != null, AppEtagTask::getGoodsName, appEtagTask.getGoodsName());
            updateWrapper.set(appEtagTask.getLightNum() != null, AppEtagTask::getLightNum, appEtagTask.getLightNum());
            updateWrapper.set(appEtagTask.getConfirmNum() != null, AppEtagTask::getConfirmNum, appEtagTask.getConfirmNum());
            updateWrapper.set(appEtagTask.getTaskStatus() != null, AppEtagTask::getTaskStatus, appEtagTask.getTaskStatus());
            updateWrapper.set(appEtagTask.getLightModel() != null, AppEtagTask::getLightModel, appEtagTask.getLightModel());
            updateWrapper.set(appEtagTask.getTaskSource() != null, AppEtagTask::getTaskSource, appEtagTask.getTaskSource());
            updateWrapper.set(appEtagTask.getCreatePerson() != null, AppEtagTask::getCreatePerson, appEtagTask.getCreatePerson());
            //updateWrapper.set(appEtagTask.getCreateTime() != null, AppEtagTask::getCreateTime, appEtagTask.getCreateTime());
            updateWrapper.set(appEtagTask.getLightTime() != null, AppEtagTask::getLightTime, appEtagTask.getLightTime());
            updateWrapper.set(appEtagTask.getConfirmTime() != null, AppEtagTask::getConfirmTime, appEtagTask.getConfirmTime());
            updateWrapper.set(appEtagTask.getConfirmPerson() != null, AppEtagTask::getConfirmPerson, appEtagTask.getConfirmPerson());
            updateWrapper.set(appEtagTask.getCompletedTime() != null, AppEtagTask::getCompletedTime, appEtagTask.getCompletedTime());
            updateWrapper.set(appEtagTask.getRemark() != null, AppEtagTask::getRemark, appEtagTask.getRemark());
            return new DataBaseActionResult<>(baseMapper.update(appEtagTask, updateWrapper));
        } catch (Exception e) {
            log.error("修改电子标签任务失败", e);
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 根据任务ID查询任务
     * @param taskId 任务ID
     * @return 任务列表
     */
    @Override
    public List<AppEtagTask> queryByTaskId(String taskId) {
        if(AppStringUtils.isEmpty(taskId)) {
            return null;
        }
        try {
            LambdaQueryWrapper<AppEtagTask> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppEtagTask::getTaskId, taskId);
            queryWrapper.orderByAsc(AppEtagTask::getCreateTime);
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据标签区域ID和状态查询任务
     * @param areaId 标签区域ID
     * @param vehicleNo 载具
     * @param status 状态列表
     * @return 任务列表
     */
    @Override
    public List<AppEtagTask> queryByTagAreaIdAndVehicleAndStatus(String areaId, String vehicleNo, List<Integer> status) {
        if (AppStringUtils.isEmpty(areaId) || status == null || status.isEmpty()) {
            return null;
        }
        MPJLambdaWrapper<AppEtagTask> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(AppEtagTask.class);
        queryWrapper.leftJoin(AppEtagTagLocation.class,
                q -> q.eq(AppEtagTask::getLocation, AppEtagTagLocation::getLocation));
        queryWrapper.leftJoin(AppEtagTagInfo.class,
                q -> q.eq(AppEtagTagLocation::getTagName, AppEtagTagInfo::getTagName));
        queryWrapper.eq(AppEtagTagInfo::getAreaId, areaId)
                .eq(AppEtagTask::getVehicleNo, vehicleNo)
                .in(AppEtagTask::getTaskStatus, status);
        queryWrapper.orderByAsc(AppEtagTask::getCreateTime);
        try {
            return super.baseMapper.selectJoinList(AppEtagTask.class, queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据载具和状态查询任务
     * @param vehicleNo 载具
     * @param status 状态列表
     * @return 列表
     */
    @Override
    public List<AppEtagTask> queryByVehicleAndStatus(String vehicleNo, List<Integer> status) {
        if (status == null || status.isEmpty()) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagTask::getVehicleNo, vehicleNo)
                .in(AppEtagTask::getTaskStatus, status)
                .orderByAsc(AppEtagTask::getCreateTime);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 忽略Null值查询条件查询
     * @param appEtagTask 查询参数
     * @return 任务列表
     */
    @Override
    public List<AppEtagTask> queryIgnoreNull(AppEtagTask appEtagTask) {
        if(appEtagTask == null) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(appEtagTask.getTaskId() != null, AppEtagTask::getTaskId, appEtagTask.getTaskId());
        queryWrapper.eq(appEtagTask.getTaskGroup() != null, AppEtagTask::getTaskGroup, appEtagTask.getTaskGroup());
        queryWrapper.eq(appEtagTask.getTaskType() != null, AppEtagTask::getTaskType, appEtagTask.getTaskType());
        queryWrapper.eq(appEtagTask.getVehicleNo() != null, AppEtagTask::getVehicleNo, appEtagTask.getVehicleNo());
        queryWrapper.eq(appEtagTask.getOrderId() != null, AppEtagTask::getOrderId, appEtagTask.getOrderId());
        queryWrapper.eq(appEtagTask.getLocation() != null, AppEtagTask::getLocation, appEtagTask.getLocation());
        queryWrapper.eq(appEtagTask.getGoodsId() != null, AppEtagTask::getGoodsId, appEtagTask.getGoodsId());
        queryWrapper.eq(appEtagTask.getGoodsName() != null, AppEtagTask::getGoodsName, appEtagTask.getGoodsName());
        queryWrapper.eq(appEtagTask.getLightNum() != null, AppEtagTask::getLightNum, appEtagTask.getLightNum());
        queryWrapper.eq(appEtagTask.getConfirmNum() != null, AppEtagTask::getConfirmNum, appEtagTask.getConfirmNum());
        queryWrapper.eq(appEtagTask.getTaskStatus() != null, AppEtagTask::getTaskStatus, appEtagTask.getTaskStatus());
        queryWrapper.eq(appEtagTask.getLightModel() != null, AppEtagTask::getLightModel, appEtagTask.getLightModel());
        queryWrapper.eq(appEtagTask.getTaskSource() != null, AppEtagTask::getTaskSource, appEtagTask.getTaskSource());
        queryWrapper.eq(appEtagTask.getCreatePerson() != null, AppEtagTask::getCreatePerson, appEtagTask.getCreatePerson());
        queryWrapper.eq(appEtagTask.getCreateTime() != null, AppEtagTask::getCreateTime, appEtagTask.getCreateTime());
        queryWrapper.eq(appEtagTask.getLightTime() != null, AppEtagTask::getLightTime, appEtagTask.getLightTime());
        queryWrapper.eq(appEtagTask.getConfirmTime() != null, AppEtagTask::getConfirmTime, appEtagTask.getConfirmTime());
        queryWrapper.eq(appEtagTask.getConfirmPerson() != null, AppEtagTask::getConfirmPerson, appEtagTask.getConfirmPerson());
        queryWrapper.eq(appEtagTask.getCompletedTime() != null, AppEtagTask::getCompletedTime, appEtagTask.getCompletedTime());
        queryWrapper.eq(appEtagTask.getRemark() != null, AppEtagTask::getRemark, appEtagTask.getRemark());
        try {
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }
}
