CentralizeGoodsExtSortKeyJob.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\jobs;
  3. use app\models\Admin;
  4. use app\models\AgentFrontCentralizeGoods;
  5. use app\models\AgentFrontCentralizeGoodsExt;
  6. use app\models\Driver;
  7. use app\models\DriverMdBind;
  8. use app\models\Option;
  9. use yii\base\BaseObject;
  10. use yii\queue\JobInterface;
  11. class CentralizeGoodsExtSortKeyJob extends BaseObject implements JobInterface
  12. {
  13. public function execute($queue) {
  14. $admin_list = Admin::find()->where(['is_delete' => 0, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT])
  15. ->select('id, name')->asArray()->all();
  16. foreach($admin_list as $admin_item) {
  17. try {
  18. $driver_reset_time = Option::get('driver_reset_time_' . $admin_item['id'], 0,
  19. 'agent_front', '00:00')['value'];
  20. $driver_reset_start_time = strtotime(date('Y-m-d ' . $driver_reset_time . ':00'));
  21. $driver_reset_end_time = $driver_reset_start_time + 120;
  22. if ($driver_reset_start_time < time() && $driver_reset_end_time > time()) {
  23. $driverArray = Driver::find()->where(['is_delete' => 0, 'admin_id' => $admin_item['id']])->asArray()->all();
  24. $driver_array_id = array_column($driverArray, 'id');
  25. foreach ($driver_array_id as $driver_id) {
  26. $md_id = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0])
  27. ->select('md_id')->column();
  28. //查询出当前仓库门店下的团货商品订单
  29. $goodsExt = AgentFrontCentralizeGoodsExt::find()->alias('ge')
  30. ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
  31. ->where([
  32. 'ge.md_id' => $md_id,
  33. 'ge.is_delete' => 0,
  34. 'ge.status' => AgentFrontCentralizeGoodsExt::STATUS_WAIT_SORTING,
  35. 'g.centralize_goods_type' => 1
  36. ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->groupBy('ge.md_id')
  37. ->select('ge.md_id, GROUP_CONCAT(ge.centralize_goods_id) as centralize_goods_id')->asArray()->all();
  38. foreach ($goodsExt as $goodsExtIndex => $goodsExtItem) {
  39. //将团货的商品进行排序
  40. debug_log(['MaxSortKey' => ($goodsExtMaxSortKey + $goodsExtIndex + 1)], '20250507.log');
  41. //查询今天已经分拣过的
  42. $agentFrontCentralizeGoodsExtSub = AgentFrontCentralizeGoodsExt::find()->where([
  43. 'AND', [
  44. 'md_id' => $goodsExtItem['md_id'],
  45. 'is_delete' => 0
  46. ],[
  47. '>', 'sort_time', $driver_reset_start_time
  48. ]
  49. ])->asArray()->one();
  50. //如果存在分拣过的 就给直接赋值
  51. if ($agentFrontCentralizeGoodsExtSub) {
  52. $sort_key = $agentFrontCentralizeGoodsExtSub['sort_key'];
  53. } else {
  54. $goodsExtMaxSortKey = AgentFrontCentralizeGoodsExt::find()->where([
  55. 'md_id' => $md_id,
  56. 'is_delete' => 0,
  57. ])->andWhere(['>', 'sort_time', $driver_reset_start_time])
  58. ->groupBy('md_id')
  59. ->max('sort_key') ?: 0;
  60. $sort_key = $goodsExtMaxSortKey + 1;
  61. }
  62. $goods_ext_edit_info = AgentFrontCentralizeGoodsExt::find()->alias('ge')
  63. ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
  64. ->where([
  65. 'ge.md_id' => $goodsExtItem['md_id'],
  66. 'ge.is_delete' => 0,
  67. 'ge.status' => AgentFrontCentralizeGoodsExt::STATUS_WAIT_SORTING,
  68. 'g.centralize_goods_type' => 1
  69. ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->select('ge.id')->asArray()->all();
  70. foreach ($goods_ext_edit_info as $item) {
  71. $item_info = AgentFrontCentralizeGoodsExt::findOne($item['id']);
  72. $item_info->sort_time = time();
  73. $item_info->sort_key = $sort_key;
  74. $item_info->save();
  75. }
  76. }
  77. }
  78. }
  79. }catch(\Exception $e) {
  80. debug_log($e->getMessage(), '20250507.log');
  81. }
  82. }
  83. }
  84. }