<?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.UserOpinionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userOpinionResultMap" type="org.springblade.business.entity.UserOpinion">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_user_name" property="createUserName"/>
        <result column="create_time" property="createTime"/>
        <result column="create_dept" property="createDept"/>
        <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="project_id" property="projectId"/>
        <result column="contract_id" property="contractId"/>
        <result column="opinion_content" property="opinionContent"/>
        <result column="number" property="number"/>
        <result column="problem_type" property="problemType"/>
        <result column="good_number" property="goodNumber"/>
    </resultMap>

    <resultMap id="businessUserOpinionMap" type="org.springblade.business.vo.BusinessUserOpinionVO">
        <result column="userOpinionId" property="userOpinionId"/>
        <result column="createTime" property="createTime"/>
        <result column="manageTime" property="manageTime"/>
        <result column="create_time" property="createTime"/>
        <result column="projectName" property="projectName"/>
        <result column="contractId" property="contractName"/>
        <result column="problemType" property="problemType"/>
        <result column="opinionContent" property="opinionContent"/>
        <result column="isCurrent" property="isCurrent"/>
        <result column="number" property="number"/>
        <result column="newNumber" property="newNumber"/>
        <result column="evaluation" property="evaluation"/>
    </resultMap>

    <select id="queryManageUserOpinionList" resultMap="businessUserOpinionMap">
        select
            uo.id as userOpinionId,
            uo.create_time as createTime,
            uof.manage_time as manageTime,
            pi.project_name as projectName,
            ci.contract_name as contractName,
            uo.problem_type as problemType,
            uo.opinion_content as opinionContent,
            case uof.is_current
            when 2 then 'true' else 'false' end as isCurrent,
            uof.number as number,
	        uo.number as newNumber,
            uof.evaluation as evaluation
        from
            u_user_opinion as uo
        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
        right join (
            select
                uof.*
            from
            (
                select user_opinion_id,is_current,manage_time,number,sort,evaluation from u_user_opinion_flow where is_deleted = 0
                <if test="currentUser != null and currentUser != ''">
                    and manage_user = #{currentUser}
                </if>
                and sort = 4 order by number DESC
            ) as uof group by uof.user_opinion_id
        ) as uof on uof.user_opinion_id = uo.id
        where uo.is_deleted = 0;
    </select>

    <select id="queryCurrentUserOpinionList" resultMap="userOpinionResultMap">
        select id,project_id,contract_id,opinion_content,problem_type,create_time,good_number, number,create_user,create_user_name from u_user_opinion where create_user = #{userId} and is_deleted = 0 order by create_time DESC
    </select>

    <select id="selectUserOpinionPage" resultMap="userOpinionResultMap">
        select id,project_id,contract_id,opinion_content,problem_type,create_time,good_number, number,create_user,create_user_name from u_user_opinion where is_deleted = 0 and create_user != #{currentUser} order by create_time DESC limit ${current},${size}
    </select>

</mapper>
