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


    <select id="selectPostList" resultType="com.sqx.modules.resumes.entity.PostDetails">
        select pd.* from post_details pd
        left join post_push p on pd.post_push_id=p.post_push_id
        where p.status=2 and p.classify=1
        <if test="ruleClassifyId!=null">
            and p.rule_classify_id = #{ruleClassifyId}
        </if>
        <if test="address!=null and address!=''">
            and p.address like concat('%',#{address},'%')
        </if>
        <if test="userId!=null">
            and p.company_id not in (select company_id from shield_company where user_id=#{userId})
        </if>
        order by p.create_time desc
    </select>

    <select id="getPostPushList" resultType="com.sqx.modules.resumes.entity.PostPush">
        SELECT
        p.*
        <if test="postPush.lng!=null and postPush.lat!=null">
            ,(st_distance(POINT(lng,lat),POINT(#{postPush.lng},#{postPush.lat})) * 111195) AS
            distance
        </if>
        FROM
        post_push p,
        company c
        WHERE p.company_id = c.company_id

        <if test="userId!=null">
            and p.company_id NOT IN(select company_id from shield_company s where s.user_id= #{userId} and
            s.shield_type=1)
        </if>
        <if test="postPush.companyId!=null">
            and p.company_id = #{postPush.companyId}
        </if>
        <if test="postPush.companyName!=null and postPush.companyName!=''">
            and c.company_name like concat("%",#{postPush.companyName},"%")
        </if>
        <if test="companyPeople!=null and companyPeople.length!=0">
            and
            <foreach collection="companyPeople" item="item" open="(" separator="or" close=")">
                c.company_people like concat("%",#{item},"%")
            </foreach>
        </if>
        <if test="education!=null and education.length!=0">
            and
            <foreach collection="education" item="item" open="(" separator="or" close=")">
                p.education like concat("%",#{item},"%")
            </foreach>
        </if>
        <if test="postPush.status !=null">
            and p.status = #{postPush.status}
        </if>
        <if test="postPush.ruleClassifyName!=null and postPush.ruleClassifyName!=''">
            and p.rule_classify_name like concat("%",#{postPush.ruleClassifyName},"%")
        </if>

        <if test="experience!=null and experience.length!=0">
            and
            <foreach collection="experience" item="item" open="(" separator="or" close=")">
                p.experience like concat("%",#{item},"%")
            </foreach>
        </if>

        <if test="salaryRange!=null and salaryRange.length!=0">
            and
            <foreach collection="salaryRange" item="item" open="(" separator="or" close=")">
                p.salary_range like concat("%",#{item},"%")
            </foreach>
        </if>

        <if test="postPush.postType!=null and postPush.postType!=''">
            and p.post_type = #{postPush.postType}
        </if>

        <if test="industry!=null and industry.length!=0">
            and
            <foreach collection="industry" item="item" open="(" separator="or" close=")">
                p.industry like concat("%",#{item},"%")
            </foreach>
        </if>
        <if test="postPush.province!=null and postPush.province!=''">
            and p.province = like concat("%",#{postPush.province},"%")
        </if>
        <if test="postPush.city!=null and postPush.city!=''">
            and p.city like concat("%",#{postPush.city},"%")
        </if>
        <if test="postPush.county!=null and postPush.county!=''">
            and p.county like concat("%",#{postPush.county},"%")
        </if>
        <if test="screen!=null">
            <if test="screen==1">
                order by is_recommend desc
            </if>
            <if test="screen==2">
                order by create_time desc
            </if>
            <if test="screen==3">
                order by distance desc
            </if>
        </if>
        <if test="screen==null">
            order by create_time desc
        </if>
    </select>
    <select id="getLetter" resultType="com.sqx.modules.resumes.vo.LetterCity">
        SELECT city_initial as letter
        FROM post_push
        GROUP BY city_initial
        ORDER BY city_initial asc

    </select>
    <select id="getCityList" resultType="java.lang.String">

        SELECT city FROM post_push WHERE city_initial = #{letter} and status=2
        <if test="city!=null">
            and city like concat("%",#{city},"%")
        </if>
        GROUP BY city_initial ORDER BY city_initial asc

    </select>
    <select id="getCountyByCity" resultType="java.lang.String">
        SELECT county
        FROM post_push p
        WHERE p.status = 2
          AND city like concat("%", #{city}, "%")
        GROUP BY county
    </select>
    <select id="getHotCity" resultType="java.lang.String">
        SELECT city
        FROM post_push p
        WHERE p.status = 2
        GROUP BY city
        ORDER BY COUNT(city) DESC
        LIMIT #{limitCount}
    </select>
    <select id="getCityCounty" resultType="com.sqx.modules.resumes.vo.CityVo">
        select * from (
                          SELECT
                              city
                          FROM
                              post_push p
                          WHERE
                              p.STATUS = 2
                          GROUP BY
                              city
                          union all
                          SELECT
                              city
                          FROM
                              resumes p
                          GROUP BY
                              city
                      ) a GROUP BY city
    </select>
    <select id="selectPostId" resultType="com.sqx.modules.resumes.entity.PostPush">
        SELECT *,
        <if test="lng!=null and lat!=null">
            (st_distance(POINT(lng,lat),POINT(#{lng},#{lat})) * 111195) AS
            distance,
        </if>
        IF(
        (SELECT COUNT(1)
        FROM my_collection m
        WHERE m.user_id = #{userId}
        AND p.post_push_id = m.post_push_id) = 0, 0, 1) AS isCollection
        FROM post_push p
        WHERE p.post_push_id = #{postPushId}
    </select>
    <select id="getCityCompanyId" resultType="java.lang.String">
        SELECT city
        FROM post_push p
        WHERE p.company_id = #{companyId}
        GROUP BY city
    </select>
    <select id="getCompanyClassify" resultType="java.lang.String">
        SELECT rule_classify_name
        FROM post_push p
        WHERE p.company_id = #{companyId}
          and p.status = 2
        GROUP BY BINARY rule_classify_name
    </select>
    <select id="getAdminPostPushList" resultType="com.sqx.modules.resumes.entity.PostPush">
        SELECT p.*,
        u.user_id,
        IFNULL(c.company_name, "企业不存在") AS company_name,
        IFNULL(u.user_name, "未设置") AS user_name
        FROM post_push p
        LEFT JOIN company c
        ON p.company_id = c.company_id
        LEFT JOIN tb_user u
        ON u.user_id = c.user_id
        <where>
            <if test="postPush.status!=null">
                and p.status = #{postPush.status}
            </if>
            <if test="postPush.companyName!=null and postPush.companyName!=''">
                and c.company_name like concat("%",#{postPush.companyName},"%")
            </if>
            <if test="postPush.education!=null and postPush.education!=''">
                and p.education like concat("%",#{postPush.education},"%")
            </if>
            <if test="postPush.experience!=null and postPush.experience!=''">
                and p.experience like concat("%",#{postPush.experience},"%")
            </if>
            <if test="postPush.salaryRange!=null and postPush.salaryRange!=''">
                and p.salary_range like concat("%",#{postPush.salaryRange},"%")
            </if>

            <if test="postPush.city!=null and postPush.city!=''">
                and (p.city like concat("%",#{postPush.city},"%") or p.province like concat("%",#{postPush.city},"%"))
            </if>


            <if test="postPush.ruleClassifyName!=null and postPush.ruleClassifyName!=''">
                and p.rule_classify_name like concat("%",#{postPush.ruleClassifyName},"%")
            </if>
            <if test="startTime!=null and startTime!='' and endTime!=null and endTime!=''">
                and date_format(p.create_time,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
            </if>
            <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
                and date_format(p.create_time,'%Y-%m-%d') &gt;= #{startTime}
            </if>
            <if test="endTime!=null and endTime!='' and  (startTime==null or startTime=='')">
                and date_format(p.create_time,'%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
        order by p.create_time desc
    </select>


    <insert id="insertPostPush" useGeneratedKeys="true" keyProperty="postPushId"
            parameterType="com.sqx.modules.resumes.entity.PostPush">
        INSERT INTO post_push
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != ruleClassifyName and '' != ruleClassifyName">
                rule_classify_name,
            </if>
            <if test="null != address and '' != address">
                address,
            </if>
            <if test="null != ruleClassifyId and '' != ruleClassifyId">
                rule_classify_id,
            </if>
            <if test="null != projectDayNum and '' != projectDayNum">
                project_day_num,
            </if>
            <if test="null != projectAward and '' != projectAward">
                project_award,
            </if>
            <if test="null != email and '' != email">
                email,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != classify ">
                classify,
            </if>
            <if test="null != payMoney ">
                pay_money,
            </if>
            <if test="null != companyId ">
                company_id,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != updateTime and '' != updateTime">
                update_time,
            </if>
            <if test="null != details and '' != details">
                details,
            </if>
            <if test="null != construction and '' != construction">
                construction,
            </if>
            <if test="null != manning and '' != manning">
                manning,
            </if>
            <if test="null != approachTime and '' != approachTime">
                approach_time,
            </if>
            <if test="null != projectPrincipalName and '' != projectPrincipalName">
                project_principal_name,
            </if>
            <if test="null != projectPrincipalPhone and '' != projectPrincipalPhone">
                project_principal_phone
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">

            <if test="null != ruleClassifyName and '' != ruleClassifyName">
                #{ruleClassifyName},
            </if>
            <if test="null != address and '' != address">
                #{address},
            </if>
            <if test="null != ruleClassifyId and '' != ruleClassifyId">
                #{ruleClassifyId},
            </if>
            <if test="null != projectDayNum and '' != projectDayNum">
                #{projectDayNum},
            </if>
            <if test="null != projectAward and '' != projectAward">
                #{projectAward},
            </if>
            <if test="null != email and '' != email">
                #{email},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != classify ">
                #{classify},
            </if>
            <if test="null != payMoney ">
                #{payMoney},
            </if>
            <if test="null != companyId ">
                #{companyId},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != updateTime and '' != updateTime">
                #{updateTime},
            </if>
            <if test="null != details and '' != details">
                #{details},
            </if>
            <if test="null != construction and '' != construction">
                #{construction},
            </if>
            <if test="null != manning and '' != manning">
                #{manning},
            </if>
            <if test="null != approachTime and '' != approachTime">
                #{approachTime},
            </if>
            <if test="null != projectPrincipalName and '' != projectPrincipalName">
                #{projectPrincipalName},
            </if>
            <if test="null != projectPrincipalPhone and '' != projectPrincipalPhone">
                #{projectPrincipalPhone}
            </if>
        </trim>
    </insert>


</mapper>