BEGIN;

WITH companies(company_id, company_name, target_count) AS (
    VALUES
        (4412::BIGINT, '瑜兴辉煌科技有限公司5', 6),
        (3013::BIGINT, '瑜兴辉煌科技有限公司', 15),
        (93001::BIGINT, '未知企业', 12),
        (93000::BIGINT, '钵施然智能', 11),
        (4414::BIGINT, '阿里巴巴', 8),
        (4410::BIGINT, 'test616', 15),
        (93002::BIGINT, '新疆石达赛特科技有限公司', 13),
        (93005::BIGINT, '新疆智图信息科技有限公司', 5),
        (93004::BIGINT, '新疆中汇新能环保科技有限公司', 15),
        (93003::BIGINT, '新疆微诊医学检验有限公司', 14),
        (93007::BIGINT, '街景梦工厂', 7),
        (93009::BIGINT, '石河子市稼乐农业生产资料有限责任公司', 9)
),
templates(
    seq_no, job_title, min_salary, max_salary, education, experience,
    area_code, job_type, vacancies, job_location, description
) AS (
    VALUES
        (1, '行政专员', 4000, 6000, '3', '0', '17', '0', 2, '石河子市东城街道', '负责日常行政事务、资料整理及部门协作。'),
        (2, '人事专员', 4500, 6500, '4', '4', '16', '0', 2, '石河子市新城街道', '负责招聘、员工关系及人事档案管理。'),
        (3, '财务会计', 5000, 7500, '4', '4', '15', '0', 2, '石河子市红山街道', '负责账务处理、报表编制和税务资料整理。'),
        (4, '销售顾问', 4500, 9000, '3', '0', '19', '0', 6, '石河子市向阳街道', '负责客户开发、产品介绍、合同跟进和客户维护。'),
        (5, '客户服务专员', 4000, 6000, '3', '0', '18', '0', 4, '石河子市老街街道', '负责客户咨询、售后问题处理及服务记录维护。'),
        (6, '市场运营专员', 5000, 8000, '4', '4', '17', '0', 3, '石河子市东城街道', '负责市场活动策划、渠道运营和数据分析。'),
        (7, '新媒体运营', 5000, 7500, '4', '2', '16', '0', 2, '石河子市新城街道', '负责公众号、短视频平台内容策划与运营。'),
        (8, '软件开发工程师', 7000, 12000, '4', '4', '17', '0', 3, '石河子市开发区', '参与业务系统开发、接口联调、测试和技术文档编写。'),
        (9, '实施运维工程师', 6000, 9000, '3', '4', '17', '0', 3, '石河子市开发区', '负责系统部署、用户培训、故障处理和日常运维。'),
        (10, '设备维修技术员', 5500, 8500, '3', '4', '20', '0', 4, '石河子镇', '负责生产设备巡检、维护保养和故障维修。'),
        (11, '质量检验员', 4500, 6500, '3', '0', '20', '0', 4, '石河子镇', '负责原料及产品质量检测、记录和异常反馈。'),
        (12, '生产操作员', 4500, 7000, '2', '0', '20', '0', 10, '石河子镇', '按照生产规范完成操作、设备点检及现场整理。'),
        (13, '农业技术员', 5000, 8000, '3', '4', '9', '0', 5, '石总场', '提供农业生产技术指导，开展田间巡查和技术服务。'),
        (14, '仓储物流专员', 4500, 6500, '3', '0', '21', '0', 4, '北泉镇', '负责货物入库、盘点、出库及物流信息维护。'),
        (15, '项目经理', 8000, 13000, '4', '5', '17', '0', 2, '石河子市开发区', '负责项目计划、资源协调、进度控制和交付验收。')
)
INSERT INTO shz.job (
    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,
    company_id, is_hot, apply_num, job_location_area_code,
    description, is_publish, data_source, job_type, job_address,
    review_status
)
SELECT
    templates.job_title,
    templates.min_salary,
    templates.max_salary,
    templates.education,
    templates.experience,
    companies.company_name,
    templates.job_location,
    CURRENT_TIMESTAMP,
    templates.vacancies,
    '0',
    'admin',
    CURRENT_TIMESTAMP,
    'admin',
    CURRENT_TIMESTAMP,
    companies.company_id,
    '0',
    '0',
    templates.area_code,
    templates.description,
    1,
    '户外招聘会8模拟数据',
    templates.job_type,
    templates.job_location,
    NULL
FROM companies
JOIN templates ON templates.seq_no <= companies.target_count
WHERE NOT EXISTS (
    SELECT 1
    FROM shz.job existing
    WHERE existing.company_id = companies.company_id
      AND existing.job_title = templates.job_title
      AND existing.data_source = '户外招聘会8模拟数据'
      AND existing.del_flag = '0'
);

INSERT INTO shz.cms_outdoor_fair_job (
    fair_id, company_id, job_id, create_by, create_time,
    update_by, update_time, del_flag
)
SELECT
    8,
    job.company_id,
    job.job_id,
    'admin',
    CURRENT_TIMESTAMP,
    'admin',
    CURRENT_TIMESTAMP,
    '0'
FROM shz.job job
WHERE job.data_source = '户外招聘会8模拟数据'
  AND job.del_flag = '0'
  AND EXISTS (
      SELECT 1
      FROM shz.fair_company fair_company
      WHERE fair_company.job_fair_id = 8
        AND fair_company.company_id = job.company_id
  )
  AND NOT EXISTS (
      SELECT 1
      FROM shz.cms_outdoor_fair_job relation
      WHERE relation.fair_id = 8
        AND relation.job_id = job.job_id
  );

INSERT INTO shz.job_contact (
    job_id, contact_person, contact_person_phone, position,
    del_flag, create_by, create_time, update_by, update_time
)
SELECT
    job.job_id,
    '招聘负责人',
    '13909930008',
    '人力资源',
    '0',
    'admin',
    CURRENT_TIMESTAMP,
    'admin',
    CURRENT_TIMESTAMP
FROM shz.job job
WHERE job.data_source = '户外招聘会8模拟数据'
  AND job.del_flag = '0'
  AND NOT EXISTS (
      SELECT 1
      FROM shz.job_contact contact
      WHERE contact.job_id = job.job_id
        AND contact.del_flag = '0'
  );

COMMIT;
