<?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.zehong.system.mapper.TComplainDealMapper">
    
    <resultMap type="TComplainDeal" id="TComplainDealResult">
        <result property="complainDealId"    column="complain_deal_id"    />
        <result property="complainName"    column="complain_name"    />
        <result property="complainPhone"    column="complain_phone"    />
        <result property="complainMatter"    column="complain_matter"    />
        <result property="transferRecord"    column="transfer_record"    />
        <result property="dealTime"    column="deal_time"    />
        <result property="dealCondition"    column="deal_condition"    />
        <result property="complainAssignEnterproseName"    column="complain_assign_enterprose_name"    />
        <result property="complainAssignEnterproseId"    column="complain_assign_enterprose_id"    />
        <result property="complainAssignManId"    column="complain_assign_man_id"    />
        <result property="complainAssignMan"    column="complain_assign_man"    />
        <result property="complainStatus"    column="complain_status"    />
        <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="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
        <result property="provincialDataFlag"    column="provincial_data_flag" />
        <result property="complainType"    column="complain_type" />
        <result property="overtimeSupervision"    column="overtime_supervision" />
        <result property="township"    column="township" />
    </resultMap>

    <sql id="selectTComplainDealVo">
        select complain_deal_id,
               complain_name,
               complain_phone,
               complain_matter,
               transfer_record,
               deal_time,
               deal_condition,
               complain_assign_enterprose_name,
               complain_assign_enterprose_id,
               complain_assign_man_id,
               complain_assign_man,
               complain_status,
               create_by,
               create_time,
               update_by,
               update_time,
               is_del,
               remarks,provincial_data_flag,complain_type,overtime_supervision,township from t_complain_deal
    </sql>

    <select id="selectTComplainDealList" parameterType="TComplainDeal" resultMap="TComplainDealResult">
        <include refid="selectTComplainDealVo"/>
        <where>  
            <if test="complainName != null  and complainName != ''"> and complain_name like concat('%', #{complainName}, '%')</if>
            <if test="complainPhone != null  and complainPhone != ''"> and complain_phone = #{complainPhone}</if>
            <if test="complainMatter != null  and complainMatter != ''"> and complain_matter = #{complainMatter}</if>
            <if test="transferRecord != null  and transferRecord != ''"> and transfer_record = #{transferRecord}</if>
            <if test="dealCondition != null  and dealCondition != ''"> and deal_condition = #{dealCondition}</if>
            <if test="complainAssignEnterproseName != null  and complainAssignEnterproseName != ''"> and complain_assign_enterprose_name like concat('%', #{complainAssignEnterproseName}, '%')</if>
            <if test="complainAssignEnterproseId != null "> and complain_assign_enterprose_id = #{complainAssignEnterproseId}</if>
            <if test="complainAssignManId != null  and complainAssignManId != ''"> and complain_assign_man_id = #{complainAssignManId}</if>
            <if test="complainAssignMan != null "> and complain_assign_man = #{complainAssignMan}</if>
            <if test="complainStatus != null  and complainStatus != ''"> and complain_status = #{complainStatus}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="provincialDataFlag != null  and provincialDataFlag != ''"> and provincial_data_flag = #{provincialDataFlag}</if>
            <if test="complainType != null  and complainType != ''"> and complain_type = #{complainType}</if>
            <if test="overtimeSupervision != null  and overtimeSupervision != ''"> and overtime_supervision = #{overtimeSupervision}</if>
            <if test="township != null  and township != ''"> and township = #{township}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectTComplainDealById" parameterType="Long" resultMap="TComplainDealResult">
        <include refid="selectTComplainDealVo"/>
        where complain_deal_id = #{complainDealId}
    </select>

    <select id="countTComplainDeal" resultType="HomepageVo">
        select COUNT(t.complain_deal_id) AS complaintTotal,
			   SUM(CASE t.complain_status WHEN '2' THEN 1 ELSE 0 END) AS complaintFeedbackTotal
        from (select * from t_complain_deal where is_del = '0') t
    </select>

    <select id="queryAllByNotDelete" resultMap="TComplainDealResult">
        <include refid="selectTComplainDealVo"/>
        where is_del = '0'
    </select>
    <select id="queryAllByNotDeleteAndCreateTime" resultType="com.zehong.system.domain.dto.TComplainDealDTO">
        select complain_deal_id as complainDealId,complain_status as complainStatus ,create_time as createTime, update_time as updateTime,MONTH(create_time) as createTimeYear ,complain_type as complainType from t_complain_deal
        where is_del = '0'
        <if test="startOfMonth != null and  endOfMonth != null">
            and create_time  <![CDATA[>=]]> #{startOfMonth} and create_time  <![CDATA[<=]]> #{endOfMonth}
        </if>
        <if test="complaintCategory != null and complaintCategory != '' and complaintCategory != 0 ">
            and complain_type = #{complaintCategory}
        </if>
        order by create_time asc
    </select>

    <insert id="insertTComplainDeal" parameterType="TComplainDeal" useGeneratedKeys="true" keyProperty="complainDealId">
        insert into t_complain_deal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="complainName != null">complain_name,</if>
            <if test="complainPhone != null">complain_phone,</if>
            <if test="complainMatter != null">complain_matter,</if>
            <if test="transferRecord != null">transfer_record,</if>
            <if test="dealTime !=null">deal_time,</if>
            <if test="dealCondition != null">deal_condition,</if>
            <if test="complainAssignEnterproseName != null">complain_assign_enterprose_name,</if>
            <if test="complainAssignEnterproseId != null">complain_assign_enterprose_id,</if>
            <if test="complainAssignManId != null">complain_assign_man_id,</if>
            <if test="complainAssignMan != null">complain_assign_man,</if>
            <if test="complainStatus != null">complain_status,</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="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
            <if test="provincialDataFlag != null">provincial_data_flag,</if>
            <if test="complainType != null">complain_type,</if>
            <if test="overtimeSupervision != null">overtime_supervision,</if>
            <if test="township != null">township,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="complainName != null">#{complainName},</if>
            <if test="complainPhone != null">#{complainPhone},</if>
            <if test="complainMatter != null">#{complainMatter},</if>
            <if test="transferRecord != null">#{transferRecord},</if>
            <if test="dealTime != null">#{dealTime},</if>
            <if test="dealCondition != null">#{dealCondition},</if>
            <if test="complainAssignEnterproseName != null">#{complainAssignEnterproseName},</if>
            <if test="complainAssignEnterproseId != null">#{complainAssignEnterproseId},</if>
            <if test="complainAssignManId != null">#{complainAssignManId},</if>
            <if test="complainAssignMan != null">#{complainAssignMan},</if>
            <if test="complainStatus != null">#{complainStatus},</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="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="provincialDataFlag != null">#{provincialDataFlag},</if>
            <if test="complainType != null">#{complainType},</if>
            <if test="overtimeSupervision != null">#{overtimeSupervision},</if>
            <if test="township != null">#{township},</if>
         </trim>
    </insert>

    <update id="clearTComplainDeal" parameterType="TComplainDeal">
        update t_complain_deal set complain_assign_enterprose_name = null,
                                   complain_assign_enterprose_id = null,
                                   complain_assign_man_id = null,
                                   complain_assign_man = null,
                                   complain_status = #{complainStatus}
        where complain_deal_id = #{complainDealId}
    </update>

    <update id="updateTComplainDeal" parameterType="TComplainDeal">
        update t_complain_deal
        <trim prefix="SET" suffixOverrides=",">
            <if test="complainName != null">complain_name = #{complainName},</if>
            <if test="complainPhone != null">complain_phone = #{complainPhone},</if>
            <if test="complainMatter != null">complain_matter = #{complainMatter},</if>
            <if test="transferRecord != null">transfer_record = #{transferRecord},</if>
            <if test="dealTime != null">deal_time = #{dealTime},</if>
            <if test="dealCondition != null">deal_condition = #{dealCondition},</if>
            <if test="complainAssignEnterproseName != null">complain_assign_enterprose_name = #{complainAssignEnterproseName},</if>
            <if test="complainAssignEnterproseId != null">complain_assign_enterprose_id = #{complainAssignEnterproseId},</if>
            <if test="complainAssignManId != null">complain_assign_man_id = #{complainAssignManId},</if>
            <if test="complainAssignMan != null">complain_assign_man = #{complainAssignMan},</if>
            <if test="complainStatus != null">complain_status = #{complainStatus},</if>
            <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="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="provincialDataFlag != null">provincial_data_flag = #{provincialDataFlag},</if>
            <if test="complainType != null">complain_type = #{complainType},</if>
            <if test="overtimeSupervision != null">overtime_supervision = #{overtimeSupervision},</if>
            <if test="township != null">township = #{township},</if>
        </trim>
        where complain_deal_id = #{complainDealId}
    </update>

    <delete id="deleteTComplainDealById" parameterType="Long">
        delete from t_complain_deal where complain_deal_id = #{complainDealId}
    </delete>

    <delete id="deleteTComplainDealByIds" parameterType="String">
        delete from t_complain_deal where complain_deal_id in 
        <foreach item="complainDealId" collection="array" open="(" separator="," close=")">
            #{complainDealId}
        </foreach>
    </delete>
    <select id="selectUserByenterproseId" resultType="java.util.HashMap">
        SELECT user_id as id ,nick_name as nickName FROM sys_user
        WHERE del_flag!=2 and  dept_id = #{enterproseId}
    </select>
</mapper>