AgentFrontCentralizeGoodsExt.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. use yii\db\ActiveRecord;
  5. /**
  6. * This is the model class for table "{{%agent_front_centralize_goods_ext}}".
  7. *
  8. * @property integer $id
  9. * @property integer $centralize_goods_id
  10. * @property integer $goods_num
  11. * @property integer $pre_sorting_num
  12. * @property integer $sorting_num
  13. * @property integer $md_id
  14. * @property integer $status
  15. * @property integer $sort_key
  16. * @property integer $sort_time
  17. * @property integer $is_delete
  18. * @property integer $created_at
  19. * @property integer $updated_at
  20. */
  21. class AgentFrontCentralizeGoodsExt extends \yii\db\ActiveRecord
  22. {
  23. const STATUS_WAIT_SORTING = 0;
  24. const STATUS_WAIT_CAR_LOADING = 1;
  25. const STATUS_WAIT_DELIVERY = 2;
  26. const STATUS_FINISH = 3;
  27. /**
  28. * @inheritdoc
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%agent_front_centralize_goods_ext}}';
  33. }
  34. public function behaviors()
  35. {
  36. return [
  37. [
  38. 'class' => TimestampBehavior::class,
  39. 'attributes' => [
  40. ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
  41. ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
  42. ]
  43. ]
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['id', 'centralize_goods_id', 'created_at', 'updated_at', 'is_delete', 'goods_num', 'md_id', 'sorting_num', 'status', 'pre_sorting_num'], 'integer'],
  53. [['sort_key', 'sort_time'], 'number']
  54. ];
  55. }
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => '',
  60. 'centralize_goods_id' => '集采商品表ID',
  61. 'md_id' => '门店ID',
  62. 'goods_num' => '商品数量',
  63. 'sorting_num' => '待分拣数量',
  64. 'sort_key' => '门店排序 根据司机以及门店进行排序',
  65. 'sort_time' => '门店排序时间方便未分拣完重置时间',
  66. 'is_delete' => '',
  67. 'created_at' => '',
  68. 'updated_at' => ''
  69. ];
  70. }
  71. public function afterSave($insert, $changedAttributes)
  72. {
  73. parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
  74. if ($insert || isset($changedAttributes['goods_num'])) {
  75. $admin = get_admin();
  76. $staff_id = 0;
  77. $front_agent_admin_id = $admin->id;
  78. if ($admin->type === Admin::ADMIN_TYPE_FRONT_AGENT_STAFF) {
  79. $staff_id = $admin->type_id;
  80. $agentFrontStaff = AgentFrontStaff::findOne($staff_id);
  81. $front_agent_admin_id = $agentFrontStaff->front_agent_admin_id;
  82. }
  83. $AgentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne($this->centralize_goods_id);
  84. if ($AgentFrontCentralizeGoods) {
  85. $attr_info = json_decode($AgentFrontCentralizeGoods->attr_info, true);
  86. $attr_name = "";
  87. if ($attr_info) {
  88. $attr_name = implode(';', array_column($attr_info['attr_list'], 'attr_name'));
  89. }
  90. AgentFrontStaffOperateLog::addOperateLog(
  91. AgentFrontStaffOperateLog::OPERATE_TYPE_GOODS_CONFIRM,
  92. $staff_id,
  93. $front_agent_admin_id,
  94. $this->id,
  95. "商品收货:" . $AgentFrontCentralizeGoods->goods_name. ';规格:'
  96. . $attr_name . ';货号:' . $AgentFrontCentralizeGoods->goods_no . ';件数:' . $AgentFrontCentralizeGoods->goods_num,
  97. );
  98. }
  99. if ($insert) {
  100. $mdBind = DriverMdBind::findOne(['md_id' => $this->md_id, 'is_delete' => 0]);
  101. if ($mdBind) {
  102. $driver_id = $mdBind->driver_id;
  103. $driver = Driver::findOne($driver_id);
  104. $driver_reset_time = Option::get('driver_reset_time_' . $driver->admin_id, 0,
  105. 'agent_front', '00:00')['value'];
  106. $driver_reset_start_time = strtotime(date('Y-m-d ' . $driver_reset_time . ':00'));
  107. //查询出当前仓库所有的门店下的单 然后统一排序
  108. // $driver_id = \app\models\Driver::find()->where(['admin_id' => $driver->admin_id, 'is_delete' => 0])->select('id')->column();
  109. $md_id = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0])
  110. ->select('md_id')->column();
  111. //查询出当前仓库门店下的团货商品订单
  112. $goodsExt = self::find()->alias('ge')
  113. ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
  114. ->where([
  115. 'ge.md_id' => $md_id,
  116. 'ge.is_delete' => 0,
  117. 'ge.status' => self::STATUS_WAIT_SORTING,
  118. 'g.centralize_goods_type' => 1
  119. ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->groupBy('ge.md_id')
  120. ->select('ge.md_id, GROUP_CONCAT(ge.centralize_goods_id) as centralize_goods_id')->asArray()->all();
  121. foreach ($goodsExt as $goodsExtIndex => $goodsExtItem) {
  122. //查询今天已经分拣过的
  123. $agentFrontCentralizeGoodsExtSub = AgentFrontCentralizeGoodsExt::find()->where([
  124. 'AND', [
  125. 'md_id' => $goodsExtItem['md_id'],
  126. 'is_delete' => 0
  127. ],[
  128. '>', 'sort_time', $driver_reset_start_time
  129. ]
  130. ])->asArray()->one();
  131. //如果存在分拣过的 就给直接赋值
  132. if ($agentFrontCentralizeGoodsExtSub) {
  133. $sort_key = $agentFrontCentralizeGoodsExtSub['sort_key'];
  134. } else {
  135. //查询当前下所有门店中最大的序号
  136. $goodsExtMaxSortKey = self::find()->where([
  137. 'md_id' => $md_id,
  138. 'is_delete' => 0,
  139. ])->andWhere(['>', 'sort_time', $driver_reset_start_time])
  140. ->groupBy('md_id')
  141. ->max('sort_key') ?: 0;
  142. $sort_key = $goodsExtMaxSortKey + 1;
  143. }
  144. $goods_ext_edit_info = self::find()->alias('ge')
  145. ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
  146. ->where([
  147. 'ge.md_id' => $goodsExtItem['md_id'],
  148. 'ge.is_delete' => 0,
  149. 'ge.status' => self::STATUS_WAIT_SORTING,
  150. 'g.centralize_goods_type' => 1
  151. ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->select('ge.id')->asArray()->all();
  152. foreach ($goods_ext_edit_info as $item) {
  153. $item_info = self::findOne($item['id']);
  154. $item_info->sort_time = time();
  155. $item_info->sort_key = $sort_key;
  156. $item_info->save();
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }