<?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.shop.mapper.ShopUserCarMapper">

    <resultMap type="cn.iocoder.yudao.module.shop.entity.ShopUserCar" id="ShopUserCarResult">
        <result property="carId"    column="car_id"    />
        <result property="userId"    column="user_id"    />
        <result property="carModel"    column="car_model"    />
        <result property="carNo"    column="car_no"    />
        <result property="maintenanceDate"    column="maintenance_date"    />
        <result property="maintenanceMileage"    column="maintenance_mileage"    />
        <result property="inspectionDate"    column="inspection_date"    />
        <result property="insuranceDate"    column="insurance_date"    />
        <result property="nextMaintenanceDate"    column="next_maintenance_date"    />
        <result property="nextMaintenanceMileage"    column="next_maintenance_mileage"    />
        <result property="nextInspectionDate"    column="next_inspection_date"    />
        <result property="insuranceExpiryDate"    column="insurance_expiry_date"    />
        <result property="carBrand"    column="car_brand"    />
        <result property="carNature"    column="car_nature"    />
        <result property="carRegisterDate"    column="car_register_date"    />
        <result property="carLicenseImg"    column="car_license_img"    />
        <result property="realName"    column="real_name"    />
        <result property="phonenumber"    column="phonenumber"    />

    </resultMap>

    <sql id="selectShopUserCarVo">
        select car_id, shop_user_car.user_id, car_model, shop_user_car.car_no, maintenance_date, maintenance_mileage,
               inspection_date, insurance_date, next_maintenance_date, next_maintenance_mileage,
               next_inspection_date, insurance_expiry_date,car_brand,car_nature,car_register_date,car_license_img,
               sys_user.nickname realName,sys_user.mobile phonenumber
        from shop_user_car shop_user_car left join system_users sys_user on shop_user_car.user_id = sys_user.id
    </sql>

    <select id="selectShopUserCarList" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserCar" resultMap="ShopUserCarResult">
        <include refid="selectShopUserCarVo"/>
        <where>
            <if test="vo.userId != null "> and sys_user.id = #{vo.userId}</if>
            <if test="vo.carNature != null "> and car_nature = #{vo.carNature}</if>
            <if test="vo.realName != null "> and sys_user.real_name like concat('%', #{vo.realName}, '%')</if>
            <if test="vo.carNo != null  and vo.carNo != ''"> and shop_user_car.car_no like concat('%', #{vo.carNo}, '%')</if>
            <if test="vo.params.beginMaintenanceDate != null and vo.params.beginMaintenanceDate != '' and vo.params.endMaintenanceDate != null and vo.params.endMaintenanceDate != ''"> and maintenance_date between #{vo.params.beginMaintenanceDate} and #{vo.params.endMaintenanceDate}</if>
            <if test="vo.params.beginMaintenanceMileage != null and vo.params.beginMaintenanceMileage != '' and vo.params.endMaintenanceMileage != null and vo.params.endMaintenanceMileage != ''"> and maintenance_mileage between #{vo.params.beginMaintenanceMileage} and #{vo.params.endMaintenanceMileage}</if>
            <if test="vo.params.beginInspectionDate != null and vo.params.beginInspectionDate != '' and vo.params.endInspectionDate != null and vo.params.endInspectionDate != ''"> and inspection_date between #{vo.params.beginInspectionDate} and #{vo.params.endInspectionDate}</if>
            <if test="vo.params.beginInsuranceDate != null and vo.params.beginInsuranceDate != '' and vo.params.endInsuranceDate != null and vo.params.endInsuranceDate != ''"> and insurance_date between #{vo.params.beginInsuranceDate} and #{vo.params.endInsuranceDate}</if>
            <if test="vo.params.beginNextMaintenanceDate != null and vo.params.beginNextMaintenanceDate != '' and vo.params.endNextMaintenanceDate != null and vo.params.endNextMaintenanceDate != ''"> and next_maintenance_date between #{vo.params.beginNextMaintenanceDate} and #{vo.params.endNextMaintenanceDate}</if>
            <if test="vo.params.beginNextMaintenanceMileage != null and vo.params.beginNextMaintenanceMileage != '' and vo.params.endNextMaintenanceMileage != null and vo.params.endNextMaintenanceMileage != ''"> and next_maintenance_mileage between #{vo.params.beginNextMaintenanceMileage} and #{vo.params.endNextMaintenanceMileage}</if>
            <if test="vo.params.beginNextInspectionDate != null and vo.params.beginNextInspectionDate != '' and vo.params.endNextInspectionDate != null and vo.params.endNextInspectionDate != ''"> and next_inspection_date between #{vo.params.beginNextInspectionDate} and #{vo.params.endNextInspectionDate}</if>
            <if test="vo.params.beginInsuranceExpiryDate != null and vo.params.beginInsuranceExpiryDate != '' and vo.params.endInsuranceExpiryDate != null and vo.params.endInsuranceExpiryDate != ''"> and insurance_expiry_date between #{vo.params.beginInsuranceExpiryDate} and #{vo.params.endInsuranceExpiryDate}</if>
        </where>
    </select>

    <select id="selectShopUserCarByCarId" parameterType="Long" resultMap="ShopUserCarResult">
        <include refid="selectShopUserCarVo"/>
        where car_id = #{carId}
    </select>
    <select id="selectShopUserCarByUserId" parameterType="Long" resultMap="ShopUserCarResult">
        <include refid="selectShopUserCarVo"/>
        where sys_user.id = #{userId}
        limit 1
    </select>
    <select id="selectUserCarsByUserId" parameterType="Long" resultMap="ShopUserCarResult">
        <include refid="selectShopUserCarVo"/>
        where sys_user.id = #{userId}
    </select>
    <select id="getDbCars" parameterType="Long" resultMap="ShopUserCarResult">
        SELECT
        suc.car_id,
        suc.car_model,
        suc.car_no
        FROM
        shop_user_car suc
        INNER JOIN system_users su on suc.user_id = su.id
        inner join inspection_contract ic ON ic.user_id = su.id and suc.car_no = ic.carnum
        where   now()   <![CDATA[ <= ]]> DATE_ADD(ic.create_time, INTERVAL 10 DAY) and su.id = #{userId}
    </select>

    <select id="selectUserCarsByUserIdCarNum" resultMap="ShopUserCarResult">
        <include refid="selectShopUserCarVo"/>
        where sys_user.id = #{userId} and shop_user_car.car_no = #{carNum}
    </select>

    <insert id="insertShopUserCar" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserCar" useGeneratedKeys="true" keyProperty="carId">
        insert into shop_user_car
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="carModel != null">car_model,</if>
            <if test="carNo != null">car_no,</if>
            <if test="maintenanceDate != null">maintenance_date,</if>
            <if test="maintenanceMileage != null">maintenance_mileage,</if>
            <if test="inspectionDate != null">inspection_date,</if>
            <if test="insuranceDate != null">insurance_date,</if>
            <if test="nextMaintenanceDate != null">next_maintenance_date,</if>
            <if test="nextMaintenanceMileage != null">next_maintenance_mileage,</if>
            <if test="nextInspectionDate != null">next_inspection_date,</if>
            <if test="insuranceExpiryDate != null">insurance_expiry_date,</if>
            <if test="carBrand != null">car_brand,</if>
            <if test="carNature != null">car_nature,</if>
            <if test="carRegisterDate != null">car_register_date,</if>
            <if test="carLicenseImg != null">car_license_img,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="carModel != null">#{carModel},</if>
            <if test="carNo != null">#{carNo},</if>
            <if test="maintenanceDate != null">#{maintenanceDate},</if>
            <if test="maintenanceMileage != null">#{maintenanceMileage},</if>
            <if test="inspectionDate != null">#{inspectionDate},</if>
            <if test="insuranceDate != null">#{insuranceDate},</if>
            <if test="nextMaintenanceDate != null">#{nextMaintenanceDate},</if>
            <if test="nextMaintenanceMileage != null">#{nextMaintenanceMileage},</if>
            <if test="nextInspectionDate != null">#{nextInspectionDate},</if>
            <if test="insuranceExpiryDate != null">#{insuranceExpiryDate},</if>
            <if test="carBrand != null">#{carBrand},</if>
            <if test="carNature != null">#{carNature},</if>
            <if test="carRegisterDate != null">#{carRegisterDate},</if>
            <if test="carLicenseImg != null">#{carLicenseImg},</if>
         </trim>
    </insert>

    <update id="updateShopUserCar" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserCar">
        update shop_user_car
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="carModel != null">car_model = #{carModel},</if>
            <if test="carNo != null">car_no = #{carNo},</if>
            <if test="maintenanceDate != null">maintenance_date = #{maintenanceDate},</if>
            <if test="maintenanceMileage != null">maintenance_mileage = #{maintenanceMileage},</if>
            <if test="inspectionDate != null">inspection_date = #{inspectionDate},</if>
            <if test="insuranceDate != null">insurance_date = #{insuranceDate},</if>
            <if test="nextMaintenanceDate != null">next_maintenance_date = #{nextMaintenanceDate},</if>
            <if test="nextMaintenanceMileage != null">next_maintenance_mileage = #{nextMaintenanceMileage},</if>
            <if test="nextInspectionDate != null">next_inspection_date = #{nextInspectionDate},</if>
            <if test="insuranceExpiryDate != null">insurance_expiry_date = #{insuranceExpiryDate},</if>
            <if test="carBrand != null">car_brand = #{carBrand},</if>
            <if test="carNature != null">car_nature = #{carNature},</if>
            <if test="carRegisterDate != null">car_register_date = #{carRegisterDate},</if>
            <if test="carLicenseImg != null">car_license_img = #{carLicenseImg},</if>
        </trim>
        where car_id = #{carId}
    </update>

    <delete id="deleteShopUserCarByCarId" parameterType="Long">
        delete from shop_user_car where car_id = #{carId}
    </delete>

    <delete id="deleteShopUserCarByCarIds" parameterType="String">
        delete from shop_user_car where car_id in
        <foreach item="carId" collection="array" open="(" separator="," close=")">
            #{carId}
        </foreach>
    </delete>
    <delete id="deleteShopUserCarByUserId">
        delete from shop_user_car
        where user_id = #{userId}
    </delete>


</mapper>
