<?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.com.countrygarden.bi.main.dao.advise.AfAdviseProcessMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="cn.com.countrygarden.bi.main.model.advise.AfAdviseProcess">
        <id column="id" property="id"/>
        <result column="advise_process_id" property="adviseProcessId"/>
        <result column="distribution_user_code"
                property="distributionUserCode"/>
        <result column="distribution_user_name"
                property="distributionUserName"/>
        <result column="distribution_time" property="distributionTime"/>
        <result column="distribution_advise_type_id" property="distributionAdviseTypeId"/>
        <result column="answer_time" property="answerTime"/>
        <result column="process_type" property="processType"/>
        <result column="content" property="content"/>
        <result column="user_code" property="userCode"/>
        <result column="user_name" property="userName"/>
        <result column="state" property="state"/>
        <result column="enable" property="enable"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="branch_type_code" property="branchTypeCode"/>
    </resultMap>

    <select id="selectAdviseProcessOneVOList"
            resultType="cn.com.countrygarden.bi.main.dto.advise.AfAdviseProcessOneVO">

        SELECT
        aap.id adviseProcessId,
        aap.process_type processType,
        aap.answer_time answerTime,
        aap. user_code userCode,
        aap.user_name
        userName,
        aap.content content,
        aap.state state,
        aap.distribution_advise_type_id distributionAdviseTypeId
        FROM
        af_advise_process aap
        where aap.enable=1
        <if test="adviseProcess!=null">
            <if test="adviseProcess.adviseFeedbackId">
                and
                aap.advise_feedback_id=#{adviseProcess.adviseFeedbackId}
            </if>
            <choose>
                 <when test="adviseProcess.ownCreateBy==true">
                     and (aap.state=#{adviseProcess.state} or aap.create_by=#{adviseProcess.userCode})
                 </when>
                 <otherwise>
                     and aap.state=#{adviseProcess.state}
                 </otherwise>
            </choose>
        </if>


        ORDER BY aap.process_type

    </select>
    <select id="selectAllAfAdviseProcess" resultType="cn.com.countrygarden.bi.main.dto.advise.AdviseProcessDTO">
SELECT

aap.advise_feedback_id adviseFeedbackId,
 MAX(aap.process_type) processType,
 MAX(aap.answer_time) answerTime,
 aaf.advise_type_id adviseTypeId,
 aaf.branch_type_code branchTypeCode
FROM
af_advise_process aap

LEFT JOIN af_advise_feedback aaf on aaf.id=aap.advise_feedback_id

WHERE aaf.`enable`=1 AND aaf.state=1 and aap.state=1 and aap.`enable`=1 and aaf.is_private=0
and  aaf.branch_type_code=#{branchTypeCode}
GROUP BY aap.advise_feedback_id

</select>

</mapper>