<?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.rc.PublicJobFairMapper">

    <resultMap type="com.ruoyi.cms.domain.rc.PublicJobFair" id="PublicJobFairResult">
        <result property="jobFairId" column="job_fair_id"/>
        <result property="jobFairTitle" column="job_fair_title"/>
        <result property="jobFairAddress" column="job_fair_address"/>
        <result property="jobFairType" column="job_fair_type"/>
        <result property="jobFairStartTime" column="job_fair_start_time"/>
        <result property="jobFairEndTime" column="job_fair_end_time"/>
        <result property="jobFairHostUnit" column="job_fair_host_unit"/>
        <result property="jobFairHelpUnit" column="job_fair_help_unit"/>
        <result property="jobFairOrganizeUnit" column="job_fair_organize_unit"/>
        <result property="jobFairIntroduction" column="job_fair_introduction"/>
        <result property="jobFairRemark" column="job_fair_remark"/>
        <result property="jobFairPhone" column="job_fair_phone"/>
        <result property="jobFairImage" column="job_fair_image"/>
        <result property="jobFairAreaImage" column="job_fair_area_image"/>
        <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="jobFairVenueId" column="job_fair_venue_id"/>
        <result property="jobFairSignUpStartTime" column="job_fair_sign_up_start_time"/>
        <result property="jobFairSignUpEndTime" column="job_fair_sign_up_end_time"/>
        <result property="isSignUp" column="is_sign_up"/>
        <result property="enterpriseNum" column="enterprise_num"/>
        <result property="boothNum" column="booth_num"/>
        <result property="divisionName" column="division_name"/>
        <result property="deptCodeLast" column="dept_code_last"/>
        <result property="deptCodeAll" column="dept_code_all"/>
        <result property="jobFairCategory" column="job_fair_category"/>
    </resultMap>

    <resultMap type="com.ruoyi.cms.domain.rc.PublicJobFairDetail" id="DetailResult">
        <result property="jobFairId" column="job_fair_id"/>
        <result property="jobFairTitle" column="job_fair_title"/>
        <result property="jobFairAddress" column="job_fair_address"/>
        <result property="jobFairIntroduction" column="job_fair_introduction"/>
        <result property="jobFairStartTime" column="job_fair_start_time"/>
        <result property="jobFairEndTime" column="job_fair_end_time"/>
        <result property="jobFairType" column="job_fair_type"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
    </resultMap>

    <resultMap type="com.ruoyi.cms.domain.rc.PublicJobFairCompanyVO" id="CompanyVOResult">
        <result property="id" column="id"/>
        <result property="companyId" column="company_id"/>
        <result property="companyName" column="name"/>
        <result property="scale" column="scale"/>
        <result property="industry" column="industry"/>
        <result property="companyType" column="nature"/>
    </resultMap>

    <sql id="selectPublicJobFairVo">
        select job_fair_id, job_fair_title, job_fair_address, job_fair_type, job_fair_start_time, job_fair_end_time,
               job_fair_host_unit, job_fair_help_unit, job_fair_organize_unit, job_fair_introduction, job_fair_remark,
               job_fair_phone, job_fair_image, job_fair_area_image, create_by, create_time, update_by, update_time,
               job_fair_venue_id, job_fair_sign_up_start_time, job_fair_sign_up_end_time, enterprise_num, booth_num,
               division_name, dept_code_last, dept_code_all, job_fair_category
        from public_job_fair
    </sql>

    <select id="selectPublicJobFairList" resultMap="PublicJobFairResult">
        <include refid="selectPublicJobFairVo"/>
        <where>
            del_flag = '0'
            <if test="query.jobFairTitle != null and query.jobFairTitle != ''">
                and job_fair_title like concat('%', #{query.jobFairTitle}, '%')
            </if>
            <if test="query.jobFairType != null and query.jobFairType != ''">
                and job_fair_type = #{query.jobFairType}
            </if>
            <if test="query.zphjbsj != null and query.zphjbsj != ''">
                and to_char(job_fair_start_time, 'YYYY-MM-DD') &lt;= #{query.zphjbsj}
                and to_char(job_fair_end_time, 'YYYY-MM-DD') &gt;= #{query.zphjbsj}
            </if>
        </where>
        order by job_fair_start_time desc
    </select>

    <select id="selectPublicJobFairDetailById" resultMap="DetailResult">
        select job_fair_id, job_fair_title, job_fair_address, job_fair_introduction,
               job_fair_start_time, job_fair_end_time, job_fair_type, latitude, longitude
        from public_job_fair
        where job_fair_id = #{jobFairId} and del_flag = '0'
    </select>

    <select id="selectCompanyListByJobFairId" resultMap="CompanyVOResult">
        select pfc.id, pfc.company_id, c.name, c.scale, c.industry, c.nature
        from public_job_fair_company pfc
        left join company c on pfc.company_id = c.company_id
        where pfc.job_fair_id = #{jobFairId} and pfc.del_flag = '0'
        order by pfc.create_time desc
    </select>

    <select id="selectJobListByJobFairIdAndCompanyId" resultType="com.ruoyi.cms.domain.Job">
        select j.job_id, j.job_title, j.min_salary, j.max_salary, j.education, j.experience,
               j.company_name, j.job_location, j.job_location_area_code, j.posting_date, j.vacancies,
               j.company_id, j.description, j.job_category
        from public_job_fair_job pfj
        inner join job j on pfj.job_id = j.job_id
        where pfj.job_fair_id = #{jobFairId} and pfj.company_id = #{companyId} and pfj.del_flag = '0' and j.del_flag = '0'
        order by j.create_time desc
    </select>

    <select id="checkUserSignUp" resultType="java.lang.Integer">
        select count(1) from rc_job_fair_sign_up
        where job_fair_id = #{jobFairId}::bigint and user_id = #{personId} and del_flag = '0' and status = '0'
    </select>

    <!-- ========== CMS后台管理SQL ========== -->

    <select id="selectPublicJobFairById" resultMap="PublicJobFairResult">
        <include refid="selectPublicJobFairVo"/>
        where job_fair_id = #{jobFairId} and del_flag = '0'
    </select>

    <insert id="insertPublicJobFair">
        insert into public_job_fair (
            job_fair_id, job_fair_title, job_fair_address, job_fair_type,
            job_fair_start_time, job_fair_end_time, job_fair_host_unit, job_fair_help_unit,
            job_fair_organize_unit, job_fair_introduction, job_fair_remark, job_fair_phone,
            job_fair_image, job_fair_area_image, job_fair_venue_id,
            job_fair_sign_up_start_time, job_fair_sign_up_end_time,
            enterprise_num, booth_num, division_name, dept_code_last, dept_code_all,
            job_fair_category, latitude, longitude,
            create_by, create_time, del_flag
        ) values (
            #{jobFairId}, #{jobFairTitle}, #{jobFairAddress}, #{jobFairType},
            #{jobFairStartTime}, #{jobFairEndTime}, #{jobFairHostUnit}, #{jobFairHelpUnit},
            #{jobFairOrganizeUnit}, #{jobFairIntroduction}, #{jobFairRemark}, #{jobFairPhone},
            #{jobFairImage}, #{jobFairAreaImage}, #{jobFairVenueId},
            #{jobFairSignUpStartTime}, #{jobFairSignUpEndTime},
            #{enterpriseNum}, #{boothNum}, #{divisionName}, #{deptCodeLast}, #{deptCodeAll},
            #{jobFairCategory}, #{latitude}, #{longitude},
            #{createBy}, #{createTime}, '0'
        )
    </insert>

    <update id="updatePublicJobFair">
        update public_job_fair
        <set>
            <if test="jobFairTitle != null">job_fair_title = #{jobFairTitle},</if>
            <if test="jobFairAddress != null">job_fair_address = #{jobFairAddress},</if>
            <if test="jobFairType != null">job_fair_type = #{jobFairType},</if>
            <if test="jobFairStartTime != null">job_fair_start_time = #{jobFairStartTime},</if>
            <if test="jobFairEndTime != null">job_fair_end_time = #{jobFairEndTime},</if>
            <if test="jobFairHostUnit != null">job_fair_host_unit = #{jobFairHostUnit},</if>
            <if test="jobFairHelpUnit != null">job_fair_help_unit = #{jobFairHelpUnit},</if>
            <if test="jobFairOrganizeUnit != null">job_fair_organize_unit = #{jobFairOrganizeUnit},</if>
            <if test="jobFairIntroduction != null">job_fair_introduction = #{jobFairIntroduction},</if>
            <if test="jobFairRemark != null">job_fair_remark = #{jobFairRemark},</if>
            <if test="jobFairPhone != null">job_fair_phone = #{jobFairPhone},</if>
            <if test="jobFairImage != null">job_fair_image = #{jobFairImage},</if>
            <if test="jobFairAreaImage != null">job_fair_area_image = #{jobFairAreaImage},</if>
            <if test="jobFairVenueId != null">job_fair_venue_id = #{jobFairVenueId},</if>
            <if test="jobFairSignUpStartTime != null">job_fair_sign_up_start_time = #{jobFairSignUpStartTime},</if>
            <if test="jobFairSignUpEndTime != null">job_fair_sign_up_end_time = #{jobFairSignUpEndTime},</if>
            <if test="enterpriseNum != null">enterprise_num = #{enterpriseNum},</if>
            <if test="boothNum != null">booth_num = #{boothNum},</if>
            <if test="divisionName != null">division_name = #{divisionName},</if>
            <if test="deptCodeLast != null">dept_code_last = #{deptCodeLast},</if>
            <if test="deptCodeAll != null">dept_code_all = #{deptCodeAll},</if>
            <if test="jobFairCategory != null">job_fair_category = #{jobFairCategory},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        where job_fair_id = #{jobFairId}
    </update>

    <update id="deletePublicJobFairByIds">
        update public_job_fair set del_flag = '2'
        where job_fair_id in
        <foreach item="jobFairId" collection="jobFairIds" open="(" separator="," close=")">
            #{jobFairId}
        </foreach>
    </update>

    <insert id="insertJobFairCompany">
        insert into public_job_fair_company (id, job_fair_id, company_id, create_by, create_time, del_flag)
        values (#{id}, #{jobFairId}, #{companyId}, #{createBy}, #{createTime}, '0')
    </insert>

    <insert id="insertJobFairCompanyBatch">
        insert into public_job_fair_company (id, job_fair_id, company_id, create_by, create_time, del_flag)
        values
        <foreach item="item" collection="list" separator=",">
            (#{item.id}, #{item.jobFairId}, #{item.companyId}, #{item.createBy}, #{item.createTime}, '0')
        </foreach>
    </insert>

    <update id="deleteJobFairCompanyById">
        update public_job_fair_company set del_flag = '2' where id = #{id}
    </update>

    <insert id="insertJobFairJob">
        insert into public_job_fair_job (id, job_fair_id, job_id, company_id, create_by, create_time, del_flag)
        values (#{id}, #{jobFairId}, #{jobId}, #{companyId}, #{createBy}, #{createTime}, '0')
    </insert>

    <insert id="insertJobFairJobBatch">
        insert into public_job_fair_job (id, job_fair_id, job_id, company_id, create_by, create_time, del_flag)
        values
        <foreach item="item" collection="list" separator=",">
            (#{item.id}, #{item.jobFairId}, #{item.jobId}, #{item.companyId}, #{item.createBy}, #{item.createTime}, '0')
        </foreach>
    </insert>

    <update id="deleteJobFairJobById">
        update public_job_fair_job set del_flag = '2' where id = #{id}
    </update>

    <resultMap type="com.ruoyi.cms.domain.rc.JobFairSignUpVO" id="SignUpVOResult">
        <result property="id" column="id"/>
        <result property="jobFairId" column="job_fair_id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="realName" column="nick_name"/>
        <result property="phone" column="phonenumber"/>
        <result property="signUpTime" column="sign_up_time"/>
        <result property="status" column="status"/>
    </resultMap>

    <select id="selectSignUpListByJobFairId" resultMap="SignUpVOResult">
        select s.id, s.job_fair_id, s.user_id, s.sign_up_time, s.status,
               u.user_name, u.nick_name, u.phonenumber
        from rc_job_fair_sign_up s
        left join sys_user u on s.user_id = u.user_id
        where s.job_fair_id = #{jobFairId}::bigint and s.del_flag = '0'
        order by s.sign_up_time desc
    </select>

    <select id="getCurrentQuarterFairs" resultMap="PublicJobFairResult">
        <include refid="selectPublicJobFairVo"/>
        where del_flag = '0'
            AND EXTRACT(QUARTER FROM job_fair_start_time) = EXTRACT(QUARTER FROM NOW())
            AND EXTRACT(YEAR FROM job_fair_start_time) = EXTRACT(YEAR FROM NOW())
        order by job_fair_start_time desc
    </select>

    <select id="getCurrentMonthFairs" resultMap="PublicJobFairResult">
        <include refid="selectPublicJobFairVo"/>
        where del_flag = '0'
            AND EXTRACT(MONTH FROM job_fair_start_time) = EXTRACT(MONTH FROM NOW())
            AND EXTRACT(YEAR FROM job_fair_start_time) = EXTRACT(YEAR FROM NOW())
        order by job_fair_start_time desc
    </select>

    <select id="getJobFairDates" resultType="java.lang.String">
        SELECT DISTINCT TO_CHAR(d.date, 'YYYY-MM-DD') AS date
        FROM public_job_fair f,
             generate_series(
                 DATE_TRUNC('day', f.job_fair_start_time),
                 DATE_TRUNC('day', f.job_fair_end_time),
                 '1 day'::interval
             ) AS d(date)
        WHERE f.del_flag = '0'
        ORDER BY date
    </select>

</mapper>
