| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%agent_front_centralize_goods_ext}}".
- *
- * @property integer $id
- * @property integer $centralize_goods_id
- * @property integer $goods_num
- * @property integer $pre_sorting_num
- * @property integer $sorting_num
- * @property integer $md_id
- * @property integer $status
- * @property integer $sort_key
- * @property integer $sort_time
- * @property integer $is_delete
- * @property integer $created_at
- * @property integer $updated_at
- */
- class AgentFrontCentralizeGoodsExt extends \yii\db\ActiveRecord
- {
- const STATUS_WAIT_SORTING = 0;
- const STATUS_WAIT_CAR_LOADING = 1;
- const STATUS_WAIT_DELIVERY = 2;
- const STATUS_FINISH = 3;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%agent_front_centralize_goods_ext}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'centralize_goods_id', 'created_at', 'updated_at', 'is_delete', 'goods_num', 'md_id', 'sorting_num', 'status', 'pre_sorting_num'], 'integer'],
- [['sort_key', 'sort_time'], 'number']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'centralize_goods_id' => '集采商品表ID',
- 'md_id' => '门店ID',
- 'goods_num' => '商品数量',
- 'sorting_num' => '待分拣数量',
- 'sort_key' => '门店排序 根据司机以及门店进行排序',
- 'sort_time' => '门店排序时间方便未分拣完重置时间',
- 'is_delete' => '',
- 'created_at' => '',
- 'updated_at' => ''
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
- if ($insert || isset($changedAttributes['goods_num'])) {
- $admin = get_admin();
- $staff_id = 0;
- $front_agent_admin_id = $admin->id;
- if ($admin->type === Admin::ADMIN_TYPE_FRONT_AGENT_STAFF) {
- $staff_id = $admin->type_id;
- $agentFrontStaff = AgentFrontStaff::findOne($staff_id);
- $front_agent_admin_id = $agentFrontStaff->front_agent_admin_id;
- }
- $AgentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne($this->centralize_goods_id);
- if ($AgentFrontCentralizeGoods) {
- $attr_info = json_decode($AgentFrontCentralizeGoods->attr_info, true);
- $attr_name = "";
- if ($attr_info) {
- $attr_name = implode(';', array_column($attr_info['attr_list'], 'attr_name'));
- }
- AgentFrontStaffOperateLog::addOperateLog(
- AgentFrontStaffOperateLog::OPERATE_TYPE_GOODS_CONFIRM,
- $staff_id,
- $front_agent_admin_id,
- $this->id,
- "商品收货:" . $AgentFrontCentralizeGoods->goods_name. ';规格:'
- . $attr_name . ';货号:' . $AgentFrontCentralizeGoods->goods_no . ';件数:' . $AgentFrontCentralizeGoods->goods_num,
- );
- }
- if ($insert) {
- $mdBind = DriverMdBind::findOne(['md_id' => $this->md_id, 'is_delete' => 0]);
- if ($mdBind) {
- $driver_id = $mdBind->driver_id;
- $driver = Driver::findOne($driver_id);
- $driver_reset_time = Option::get('driver_reset_time_' . $driver->admin_id, 0,
- 'agent_front', '00:00')['value'];
- $driver_reset_start_time = strtotime(date('Y-m-d ' . $driver_reset_time . ':00'));
- //查询出当前仓库所有的门店下的单 然后统一排序
- // $driver_id = \app\models\Driver::find()->where(['admin_id' => $driver->admin_id, 'is_delete' => 0])->select('id')->column();
- $md_id = DriverMdBind::find()->where(['driver_id' => $driver_id, 'is_delete' => 0])
- ->select('md_id')->column();
- //查询出当前仓库门店下的团货商品订单
- $goodsExt = self::find()->alias('ge')
- ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
- ->where([
- 'ge.md_id' => $md_id,
- 'ge.is_delete' => 0,
- 'ge.status' => self::STATUS_WAIT_SORTING,
- 'g.centralize_goods_type' => 1
- ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->groupBy('ge.md_id')
- ->select('ge.md_id, GROUP_CONCAT(ge.centralize_goods_id) as centralize_goods_id')->asArray()->all();
- foreach ($goodsExt as $goodsExtIndex => $goodsExtItem) {
- //查询今天已经分拣过的
- $agentFrontCentralizeGoodsExtSub = AgentFrontCentralizeGoodsExt::find()->where([
- 'AND', [
- 'md_id' => $goodsExtItem['md_id'],
- 'is_delete' => 0
- ],[
- '>', 'sort_time', $driver_reset_start_time
- ]
- ])->asArray()->one();
- //如果存在分拣过的 就给直接赋值
- if ($agentFrontCentralizeGoodsExtSub) {
- $sort_key = $agentFrontCentralizeGoodsExtSub['sort_key'];
- } else {
- //查询当前下所有门店中最大的序号
- $goodsExtMaxSortKey = self::find()->where([
- 'md_id' => $md_id,
- 'is_delete' => 0,
- ])->andWhere(['>', 'sort_time', $driver_reset_start_time])
- ->groupBy('md_id')
- ->max('sort_key') ?: 0;
- $sort_key = $goodsExtMaxSortKey + 1;
- }
- $goods_ext_edit_info = self::find()->alias('ge')
- ->leftJoin(['g' => AgentFrontCentralizeGoods::tableName()], 'ge.centralize_goods_id = g.id')
- ->where([
- 'ge.md_id' => $goodsExtItem['md_id'],
- 'ge.is_delete' => 0,
- 'ge.status' => self::STATUS_WAIT_SORTING,
- 'g.centralize_goods_type' => 1
- ])->andWhere(['<=', 'sort_time', $driver_reset_start_time])->select('ge.id')->asArray()->all();
- foreach ($goods_ext_edit_info as $item) {
- $item_info = self::findOne($item['id']);
- $item_info->sort_time = time();
- $item_info->sort_key = $sort_key;
- $item_info->save();
- }
- }
- }
- }
- }
- }
- }
|