package com.yc.ship.module.resource.dal.mysql.car;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.resource.controller.admin.car.vo.CarPageReqVO;
import com.yc.ship.module.resource.controller.admin.car.vo.CarReqVO;
import com.yc.ship.module.resource.controller.admin.car.vo.CarTypeNumRespVO;
import com.yc.ship.module.resource.dal.dataobject.car.CarDO;
import com.yc.ship.module.resource.enums.DeleteStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 车辆管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface CarMapper extends BaseMapperX<CarDO> {

    default PageResult<CarDO> selectPage(CarPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CarDO>()
                .likeIfPresent(CarDO::getCarNo, reqVO.getCarNo())
                .eqIfPresent(CarDO::getCarTypeId, reqVO.getCarTypeId())
                .eqIfPresent(CarDO::getCarMaxLoad, reqVO.getCarMaxLoad())
                .eqIfPresent(CarDO::getCompanyType, reqVO.getCompanyType())
                .eqIfPresent(CarDO::getCompanyId, reqVO.getCompanyId())
                .eqIfPresent(CarDO::getCarStatus, reqVO.getCarStatus())
                .eqIfPresent(CarDO::getCarTag, reqVO.getCarTag())
                .eqIfPresent(CarDO::getRemark, reqVO.getRemark())
                .eqIfPresent(CarDO::getDriverId, reqVO.getDriverId())
                .eqIfPresent(CarDO::getSort, reqVO.getSort())
                .betweenIfPresent(CarDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CarDO::getId));
    }

    default List<CarDO> selectSimpleList(CarPageReqVO reqVO) {
        LambdaQueryWrapperX<CarDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.select(CarDO::getId, CarDO::getCarNo, CarDO::getDriverId).orderByDesc(CarDO::getId);
        if (reqVO != null) {
            queryWrapper.likeIfPresent(CarDO::getCarNo, reqVO.getCarNo())
                    .eqIfPresent(CarDO::getCarTypeId, reqVO.getCarTypeId())
                    .eqIfPresent(CarDO::getCarMaxLoad, reqVO.getCarMaxLoad())
                    .eqIfPresent(CarDO::getCompanyType, reqVO.getCompanyType())
                    .eqIfPresent(CarDO::getCompanyId, reqVO.getCompanyId())
                    .eqIfPresent(CarDO::getCarStatus, reqVO.getCarStatus())
                    .eqIfPresent(CarDO::getCarTag, reqVO.getCarTag())
                    .eqIfPresent(CarDO::getRemark, reqVO.getRemark())
                    .eqIfPresent(CarDO::getDriverId, reqVO.getDriverId())
                    .eqIfPresent(CarDO::getSort, reqVO.getSort())
                    .betweenIfPresent(CarDO::getCreateTime, reqVO.getCreateTime());
        }
        return selectList(queryWrapper);
    }

    default List<CarDO> getCarList(CarReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CarDO>()
                .likeIfPresent(CarDO::getCarNo, reqVO.getCarNo())
                .eqIfPresent(CarDO::getCarTypeId, reqVO.getCarTypeId())
                .eqIfPresent(CarDO::getCarMaxLoad, reqVO.getCarMaxLoad())
                .eqIfPresent(CarDO::getCompanyId, reqVO.getCompanyId())
                .eqIfPresent(CarDO::getCarStatus, reqVO.getCarStatus())
                .inIfPresent(CarDO::getId, reqVO.getCarIds())
                .eq(CarDO::getDeleted, DeleteStatusEnum.ENABLE.getStatus()));
    }

    /**
     * 按车辆类型统计每个类型的车辆数量
     */
    @Select("SELECT car_type_id as 'carTypeId', COUNT(1) AS 'carTypeNum' FROM dispatch_car WHERE deleted = 0 GROUP BY car_type_id")
    List<CarTypeNumRespVO> getstatiscisCarTypeNum();
}