<?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="org.springblade.business.mapper.UserOpinionFlowMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userOpinionFlowResultMap" type="org.springblade.business.entity.UserOpinionFlow">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="user_opinion_id" property="userOpinionId"/>
        <result column="is_current" property="isCurrent"/>
        <result column="number" property="number"/>
        <result column="sort" property="sort"/>
        <result column="manage_user" property="manageUser"/>
        <result column="manage_user_name" property="manageUserName"/>
        <result column="manage_user_phone" property="manageUserPhone"/>
        <result column="evaluation" property="evaluation"/>
        <result column="reply_name" property="replyName"/>
        <result column="reply_content" property="replyContent"/>
        <result column="manage_time" property="manageTime"/>
    </resultMap>

    <resultMap id="AssessmentUserVOMap" type="org.springblade.business.vo.AssessmentUserVOS">
        <result column="uoFlowId" property="uoFlowId"/>
        <result column="assessmentId" property="assessmentId"/>
        <result column="createTime" property="createTime"/>
        <result column="assessmentMoney" property="assessmentMoney"/>
        <result column="assessmentContent" property="assessmentContent"/>
        <result column="projectName" property="projectName"/>
        <result column="contractName" property="contractName"/>
        <result column="userOpinionId" property="userOpinionId"/>
        <result column="opinionContent" property="opinionContent"/>
    </resultMap>

    <select id="countUserAssessmentPage" resultType="java.lang.Integer">
        select
          count(result.id)
        from
        (
            select id,date_format(create_time,'%Y-%m-%d %H:%i') as createTime from u_user_opinion_flow where
                is_deleted = 0
                and sort = 4
                and is_current = 2
                and evaluation = 3
            <if test="vo.userId != null">
                and manage_user = #{vo.userId}
            </if>
        ) AS result
        where
        1 = 1
        <if test="vo.startTime != null and vo.startTime != '' and vo.endTime != null and vo.endTime != ''">
            and result.createTime between #{vo.startTime} and #{vo.endTime}
        </if>
    </select>

    <select id="queryUserAssessmentPage" resultMap="AssessmentUserVOMap">
        select
          result.uoFlowId,
          result.assessmentId,
          result.createTime,
          result.assessmentMoney,
          result.assessmentContent,
          result.projectName,
          result.contractName,
          result.userOpinionId,
          result.opinionContent
        from (
            select
                uof.id AS uoFlowId,
                au.id AS assessmentId,
                date_format(au.create_time,'%Y-%m-%d %H:%i') as createTime,
                au.assessment_money AS assessmentMoney,
                au.assessment_content AS assessmentContent,
                pi.project_name AS projectName,
                ci.contract_name AS contractName,
                uo.id AS userOpinionId,
                uo.opinion_content AS opinionContent
            from
              u_user_opinion_flow AS uof
            left join u_assessment_user AS au on uof.id = au.user_opinion_flow_id
            right join u_user_opinion AS uo on uof.user_opinion_id = uo.id
            left join m_project_info AS pi on uo.project_id = pi.id
            left join m_contract_info AS ci on uo.contract_id = ci.id
            where
              uof.is_deleted = 0 and au.is_deleted = 0 and uo.is_deleted = 0
            and uof.sort = 4
            and uof.is_current = 2
            and uof.evaluation = 3
            <if test="vo.userId != null">
                and uof.manage_user = #{vo.userId}
            </if>
        ) AS result
        where
          1 = 1
        <if test="vo.startTime != null and vo.startTime != '' and vo.endTime != null and vo.endTime != ''">
            and result.createTime between #{vo.startTime} and #{vo.endTime}
        </if>
        limit #{current},#{size}
    </select>

    <select id="queryLeftFlowManageUser" resultMap="userOpinionFlowResultMap">
        select id,manage_user,manage_user_name,manage_user_phone from u_user_opinion_flow where is_deleted = 0 and number = #{number} and sort = 2 and user_opinion_id = #{userOpinionId}
    </select>

    <select id="queryCurrentUserOpinionFlowByUserOpinionId" resultMap="userOpinionFlowResultMap">
        select
            id,
            user_opinion_id,
            evaluation,
            manage_user,
            manage_user_name,
            manage_user_phone,
            sort,
            manage_time,
            reply_name,
            reply_content,
            is_current
        from u_user_opinion_flow where is_deleted = 0 and user_opinion_id = #{key} and number = #{number} order by sort ASC
    </select>

    <select id="selectIsSolveByUserOpinionKey" resultType="java.lang.Integer">
        select is_current from u_user_opinion_flow where is_deleted = 0 and user_opinion_id = #{key} order by number DESC,sort DESC limit 1
    </select>

    <select id="selectUserOpinionFlowPage" resultMap="userOpinionFlowResultMap">
        select * from u_user_opinion_flow where is_deleted = 0
    </select>

</mapper>
