<?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.sqx.modules.errand.dao.ErrandComplaintDao">

    <select id="findAppComplaint" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select ec.*, eit.illegal as illegal, tu.nick_name as nickName, ti.shop_address_detail as shopAddressDetail,
        ti.user_address_detail as userAddressDetail
        from errand_complaint ec
        left join errand_illegal_type eit on ec.illegal_id = eit.id
        left join tb_user tu on ec.rider_user_id = tu.user_id
        left join tb_indent ti on ec.indent_number = ti.indent_number
        where 1 = 1
        <if test="userId!=null">
            and ec.rider_user_id = #{userId}
        </if>
        <if test="illegalId!=null">
            and ec.illegal_id = #{illegalId}
        </if>
        <if test="complaintState!=null">
            and ec.complaint_state = #{complaintState}
        </if>
        <if test="complaintState==null">
            and ec.complaint_state in (1, 2, 3, 4)
        </if>
        order by ec.complaint_time desc
    </select>

    <select id="findComplaint" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select ec.*, eit.illegal as illegal, tu.nick_name as nickName, ti.shop_address_detail as shopAddressDetail,
        ti.user_address_detail as userAddressDetail, sit.illegal as shopIllegal
        from errand_complaint ec
        left join errand_illegal_type eit on ec.illegal_id = eit.id
        left join shop_illegal_type sit on ec.shop_illegal_id = sit.id
        left join tb_user tu on ec.rider_user_id = tu.user_id
        left join tb_indent ti on ec.indent_number = ti.indent_number
        where complaint_id = #{complaintId}
    </select>

    <select id="findAllComplaint" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select ec.*, eit.illegal as illegal, tu.nick_name as nickName, ti.shop_address_detail as shopAddressDetail,
        ti.user_address_detail as userAddressDetail, sit.illegal as shopIllegal
        from errand_complaint ec
        left join errand_illegal_type eit on ec.illegal_id = eit.id
        left join shop_illegal_type sit on ec.shop_illegal_id = sit.id
        left join tb_user tu on ec.rider_user_id = tu.user_id
        left join tb_indent ti on ec.indent_number = ti.indent_number
        where 1 = 1
        <if test="userId!=null">
            and ec.rider_user_id = #{userId}
        </if>
        <if test="illegalId!=null">
            and ec.illegal_id = #{illegalId}
        </if>
        <if test="complaintState!=null">
            and ec.complaint_state = #{complaintState}
        </if>
        <if test="indentNumber!=null and indentNumber!=''">
            and ec.indent_number = #{indentNumber}
        </if>
        order by ec.complaint_time desc
    </select>

    <select id="deductMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(deduct_money), 0) from errand_complaint where 1 = 1
        <if test="dateType=='day'">
            and date_format(complaint_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(complaint_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(complaint_time,'%Y')=date_format(#{date},'%Y')
        </if>
        and complaint_state = 3
    </select>

    <select id="selectComplaintList" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select * from errand_complaint where rider_user_id = #{userId} and complaint_state = 5
    </select>

    <select id="selectComplaint" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select ec.*, ti.shop_address_detail as shopAddressDetail, ti.user_address_detail as userAddressDetail,
        sit.illegal as shopIllegal, sit.forfeit_money as shopForfeitMoney, tu.nick_name as nickName, tu.avatar as avatar
        from errand_complaint ec left join tb_indent ti on ec.indent_number = ti.indent_number
        left join shop_illegal_type sit on ec.shop_illegal_id = sit.id
        left join tb_user tu on ec.rider_user_id = tu.user_id
        where ec.shop_id = #{shopId} and ec.complaint_state in (1, 5, 6)
        order by complaint_time desc
    </select>

    <select id="selectUserComplaint" resultType="com.sqx.modules.errand.entity.ErrandComplaint">
        select ec.*, ti.shop_address_detail as shopAddressDetail, ti.user_address_detail as userAddressDetail,
        eit.illegal as illegal, eit.forfeit_money as forfeitMoney, tu.nick_name as nickName, tu.avatar as avatar
        from errand_complaint ec left join tb_indent ti on ec.indent_number = ti.indent_number
        left join errand_illegal_type eit on ec.illegal_id = eit.id
        left join tb_user tu on ec.rider_user_id = tu.user_id
        where ec.user_id = #{userId}
        order by complaint_time desc
    </select>

</mapper>