<?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="MdeJobAdvertisedMapper">
    <resultMap type="com.wechat.model.dbEntity.MdeJobAdvertised" id="MdeJobAdvertised">
        <id column="job_id" property="jobId"/>
        <result column="publisher" property="publisher"/>
        <result column="job_name" property="jobName"/>
        <result column="position_id" property="positionId"/>
        <result column="add_province" property="addProvince"/>
        <result column="add_city" property="addCity"/>
        <result column="add_area" property="addArea"/>
        <result column="add_detail" property="addDetail"/>
        <result column="add_longitude" property="addLongitude"/>
        <result column="add_latitude" property="addLatitude"/>
        <result column="work_experience" property="workExperience"/>
        <result column="education" property="education"/>
        <result column="job_describe" property="jobDescribe"/>
        <result column="job_status" property="jobStatus"/>
        <result column="recommend_flag" property="recommendFlag"/>
        <result column="version_no" property="versionNo"/>
        <result column="remarks" property="remarks"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_by" property="createBy"/>
        <result column="create_date" property="createDate"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_date" property="updateDate"/>
        <result column="job_requirement" property="jobRequirement"/>
        <result column="job_invitation" property="jobInvitation"/>
        <result column="pay_from" property="payFrom"/>
        <result column="pay_to" property="payTo"/>
        <result column="contact_information" property="contactInformation"/>

        <!-- 酒店名称 -->
        <result column="publisher_name" property="publisherName"/>
        <result column="foreign_name" property="foreignName"/>
        <result column="identity_flag" property="identityFlag"/>
        <result column="RECOMMEND_FLAG" property="recommendFlag"/>
        <result column="PREPARE_FLAG" property="prepareFlag"/>
        <result column="PREPARE_YEAR" property="prepareYear"/>
        <result column="PREPARE_QUARTER" property="prepareQuarter"/>
    </resultMap>
    <resultMap type="com.wechat.model.dbEntity.MdeJobAdvertised" id="mdeJobAdvertisedJoin">
        <id column="job_id" property="jobId"/>
        <result column="job_name" property="jobName"/>
        <collection property="userCodeList" ofType="com.wechat.model.dbEntity.MdeJobInvitationRel">
            <result column="USER_CODE" property="userCode"/>
        </collection>
    </resultMap>
    <resultMap type="com.wechat.model.dbEntity.MdeJobAdvertised" id="mdePositionCvRelMap">
        <id column="job_id" property="jobId"/>
        <result column="job_name" property="jobName"/>
        <collection property="userCodeList" ofType="com.wechat.model.dbEntity.MdeJobInvitationRel">
            <result column="USER_CODE" property="userCode"/>
        </collection>
    </resultMap>
    <sql id="mdeJobAdvertisedColumns">
        a.job_id AS "jobId",
		a.publisher AS "publisher",
		a.job_name AS "jobName",
		a.position_id AS "positionId",
		a.add_province AS "addProvince",
		a.add_city AS "addCity",
		a.add_area AS "addArea",
		a.add_detail AS "addDetail",
		a.add_longitude AS "addLongitude",
		a.add_latitude AS "addLatitude",
		a.work_experience AS "workExperience",
		a.education AS "education",
		a.job_describe AS "jobDescribe",
		a.job_status AS "jobStatus",
		a.RECOMMEND_FLAG AS "recommendFlag",
		a.pay_from AS "payFrom",
		a.pay_to AS "payTo",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate",
		a.job_requirement AS "jobRequirement",
		a.job_invitation AS "jobInvitation",
		a.contact_information AS "contactInformation"
    </sql>

    <sql id="userColumJoin">
        ,u.name AS "publisherName"
		,u.head_portrait AS "headPortrait"
		,u.foreign_name as "foreignName"
		,u.PREPARE_FLAG as prepareFlag
		,u.identity_flag as "identityFlag"
    </sql>
    <sql id="areaColumJoin">
        <choose>
            <when test="lang == 'en'.toString()">
                ,s1.name_en as addProvinceName,s2.name_en as addCityName,s3.name_en as addAreaName
            </when>
            <otherwise>
                ,s1.name as addProvinceName,s2.name as addCityName,s3.name as addAreaName
            </otherwise>
        </choose>
    </sql>
    <sql id="userLeftJoin">
        inner join mde_user u on u.user_code = a.publisher and u.del_flag=0
    </sql>
    <sql id="arealeftJoin">
        inner JOIN sys_area s1 ON a.ADD_PROVINCE = s1.code
		inner JOIN sys_area s2 ON a.ADD_CITY = s2.code
		LEFT JOIN sys_area s3 ON a.ADD_AREA = s3.code
    </sql>

    <!-- 已发职位 or 已发职位的关键词检索 表连接  -->
    <sql id="mdeJobAdvertisedJoinCommon">
        LEFT JOIN (
        SELECT
        rel.POS_ID,
        COUNT( rel.INTV_ID ) AS cvNum,
        min( rel.CV_STATUS ) AS newFlag
        FROM
        mde_position_cv_rel rel
        INNER JOIN mde_user u ON u.USER_CODE = rel.CANDIDATE
        WHERE
        rel.PUBLISHER = #{userCode}
        AND u.DEL_FLAG = 0
        GROUP BY
        rel.POS_ID
        ) rel2 ON ad.JOB_ID = rel2.POS_ID
        LEFT JOIN sys_dict s ON s.`value` = ad.WORK_EXPERIENCE
        LEFT JOIN sys_dict edu ON edu.`value` = ad.education
        LEFT JOIN sys_area s1 ON ad.ADD_PROVINCE = s1.CODE
        LEFT JOIN sys_area s2 ON ad.ADD_CITY = s2.CODE
        LEFT JOIN mde_position p ON ad.POSITION_ID = p.POSITION_ID
        WHERE
        ad.DEL_FLAG = 0
        AND ad.PUBLISHER = #{userCode}
        AND s.type = 'job_year'
        AND edu.type = 'education'
        AND p.DEL_FLAG =0
    </sql>

    <!-- 已发职位 or 已发职位的关键词检索 select -->
    <sql id="mdeJobAdvertisedSelectCommon">
        ad.JOB_NAME AS jobName,
        ad.JOB_ID As jobId,
        ad.RECOMMEND_FLAG as recommendFlag,
        ad.PAY_FROM as payFrom,
        ad.PAY_TO as payTo,
        <choose>
            <when test="lang=='en'.toString()">
                s.en as workExperience,
                edu.en AS education,
                s1.name_en as addProvinceName,s2.name_en as addCityName,
                p.POSITION_NAME_EN as positionName,
            </when>
            <otherwise>
                s.label AS workExperience,
                edu.label AS education,
                s1.name as addProvinceName,s2.name as addCityName,
                p.POSITION_NAME_CN as positionName,
            </otherwise>
        </choose>
        p.POSITION_ID as positionId,
        ad.UPDATE_DATE AS publishDate,
        ad.JOB_STATUS AS jobStatus,
        rel2.cvNum AS cvNum,
        IFNULL( rel2.newFlag, 9 ) AS newFlag,
        ad.VERSION_NO as versionNo
    </sql>
    <!-- 获取职位信息 -->
    <select id="get" resultMap="MdeJobAdvertised">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        ,u.PREPARE_YEAR as prepareYear
        ,u.PREPARE_QUARTER as prepareQuarter
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin">
        </include>
        WHERE a.job_id = #{id}
        and a.del_flag=0
    </select>

    <!-- 更加id获取职位信息 -->
    <select id="getInfoById" resultType="com.wechat.model.dbEntity.MdeJobAdvertisedEntity">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        FROM mde_job_advertised a
        WHERE a.job_id = #{id}
    </select>

    <!-- 获取职位信息 -->
    <select id="getInfoByIds" resultType="com.wechat.model.dto.ShowOfJobsDto">
        SELECT
        a.job_id AS "jobId",
        a.publisher AS "publisher",
        a.job_name AS "jobName",
        a.position_id AS "positionId",
        a.job_name as "positionName",
        pos.REMARKS as "abbreviation",
        a.work_experience AS "workExperience",
        a.education AS "education",
        a.job_describe AS "jobDescribe",
        a.job_requirement AS "jobRequirement",
        a.PAY_FROM as payFrom,
        a.PAY_TO as payTo,
        a.create_date AS "createDate",
        a.job_invitation AS "jobInvitation"
        ,s1.name as addProvinceName
        ,s2.name as addCityName
        ,s3.name as addAreaName
        FROM mde_job_advertised a
        <include refid="arealeftJoin">
        </include>
        inner join mde_position pos
        on a.position_id = pos.position_id
        WHERE a.JOB_STATUS = "0"
        and a.job_id in
        <foreach collection="idList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        order by pos.POSITION_GRADE
    </select>

    <insert id="insert">
        INSERT INTO mde_job_advertised(
        publisher,
        job_name,
        position_id,
        add_province,
        add_city,
        add_area,
        add_detail,
        add_longitude,
        add_latitude,
        work_experience,
        education,
        job_describe,
        remarks,
        create_by,
        create_date,
        update_by,
        update_date,
        <if test="recommendFlag != null and recommendFlag != ''">
            recommend_flag,
        </if>
        <if test="payFrom != null and payFrom != 0">
            pay_from,
        </if>
        <if test="payTo != null and payTo != 0">
            pay_to,
        </if>
        job_requirement,
        job_invitation,
        contact_information
        ) VALUES (
        #{publisher},
        #{jobName},
        #{positionId},
        #{addProvince},
        #{addCity},
        #{addArea},
        #{addDetail},
        #{addLongitude},
        #{addLatitude},
        #{workExperience},
        #{education},
        #{jobDescribe},
        #{remarks},
        #{createBy},
        #{createDate},
        #{updateBy},
        #{updateDate},
        <if test="recommendFlag != null and recommendFlag != ''">
            #{recommendFlag},
        </if>
        <if test="payFrom != null and payFrom != 0">
            #{payFrom},
        </if>
        <if test="payTo != null and payTo != 0">
            #{payTo},
        </if>
        #{jobRequirement},
        #{jobInvitation},
        #{contactInformation}
        )
    </insert>

    <update id="update">
        UPDATE mde_job_advertised SET
        <if test="publisher != null and publisher != ''">
            publisher = #{publisher},
        </if>
        <if test="jobName != null and jobName != ''">
            job_name = #{jobName},
        </if>
        <if test="positionId != null and positionId != ''">
            position_id = #{positionId},
        </if>
        <if test="addProvince != null and addProvince != ''">
            add_province = #{addProvince},
        </if>
        <if test="addCity != null and addCity != ''">
            add_city = #{addCity},
        </if>
        add_area = #{addArea},
        add_detail = #{addDetail},
        <if test="addLongitude != null and addLongitude != ''">
            add_longitude = #{addLongitude},
        </if>
        <if test="addLatitude != null and addLatitude != ''">
            add_latitude = #{addLatitude},
        </if>
        <if test="workExperience != null and workExperience != ''">
            work_experience = #{workExperience},
        </if>
        <if test="education != null and education != ''">
            education = #{education},
        </if>
        <if test="jobDescribe != null and jobDescribe != ''">
            job_describe = #{jobDescribe},
        </if>
        <if test="jobStatus != null and jobStatus != ''">
            job_status = #{jobStatus},
        </if>
        <if test="remarks != null and remarks != ''">
            remarks = #{remarks},
        </if>
        <if test="recommendFlag != null and recommendFlag != ''">
            recommend_flag = #{recommendFlag},
        </if>
        pay_from = #{payFrom},
        pay_to = #{payTo},
        <if test="updateBy != null and updateBy != ''">
            update_by = #{updateBy},
        </if>
        <if test="updateDate != null">
            update_date = #{updateDate},
        </if>
        <if test="createDate != null">
            create_date = #{createDate},
        </if>
        <if test="jobRequirement != null and jobRequirement != ''">
            job_requirement = #{jobRequirement},
        </if>
        <if test="jobInvitation != null and jobInvitation != ''">
            job_invitation = #{jobInvitation},
        </if>
        <if test="contactInformation !=null and contactInformation != ''">
            contact_information = #{contactInformation},
        </if>
        version_no = version_no +1
        WHERE job_id = #{jobId}
        <if test="versionNo !=null and versionNo != ''">
            and version_no = #{versionNo}
        </if>
    </update>

    <update id="delete">
        UPDATE mde_job_advertised
        SET version_no = version_no + 1,
            del_flag   = #{delFlag}
        WHERE job_id = #{id}
          and version_no = #{versionNo}
    </update>
    
    <delete id="deleteByJobId">
        DELETE FROM mde_job_advertised
        WHERE job_id = #{id}
          AND version_no = #{versionNo}
          AND PUBLISHER = #{userCode}
    </delete>

    <select id="getJobCvNum" resultType="com.wechat.model.requestDto.AdJobCvNumReq">
        SELECT
            ad.JOB_ID as jobId,
            ad.JOB_NAME as jobName,
            ad.JOB_STATUS as status,
            IFNULL(rel2.cvNum,0)  as cvNum
        FROM
            mde_job_advertised ad
                LEFT JOIN (
                SELECT
                    rel.POS_ID,
                    COUNT( rel.INTV_ID ) AS cvNum,
                    min( rel.CV_STATUS ) AS newFlag
                FROM
                    mde_position_cv_rel rel
                        INNER JOIN mde_user u ON u.USER_CODE = rel.CANDIDATE
                WHERE
                    rel.PUBLISHER = #{userCode}
                  AND u.DEL_FLAG = 0
                GROUP BY
                    rel.POS_ID
            ) rel2 ON ad.JOB_ID = rel2.POS_ID
        WHERE
            ad.JOB_ID = #{id}
    </select>

    <!-- 推荐职位 （已登录） -->
    <select id="getRecommentJobs" resultMap="MdeJobAdvertised">
        <![CDATA[
        SET @num = 0;
        SET @cont = 0;
        SET @publisher = '';

        SELECT m.JOB_ID,
               m.PUBLISHER,
               m.row_number,
               m.JOB_NAME,
               m.ADD_PROVINCE,
               s1.`name` AS addProvinceName,
               s2.`name` AS addCityName,
               s3.`name` AS addAreaName,
               m.ADD_CITY,
               m.ADD_AREA,
               m.ADD_DETAIL,
               m.JOB_DESCRIBE,
               m.CREATE_DATE,
               m.UPDATE_DATE,
               m.publisher_name,
               m.PREPARE_FLAG,
               m.headPortrait,
               m.PAY_FROM,
               m.PAY_TO,
               m.foreignName
        FROM (SELECT base.*,
                     CASE
                         WHEN @publisher = base.PUBLISHER
                             THEN @num := @num + 1
                         ELSE @num := 1
                         END row_number,
				@publisher := base.PUBLISHER,
				@cont := @cont + 1
              FROM
                  (
                  SELECT
                  a.JOB_ID, a.PUBLISHER, a.JOB_NAME, a.ADD_PROVINCE, a.ADD_CITY, a.ADD_AREA, a.ADD_DETAIL, a.JOB_DESCRIBE, a.CREATE_DATE, a.UPDATE_DATE, a.PAY_FROM, a.PAY_TO, u.`NAME` AS publisher_name, u.PREPARE_FLAG, u.head_portrait AS headPortrait, u.foreign_name AS foreignName
                  FROM
                  mde_job_advertised a
                  INNER JOIN mde_user u ON u.USER_CODE = a.PUBLISHER
                  AND u.VIP_FLAG != 0
                  WHERE
                  a.job_status = 0
                  and a.RECOMMEND_FLAG = '1'
                  AND date ( a.UPDATE_DATE ) > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH )
                  ORDER BY
                  RAND( )
                  ) base
              ORDER BY
                  base.PUBLISHER) m
                 LEFT JOIN sys_area s1 ON m.ADD_PROVINCE = s1.CODE
                 LEFT JOIN sys_area s2 ON m.ADD_CITY = s2.CODE
                 LEFT JOIN sys_area s3 ON m.ADD_AREA = s3.CODE
        WHERE m.row_number <= 2
        ORDER BY RAND()
        LIMIT #{limitSize}
        ]]>
    </select>

    <!-- 相似职位 -->
    <select id="getSimilarJobs" resultType="com.wechat.model.dto.JobAdvertisedListDto">
        SELECT
        a.job_id AS "jobId",
        a.job_name AS "jobName",
        a.position_id AS "positionId",
        a.add_detail AS "addDetail",
        a.add_longitude AS "addLongitude",
        a.add_latitude AS "addLatitude",
        a.work_experience AS "workExperience",
        a.education AS "education",
        a.PAY_FROM as payFrom,
        a.PAY_TO as payTo,
        puvView.PAGE_VIEW as pageView,
        a.update_date AS "updateDate"
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/>
        <include refid="arealeftJoin">
        </include>
        left join MDE_VIEW_JOB_PUV_ALL puvView
        on puvView.JOB_ID = a.JOB_ID
        <where>
            a.del_flag = 0 and a.job_id != #{jobId} and a.job_status =0
            and a.position_id = (select position_id from mde_job_advertised where job_id=#{jobId})
            AND a.job_id >=
            ((SELECT MAX(job_id) FROM mde_job_advertised) - (SELECT MIN(job_id) FROM mde_job_advertised)) * RAND() +
            ( SELECT MIN(job_id) FROM mde_job_advertised )
        </where>
        order by a.RECOMMEND_FLAG desc,a.update_date desc
        limit #{limitSize}
    </select>
    <!-- 酒店的其他职位 -->
    <select id="gethotelOtherPositionList"
            resultMap="MdeJobAdvertised">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/>
        <include refid="arealeftJoin">
        </include>
        <where>
            a.del_flag = 0 and job_id != #{jobId} and a.job_status =0
            and a.publisher = #{publisher}
        </where>
        order by a.RECOMMEND_FLAG desc,a.update_date desc
        limit #{limitSize}
    </select>
    <!-- rayson-职位搜索  （未登录） -->
    <select id="searchJobNotLogin" resultMap="MdeJobAdvertised">
        SELECT
        j.JOB_ID,
        j.PUBLISHER,
        u.`NAME` AS publisher_name,
        u.foreign_name,
        u.identity_flag,
        j.JOB_NAME,
        j.CREATE_DATE
        FROM
        mde_job_advertised j
        INNER JOIN mde_user u ON u.USER_CODE = j.PUBLISHER
        INNER JOIN sys_area a ON j.ADD_PROVINCE = a.code
        INNER JOIN sys_area b ON j.ADD_CITY = b.code
        LEFT JOIN sys_area c ON j.ADD_AREA = c.code
        WHERE
        u.IDENTITY_FLAG = 1
        AND u.DEL_FLAG = 0
        AND j.JOB_STATUS = 0
        AND j.DEL_FLAG = 0
<!--        <if test="keyWord != null and keyWord.size() >0">-->
<!--            AND-->
<!--            <foreach collection="keyWord" item="key" open="(" close=")" separator="or">-->
<!--                CONCAT(a.`name`, b.`name`, c.`name`,j.ADD_DETAIL) LIKE CONCAT('%', #{key}, '%')-->
<!--                OR u.`NAME` LIKE CONCAT('%', #{key}, '%')-->
<!--                OR j.JOB_NAME LIKE CONCAT('%', #{key}, '%')-->
<!--            </foreach>-->
<!--        </if>-->
        <if test="keyWord !=null">
            AND
            (
            MATCH(j.JOB_NAME,j.ADD_DETAIL) AGAINST(#{keyWord} IN BOOLEAN MODE)
            or
            MATCH( u.NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
            or
            MATCH( a.NAME,a.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
            or
            MATCH( b.NAME,b.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
            or
            MATCH( c.NAME,c.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
            )
        </if>
        GROUP BY j.PUBLISHER
        order by j.RECOMMEND_FLAG desc,j.update_date desc
        limit 10
    </select>

    <!-- rayson-职位搜索 -->
    <select id="searchJob" resultType="com.wechat.model.dto.JobAdvertisedListDto">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        ,pos.POSITION_GRADE as positionGrade
        ,puvView.PAGE_VIEW as pageView
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin"/><!-- sys_area s1,s2,s3 -->
        inner join MDE_POSITION pos
        on pos.POSITION_ID = a.POSITION_ID
        left join MDE_VIEW_JOB_PUV_ALL puvView
        on puvView.JOB_ID = a.JOB_ID
        <where>
            a.del_flag = 0
            and a.job_status = 0
            AND u.del_flag = 0
            AND u.IDENTITY_FLAG = 1
<!--            <if test="keyWord !=null and keyWord.size() != 0">-->
<!--                and-->
<!--                (-->
<!--                (-->
<!--                &lt;!&ndash; 职位名搜索 &ndash;&gt;-->
<!--                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">-->
<!--                    a.JOB_NAME like concat('%',#{key},'%')-->
<!--                </foreach>-->
<!--                )-->
<!--                &lt;!&ndash; 加入 按照酒店名称搜索 &ndash;&gt;-->
<!--                or (-->
<!--                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">-->
<!--                    u.name like concat("%", #{key},"%")-->
<!--                </foreach>-->
<!--                )-->
<!--                )-->
<!--            </if>-->
            <if test="keyWord !=null">
                AND
                (
                MATCH(a.JOB_NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH( u.NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                )
            </if>
            <if test="position != null and position.size() != 0">
                and a.position_id in
                <foreach item="ideach" collection="position" open="(" separator="," close=")">
                    #{ideach}
                </foreach>
            </if>
            <if test="prepareFlag !=null and prepareFlag != ''">
                and u.PREPARE_FLAG = #{prepareFlag}
            </if>
            <if test="fromDate !=null and fromDate != ''">
                and date_format( a.update_date, '%Y-%m-%d' ) >= #{fromDate}
            </if>
            <choose>
                <when test="addArea != null">
                    and a.add_area = #{addArea}
                </when>
                <when test="addCity != null">
                    and a.add_city = #{addCity}
                </when>
                <when test="addProvince != null">
                    and a.add_province = #{addProvince}
                </when>
            </choose>
        </where>
        <choose>
            <when test="orderColumn != null and orderColumn != ''">
                <!-- 拼接SQL 所以使用${XX} 不是 #{XX} -->
                ORDER BY ${orderColumn}
            </when>
            <otherwise>
                ORDER BY a.RECOMMEND_FLAG desc,a.update_date desc
            </otherwise>
        </choose>
    </select>

    <select id="searchJobCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin"/><!-- sys_area s1,s2,s3 -->
        <where>
            a.del_flag = 0
            and a.job_status = 0
            AND u.del_flag = 0
            AND u.IDENTITY_FLAG = 1
            <if test="keyWord !=null and keyWord.size() != 0">
                and
                (
                (
                <!-- 职位名搜索 -->
                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">
                    a.JOB_NAME like concat('%',#{key},'%')
                </foreach>
                )
                <!-- 加入 按照酒店名称搜索 -->
                or (
                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">
                    u.name like concat("%", #{key},"%")
                </foreach>
                )
                )
            </if>
            <if test="position != null and position.size() != 0">
                and a.position_id in
                <foreach item="ideach" collection="position" open="(" separator="," close=")">
                    #{ideach}
                </foreach>
            </if>
            <if test="prepareFlag !=null and prepareFlag != ''">
                and u.PREPARE_FLAG = #{prepareFlag}
            </if>
            <if test="fromDate !=null and fromDate != ''">
                and date_format( a.update_date, '%Y-%m-%d' ) >= #{fromDate}
            </if>
            <choose>
                <when test="addArea != null">
                    and a.add_area = #{addArea}
                </when>
                <when test="addCity != null">
                    and a.add_city = #{addCity}
                </when>
                <when test="addProvince != null">
                    and a.add_province = #{addProvince}
                </when>
            </choose>
        </where>
    </select>


    <!-- rayson- 职位关键词搜索（用户版） -->
    <select id="searchPositionByKeyWord" resultType="com.wechat.model.dto.JobAdvertisedListDto">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        ,pos.POSITION_GRADE as positionGrade
        ,puvView.PAGE_VIEW as pageView
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin"/><!-- sys_area s1,s2,s3 -->
        INNER JOIN MDE_POSITION pos
        on pos.POSITION_ID = a.POSITION_ID
        LEFT JOIN MDE_VIEW_JOB_PUV_ALL puvView
        on puvView.JOB_ID = a.JOB_ID
        LEFT JOIN MDE_POSITION_ANOTHER_NAME	mpa
        on mpa.POSITION_ID = a.POSITION_ID
        <where>
            a.del_flag = 0
            and a.job_status = 0
            AND u.del_flag = 0
            AND u.IDENTITY_FLAG = 1
            AND mpa.DEL_FLAG =0
            <if test="keyWord !=null">
                AND
                (
                    MATCH(a.JOB_NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( u.NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s1.NAME,s1.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s2.NAME,s2.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s3.NAME,s3.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    OR
                    MATCH (pos.POSITION_NAME_CN, pos.POSITION_NAME_EN ) AGAINST (#{keyWord} IN BOOLEAN MODE )
                    OR
                    MATCH ( mpa.POSITION_NAME ) AGAINST (#{keyWord} IN BOOLEAN MODE )
                )
            </if>
            GROUP BY a.JOB_ID
            ORDER BY a.RECOMMEND_FLAG desc,a.update_date desc
        </where>
    </select>

    <!-- rayson- 职位关键词搜索（用户版） 统计-->
    <select id="searchPositionByKeyWordCount"  resultType="java.lang.Integer">
        SELECT count(1) FROM
        (
        SELECT
        count(1)
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin"/><!-- sys_area s1,s2,s3 -->
        INNER JOIN MDE_POSITION pos
        on pos.POSITION_ID = a.POSITION_ID
        LEFT JOIN MDE_VIEW_JOB_PUV_ALL puvView
        on puvView.JOB_ID = a.JOB_ID
        LEFT JOIN MDE_POSITION_ANOTHER_NAME	mpa
        on mpa.POSITION_ID = a.POSITION_ID
        <where>
            a.del_flag = 0
            and a.job_status = 0
            AND u.del_flag = 0
            AND u.IDENTITY_FLAG = 1
            AND mpa.DEL_FLAG =0
            <if test="keyWord !=null">
                AND
                (
                    MATCH(a.JOB_NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( u.NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s1.NAME,s1.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s2.NAME,s2.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    or
                    MATCH( s3.NAME,s3.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                    OR
                    MATCH (pos.POSITION_NAME_CN, pos.POSITION_NAME_EN ) AGAINST (#{keyWord} IN BOOLEAN MODE )
                    OR
                    MATCH ( mpa.POSITION_NAME ) AGAINST (#{keyWord} IN BOOLEAN MODE )
                )
            </if>
            GROUP BY a.JOB_ID
            )as count
        </where>
    </select>


    <!-- 职位搜索 点击量-->
    <select id="searchJobPv" resultType="com.wechat.model.dto.JobAdvertisedListDto">
        SELECT
        <include refid="mdeJobAdvertisedColumns"/>
        <include refid="userColumJoin">
        </include>
        <include refid="areaColumJoin">
        </include>
        ,pos.POSITION_GRADE as positionGrade
        FROM mde_job_advertised a
        <include refid="userLeftJoin"/><!-- mde_user u -->
        <include refid="arealeftJoin"/><!-- sys_area s1,s2,s3 -->
        inner join MDE_POSITION pos
        on pos.POSITION_ID = a.POSITION_ID
        <where>
            a.del_flag = 0
            and a.job_status = 0
            AND u.del_flag = 0
            AND u.IDENTITY_FLAG = 1
            <if test="keyWord !=null and keyWord.size() != 0">
                and
                (
                (
                <!-- 职位名搜索 -->
                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">
                    a.JOB_NAME like concat('%',#{key},'%')
                </foreach>
                )
                <!-- 加入 按照酒店名称搜索 -->
                or (
                <foreach item="key" collection="keyWord" open="(" separator=" or " close=")">
                    u.name like concat("%", #{key},"%")
                </foreach>
                )
                )
            </if>
            <if test="position != null and position.size() != 0">
                and a.position_id in
                <foreach item="ideach" collection="position" open="(" separator="," close=")">
                    #{ideach}
                </foreach>
            </if>
            <if test="prepareFlag !=null and prepareFlag != ''">
                and u.PREPARE_FLAG = #{prepareFlag}
            </if>
            <if test="fromDate !=null and fromDate != ''">
                and date_format( a.update_date, '%Y-%m-%d' ) >= #{fromDate}
            </if>
            <choose>
                <when test="addArea != null">
                    and a.add_area = #{addArea}
                </when>
                <when test="addCity != null">
                    and a.add_city = #{addCity}
                </when>
                <when test="addProvince != null">
                    and a.add_province = #{addProvince}
                </when>
            </choose>
        </where>
        <choose>
            <when test="orderColumn != null and orderColumn != ''">
                <!-- 拼接SQL 所以使用${XX} 不是 #{XX} -->
                ORDER BY ${orderColumn}
            </when>
            <otherwise>
                ORDER BY a.RECOMMEND_FLAG desc,a.update_date desc
            </otherwise>
        </choose>
    </select>

    <!-- 查看该企业是否发布某个职位 -->
    <select id="findByPublisherAndJobId" parameterType="com.wechat.model.dbEntity.MdeJobInvitationRel"
            resultMap="MdeJobAdvertised">
        select
        <include refid="mdeJobAdvertisedColumns">
        </include>
        from mde_job_advertised a
        where a.PUBLISHER =#{publisher}
        and a.job_Id = #{jobId}
        AND a.JOB_STATUS =0
        AND a.DEL_FLAG = 0
    </select>
    <select id="getCollectionJobList" resultType="com.wechat.model.responseDto.JobAdvertisedRes">
        SELECT
        a.job_id as jobId,
        a.job_name as jobName,
        a.PAY_FROM as payFrom,
        a.PAY_TO as payTo,
        <choose>
            <when test="lang == 'en'.toString()">
                s1.name_en as addProvince,
                s.name_en as addCity,
            </when>
            <otherwise>
                s1.name as addProvince,
                s.name as addCity,
            </otherwise>
        </choose>
        u.NAME as name,
        u.foreign_name as foreignName
        FROM
        mde_job_advertised a
        LEFT JOIN mde_user u ON a.publisher = u.user_code AND u.del_flag = 0
        LEFT JOIN sys_area s ON a.add_city = s.code AND s.del_flag = 0
        LEFT JOIN sys_area s1 ON a.add_province = s1.code AND s1.del_flag = 0
        WHERE a.del_flag=0 and a.job_id in
        <foreach collection="jobListStr" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!-- 获取正在发布中的职位 -->
    <select id="getPublishingJobsByUserCode" resultType="com.wechat.model.responseDto.JobAdvertisedNologinResp">
        SELECT
        ad.JOB_NAME AS jobName,
        ad.JOB_ID As jobId,
        ad.RECOMMEND_FLAG as recommendFlag,
        ad.PAY_FROM as payFrom,
        ad.PAY_TO as payTo,
        <choose>
            <when test="lang=='en'.toString()">
                s.en as workExperience,
                edu.en AS education,
                s1.name_en as addProvinceName,s2.name_en as addCityName,
            </when>
            <otherwise>
                s.label AS workExperience,
                edu.label AS education,
                s1.name as addProvinceName,s2.name as addCityName,
            </otherwise>
        </choose>
        ad.UPDATE_DATE AS publishDate,
        ad.CREATE_DATE AS createDate,
        ad.JOB_STATUS AS jobStatus
        FROM
        mde_job_advertised ad,
        sys_dict s,
        sys_dict edu,
        sys_area s1,
        sys_area s2
        WHERE
        s.`value` = ad.WORK_EXPERIENCE
        AND ad.DEL_FLAG = 0
        AND ad.PUBLISHER = #{userCode}
        AND ad.JOB_STATUS = 0
        AND s.type = 'job_year'
        AND edu.`value` = ad.education
        AND edu.type = 'education'
        AND ad.ADD_PROVINCE = s1.code
        AND ad.ADD_CITY = s2.code
        order by ad.RECOMMEND_FLAG desc,ad.update_date desc
    </select>

    <!-- 获取正在发布中的职位 非登陆-->
    <select id="getPublishingJobsNologin" resultType="com.wechat.model.responseDto.JobAdvertisedNologinResp">
        SELECT
        ad.JOB_NAME AS jobName,
        ad.JOB_ID As jobId,
        ad.RECOMMEND_FLAG as recommendFlag,
        ad.PAY_FROM as payFrom,
        ad.PAY_TO as payTo,
        <choose>
            <when test="lang=='en'.toString()">
                s.en as workExperience,
                edu.en AS education,
                s1.name_en as addProvinceName,s2.name_en as addCityName,
            </when>
            <otherwise>
                s.label AS workExperience,
                edu.label AS education,
                s1.name as addProvinceName,s2.name as addCityName,
            </otherwise>
        </choose>
        ad.UPDATE_DATE AS publishDate,
        ad.CREATE_DATE as createDate,
        ad.JOB_STATUS AS jobStatus
        FROM
        mde_job_advertised ad,
        sys_dict s,
        sys_dict edu,
        sys_area s1,
        sys_area s2
        WHERE
        s.`value` = ad.WORK_EXPERIENCE
        AND ad.DEL_FLAG = 0
        AND ad.PUBLISHER = #{userCode}
        AND ad.JOB_STATUS = 0
        AND s.type = 'job_year'
        AND edu.`value` = ad.education
        AND edu.type = 'education'
        AND ad.ADD_PROVINCE = s1.code
        AND ad.ADD_CITY = s2.code
        order by ad.RECOMMEND_FLAG desc,ad.update_date desc
    </select>

    <select id="alreadyPublishNotVipCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        mde_job_advertised ad
        WHERE
        ad.DEL_FLAG = 0
        AND ad.PUBLISHER = #{userCode}
        <if test="jobStatus !=null and jobStatus != ''">
            AND ad.JOB_STATUS = #{jobStatus}
        </if>
    </select>


    <select id="alreadyPublishCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        mde_job_advertised ad
        <include refid="mdeJobAdvertisedJoinCommon"/>
        <if test="jobStatus !=null and jobStatus != ''">
            AND ad.JOB_STATUS = #{jobStatus}
        </if>
        <if test="positionIdList != null">
            AND ad.POSITION_ID in
            <foreach collection="positionIdList" item="positionId" separator="," open="(" close=")">
                #{positionId}
            </foreach>
        </if>
        <if test="newDelivery">
            AND rel2.newFlag = 0
        </if>
        <if test="dayStart !=null">
            <![CDATA[
			AND ad.CREATE_DATE >= #{dayStart}
			]]>
        </if>
        <if test="dayEnd !=null">
            <![CDATA[
			AND ad.CREATE_DATE <= #{dayEnd}
			]]>
        </if>
        <if test="workExperience != null and workExperience != ''">
            AND s.label = #{workExperience}
        </if>
    </select>


    <!-- 获取正在发布中的职位 -->
    <select id="alreadyPublish" resultType="com.wechat.model.responseDto.JobAdvertisedResp">
        SELECT
        <include refid="mdeJobAdvertisedSelectCommon"/>
        FROM
        mde_job_advertised ad
        <include refid="mdeJobAdvertisedJoinCommon"/>
        <if test="jobStatus !=null and jobStatus != ''">
            AND ad.JOB_STATUS = #{jobStatus}
        </if>
        <if test="positionIdList != null">
            AND ad.POSITION_ID in
            <foreach collection="positionIdList" item="positionId" separator="," open="(" close=")">
                #{positionId}
            </foreach>
        </if>
        <if test="newDelivery">
            AND rel2.newFlag = 0
        </if>
        <if test="dayStart !=null">
            <![CDATA[
			AND ad.CREATE_DATE >= #{dayStart}
			]]>
        </if>
        <if test="dayEnd !=null">
            <![CDATA[
			AND ad.CREATE_DATE <= #{dayEnd}
			]]>
        </if>
        <if test="workExperience != null and workExperience != ''">
            AND s.label = #{workExperience}
        </if>
        order by ad.JOB_STATUS,ad.RECOMMEND_FLAG desc,ad.update_date desc
    </select>

    <!-- 获取发布的职位列表 -->
    <select id="alreadyPublishJobList" resultType="com.wechat.model.responseDto.AlreadyPublishJobResp">
        SELECT
            ad.JOB_ID as jobId,
            ad.JOB_NAME AS jobName
        FROM
            mde_job_advertised ad
            left join mde_position p on ad.POSITION_ID = p.POSITION_ID
        WHERE
            ad.DEL_FLAG = 0
            AND ad.PUBLISHER = #{userCode}
            AND p.DEL_FLAG =0
            AND ad.JOB_STATUS = 0
        GROUP BY
            ad.JOB_NAME
        ORDER BY
            ad.JOB_STATUS ASC,
            ad.UPDATE_DATE desc
    </select>

    <!-- rayson- 已发职位关键词检索 统计 -->
    <select id="searchAlreadyPublishByKeyWordCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        mde_job_advertised ad
        <include refid="mdeJobAdvertisedJoinCommon"/>
        <if test="keyWord !=null">
            AND
            (
                MATCH(ad.JOB_NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(p.POSITION_NAME_CN,p.POSITION_NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(s1.NAME,s1.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(s2.NAME,s2.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                edu.label like concat('%',#{keyWord},'%')
            )
        </if>
    </select>

    <!-- rayson- 已发职位关键词检索 -->
    <select id="searchAlreadyPublishByKeyWord" resultType="com.wechat.model.responseDto.JobAdvertisedResp">
        SELECT
        <include refid="mdeJobAdvertisedSelectCommon"/>
        FROM
        mde_job_advertised ad
        <include refid="mdeJobAdvertisedJoinCommon"/>
        <if test="keyWord !=null">
            AND
            (
                MATCH(ad.JOB_NAME) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(p.POSITION_NAME_CN,p.POSITION_NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(s1.NAME,s1.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                MATCH(s2.NAME,s2.NAME_EN) AGAINST(#{keyWord} IN BOOLEAN MODE)
                or
                edu.label like concat('%',#{keyWord},'%')
            )
            order by ad.JOB_STATUS,ad.RECOMMEND_FLAG desc,ad.update_date desc
        </if>
    </select>



    <!-- 推荐职位 分页查询 -->
    <select id="getPagingRecommentJobs" resultType="com.wechat.model.dto.JobAdvertisedListDto">
        SELECT job.JOB_ID          as jobId,
               job.PUBLISHER       as publisher,
               job.JOB_NAME        as jobName,
               job.ADD_PROVINCE    as addProvince,
               job.ADD_CITY        as addCity,
               job.ADD_AREA        as addArea,
               s1.`name`           AS addProvinceName,
               s2.`name`           AS addCityName,
               s3.`name`           AS addAreaName,
               job.ADD_DETAIL      as addDetail,
               job.work_experience AS workExperience,
               job.education       AS education,
               job.PAY_FROM        as payFrom,
               job.PAY_TO          as payTo,
               job.UPDATE_DATE     as updateDate,
               puber.`NAME`        AS publisherName,
               puber.head_portrait AS headPortrait,
               puber.foreign_name  AS foreignName,
               puber.PREPARE_FLAG  as prepareFlag,
               puber.VIP_FLAG      as vipFlag
        FROM MDE_JOB_ADVERTISED job
                 INNER JOIN mde_position bp ON job.POSITION_ID = bp.POSITION_ID
                 INNER JOIN (SELECT b.DEPD_ID,
                                    b.POSITION_GRADE
                             FROM mde_work_experience a
                                      INNER JOIN mde_position_another_name b ON a.POSITION_TITLE = b.POSITION_NAME
                             WHERE a.USER_CODE = #{userCode}
                               AND a.SHOW_FLAG = '0'
                               and a.DEL_FLAG = '0'
                             UNION
                             SELECT b.DEPD_ID,
                                    b.POSITION_GRADE
                             FROM MDE_SUBSCRIBED_POSITIONS a
                                      INNER JOIN MDE_POSITION b ON a.POSITION_ID = b.POSITION_ID
                             WHERE a.USER_CODE = #{userCode}
                               AND a.DEL_FLAG = '0') uWork ON bp.DEPD_ID = uWork.DEPD_ID
		<![CDATA[	AND bp.POSITION_GRADE <= uWork.POSITION_GRADE    ]]>
			INNER JOIN mde_user puber ON job.PUBLISHER = puber.USER_CODE
                 LEFT JOIN sys_area s1 ON job.ADD_PROVINCE = s1.CODE
                 LEFT JOIN sys_area s2 ON job.ADD_CITY = s2.CODE
                 LEFT JOIN sys_area s3 ON job.ADD_AREA = s3.CODE
        WHERE job.JOB_STATUS = '0'
          and job.DEL_FLAG = '0'
          and puber.DEL_FLAG = '0'
          and bp.DEL_FLAG = '0'
          and job.UPDATE_DATE > #{selDate}
        ORDER BY job.RECOMMEND_FLAG DESC,
                 puber.VIP_FLAG DESC,
                 job.UPDATE_DATE DESC
    </select>

    <!-- 推荐职位分页查询 计数 -->
    <select id="getPagingRecommentJobsCount" resultType="java.lang.Integer">
        SELECT count(job.JOB_ID)
        FROM MDE_JOB_ADVERTISED job
                 INNER JOIN mde_position bp ON job.POSITION_ID = bp.POSITION_ID
                 INNER JOIN (SELECT b.DEPD_ID,
                                    b.POSITION_GRADE
                             FROM mde_work_experience a
                                      INNER JOIN mde_position_another_name b ON a.POSITION_TITLE = b.POSITION_NAME
                             WHERE a.USER_CODE = #{userCode}
                               AND a.SHOW_FLAG = '0'
                               and a.DEL_FLAG = '0'
                             UNION
                             SELECT b.DEPD_ID,
                                    b.POSITION_GRADE
                             FROM MDE_SUBSCRIBED_POSITIONS a
                                      INNER JOIN MDE_POSITION b ON a.POSITION_ID = b.POSITION_ID
                             WHERE a.USER_CODE = #{userCode}
                               AND a.DEL_FLAG = '0') uWork ON bp.DEPD_ID = uWork.DEPD_ID
			<![CDATA[	AND bp.POSITION_GRADE <= uWork.POSITION_GRADE    ]]>
			INNER JOIN mde_user puber ON job.PUBLISHER = puber.USER_CODE
        WHERE job.JOB_STATUS = '0'
          and job.DEL_FLAG = '0'
          and puber.DEL_FLAG = '0'
          and bp.DEL_FLAG = '0'
          and job.UPDATE_DATE > #{selDate}
    </select>

    <!--获取已经投递过职位的用户code -->
    <select id="getAlreadyAdvertisedByJobId" resultMap="mdePositionCvRelMap">
        SELECT j.job_id,
               j.JOB_NAME,
               p.CANDIDATE as USER_CODE
        FROM mde_job_advertised j
                 LEFT JOIN mde_position_cv_rel p ON j.JOB_ID = p.POS_ID
            AND p.DEL_FLAG = 0
        WHERE j.PUBLISHER = #{publisher}
          AND j.JOB_STATUS = 0
          AND j.DEL_FLAG = 0
    </select>

    <!-- 获取 企业正在招聘的职位 -->
    <select id="getAdvertisedRecruitment" resultMap="mdeJobAdvertisedJoin">
        SELECT j.JOB_ID,
               j.JOB_NAME,
               r.user_code
        FROM mde_job_advertised j
                 LEFT JOIN mde_job_invitation_rel r ON j.JOB_ID = r.job_id
            AND j.PUBLISHER = r.PUBLISHER AND r.DEL_FLAG = 0
        WHERE j.JOB_STATUS = 0
          AND j.DEL_FLAG = 0
          AND j.PUBLISHER = #{publisher}
        order by j.RECOMMEND_FLAG desc, j.update_date desc
    </select>
    <!-- 修改职位状态  关闭职位的同时取消职位首推-->
    <update id="updateJobAdvertisedStatus">
        UPDATE mde_job_advertised
        SET JOB_STATUS = #{jobStatus},
        <if test="jobStatus == 1">
            RECOMMEND_FLAG = '0',
        </if>
        UPDATE_DATE = now(),
        VERSION_NO = VERSION_NO + 1
        WHERE
        JOB_ID = #{jobId}
        AND PUBLISHER = #{userCode}
        AND DEL_FLAG = 0
    </update>

    <!-- 修改首推状态 -->
    <update id="updateJobRecommend">
        UPDATE mde_job_advertised
        SET RECOMMEND_FLAG = #{recommendFlag},
            UPDATE_DATE    = now(),
            VERSION_NO     = VERSION_NO + 1
        WHERE JOB_ID = #{jobId}
          AND PUBLISHER = #{userCode}
          AND DEL_FLAG = 0
    </update>

    <!-- 企业简历总数量 -->
    <select id="getAllUserCvCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised j,
             mde_position_cv_rel r
        WHERE j.JOB_ID = r.POS_ID
          AND j.DEL_FLAG = 0
          AND r.DEL_FLAG = 0
          AND j.PUBLISHER = #{userCode}
    </select>
    <!-- 按发布者获取新简历数量 -->
    <select id="getNewCvCountByPublisher" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised j,
             mde_position_cv_rel r
        WHERE j.JOB_ID = r.POS_ID
          AND j.DEL_FLAG = 0
          AND r.DEL_FLAG = 0
          AND r.CV_STATUS = 0
          AND j.PUBLISHER = #{userCode}
    </select>
    <!-- 邀请面试数量 -->
    <select id="inviteAuditionCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised j,
             mde_position_cv_rel r
        WHERE j.JOB_ID = r.POS_ID
          AND j.DEL_FLAG = 0
          AND r.DEL_FLAG = 0
          AND r.CV_STATUS = 3
          AND j.PUBLISHER = #{userCode}
    </select>
    <!-- 录用简历数量 -->
    <select id="matriculateUserCvCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised j,
             mde_position_cv_rel r
        WHERE j.JOB_ID = r.POS_ID
          AND j.DEL_FLAG = 0
          AND r.DEL_FLAG = 0
          AND r.CV_STATUS = 4
          AND j.PUBLISHER = #{userCode}
    </select>
    <!-- 淘汰数量 -->
    <select id="eliminateUserCvCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised j,
             mde_position_cv_rel r
        WHERE j.JOB_ID = r.POS_ID
          AND j.DEL_FLAG = 0
          AND r.DEL_FLAG = 0
          AND r.CV_STATUS = 5
          AND j.PUBLISHER = #{userCode}
    </select>
    <!-- 随机抽取优选职位  大于50  （未登录）  -->
    <select id="getNotLoginRecommentJobsRandJoin" resultMap="MdeJobAdvertised">
        SELECT t1.JOB_ID,
               t1.PUBLISHER,
               t1.JOB_NAME,
               t1.JOB_DESCRIBE,
               t1.CREATE_DATE,
               t1.`NAME` AS publisher_name
        from (SELECT a.JOB_ID,
                     a.PUBLISHER,
                     a.JOB_NAME,
                     a.JOB_DESCRIBE,
                     a.CREATE_DATE,
                     a.UPDATE_DATE,
                     u.`NAME`
              FROM mde_job_advertised a,
                   mde_user u
              WHERE u.USER_CODE = a.PUBLISHER
                AND u.DEL_FLAG = 0
                AND u.IDENTITY_FLAG = 1
                AND u.VIP_FLAG = 1
                AND a.DEL_FLAG = 0
                AND a.JOB_STATUS = 0
              GROUP BY a.PUBLISHER) as t1
                 JOIN (SELECT ROUND(RAND() * (
                (SELECT MAX(JOB_ID) FROM mde_job_advertised) - (SELECT MIN(JOB_ID) FROM mde_job_advertised)) +
                                    (SELECT MIN(JOB_ID) FROM mde_job_advertised)) AS JOB_ID) AS t2
        WHERE t1.JOB_ID >= t2.JOB_ID
        ORDER BY t1.UPDATE_DATE desc
        LIMIT #{limitSize}
    </select>

    <!-- 随机抽取优选职位  小于50  （未登录）  -->
    <select id="getNotLoginRecommentJobsRand" resultMap="MdeJobAdvertised">
        SELECT a.JOB_ID,
               a.PUBLISHER,
               a.JOB_NAME,
               a.JOB_DESCRIBE,
               a.CREATE_DATE,
               u.`NAME` AS publisher_name
        FROM mde_job_advertised a,
             mde_user u
        WHERE u.USER_CODE = a.PUBLISHER
          AND u.DEL_FLAG = 0
          AND u.IDENTITY_FLAG = 1
          AND u.VIP_FLAG = 1
          AND a.DEL_FLAG = 0
          AND a.JOB_STATUS = 0
        GROUP BY a.PUBLISHER
        ORDER BY RAND()
        LIMIT #{limitSize}
    </select>

    <!--优选人才数量  -->
    <select id="getNotLoginRecommentJobsCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM mde_job_advertised a,
             mde_user u
        WHERE u.USER_CODE = a.PUBLISHER
          AND u.DEL_FLAG = 0
          AND u.IDENTITY_FLAG = 1
          AND u.VIP_FLAG = 1
          AND a.DEL_FLAG = 0
          AND a.JOB_STATUS = 0
    </select>

    <update id="batchUpdatePublishDate">
        UPDATE mde_job_advertised
        SET UPDATE_DATE = NOW(),
            version_no  = version_no + 1
        WHERE JOB_STATUS = 0
          and PUBLISHER = #{publisher}
    </update>

    <update id="updatePublishDate">
        UPDATE mde_job_advertised
        SET UPDATE_DATE = NOW(),
            version_no  = version_no + 1
        WHERE JOB_STATUS = 0
          and PUBLISHER = #{publisher}
          and JOB_ID = #{jobId}
    </update>

    <!--获取酒店可首推和已首推个数  -->
    <select id="getAvailableAndUsedCount" resultType="com.wechat.model.dto.AvailableAndUsedCountDto">
        SELECT ifnull(count(a.PUBLISHER), 0) as used,
               ifnull(s.remarks, 0)          as available
        FROM mde_user u
                 left join mde_job_advertised a
                           on u.USER_CODE = a.PUBLISHER
                               AND a.DEL_FLAG = 0
                               AND a.JOB_STATUS = 0
                               AND a.RECOMMEND_FLAG = 1
                 left join sys_dict s
                           on s.value = u.VIP_FLAG
                               and s.type = 'vip_level'
        WHERE u.USER_CODE = #{userCode}
          AND u.DEL_FLAG = 0
          AND u.IDENTITY_FLAG = 1
        GROUP By u.USER_CODE
    </select>


    <select id="findRepFormPvByDate" resultType="com.wechat.model.dto.JobRepFormPvDto">
        SELECT
        <choose>
            <when test='ymd =="d"'>
                a.WRIT_DATE as imagX,
            </when>
            <when test='ymd =="m"'>
                date_format( a.WRIT_DATE, '%Y-%m' ) as imagX,
            </when>
            <when test='ymd =="y"'>
                date_format( a.WRIT_DATE, '%Y' ) as imagX,
            </when>
            <otherwise>
                a.WRIT_DATE as imagX,
            </otherwise>
        </choose>
        IFNULL(cvHis.num,0) as cvNums,
        SUM( a.PAGE_VIEW ) as pv
        FROM
        mde_job_advertised job
        inner join
        mde_job_pv_uv a on job.JOB_ID = a.JOB_ID
        LEFT JOIN (
        SELECT
        <choose>
            <when test='ymd =="d"'>
                rel.DELIVER_DATE as deliverDate,
            </when>
            <when test='ymd =="m"'>
                date_format( rel.DELIVER_DATE , '%Y-%m' ) as deliverDate,
            </when>
            <when test='ymd =="y"'>
                date_format( rel.DELIVER_DATE , '%Y' ) as deliverDate,
            </when>
            <otherwise>
                rel.DELIVER_DATE as deliverDate,
            </otherwise>
        </choose>
        count( rel.DELIVER_DATE ) num
        FROM
        mde_position_cv_rel rel
        WHERE
        rel.PUBLISHER = #{userCode}
        <![CDATA[   AND rel.DELIVER_DATE >= #{from}
				AND rel.DELIVER_DATE <= #{to}   ]]>
        <choose>
            <when test='ymd =="d"'>
                GROUP BY rel.DELIVER_DATE
                ) cvHis ON cvHis.deliverDate = a.WRIT_DATE
            </when>
            <when test='ymd =="m"'>
                GROUP BY date_format( rel.DELIVER_DATE , '%Y-%m' )
                ) cvHis ON cvHis.deliverDate = date_format( a.WRIT_DATE, '%Y-%m' )
            </when>
            <when test='ymd =="y"'>
                GROUP BY date_format( rel.DELIVER_DATE , '%Y' )
                ) cvHis ON cvHis.deliverDate = date_format( a.WRIT_DATE, '%Y' )
            </when>
            <otherwise>
                GROUP BY rel.DELIVER_DATE
                ) cvHis ON cvHis.deliverDate = a.WRIT_DATE
            </otherwise>
        </choose>
        WHERE
        job.PUBLISHER = #{userCode}
        <![CDATA[
			AND a.WRIT_DATE >= #{from}
			AND a.WRIT_DATE <= #{to} 
			]]>
        <if test="jobId != null and jobId != 0 ">
            AND a.JOB_ID = #{jobId}
        </if>
        <choose>
            <when test='ymd =="d"'>
                GROUP BY a.WRIT_DATE
            </when>
            <when test='ymd =="m"'>
                GROUP BY date_format( a.WRIT_DATE, '%Y-%m' )
            </when>
            <when test='ymd =="y"'>
                GROUP BY date_format( a.WRIT_DATE, '%Y' )
            </when>
            <otherwise>
                GROUP BY a.WRIT_DATE
            </otherwise>
        </choose>
        ORDER BY
        a.WRIT_DATE
    </select>

    <select id="findGeneralPvByDate" resultType="java.lang.Integer">
        <!-- 点击量 -->
        SELECT
        SUM( a.PAGE_VIEW ) as num
        FROM
        mde_job_advertised job
        inner join
        mde_job_pv_uv a on job.JOB_ID = a.JOB_ID
        WHERE
        job.PUBLISHER = #{userCode}
        <![CDATA[
			AND a.WRIT_DATE >= #{from}
			AND a.WRIT_DATE <= #{to} 
			]]>
        <if test="jobId != null and jobId != 0 ">
            AND a.JOB_ID = #{jobId}
        </if>
    </select>
</mapper>