<?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.ruoyi.cms.mapper.JobMapper">

    <resultMap type="Job" id="JobResult">
        <result property="jobId"    column="job_id"    />
        <result property="jobTitle"    column="job_title"    />
        <result property="minSalary"    column="min_salary"    />
        <result property="maxSalary"    column="max_salary"    />
        <result property="education"    column="education"    />
        <result property="experience"    column="experience"    />
        <result property="companyName"    column="company_name"    />
        <result property="jobLocation"    column="job_location"    />
        <result property="postingDate"    column="posting_date"    />
        <result property="vacancies"    column="vacancies"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="view"    column="view"  />
        <result property="companyId"    column="company_id"    />
        <result property="isHot"    column="is_hot"    />
        <result property="dataSource"    column="data_source"    />
        <result property="jobUrl"    column="job_url"    />
        <result property="rowId"    column="row_id"    />
        <result property="jobCategory"    column="job_category"    />
        <result property="isExplain"    column="is_explain"    />
        <result property="explainUrl"    column="explain_url"    />
        <result property="cover"    column="cover"    />
        <result property="jobType"    column="job_type"    />
        <result property="jobAddress"    column="job_address"    />

    </resultMap>

    <resultMap type="com.ruoyi.cms.domain.Job" id="JobEsResult">
        <result property="jobId"    column="job_id"    />
        <result property="jobTitle"    column="job_title"    />
        <result property="minSalary"    column="min_salary"    />
        <result property="maxSalary"    column="max_salary"    />
        <result property="education"    column="education"    />
        <result property="experience"    column="experience"    />
        <result property="companyName"    column="company_name"    />
        <result property="jobLocation"    column="job_location"    />
        <result property="postingDate"    column="posting_date"    />
        <result property="vacancies"    column="vacancies"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="view"    column="view"  />
        <result property="companyId"    column="company_id"    />
        <result property="isHot"    column="is_hot"    />
        <result property="applyNum"    column="apply_num"    />
        <result property="dataSource"    column="data_source"    />
        <result property="jobLocationAreaCode"    column="job_location_area_code"    />
        <result property="description"    column="description"    />
        <result property="isPublish"    column="is_publish"    />
        <result property="dataSource"    column="data_source"    />
        <result property="jobUrl"    column="job_url"    />
        <result property="rowId"    column="row_id"    />
        <result property="jobCategory"    column="job_category"    />
        <result property="isExplain"    column="is_explain"    />
        <result property="explainUrl"    column="explain_url"    />
        <result property="cover"    column="cover"    />
        <result property="jobType"    column="job_type"    />
        <result property="type"    column="type"    />
        <result property="industry"    column="industry"    />
        <result property="scale"    column="scale"    />
        <result property="companyNature"    column="company_nature"    />
        <result property="code"    column="code"    />
        <result property="jobAddress"    column="job_address"    />

        <association property="companyVo" resultMap="CompanyResult"/>
    </resultMap>

    <resultMap type="com.ruoyi.cms.domain.vo.CompanyVo" id="CompanyResult">
        <result property="companyId" column="company_id"/>
        <result property="name" column="name"/>
        <result property="companyDescription"    column="company_description"/>
        <collection property="companyContactList" ofType="com.ruoyi.common.core.domain.entity.CompanyContact"
                resultMap="CompanyContactResult"/>
    </resultMap>

    <resultMap type="com.ruoyi.common.core.domain.entity.CompanyContact" id="CompanyContactResult">
        <result property="contactPerson" column="contact_person"/>
        <result property="contactPersonPhone" column="contact_person_phone"/>
    </resultMap>

    <sql id="selectJobVo">
        select job_id, job_title, min_salary, max_salary, education, experience, company_name, job_location, posting_date, vacancies, del_flag, create_by, create_time, update_by, update_time, remark, latitude, longitude, "view", company_id , is_hot ,apply_num,is_publish, description,job_location_area_code,data_source,job_url,job_category,is_explain,explain_url,cover,job_type,job_address from job
    </sql>
    <insert id="insertBatchRowWork">
        INSERT INTO row_work (
        TaskId, TaskName, Std_class, SF, ZCMC, Aca112, Acb22a, Aac011, Acb240,
        Recruit_Num, Acb202, Aab302, Acb241, Salary, SalaryLow, SalaryHight, Aae397,
        AAB004, AAB022, AAB019, AAE006, AAB092, ORG, ACE760, AAE004, AAE005,
        Num_employers, Experience, Highlight, Minimum_age, Maximum_age, Sex,
        IndustryType, IndustrySub, AAB019_OK, Aac011_OK, Experience_OK, Num_OK,
        Collect_time, ClearFlag, Province, City, County, importdate, YearMonth,
        IsRepeat, latitude, longitude
        ) VALUES
        <foreach collection="list" item="job" separator=",">
        (
        #{job.TaskId}, #{job.TaskName}, #{job.Std_class}, #{job.SF}, #{job.ZCMC}, #{job.Aca112}, #{job.Acb22a},
        #{job.Aac011}, #{job.Acb240}, #{job.Recruit_Num}, #{job.Acb202}, #{job.Aab302}, #{job.Acb241}, #{job.Salary},
        #{job.SalaryLow}, #{job.SalaryHight}, #{job.Aae397}, #{job.AAB004}, #{job.AAB022}, #{job.AAB019},
        #{job.AAE006}, #{job.AAB092}, #{job.ORG}, #{job.ACE760}, #{job.AAE004}, #{job.AAE005}, #{job.Num_employers},
        #{job.Experience}, #{job.Highlight}, #{job.Minimum_age}, #{job.Maximum_age}, #{job.Sex},
        #{job.IndustryType}, #{job.IndustrySub}, #{job.AAB019_OK}, #{job.Aac011_OK}, #{job.Experience_OK},
        #{job.Num_OK}, #{job.Collect_time}, #{job.ClearFlag}, #{job.Province}, #{job.City}, #{job.County},
        #{job.importdate}, #{job.YearMonth}, #{job.IsRepeat}, #{job.latitude}, #{job.longitude}
        )
        </foreach>
    </insert>
    <insert id="batchInsert">
        INSERT INTO job (
        job_title, min_salary, max_salary, education, experience, company_name, job_location,
        job_location_area_code, posting_date, vacancies, latitude, longitude, "view", company_id,
        is_hot, apply_num, description, is_publish, data_source, job_url, remark, del_flag,
        create_by, create_time, row_id, job_category,jobType,job_address
        ) VALUES
        <foreach collection="list" item="job" separator=",">
        (
        #{job.jobTitle}, #{job.minSalary}, #{job.maxSalary}, #{job.education}, #{job.experience},
        #{job.companyName}, #{job.jobLocation}, #{job.jobLocationAreaCode}, #{job.postingDate},
        #{job.vacancies}, #{job.latitude}, #{job.longitude}, #{job.view}, #{job.companyId},
        #{job.isHot}, #{job.applyNum}, #{job.description}, #{job.isPublish}, #{job.dataSource},
        #{job.jobUrl}, #{job.remark}, #{job.delFlag}, #{job.createBy}, #{job.createTime},
        #{job.rowId}, #{job.jobCategory},#{job.jobType},#{job.jobAddress}
        )
        </foreach>
    </insert>
    <insert id="insertBatchRowWorkTmp">
        INSERT INTO row_work_tmp (
        TaskId, TaskName, Std_class, SF, ZCMC, Aca112, Acb22a, Aac011, Acb240,
        Recruit_Num, Acb202, Aab302, Acb241, Salary, SalaryLow, SalaryHight, Aae397,
        AAB004, AAB022, AAB019, AAE006, AAB092, ORG, ACE760, AAE004, AAE005,
        Num_employers, Experience, Highlight, Minimum_age, Maximum_age, Sex,
        IndustryType, IndustrySub, AAB019_OK, Aac011_OK, Experience_OK, Num_OK,
        Collect_time, ClearFlag, Province, City, County, importdate, YearMonth,
        IsRepeat, latitude, longitude
        ) VALUES
        <foreach collection="list" item="job" separator=",">
            (
            #{job.TaskId}, #{job.TaskName}, #{job.Std_class}, #{job.SF}, #{job.ZCMC}, #{job.Aca112}, #{job.Acb22a},
            #{job.Aac011}, #{job.Acb240}, #{job.Recruit_Num}, #{job.Acb202}, #{job.Aab302}, #{job.Acb241}, #{job.Salary},
            #{job.SalaryLow}, #{job.SalaryHight}, #{job.Aae397}, #{job.AAB004}, #{job.AAB022}, #{job.AAB019},
            #{job.AAE006}, #{job.AAB092}, #{job.ORG}, #{job.ACE760}, #{job.AAE004}, #{job.AAE005}, #{job.Num_employers},
            #{job.Experience}, #{job.Highlight}, #{job.Minimum_age}, #{job.Maximum_age}, #{job.Sex},
            #{job.IndustryType}, #{job.IndustrySub}, #{job.AAB019_OK}, #{job.Aac011_OK}, #{job.Experience_OK},
            #{job.Num_OK}, #{job.Collect_time}, #{job.ClearFlag}, #{job.Province}, #{job.City}, #{job.County},
            #{job.importdate}, #{job.YearMonth}, #{job.IsRepeat}, #{job.latitude}, #{job.longitude}
            )
        </foreach>
    </insert>
    <update id="isHot">
        UPDATE job
        SET is_hot = 1
        WHERE job_id IN (
            select b.job_id
            from(
                    SELECT job_id
                    FROM job
                    ORDER BY "view" DESC
                        LIMIT 10)
                as b
        );
    </update>

    <select id="selectJobList" parameterType="Job" resultMap="JobResult">
        <include refid="selectJobVo"/>
        <where>  del_flag = '0'
            <if test="jobTitle != null  and jobTitle != ''"> and job_title = #{jobTitle}</if>
            <if test="minSalary != null "> and min_salary = #{minSalary}</if>
            <if test="maxSalary != null "> and max_salary = #{maxSalary}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="experience != null  and experience != ''"> and experience = #{experience}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', cast(#{companyName, jdbcType=VARCHAR} as varchar), '%')</if>
            <if test="jobLocation != null  and jobLocation != ''"> and job_location = #{jobLocation}</if>
            <if test="postingDate != null and postingDate!=''"> and posting_date = #{postingDate}</if>
            <if test="vacancies != null "> and vacancies = #{vacancies}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="view != null "> and "view" = #{view}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isHot != null "> and is_hot = #{isHot}</if>
            <if test="applyNum != null "> and apply_num = #{applyNum}</if>
            <if test="code != null "> and company_id in(select company_id from company where code=#{code})</if>
            <if test="compensation!=null ">
                <if test="compensation==0 ">
                    and min_salary &lt; 1750
                </if>
                <if test="compensation==1 ">
                    and max_salary &lt; 2000 and min_salary &gt;= 1750
                </if>
                <if test="compensation==2 ">
                    and max_salary &lt; 3000 and min_salary &gt;= 2000
                </if>
                <if test="compensation==3 ">
                    and max_salary &lt; 4000 and min_salary &gt;= 3000
                </if>
                <if test="compensation==4 ">
                    and max_salary &lt; 5000 and min_salary &gt;= 4000
                </if>
                <if test="compensation==5 ">
                    and min_salary &gt; 5000
                </if>
            </if>
        </where>
        order by is_explain desc
    </select>
    <select id="selectAppJobList" resultType="com.ruoyi.cms.domain.query.ESJobSearch">
        <include refid="selectJobVo"/>
        <where>  del_flag = '0'
            <if test="jobTitle != null  and jobTitle != ''"> and job_title = #{jobTitle}</if>
            <if test="minSalary != null "> and min_salary = #{minSalary}</if>
            <if test="maxSalary != null "> and max_salary = #{maxSalary}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="experience != null  and experience != ''"> and experience = #{experience}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', cast(#{companyName, jdbcType=VARCHAR} as varchar), '%')</if>
            <if test="jobLocation != null  and jobLocation != ''"> and job_location = #{jobLocation}</if>
            <if test="postingDate != null and postingDate!='' "> and posting_date = #{postingDate}</if>
            <if test="vacancies != null "> and vacancies = #{vacancies}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="view != null "> and "view" = #{view}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isHot != null "> and is_hot = #{isHot}</if>
            <if test="applyNum != null "> and apply_num = #{applyNum}</if>
            <if test="isPublish != null "> and is_publish = #{isPublish}</if>
            <if test="countyIds!=null and countyIds.size!=0">
                and job_location_area_code in <foreach collection="countyIds" index="item" close=")" open="(">
                #{item}
            </foreach>
            </if>
        </where>
    </select>
    <select id="selectAllRowWork" resultType="com.ruoyi.cms.domain.RowWork">
        select  Id, TaskId, TaskName, Std_class, SF, ZCMC, Aca112, Acb22a, Aac011, Acb240,
                Recruit_Num, Acb202, Aab302, Acb241, Salary, SalaryLow, SalaryHight, Aae397,
                AAB004, AAB022, AAB019, AAE006, AAB092, ORG as "ORG", ACE760, AAE004, AAE005,
                Num_employers, Experience, Highlight, Minimum_age, Maximum_age, Sex,
                IndustryType, IndustrySub, AAB019_OK, Aac011_OK, Experience_OK, Num_OK,
                Collect_time, ClearFlag, Province, City, County, importdate, YearMonth,
                IsRepeat, latitude, longitude,Std_class as JobCategory from row_work
    </select>
    <select id="selectRowCompany" resultType="com.ruoyi.common.core.domain.entity.Company">
        SELECT *
        FROM (
                 SELECT
                     AAB004 AS name,
                     MIN(AAE006) AS location,
                     MIN(IndustryType) as remark,
                     MIN(IndustrySub) as industry,
                     MIN(Num_OK) as scale,
                     MIN(AAB092) as description,
                     MIN(AAB019_OK) as nature
                 FROM row_work
                 GROUP BY AAB004
             ) AS b
        WHERE NOT EXISTS (
                SELECT 1
                FROM company
                WHERE b.name = company.name
            )
    </select>
    <select id="selectAllJob" resultMap="JobEsResult">
        SELECT j.*,c.industry,c.scale,c.nature as company_nature,c.code,c.description as company_description,c.name,c.company_id,t.contact_person,t.contact_person_phone FROM job as j
        left join company as c on c.company_id = j.company_id and j.del_flag='0' and c.del_flag='0'
        LEFT JOIN (SELECT  t1.*, ROW_NUMBER() OVER (PARTITION BY t1.company_id ORDER BY t1.id) AS rn  FROM company_contact AS t1  WHERE t1.del_flag = '0' ) AS t
        ON t.company_id = j.company_id AND t.rn = 1 WHERE j.del_flag = '0' AND c.del_flag = '0' limit #{offset},#{batchSize}
    </select>
    <select id="selectAllInsertRowWork" resultType="com.ruoyi.cms.domain.RowWork">
        select  Id, TaskId, TaskName, Std_class, SF, ZCMC, Aca112, Acb22a, Aac011, Acb240,
                Recruit_Num, Acb202, Aab302, Acb241, Salary, SalaryLow, SalaryHight, Aae397,
                AAB004, AAB022, AAB019, AAE006, AAB092, ORG as "ORG", ACE760, AAE004, AAE005,
                Num_employers, Experience, Highlight, Minimum_age, Maximum_age, Sex,
                IndustryType, IndustrySub, AAB019_OK, Aac011_OK, Experience_OK, Num_OK,
                Collect_time, ClearFlag, Province, City, County, importdate, YearMonth,
                IsRepeat, latitude, longitude,Std_class as JobCategory from row_work where not exists(
            select 1
            from job
            where job.row_id = row_work.id
        ) AND id IN ( SELECT max(id) FROM row_work GROUP BY Aca112,Acb22a)
    </select>
    <select id="selectVectorJob" resultType="com.ruoyi.cms.domain.VectorJob">
        SELECT
            j.job_id,
            j.job_title,
            ed.dict_label as education,
            ex.dict_label as experience,
            j.company_name,
            j.job_location,
            j.description,
            ar.dict_label as area,
            ab.dict_label as nature,
            ac.dict_label as scale,
            concat(j.min_salary,'元-',j.max_salary,'元') as salary
        FROM
            job as j
                inner join company as c on c.company_id = j.company_id
                inner join 	bussiness_dict_data as ed on ed.dict_type = 'education' and ed.dict_value = j.education
                inner join  bussiness_dict_data as ex on ex.dict_type = 'experience' and ex.dict_value = j.experience
                left join  bussiness_dict_data as ar on ar.dict_type = 'area' and ar.dict_value = j.job_location_area_code
                left join  bussiness_dict_data as ab on ab.dict_type = 'company_nature' and ab.dict_value = c.nature
                left join  bussiness_dict_data as ac on ac.dict_type = 'scale' and ac.dict_value = c.scale
        where job_id  =#{jobId}
    </select>

    <sql id="selectAppUserResult">
        select a.user_id, a.name, a.age, a.sex, a.birth_date, a.education, a.political_affiliation, a.phone, a.avatar, a.salary_min, a.salary_max, a.area, a.status, a.del_flag, a.login_ip, a.login_date, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,a.job_title_id,a.is_recommend,a.id_card,a.work_experience,a.is_company_user from job_apply e
    </sql>

    <select id="selectApplyJobUserList" resultType="com.ruoyi.common.core.domain.entity.AppUser">
        <include refid="selectAppUserResult"/>inner join app_user a on e.user_id =a.user_id and a.del_flag='0'
        where e.del_flag='0' and e.job_id=#{jobId}
    </select>

    <select id="getJobInfo" resultType="com.ruoyi.cms.domain.Job">
        SELECT j.*,c.code,c.name as companyName,c.company_id FROM job as j
          left join company as c on c.company_id = j.company_id and j.del_flag='0' and j.job_id=#{jobId}
          and c.del_flag='0' limit 1
    </select>

    <select id="getTotals" parameterType="Job" resultType="java.lang.Integer">
        select count(job_id) as total_count from job
        <where>  del_flag = '0'
            <if test="companyName != null  and companyName != ''"> and company_name =#{companyName}</if>
            <if test="dataSource != null and dataSource!='' "> and data_source = #{dataSource}</if>
            <if test="description != null and description!='' "> and description = #{description}</if>
            <if test="education != null  and education != ''"> and education = #{education}</if>
            <if test="experience != null  and experience != ''"> and experience = #{experience}</if>
            <if test="isPublish != null "> and is_publish = #{isPublish}</if>
            <if test="jobLocation != null  and jobLocation != ''"> and job_location = #{jobLocation}</if>
            <if test="jobTitle != null  and jobTitle != ''"> and job_title = #{jobTitle}</if>
            <if test="minSalary != null "> and min_salary = #{minSalary}</if>
            <if test="maxSalary != null "> and max_salary = #{maxSalary}</if>
            <if test="vacancies != null "> and vacancies = #{vacancies}</if>
        </where>
    </select>

</mapper>