package com.cpxt.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.biz.domain.BizDriver;
import com.cpxt.biz.domain.BizDriverClock;
import com.cpxt.biz.domain.BizOrder;
import com.cpxt.biz.mapper.BizDriverClockMapper;
import com.cpxt.biz.mapper.BizDriverMapper;
import com.cpxt.biz.mapper.BizOrderMapper;
import com.cpxt.biz.service.IBizDriverClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 司机打卡记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class BizDriverClockServiceImpl implements IBizDriverClockService
{
    @Autowired
    private BizDriverClockMapper bizDriverClockMapper;

    @Autowired
    private BizOrderMapper bizOrderMapper;

    @Autowired
    private BizDriverMapper bizDriverMapper;

    /**
     * 查询司机打卡记录
     * 
     * @param id 司机打卡记录主键
     * @return 司机打卡记录
     */
    @Override
    public BizDriverClock selectBizDriverClockById(Long id)
    {
        return bizDriverClockMapper.selectById(id);
    }

    /**
     * 查询司机打卡记录列表
     * 
     * @param bizDriverClock 司机打卡记录
     * @return 司机打卡记录
     */
    @Override
    public List<BizDriverClock> selectBizDriverClockList(BizDriverClock bizDriverClock)
    {
        LambdaQueryWrapper<BizDriverClock> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizDriverClock.getDriverId())){
            queryWrapper.eq(BizDriverClock::getDriverId,bizDriverClock.getDriverId());
        }
        return bizDriverClockMapper.selectList(queryWrapper);
    }

    @Override
    public Page<BizDriverClock> selectBizDriverClockPage(BizDriverClock bizDriverClock, int pageNum, int pageSize) {
        LambdaQueryWrapper<BizDriverClock> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizDriverClock.getDriverId())){
            queryWrapper.eq(BizDriverClock::getDriverId,bizDriverClock.getDriverId());
        }
        queryWrapper.orderByDesc(BizDriverClock::getUpdateTime);
        return bizDriverClockMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
    }

    /**
     * 新增司机打卡记录
     * 
     * @param bizDriverClock 司机打卡记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizDriverClock(BizDriverClock bizDriverClock)
    {
        // 新增司机打卡记录时
        // 1、同步修改订单表中订单司机是该司机并且状态为待取件和配送中的订单的车牌号为打卡车辆车牌号
        LambdaQueryWrapper<BizOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizOrder::getDriverId,bizDriverClock.getDriverId());
        queryWrapper.in(BizOrder::getOrderStatus, Arrays.asList("1","2"));
        queryWrapper.eq(BizOrder::getStatus,1);
        List<BizOrder> bizOrderList = bizOrderMapper.selectList(queryWrapper);
        if (bizOrderList.size() > 0){
            for (BizOrder bizOrder : bizOrderList) {
                bizOrder.setCarId(bizDriverClock.getCarId());
                bizOrder.setCarNo(bizDriverClock.getCarNo());
                bizOrderMapper.updateById(bizOrder);
            }
        }
        // 2、上班打卡修改司机的状态为在线
        BizDriver bizDriver = bizDriverMapper.selectById(bizDriverClock.getDriverId());
        bizDriver.setOnlineStatus(1);
        bizDriverMapper.updateById(bizDriver);
        return bizDriverClockMapper.insert(bizDriverClock);
    }

    /**
     * 修改司机打卡记录
     * 
     * @param bizDriverClock 司机打卡记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBizDriverClock(BizDriverClock bizDriverClock)
    {
        // 同步修改司机的状态为离线
        BizDriver bizDriver = bizDriverMapper.selectById(bizDriverClock.getDriverId());
        bizDriver.setOnlineStatus(0);
        bizDriverMapper.updateById(bizDriver);
        return bizDriverClockMapper.updateById(bizDriverClock);
    }

    /**
     * 批量删除司机打卡记录
     * 
     * @param ids 需要删除的司机打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteBizDriverClockByIds(Long[] ids)
    {
        return bizDriverClockMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除司机打卡记录信息
     * 
     * @param id 司机打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteBizDriverClockById(Long id)
    {
        return bizDriverClockMapper.deleteById(id);
    }
}
