$model = (new Zhaopin())->where('is_shelves', '0');
if (!empty($param['keyword'])) {
$model = $model->where('gangwei_name', 'like', '%' . trim($param['keyword']) . '%');
}
if (isset($param['zhaopin_leixing']) && $param['zhaopin_leixing'] != '') {
$model = $model->where('zhaopin_leixing', '=', trim($param['zhaopin_leixing']));
}
$get_result = Genjinlog::where('type', BusinessTypeEnum::RECRUIT)
->field(['yewu_id', 'status', 'max(createtime) as max_createtime'])->group('yewu_id')
->buildSql(); // 转换为子查询的SQL字符串
$latestFollowUps = Genjinlog::alias('main')
->join([$get_result => 'latest'], 'main.yewu_id = latest.yewu_id AND main.createtime = latest.max_createtime')
->field(['main.yewu_id', 'main.createtime', 'main.status'])
->orderRaw("CASE WHEN main.status = 1 THEN 0 WHEN main.status = 2 THEN 1 ELSE 2 END") // 保持原始排序逻辑
->select()
->column('yewu_id');
$get_ids = $model->column('id');
if ($latestFollowUps) {
$diff_array = array_diff($get_ids, $latestFollowUps);
$latestFollowUps = array_merge($latestFollowUps, $diff_array);
$model = $model->whereIn('id', $latestFollowUps);
$new_id_order_map = array_flip($latestFollowUps);
$model = $model->orderRaw("FIELD(id, " . implode(',', array_keys($new_id_order_map)) . ")");
}
$data = $model
->paginate()->toArray();
一条sql 语句 根据数据类型排序
获取最新跟进记录状态 正在热招的排在最前面 暂时不招的排在第二 暂无数据的排在最下面