<?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="com.ruoyi.bst.fault.mapper.FaultMapper">

    <resultMap type="FaultVO" id="FaultResult" autoMapping="true"/>

    <sql id="selectFaultVo">
        select
            bf.id,
            bf.user_name,
            bf.vehicle_code,
            bf.picture,
            bf.order_id,
            bf.area_id,
            bf.fault_site,
            bf.fault_detail,
            bf.appeal_status,
            bf.create_time,
            bf.order_end_time,
            bf.user_id,
            bf.vehicle_num,
            bf.store_id,
            bf.verify_by,
            bf.verify_end_time,
            bf.verify_remark,
            ba.name as area_name,
            su_verify.user_name as verify_name,
            su.agent_id
        from  <include refid="searchTables"/>
    </sql>
    <sql id="searchTables">
        bst_fault bf
            left join bst_area ba on bf.area_id = ba.id
            left join sys_user su on bf.store_id = su.user_id
            left join sys_user su_verify on bf.verify_by = su_verify.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null  and query.id != ''"> and bf.id = #{query.id}</if>
        <if test="query.userName != null  and query.userName != ''"> and bf.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.vehicleCode != null "> and bf.vehicle_code = #{query.vehicleCode}</if>
        <if test="query.picture != null  and query.picture != ''"> and bf.picture = #{query.picture}</if>
        <if test="query.orderId != null  and query.orderId != ''"> and bf.order_id = #{query.orderId}</if>
        <if test="query.storeId != null  and query.storeId != ''"> and bf.store_id = #{query.storeId}</if>
        <if test="query.userId != null  and query.userId != ''"> and bf.user_id = #{query.userId}</if>
        <if test="query.faultSiteList != null  and query.faultSiteList != ''">
            AND
            <foreach collection="query.faultSiteList" item="item" open="(" separator=" AND " close=")">
                FIND_IN_SET(#{item}, bf.fault_site) > 0
            </foreach>
        </if>
        <if test="query.ids != null and query.ids.size() > 0">
            and bf.id in
            <foreach collection="query.ids" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.faultDetail != null  and query.faultDetail != ''"> and bf.fault_detail = #{query.faultDetail}</if>
        <if test="query.appealStatus != null  and query.appealStatus != ''"> and bf.appeal_status = #{query.appealStatus}</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
            .userSetAlias("bf.store_id,su.agent_id")
            .areaAlias("bf.area_id", query.areaPermissions)
            .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectFaultList" parameterType="FaultQuery" resultMap="FaultResult">
        <include refid="selectFaultVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by bf.create_time desc
    </select>

    <select id="selectFaultById" parameterType="Long" resultMap="FaultResult">
        <include refid="selectFaultVo"/>
        where bf.id = #{id}
    </select>

    <select id="selectFaultByUserId" resultType="com.ruoyi.bst.fault.domain.FaultVO">
        select
            bf.order_end_time,
            bf.vehicle_code,
            bf.fault_site,
            bf.create_time
        from bst_fault bf where user_id = #{userId}

    </select>

    <insert id="insertFault" parameterType="Fault" useGeneratedKeys="true" keyProperty="id">
        insert into bst_fault
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="vehicleCode != null">vehicle_code,</if>
            <if test="picture != null and picture != ''">picture,</if>
            <if test="storeId != null and storeId != ''">store_id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="faultSite != null and faultSite != ''">fault_site,</if>
            <if test="faultDetail != null">fault_detail,</if>
            <if test="appealStatus != null and appealStatus != ''">appeal_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="orderEndTime != null">order_end_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="vehicleNum != null">vehicle_num</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="vehicleCode != null">#{vehicleCode},</if>
            <if test="picture != null and picture != ''">#{picture},</if>
            <if test="storeId != null and storeId != ''">#{storeId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="faultSite != null and faultSite != ''">#{faultSite},</if>
            <if test="faultDetail != null">#{faultDetail},</if>
            <if test="appealStatus != null and appealStatus != ''">#{appealStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="orderEndTime != null">#{orderEndTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="vehicleNum != null">#{vehicleNum}</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Fault" useGeneratedKeys="true" keyProperty="id">
        insert into bst_fault
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_name,
            vehicle_code,
            picture,
            order_id,
            fault_site,
            fault_detail,
            appeal_status,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.userName != null  and i.userName != ''">#{i.userName},</if>
                <if test="i.userName == null  or i.userName == ''">default,</if>
                <if test="i.vehicleCode != null ">#{i.vehicleCode},</if>
                <if test="i.vehicleCode == null ">default,</if>
                <if test="i.picture != null  and i.picture != ''">#{i.picture},</if>
                <if test="i.picture == null  or i.picture == ''">default,</if>
                <if test="i.orderId != null ">#{i.orderId},</if>
                <if test="i.orderId == null ">default,</if>
                <if test="i.faultSite != null  and i.faultSite != ''">#{i.faultSite},</if>
                <if test="i.faultSite == null  or i.faultSite == ''">default,</if>
                <if test="i.faultDetail != null ">#{i.faultDetail},</if>
                <if test="i.faultDetail == null ">default,</if>
                <if test="i.appealStatus != null  and i.appealStatus != ''">#{i.appealStatus},</if>
                <if test="i.appealStatus == null  or i.appealStatus == ''">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_fault
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="user_name = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userName != null  and item.userName != ''">
                        WHEN #{item.id} THEN #{item.userName}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `user_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="vehicle_code = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.vehicleCode != null ">
                        WHEN #{item.id} THEN #{item.vehicleCode}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `vehicle_code`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null  and item.picture != ''">
                        WHEN #{item.id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="order_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.orderId != null ">
                        WHEN #{item.id} THEN #{item.orderId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `order_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="fault_site = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.faultSite != null  and item.faultSite != ''">
                        WHEN #{item.id} THEN #{item.faultSite}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `fault_site`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="fault_detail = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.faultDetail != null ">
                        WHEN #{item.id} THEN #{item.faultDetail}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `fault_detail`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="appeal_status = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.appealStatus != null  and item.appealStatus != ''">
                        WHEN #{item.id} THEN #{item.appealStatus}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `appeal_status`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateFault" parameterType="Fault">
        update bst_fault
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <!--updateByQuery-->
    <update id="updateByQuery">
        update bst_fault bf
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <sql id="updateColumns">
        <if test="data.userName != null and data.userName != ''">user_name = #{data.userName},</if>
        <if test="data.vehicleCode != null">vehicle_code = #{data.vehicleCode},</if>
        <if test="data.picture != null and data.picture != ''">picture = #{data.picture},</if>
        <if test="data.orderId != null">order_id = #{data.orderId},</if>
        <if test="data.faultSite != null and data.faultSite != ''">fault_site = #{data.faultSite},</if>
        <if test="data.faultDetail != null">fault_detail = #{data.faultDetail},</if>
        <if test="data.appealStatus != null and data.appealStatus != ''">appeal_status = #{data.appealStatus},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.verifyBy != null">verify_by = #{data.verifyBy},</if>
        <if test="data.verifyEndTime != null">verify_end_time = #{data.verifyEndTime},</if>
        <if test="data.verifyRemark != null">verify_remark = #{data.verifyRemark},</if>
    </sql>

    <delete id="deleteFaultById" parameterType="Long">
        delete from bst_fault where id = #{id}
    </delete>

    <delete id="deleteFaultByIds" parameterType="String">
        delete from bst_fault where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- selectCount -->
    <select id="selectCount" parameterType="FaultQuery" resultType="Integer">
        select count(bf.id)
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <!-- selectIdByQuery -->

    <select id="selectIdByQuery" resultType="Long">
        select bf.id
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

</mapper>
