package com.cpxt.biz.service.impl;

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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.common.utils.SecurityUtils;
import com.cpxt.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.mapper.BizCarMapper;
import com.cpxt.biz.domain.BizCar;
import com.cpxt.biz.service.IBizCarService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizCarServiceImpl implements IBizCarService 
{
    @Autowired
    private BizCarMapper bizCarMapper;

    /**
     * 查询车辆
     * 
     * @param id 车辆主键
     * @return 车辆
     */
    @Override
    public BizCar selectBizCarById(Long id)
    {
        return bizCarMapper.selectById(id);
    }

    /**
     * 查询车辆列表（分页）
     *
     * @param bizCar 车辆
     * @return 车辆
     */
    @Override
    public Page<BizCar> selectBizCarPage(BizCar bizCar,int pageNum,int pageSize)
    {
        LambdaQueryWrapper<BizCar> queryWrapper = buildQueryWrapper(bizCar);
        return bizCarMapper.selectPage(new Page<BizCar>(pageNum,pageSize),queryWrapper);
    }

    private LambdaQueryWrapper<BizCar> buildQueryWrapper(BizCar bizCar) {
        LambdaQueryWrapper<BizCar> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizCar.getModelName())) {
            queryWrapper.like(BizCar::getModelName, bizCar.getModelName());
        }
        if (ObjectUtil.isNotEmpty(bizCar.getCarType())) {
            queryWrapper.eq(BizCar::getCarType, bizCar.getCarType());
        }
        if (ObjectUtil.isNotEmpty(bizCar.getCarNo())) {
            queryWrapper.like(BizCar::getCarNo, bizCar.getCarNo());
        }
        if (ObjectUtil.isNotEmpty(bizCar.getVin())) {
            queryWrapper.like(BizCar::getVin, bizCar.getVin());
        }
        if (ObjectUtil.isNotEmpty(bizCar.getSerialNo())) {
            queryWrapper.like(BizCar::getSerialNo, bizCar.getSerialNo());
        }
        queryWrapper.eq(BizCar::getStatus,1);
        return queryWrapper;
    }

    /**
     * 查询车辆列表
     * 
     * @param bizCar 车辆
     * @return 车辆
     */
    @Override
    public List<BizCar> selectBizCarList(BizCar bizCar)
    {
        LambdaQueryWrapper<BizCar> queryWrapper = buildQueryWrapper(bizCar);
        return bizCarMapper.selectList(queryWrapper);
    }

    /**
     * 新增车辆
     * 
     * @param bizCar 车辆
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizCar(BizCar bizCar)
    {
        int i = bizCarMapper.insert(bizCar);
        // 新增完车辆后，创建该车辆的行驶轨迹表
        if (i > 0){
            Long id = bizCar.getId();
            String tableName = "track_" + id;
            bizCarMapper.createTable(tableName); // 创建该车辆的行驶轨迹表
            bizCarMapper.addPrimaryKey(tableName); // 添加主键
            bizCarMapper.setAutoIncrement(tableName); // 设置自增
        }
        return i;
    }

    /**
     * 修改车辆
     * 
     * @param bizCar 车辆
     * @return 结果
     */
    @Override
    public int updateBizCar(BizCar bizCar)
    {
        return bizCarMapper.updateById(bizCar);
    }

    /**
     * 批量删除车辆
     * 
     * @param ids 需要删除的车辆主键
     * @return 结果
     */
    @Override
    public int deleteBizCarByIds(Long[] ids)
    {
        UpdateWrapper<BizCar> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object[]) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCarMapper.update(updateWrapper);
    }

    /**
     * 删除车辆信息
     * 
     * @param id 车辆主键
     * @return 结果
     */
    @Override
    public int deleteBizCarById(Long id)
    {
        UpdateWrapper<BizCar> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCarMapper.update(null,updateWrapper);
    }

    @Override
    public List<Map> selectBizTrackList(Long carId, String startTime, String endTime) {
        String tableName = "track_" + carId;
        return bizCarMapper.selectBizTrackList(tableName,startTime,endTime);
    }
}
