<?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.frequency.appeal.mapper.AppealMapper">

    <resultMap type="com.ruoyi.frequency.appeal.entity.Appeal" id="AppealResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="orderId"    column="order_id"    />
        <result property="content"    column="content"    />
        <result property="picture"    column="picture"    />
        <result property="appealUserId"    column="appeal_user_id"    />
        <result property="appealUserType"    column="appeal_user_type"    />
        <result property="passiveAppealUserType"    column="passive_appeal_user_type"    />
        <result property="passiveAppealUserId"    column="passive_appeal_user_id"    />
        <result property="appealResult"    column="appeal_result"    />
        <result property="orderNo"    column="order_no"    />
        <result property="title"    column="title"    />
        <result property="orderContent"    column="orderContent"    />
        <result property="sourceMaterial"    column="source_material"    />
        <result property="amount"    column="amount"    />
    </resultMap>

    <sql id="selectAppealVo">
        SELECT
            t.id,
            t.create_by,
            t.create_time,
            t.update_by,
            t.update_time,
            t.del_flag,
            t.order_id,
            t.content,
            t.picture,
            t.appeal_user_id,
            t.passive_appeal_user_id,
            t.appeal_result,
            t.passive_appeal_user_type,
            t.appeal_user_type,
            t.video_url,
            t.amount,
            t2.order_no,
            t2.title,
            t2.content orderContent,
            t2.source_material
        FROM
            t_appeal AS t
                LEFT JOIN t_order AS t2 ON t.order_id = t2.id
                LEFT JOIN t_customer AS c ON c.id = t.appeal_user_id
                AND t.appeal_user_type = 1
                LEFT JOIN t_store AS s ON s.id = t.appeal_user_id
                AND t.appeal_user_type = 2
    </sql>

    <select id="selectAppealList" parameterType="com.ruoyi.frequency.appeal.entity.Appeal" resultMap="AppealResult">
        <include refid="selectAppealVo"/>
        <where>
            and t.del_flag != 2
            <if test="orderNo != null  and orderNo != ''">and t2.order_no like concat('%',#{orderNo}, '%')</if>
            <if test="beginTime != null ">
                and t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and t.create_time &lt; #{endTime}
            </if>
            <if test="appealResult != null ">
                and appeal_result = #{appealResult}
            </if>
            <if test="appealUserInfo != null and appealUserInfo.nickname != null  and appealUserInfo.nickname != ''">
                and (
                s.nickname like concat('%',#{appealUserInfo.nickname}, '%')
                or
                c.nickname like concat('%',#{appealUserInfo.nickname}, '%')
                )
            </if>
            <if test="appealUserInfo != null and appealUserInfo.mobile != null  and appealUserInfo.mobile != ''">
                and (
                s.mobile like concat('%',#{appealUserInfo.mobile}, '%')
                or
                c.mobile like concat('%',#{appealUserInfo.mobile}, '%')
                )
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectAppealById" parameterType="Long" resultMap="AppealResult">
        <include refid="selectAppealVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertAppeal" parameterType="com.ruoyi.frequency.appeal.entity.Appeal">
        insert into t_appeal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="orderId != null">order_id,</if>
            <if test="content != null">content,</if>
            <if test="picture != null">picture,</if>
            <if test="appealUser != null">appeal_user,</if>
            <if test="passiveAppealUser != null">passive_appeal_user,</if>
            <if test="appealResult != null">appeal_result,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="content != null">#{content},</if>
            <if test="picture != null">#{picture},</if>
            <if test="appealUser != null">#{appealUser},</if>
            <if test="passiveAppealUser != null">#{passiveAppealUser},</if>
            <if test="appealResult != null">#{appealResult},</if>
        </trim>
    </insert>

    <update id="updateAppeal" parameterType="com.ruoyi.frequency.appeal.entity.Appeal">
        update t_appeal
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="picture != null">picture = #{picture},</if>
            <if test="appealUser != null">appeal_user = #{appealUser},</if>
            <if test="passiveAppealUser != null">passive_appeal_user = #{passiveAppealUser},</if>
            <if test="appealResult != null">appeal_result = #{appealResult},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppealById" parameterType="Long">
        delete from t_appeal where id = #{id}
    </delete>

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

    <select id="appealPage" resultType="com.ruoyi.controller.resp.AppealPageResp">
        SELECT
            t.id,
            t.create_time,
            t.order_id,
            t.content,
            t.picture,
            t.appeal_user_id,
            t.passive_appeal_user_id,
            t.appeal_user_type,
            t.passive_appeal_user_type,
            t.appeal_result,
            t2.order_no,
            t2.title,
            t2.content orderContent,
            t2.source_material,
            t.video_url,
            t.amount,
            t2.amount orderAmount,
            t2.create_time orderTime,
            t2.status orderStatus,
            t.refund_price
        FROM
            t_appeal AS t
                LEFT JOIN t_order AS t2 ON t.order_id = t2.id
        WHERE
            t.del_flag = 1
          and (
                (#{userVo.userType} = 1 and t2.customer_id = #{userVo.userId})
                or
                (#{userVo.userType} = 2 and t2.store_id = #{userVo.userId})
            )
        ORDER BY
            t.create_time DESC
    </select>

    <select id="appealDetail" resultType="com.ruoyi.controller.resp.AppealDetailResp">
        SELECT
            t.id,
            t.create_time,
            t.order_id,
            t.content,
            t.picture,
            t.appeal_user_id,
            t.passive_appeal_user_id,
            t.appeal_user_type,
            t.passive_appeal_user_type,
            t.appeal_result,
            t.del_flag,
            t2.order_no,
            t2.title,
            t2.content orderContent,
            t.video_url,
            t2.source_material,
            t.amount,
            t2.amount orderAmount,
            t2.create_time orderTime,
            t2.status orderStatus,
            t2.if_highend,
            t.refund_price
        FROM
            t_appeal AS t
                LEFT JOIN t_order AS t2 ON t.order_id = t2.id
        WHERE
            t.id = #{id}
    </select>
    <select id="statistics" resultType="com.ruoyi.frequency.appeal.model.result.AppealCount">
        select (SELECT
        count( t.appeal_user_id) AS num
        FROM
        t_appeal AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        ) num,
        (SELECT
        count( t.appeal_user_id) AS number
        FROM
        t_appeal AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        and t.appeal_result = 2
        ) passNumber,
        (SELECT
        count( t.appeal_user_id) AS number
        FROM
        t_appeal AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        and t.appeal_result = 3
        ) noPassNumber,
        (SELECT
        count( t.appeal_user_id) AS number
        FROM
        t_appeal AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        and t.appeal_result = 1
        ) auditNumber
    </select>
</mapper>
