<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.custom.mapper.CarMainMapper">

    <sql id="baseCarMainColumn">
        tbcm.id,
        tbcm.engine_number,
        tbcm.vin,
        tbcm.license_number,
        tbcm.car_model,
        tbcm.maintenance_date,
        tbcm.maintenance_mileage,
        tbcm.inspection_date,
        tbcm.insurance_date,
        tbcm.check_date,
        tbcm.next_maintenance_date,
        tbcm.next_maintenance_mileage,
        tbcm.next_inspection_date,
        tbcm.insurance_expiry_date,
        tbcm.next_check_date,
        tbcm.car_brand,
        tbcm.car_nature,
        tbcm.car_category,
        tbcm.car_register_date,
        tbcm.car_license_img,
        tbcm.recently_handled_business,
        tbcm.recently_handle_business_time,
        tbcm.deleted,
        tbcm.creator,
        tbcm.create_time,
        tbcm.updater,
        tbcm.update_time
    </sql>

    <select id="findPage" resultType="cn.iocoder.yudao.module.custom.vo.CarMainRespVO">
        SELECT
        <include refid="baseCarMainColumn"></include>,
        bcb.brand_name AS brandStr,
        bcb.logo_img AS logoImg,
        bcm.model_name AS modelStr
        FROM
            `base_car_main` tbcm
            LEFT JOIN base_car_brand bcb ON bcb.deleted = 0 AND tbcm.car_brand = bcb.id
            LEFT JOIN base_car_model bcm ON bcm.deleted = 0 AND tbcm.car_model = bcm.id
        WHERE
            tbcm.deleted = 0
        <if test="dto.licenseNumber != null and dto.licenseNumber != ''">
            AND tbcm.license_number LIKE CONCAT('%',#{dto.licenseNumber},'%')
        </if>
        <if test="dto.carBrand != null and dto.carBrand != ''">
            AND tbcm.car_brand = #{dto.carBrand}
        </if>
        <if test="dto.carCategory != null and dto.carCategory != ''">
            AND tbcm.car_category = #{dto.carCategory}
        </if>
        <if test="dto.recentlyHandledBusiness != null and dto.recentlyHandledBusiness != ''">
            AND tbcm.recently_handled_business = #{dto.recentlyHandledBusiness}
        </if>
        <if test="dto.recentlyHandleBusinessTime != null">
            AND tbcm.recently_handle_business_time = #{dto.recentlyHandleBusinessTime}
        </if>
        <if test="dto.vin != null and dto.vin != ''">
            AND tbcm.vin LIKE CONCAT('%',#{dto.vin},'%')
        </if>
        <if test="dto.recentlyHandledBusiness != null and dto.recentlyHandledBusiness != ''">
            AND tbcm.tenant_id = #{dto.tenant}
        </if>
        <if test="dto.carModel != null and dto.carModel != ''">
            AND tbcm.car_model = #{dto.carModel}
        </if>
        <if test="dto.carNature != null and dto.carNature != ''">
            AND tbcm.car_nature = #{dto.carNature}
        </if>
        <if test="dto.engineNumber != null and dto.engineNumber != ''">
            AND tbcm.engine_number LIKE CONCAT('%',#{dto.engineNumber},'%')
        </if>
        <if test="dto.userId != null and dto.userId != ''">
            AND tbcm.user_id = #{dto.userId}
        </if>
        ORDER BY
              tbcm.car_register_date DESC
    </select>

    <select id="findOne" resultType="cn.iocoder.yudao.module.custom.vo.CarMainRespVO">
        SELECT
        <include refid="baseCarMainColumn"></include>,
        bcb.brand_name AS brandStr,
        bcb.logo_img AS logoImg
        FROM
            `base_car_main` tbcm
            LEFT JOIN base_car_brand bcb ON bcb.deleted = 0 AND tbcm.car_brand = bcb.id
        WHERE
            tbcm.deleted = 0
        AND
            tbcm.id = #{id}
    </select>

    <select id="isDataKeyValueRepeat" resultType="cn.iocoder.yudao.module.custom.entity.CarMain">
        SELECT
            tbcm.id
        FROM `base_car_main` tbcm
        WHERE
            tbcm.deleted = 0
        <if test="dto.licenseNumber != null and dto.licenseNumber != ''">
            AND tbcm.license_number = #{dto.licenseNumber}
        </if>
        <if test="dto.vin != null and dto.vin != ''">
            AND tbcm.vin = #{dto.vin}
        </if>
        <if test="dto.engineNumber != null and dto.engineNumber != ''">
            AND tbcm.engine_number = #{dto.engineNumber}
        </if>

    </select>
    <select id="remindCarMainPage" resultType="cn.iocoder.yudao.module.custom.vo.CarMainRespVO">
        SELECT
            <include refid="baseCarMainColumn"></include>,
            bcc.is_owner AS isOwner,
            bcm.cus_name AS cusName,
            bcm.phone_number AS phoneNumber,
            bcb.brand_name AS brandStr,
            bcmm.model_name AS modelStr,
            bcb.brand_type AS brand_type,
            DATEDIFF(tbcm.next_inspection_date, CURDATE()) AS njCont,
            DATEDIFF(tbcm.next_maintenance_date, CURDATE()) AS byCont,
            DATEDIFF(tbcm.insurance_expiry_date, CURDATE()) AS bxCont
        FROM
            base_car_main tbcm
                LEFT JOIN base_customer_car bcc ON tbcm.id = bcc.car_id AND bcc.deleted = 0
                LEFT JOIN base_customer_main bcm ON bcc.cus_id = bcm.id AND bcm.deleted = 0
                LEFT JOIN base_car_brand bcb ON bcb.deleted = 0 AND tbcm.car_brand = bcb.id
                LEFT JOIN base_car_model bcmm ON bcmm.deleted = 0 AND tbcm.car_model = bcmm.id
        WHERE
            tbcm.deleted = 0
        <if test="entity.licenseNumber != null and entity.licenseNumber != ''">
            AND tbcm.license_number LIKE CONCAT('%',#{entity.licenseNumber},'%')
        </if>
        <if test="entity.cusName != null and entity.cusName != ''">
            AND bcm.cus_name LIKE CONCAT('%',#{entity.cusName},'%')
        </if>
        <if test="entity.selectSign == '01'">
            AND tbcm.next_inspection_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 MONTH)
        </if>
        <if test="entity.selectSign == '02'">
            AND tbcm.next_maintenance_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
        </if>
        <if test="entity.selectSign == '03'">
            AND tbcm.insurance_expiry_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 45 DAY)
        </if>
    </select>

</mapper>