<?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.sqx.modules.resumes.dao.ResumesDao">


    <select id="selectResumesList" resultType="com.sqx.modules.resumes.entity.Resumes">
        select r.*,u.user_name as userName,u.avatar from resumes r
        left join tb_user u on u.user_id=r.user_id
        <where>
            <if test="resumes.isShow!=null">
                and r.is_show = #{resumes.isShow}
            </if>
            <if test="resumes.resumesName!=null and resumes.resumesName!=''">
                and r.resumes_name like concat('%',#{resumes.resumesName},'%')
            </if>
            <if test="resumes.resumesPhone!=null and resumes.resumesPhone!=''">
                and r.resumes_phone like concat('%',#{resumes.resumesPhone},'%')
            </if>
            <if test="resumes.resumesMajor!=null and resumes.resumesMajor!=''">
                and r.resumes_major like concat('%',#{resumes.resumesMajor},'%')
            </if>
            <if test="resumes.resumesPost!=null and resumes.resumesPost!=''">
                and r.resumes_post like concat('%',#{resumes.resumesPost},'%')
            </if>
            <if test="resumes.resumesWorkExperience!=null and resumes.resumesWorkExperience!=''">
                and r.resumes_work_experience like concat('%',#{resumes.resumesWorkExperience},'%')
            </if>
            <if test="resumes.status!=null and resumes.status!=0">
                and r.status=#{resumes.status}
            </if>
            <if test="resumes.isRecommend!=null and resumes.isRecommend==1">
                and (r.is_recommend =1 or r.is_recommend is null )
            </if>
            <if test="resumes.isRecommend!=null and resumes.isRecommend==2">
                and r.is_recommend=#{resumes.isRecommend}
            </if>
            <if test="resumes.resumesEducation!=null and resumes.resumesEducation!=''">
                and r.resumes_education like concat('%',#{resumes.resumesEducation},'%')
            </if>
            <if test="resumes.resumesMarriage!=null and resumes.resumesMarriage!=''">
                and r.resumes_marriage like concat('%',#{resumes.resumesMarriage},'%')
            </if>
            <if test="resumes.industryName!=null and resumes.industryName!=''">
                and r.industry_name like concat('%',#{resumes.industryName},'%')
            </if>
            <if test="resumes.rulePostName!=null and resumes.rulePostName!=''">
                and r.rule_post_name like concat('%',#{resumes.rulePostName},'%')
            </if>
            <if test="resumes.resumesCompensation!=null and resumes.resumesCompensation!=''">
                and r.resumes_compensation like concat('%',#{resumes.resumesCompensation},'%')
            </if>
            <if test="resumes.school!=null and resumes.school!=''">
                and r.school like concat('%',#{resumes.school},'%')
            </if>
            <if test="resumes.resumesAge!=null and resumes.resumesAge!=''">
                and r.resumes_age =#{resumes.resumesAge}
            </if>
            <if test="startTime!=null and startTime!='' and startTime!=null and startTime!=''">
                and date_format(r.create_time,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
            </if>
            <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
                and date_format(cr.create_time,'%Y-%m-%d') &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!='' and (startTime==null or startTime=='')">
                and date_format(r.create_time,'%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
        order by r.update_time desc
    </select>
    <select id="selectResumesListHr" resultType="com.sqx.modules.resumes.entity.ResumesListDto">
        SELECT
        u.user_name AS userName,
        u.avatar AS userAvatar,
        u.sex As userSex,
        u.age AS userAge,
        last_work.type AS workType,
        last_work.company_name AS companyName,
        intent.intentIndustry as intentIndustry,
        last_work.start_time As lastWorkStartTime,
        last_work.end_time AS lastWorkEndTime,
        last_work.work_content AS workContent,
        e.school AS school,
        e.degree AS degree,
        intent.all_expected_positions AS expectedPosition,
        intent.all_expected_cities AS expectedCity,
        intent.min_salary AS minSalary,
        intent.max_salary AS maxSalary,
--         resumes.status AS resumesStatus,
        resumes.resumes_education AS resumesEducation,
        resumes.resumes_work_experience AS resumesWorkExperience,
        resumes.resumes_id AS resumesId,
        resumes.is_recommend AS isRecommend,
        resumes.resumes_status AS resumesStatus
        FROM
        tb_user u
        INNER JOIN resumes
        ON u.user_id = resumes.user_id
        AND resumes.`status` = 1
        INNER JOIN (
        SELECT
        edu.*,
        ROW_NUMBER() OVER (PARTITION BY edu.user_id ORDER BY edu.end_time DESC) AS edu_rn
        FROM edu
        WHERE edu.is_use = 1
        ) e ON u.user_id = e.user_id AND e.edu_rn = 1
        INNER JOIN (
        SELECT
        wed.*,
        we.type,
        we.company_name,
        we.user_id,
        ROW_NUMBER() OVER (PARTITION BY we.user_id ORDER BY wed.end_time DESC) AS work_rn
        FROM work_exp we
        INNER JOIN work_exp_detail wed ON we.work_exp_id = wed.work_exp_id
        WHERE we.is_use = 1
        ) last_work ON u.user_id = last_work.user_id AND last_work.work_rn = 1

        INNER JOIN (
        SELECT
        user_id,
        GROUP_CONCAT(DISTINCT rule_classify_name SEPARATOR ',') AS all_expected_positions,
        GROUP_CONCAT(DISTINCT citys SEPARATOR ',') AS all_expected_cities,
        GROUP_CONCAT(DISTINCT industry SEPARATOR',') AS intentIndustry,
        MIN(min_salary) AS min_salary,
        MAX(max_salary) AS max_salary
        FROM intention
        WHERE is_default = 1
        GROUP BY user_id
        ) intent ON u.user_id = intent.user_id
    <where>
        <if test="resumesListDto.expectedCity!=null and resumesListDto.expectedCity!=''">
            AND FIND_IN_SET(#{resumesListDto.expectedCity},intent.all_expected_cities)
        </if>
        <if test="resumesListDto.resumesId !=null and resumesListDto.resumesId!=''">
             AND resumes.resumes_id = #{resumesListDto.resumesId}
        </if>
        <if test="resumesListDto.expectedPosition != null and resumesListDto.expectedPosition != ''">
            AND FIND_IN_SET(#{resumesListDto.expectedPosition}, intent.all_expected_positions)
        </if>
        <if test="resumesListDto.isRecommend != null">
            AND resumes.is_recommend = 1
        </if>
        <if test="resumesListDto.resumesStatus != null">
            AND resumes.resumes_status = #{resumesListDto.resumesStatus}
        </if>
<!--        <if test=""-->
    </where>
    </select>
    <select id="selectResumesListByCompany" resultType="com.sqx.modules.resumes.entity.Resumes">
        SELECT
        r.*,
        u.user_name AS userName,
        u.avatar
        FROM
        resumes r
        LEFT JOIN tb_user u
        ON u.user_id = r.user_id
        WHERE u.`user_id` NOT IN (SELECT user_id FROM shield_company s WHERE s.`company_id`=#{companyId} and
        shield_type=2)
        AND (r.resumes_status !=3)
        AND r.is_show = 1
        <if test="resumes.resumesPost==null or resumes.resumesPost==''">
            <if test="companyClassify!=null and companyClassify.size()>0">
                and r.resumes_post in
                <foreach collection="companyClassify" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </if>
        <if test="resumes.resumesName!=null and resumes.resumesName!=''">
            and r.resumes_name like concat('%',#{resumes.resumesName},'%')
        </if>
        <if test="resumes.industryName!=null and resumes.industryName!=''">
            and r.industry_name = #{resumes.industryName}
        </if>
        <if test="resumes.resumesPhone!=null and resumes.resumesPhone!=''">
            and r.resumes_phone like concat('%',#{resumes.resumesPhone},'%')
        </if>
        <if test="resumes.resumesMajor!=null and resumes.resumesMajor!=''">
            and r.resumes_major like concat('%',#{resumes.resumesMajor},'%')
        </if>
        <if test="resumes.resumesPost!=null and resumes.resumesPost!=''">
            and r.resumes_post like concat('%', #{resumes.resumesPost},'%')
        </if>
        <if test="resumes.resumesWorkExperience!=null and resumes.resumesWorkExperience!=''">
            and r.resumes_work_experience like concat('%',#{resumes.resumesWorkExperience},'%')
        </if>
        <if test="resumes.status!=null and resumes.status!=0">
            and r.status=#{resumes.status}
        </if>
        <if test="resumes.ruleClassifyId!=null and resumes.ruleClassifyId!=0">
            and r.rule_classify_id=#{resumes.ruleClassifyId}
        </if>
        <if test="resumes.resumesAge!=null and resumes.resumesAge!=''">
            and r.resumesAge=#{resumes.resumesAge}
        </if>
        <if test="resumes.resumesMarriage!=null and resumes.resumesMarriage!=''">
            and r.resumes_marriage=#{resumes.resumesMarriage}
        </if>
        <if test="resumes.resumesCompensation!=null and resumes.resumesCompensation!=''">
            and r.resumes_compensation=#{resumes.resumesCompensation}
        </if>
        <if test="resumes.isRecommend!=null and resumes.isRecommend==2">
            and r.is_recommend=#{resumes.isRecommend}
        </if>
        <if test="resumes.isRecommend!=null and resumes.isRecommend==1">
            and (r.is_recommend=#{resumes.isRecommend} or r.is_recommend is null)
        </if>
        <if test="resumes.resumesEducation!=null and resumes.resumesEducation!=''">
            and r.resumes_education like concat('%',#{resumes.resumesEducation},'%')
        </if>
        <if test="resumes.resumesWorkExperience!=null and resumes.resumesWorkExperience!=''">
            and r.resumes_work_experience like concat('%',#{resumes.resumesWorkExperience},'%')
        </if>
        <if test="resumes.rulePostName!=null and resumes.rulePostName!=''">
            and r.rule_post_name like concat('%',#{resumes.rulePostName},'%')
        </if>
        <if test="resumes.resumesIntention!=null and resumes.resumesIntention!=''">
            and (r.resumes_intention like concat('%',#{resumes.resumesIntention},'%') or r.city like
            concat('%',#{resumes.resumesIntention},'%') )
        </if>
        <if test="resumes.city!=null and resumes.city!=''">
            and (r.city like concat('%',#{resumes.city},'%') or r.city like
            concat('%',#{resumes.city},'%') )
        </if>
        <if test="resumes.school!=null and resumes.school!=''">
            and r.school like concat('%',#{resumes.school},'%')
        </if>
        <if test="screen!=null and screen==1">
            order by is_recommend desc
        </if>
        <if test="screen!=null and screen==2">
            order by update_time desc
        </if>
    </select>


    <insert id="insertResumes" useGeneratedKeys="true" keyProperty="resumesId"
            parameterType="com.sqx.modules.resumes.entity.Resumes">
        INSERT INTO resumes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != resumesName and '' != resumesName">
                resumes_name,
            </if>
            <if test="null != resumesAge and '' != resumesAge">
                resumes_age,
            </if>
            <if test="null != resumesPhone and '' != resumesPhone">
                resumes_phone,
            </if>
            <if test="null != resumesEducation and '' != resumesEducation">
                resumes_education,
            </if>
            <if test="null != resumesMarriage and '' != resumesMarriage">
                resumes_marriage,
            </if>
            <if test="null != resumesCompensation and '' != resumesCompensation">
                resumes_compensation,
            </if>
            <if test="null != resumesEmail and '' != resumesEmail">
                resumes_email,
            </if>
            <if test="null != resumesWorkExperience and '' != resumesWorkExperience">
                resumes_work_experience,
            </if>
            <if test="null != resumesIntention and '' != resumesIntention">
                resumes_intention,
            </if>
            <if test="null != resumesMajor and '' != resumesMajor">
                resumes_major,
            </if>
            <if test="null != resumesPost and '' != resumesPost">
                resumes_post,
            </if>
            <if test="null != resumesDetails and '' != resumesDetails">
                resumes_details,
            </if>
            <if test="null != resumesImageName and '' != resumesImageName">
                resumes_image_name,
            </if>
            <if test="null != resumesImage and '' != resumesImage">
                resumes_image,
            </if>
            <if test="null != userId ">
                user_id,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != updateTime and '' != updateTime">
                update_time,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != school and '' != school">
                school,
            </if>
            <if test="null != ruleClassifyId and '' != ruleClassifyId">
                rule_classify_id,
            </if>
            <if test="null != address and '' != address">
                address,
            </if>
            <if test="null != resumesSex">
                resumes_sex,
            </if>
            <if test="null != rulePostName and '' != rulePostName">
                rule_post_name,
            </if>
            <if test="null != rulePostId ">
                rule_post_id,
            </if>
            <if test="null != isRecommend ">
                is_recommend,
            </if>
            <if test="null != city ">
                city,
            </if>
            <if test="null != major">
                major,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != resumesName and '' != resumesName">
                #{resumesName},
            </if>
            <if test="null != resumesAge and '' != resumesAge">
                #{resumesAge},
            </if>
            <if test="null != resumesPhone and '' != resumesPhone">
                #{resumesPhone},
            </if>
            <if test="null != resumesEducation and '' != resumesEducation">
                #{resumesEducation},
            </if>
            <if test="null != resumesMarriage and '' != resumesMarriage">
                #{resumesMarriage},
            </if>
            <if test="null != resumesCompensation and '' != resumesCompensation">
                #{resumesCompensation},
            </if>
            <if test="null != resumesEmail and '' != resumesEmail">
                #{resumesEmail},
            </if>
            <if test="null != resumesWorkExperience and '' != resumesWorkExperience">
                #{resumesWorkExperience},
            </if>
            <if test="null != resumesIntention and '' != resumesIntention">
                #{resumesIntention},
            </if>
            <if test="null != resumesMajor and '' != resumesMajor">
                #{resumesMajor},
            </if>
            <if test="null != resumesPost and '' != resumesPost">
                #{resumesPost},
            </if>
            <if test="null != resumesDetails and '' != resumesDetails">
                #{resumesDetails},
            </if>
            <if test="null != resumesImageName and '' != resumesImageName">
                #{resumesImageName},
            </if>
            <if test="null != resumesImage and '' != resumesImage">
                #{resumesImage},
            </if>
            <if test="null != userId ">
                #{userId},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != updateTime and '' != updateTime">
                #{updateTime},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != school and '' != school">
                #{school},
            </if>
            <if test="null != ruleClassifyId and '' != ruleClassifyId">
                #{ruleClassifyId},
            </if>
            <if test="null != address and '' != address">
                #{address},
            </if>
            <if test="null != resumesSex">
                #{resumesSex},
            </if>
            <if test="null != rulePostName and '' != rulePostName">
                #{rulePostName},
            </if>
            <if test="null != rulePostId ">
                #{rulePostId},
            </if>
            <if test="null != isRecommend ">
                #{isRecommend},
            </if>
            <if test="null != city ">
                #{city},
            </if>
            <if test="null != major ">
                #{major},
            </if>
        </trim>
    </insert>

    <select id="selectResumesStatusByUpdateTime" resultType="com.sqx.modules.resumes.entity.Resumes">
        select *
        from resumes
        where is_recommend = 2
          and date_format(DATE_ADD(update_time, INTERVAL 3 day), '%Y-%m-%d') &lt;= date_format(now(), '%Y-%m-%d')
    </select>
    <select id="exportExcelCompanyList" resultType="com.sqx.modules.resumes.entity.Resumes">

        select r.*,u.user_name as userName,u.avatar from resumes r
        left join tb_user u on u.user_id=r.user_id
        where 1=1
        <if test="resumes.resumesName!=null and resumes.resumesName!=''">
            and r.resumes_name like concat('%',#{resumes.resumesName},'%')
        </if>
        <if test="resumes.resumesPhone!=null and resumes.resumesPhone!=''">
            and r.resumes_phone like concat('%',#{resumes.resumesPhone},'%')
        </if>
        <if test="resumes.resumesMajor!=null and resumes.resumesMajor!=''">
            and r.resumes_major like concat('%',#{resumes.resumesMajor},'%')
        </if>
        <if test="resumes.resumesPost!=null and resumes.resumesPost!=''">
            and r.resumes_post like concat('%',#{resumes.resumesPost},'%')
        </if>
        <if test="resumes.resumesWorkExperience!=null and resumes.resumesWorkExperience!=''">
            and r.resumes_work_experience like concat('%',#{resumes.resumesWorkExperience},'%')
        </if>
        <if test="resumes.status!=null and resumes.status!=0">
            and r.status=#{resumes.status}
        </if>
        <if test="resumes.isRecommend!=null and resumes.isRecommend==1">
            and (r.is_recommend =1 or r.is_recommend is null )
        </if>
        <if test="resumes.isRecommend!=null and resumes.isRecommend==2">
            and r.is_recommend=#{resumes.isRecommend}
        </if>
        <if test="resumes.resumesEducation!=null and resumes.resumesEducation!=''">
            and r.resumes_education like concat('%',#{resumes.resumesEducation},'%')
        </if>
        <if test="resumes.resumesMarriage!=null and resumes.resumesMarriage!=''">
            and r.resumes_marriage like concat('%',#{resumes.resumesMarriage},'%')
        </if>
        <if test="resumes.industryName!=null and resumes.industryName!=''">
            and r.industry_name like concat('%',#{resumes.industryName},'%')
        </if>
        <if test="resumes.rulePostName!=null and resumes.rulePostName!=''">
            and r.rule_post_name like concat('%',#{resumes.rulePostName},'%')
        </if>
        <if test="resumes.resumesCompensation!=null and resumes.resumesCompensation!=''">
            and r.resumes_compensation like concat('%',#{resumes.resumesCompensation},'%')
        </if>
        <if test="resumes.school!=null and resumes.school!=''">
            and r.school like concat('%',#{resumes.school},'%')
        </if>
        <if test="resumes.resumesAge!=null and resumes.resumesAge!=''">
            and r.resumes_age =#{resumes.resumesAge}
        </if>
        <if test="startTime!=null and startTime!='' and startTime!=null and startTime!=''">
            and date_format(r.create_time,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
        </if>
        <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
            and date_format(cr.create_time,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!='' and (startTime==null or startTime=='')">
            and date_format(r.create_time,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        order by r.update_time desc


    </select>
    <select id="selectResumesByResumesId" resultType="com.sqx.modules.resumes.entity.Resumes">
        select *
        <if test="userId!=null">
            ,(select if(count(0)>0,1,0) from my_collection m where m.resumes_id = r.resumes_id and m.user_id = #{userId} and m.type =2 ) as isCollection
        </if>
        from resumes r
        where r.resumes_id = #{resumesId}

    </select>


</mapper>