<?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.zhongzheng.modules.base.mapper.UserProfileMapper">

    <resultMap type="com.zhongzheng.modules.base.domain.UserProfile" id="UserProfileResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="oneTime" column="one_time"/>
        <result property="lastTime" column="last_time"/>
        <result property="keyValue" column="key_value"/>
        <result property="createByName" column="create_by_name"/>
        <result property="currentStatus" column="current_status"/>
        <result property="changeStatus" column="change_status"/>
        <result property="text" column="text"/>
        <result property="auditTime" column="audit_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.base.vo.UserProfileVo" id="UserProfileVo">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="sex" column="sex"/>
        <result property="status" column="status"/>
        <result property="oneTime" column="one_time"/>
        <result property="lastTime" column="last_time"/>
        <result property="keyValue" column="key_value"/>
        <result property="currentStatus" column="current_status"/>
        <result property="changeStatus" column="change_status"/>
        <result property="realname" column="realname"/>
        <result property="userAccount" column="user_account"/>
        <result property="telphone" column="telphone"/>
        <result property="idCard" column="id_card"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsCode" column="goods_code"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="text" column="text"/>
        <result property="auditTime" column="audit_time"/>
    </resultMap>

    <select id="selectUserProfile" parameterType="com.zhongzheng.modules.base.bo.UserProfileQueryBo" resultMap="UserProfileVo">
        SELECT
        up.*,
        (SELECT u.realname FROM `user` u where u.user_id = up.user_id) as realname,
        (SELECT u.user_account FROM `user` u where u.user_id = up.user_id) as user_account,
        (SELECT u.telphone FROM `user` u where u.user_id = up.user_id) as telphone,
        (SELECT u.id_card FROM `user` u where u.user_id = up.user_id) as id_card,
        (SELECT g.goods_name FROM goods g where up.goods_id = g.goods_id) as goods_name,
        (SELECT g.`code` FROM goods g where up.goods_id = g.goods_id) as goods_code,
        (SELECT cet.education_name FROM goods g LEFT JOIN course_education_type cet on g.education_type_id = cet.id where up.goods_id = g.goods_id) as education_name,
        (SELECT cet.project_name FROM goods g LEFT JOIN course_project_type cet on g.project_id = cet.id where up.goods_id = g.goods_id) as project_name,
        (SELECT cet.business_name FROM goods g LEFT JOIN course_business cet on g.business_id = cet.id where up.goods_id = g.goods_id) as business_name
        FROM
        user_profile up
        where 1=1
        and up.current_status = 1
        <if test="status != null and status.size()!=0 ">
            AND up.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="id != null and id !='' ">
            AND up.id =#{id}
        </if>
        <if test="typeStatus != null and typeStatus !='' ">
            and up.type_status = #{typeStatus}
        </if>
        <if test="userId != null and userId !='' ">
            AND up.user_id =#{userId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id =#{goodsId}
        </if>
        <if test="educationTypeId != null and educationTypeId !='' ">
            AND (SELECT cet.id FROM goods g LEFT JOIN course_education_type cet on g.education_type_id = cet.id where up.goods_id = g.goods_id) =#{educationTypeId}
        </if>
        <if test="businessId != null and businessId !='' ">
            AND (SELECT cet.id FROM goods g LEFT JOIN course_business cet on g.business_id = cet.id where up.goods_id = g.goods_id) =#{businessId}
        </if>
        <if test="schoolId != null and schoolId !='' ">
            AND (SELECT g.school_id FROM goods g  where up.goods_id = g.goods_id)  =#{schoolId}
        </if>
        <if test="majorId != null and majorId !='' ">
            AND (SELECT g.major_id FROM goods g  where up.goods_id = g.goods_id) =#{majorId}
        </if>
        <if test="changeStatus != null and changeStatus !='' ">
            AND up.change_status =#{changeStatus}
        </if>
        <if test="realname != null and realname !='' ">
            AND (SELECT count(1) FROM `user` u where u.user_id = up.user_id and u.realname like concat('%', #{realname}, '%') )> 0
        </if>
        order by up.create_time desc
    </select>

    <select id="getInfo" parameterType="com.zhongzheng.modules.base.bo.UserProfileQueryBo" resultMap="UserProfileVo">
        SELECT
        up.*,
        (SELECT u.realname FROM `user` u where u.user_id = up.user_id) as realname,
        (SELECT u.sex FROM `user` u where u.user_id = up.user_id) as sex,
        (SELECT u.user_account FROM `user` u where u.user_id = up.user_id) as user_account,
        (SELECT u.telphone FROM `user` u where u.user_id = up.user_id) as telphone,
        (SELECT u.id_card FROM `user` u where u.user_id = up.user_id) as id_card,
        (SELECT g.goods_name FROM goods g where up.goods_id = g.goods_id) as goods_name,
        (SELECT g.`code` FROM goods g where up.goods_id = g.goods_id) as goods_code,
        (SELECT cet.education_name FROM goods g LEFT JOIN course_education_type cet on g.education_type_id = cet.id where up.goods_id = g.goods_id) as education_name,
        (SELECT cet.project_name FROM goods g LEFT JOIN course_project_type cet on g.project_id = cet.id where up.goods_id = g.goods_id) as project_name,
        (SELECT cet.business_name FROM goods g LEFT JOIN course_business cet on g.business_id = cet.id where up.goods_id = g.goods_id) as business_name
        FROM
        user_profile up
        where 1=1
          and up.current_status = 1
        <if test="typeStatus != null and typeStatus !='' ">
            and up.type_status = #{typeStatus}
        </if>
        <if test="status != null and status.size()!=0 ">
            AND up.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="id != null and id !='' ">
            AND up.id =#{id}
        </if>
        <if test="userId != null and userId !='' ">
            AND up.user_id =#{userId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id =#{goodsId}
        </if>
        order by up.create_time desc LIMIT 1
    </select>

    <select id="listRecord" parameterType="com.zhongzheng.modules.base.bo.UserProfileQueryBo" resultMap="UserProfileVo">
        SELECT
        up.*,
        (SELECT u.realname FROM `user` u where u.user_id = up.user_id) as realname,
        (SELECT u.user_account FROM `user` u where u.user_id = up.user_id) as user_account,
        (SELECT u.telphone FROM `user` u where u.user_id = up.user_id) as telphone,
        (SELECT u.id_card FROM `user` u where u.user_id = up.user_id) as id_card,
        (SELECT g.goods_name FROM goods g where up.goods_id = g.goods_id) as goods_name,
        (SELECT g.`code` FROM goods g where up.goods_id = g.goods_id) as goods_code,
        (SELECT cet.education_name FROM goods g LEFT JOIN course_education_type cet on g.education_type_id = cet.id where up.goods_id = g.goods_id) as education_name,
        (SELECT cet.project_name FROM goods g LEFT JOIN course_project_type cet on g.project_id = cet.id where up.goods_id = g.goods_id) as project_name,
        (SELECT cet.business_name FROM goods g LEFT JOIN course_business cet on g.business_id = cet.id where up.goods_id = g.goods_id) as business_name
        FROM
        user_profile up
        where 1=1
        and up.status != 2
        AND up.change_status != 2
        <if test="typeStatus != null and typeStatus !='' ">
            and up.type_status = #{typeStatus}
        </if>
        <if test="status != null and status.size()!=0 ">
            AND up.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="id != null and id !='' ">
            AND up.id =#{id}
        </if>
        <if test="userId != null and userId !='' ">
            AND up.user_id =#{userId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id =#{goodsId}
        </if>
        <if test="educationTypeId != null and educationTypeId !='' ">
            AND g.education_type_id =#{educationTypeId}
        </if>
        <if test="businessId != null and businessId !='' ">
            AND g.business_id =#{businessId}
        </if>
        <if test="schoolId != null and schoolId !='' ">
            AND g.school_id =#{schoolId}
        </if>
        <if test="majorId != null and majorId !='' ">
            AND g.major_id =#{majorId}
        </if>
        <if test="changeStatus != null and changeStatus !='' ">
            AND up.change_status =#{changeStatus}
        </if>
        order by up.create_time desc
    </select>

    <select id="selectCountProfile" parameterType="map" resultType="Long">
        SELECT
            COUNT(1)
        FROM
            profile_tp p
        WHERE
            1 = 1
          AND FIND_IN_SET(#{goodsId},p.goods_ids)
          and FIND_IN_SET(#{userId},p.user_ids)
    </select>

    <select id="selectGrade" parameterType="map" resultType="Long">
        SELECT
            cgu.grade_id
        FROM
            class_grade_user cgu
                LEFT JOIN class_grade_goods cgg on cgu.grade_id = cgg.grade_id
                LEFT JOIN class_grade cg on cg.grade_id = cgg.grade_id
        WHERE
            cgu.user_id = #{userId}
          and cgg.goods_id = #{goodsId}
          and cg.`status` = 1
          and cgu.`status` = 1
        ORDER BY cgu.create_time desc
            LIMIT 1
    </select>
</mapper>
