| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- namespace addons\CloudStock\common\models;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\user\UserPartitionRelationship;
- use addons\CloudStock\common\models\CloudStockAgent;
- /**
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $goods_id 商品id
- * @property int $limit_nums 增加额外限购数量
- * @property int $pre_sale_quantity 预售数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CloudStockAgentGuota extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_agent_guota}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'goods_id', 'limit_nums', 'pre_sale_quantity', 'status', 'created_at', 'updated_at'], 'integer'],
- [['goods_id'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'goods_id' => '商品id',
- 'limit_nums' => '增加额外限购数量',
- 'pre_sale_quantity' => '预售数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function addData(array $params)
- {
- try {
- if (!empty($params['mall_id']) && !empty($params['user_id']) && !empty($params['goods_id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- }
- if (isset($params['limit_nums'])) $model->limit_nums = $params['limit_nums'];
- if (isset($params['pre_sale_quantity'])) $model->pre_sale_quantity = $params['pre_sale_quantity'];
- } else {
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function asynComputeFillNums($mall_id, $uid_arr = [])
- {
- $data = [
- 'mall_id' => $mall_id,
- 'uid_arr' => $uid_arr,
- ];
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, $data, self::class, 'computeFillNums');
- }
- public static function computeFillNumsOld($params)
- {
- $mall_id = $params['mall_id'];
- $i = 1;
- $page_size = 1000;
- $goods_list = CloudStockGoods::lists([
- 'where' => [['mall_id' => $mall_id], ['status' => ['status' => StatusEnum::ENABLED]]],
- ]);
- $where = ['mall_id' => $mall_id, 'status' => ['status' => StatusEnum::ENABLED]];
- if (!empty($params['uid_arr'])) $where['user_id'] = $params['uid_arr'];
- while ($list = CloudStockAgent::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- foreach ($list as $item) {
- $user_id = $item['user_id'];
- $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id, 1);
- $child_agent_list = CloudStockAgent::find()
- ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
- // ->andWhere(['<','level',$item['level']])
- ->asArray()
- ->all();
- foreach ($goods_list as $goods) {
- $limit_nums = 0;
- if (!isset($goods['additional_purchase_restrictions'])) continue;
- $additional_purchase_restrictions_arr = json_decode($goods['additional_purchase_restrictions'], true);
- if (empty($additional_purchase_restrictions_arr) || !is_array($additional_purchase_restrictions_arr)) continue;
- $additional_purchase_restrictions_arr = array_column($additional_purchase_restrictions_arr, null, 'level');
- foreach ($child_agent_list as $child_agent) {
- if (!isset($additional_purchase_restrictions_arr[$child_agent['level']])) continue;
- $limit_nums += $additional_purchase_restrictions_arr[$child_agent['level']]['nums'];
- }
- $add_data = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'goods_id' => $goods['goods_id'],
- 'limit_nums' => $limit_nums,
- ];
- $res = self::addData($add_data);
- if ($res) var_dump($res['id']);
- }
- }
- $i++;
- }
- $i = 1;
- while ($list = CloudStockAgent::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- foreach ($list as $item) {
- $user_id = $item['user_id'];
- $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id);
- $child_agent_list = CloudStockAgent::find()
- ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
- ->andWhere(['<', 'level', $item['level']])
- ->asArray()
- ->all();
- $child_user_id_arr = [];
- if (!empty($child_agent_list)) {
- $child_user_id_arr = array_column($child_agent_list, 'user_id');
- }
- $limit_nums_list = self::lists([
- 'where' => [
- ['user_id' => $child_user_id_arr],
- ],
- 'select' => 'sum(limit_nums) as limit_nums,goods_id',
- 'group' => 'goods_id',
- 'index' => 'goods_id'
- ]);
- foreach ($goods_list as $goods) {
- $pre_sale_quantity = 0;
- if (isset($limit_nums_list[$goods['goods_id']])) {
- $pre_sale_quantity += $limit_nums_list[$goods['goods_id']]['limit_nums'];
- }
- $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
- if (!empty($monthly_purchase_arr)) {
- $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
- }
- foreach ($child_agent_list as $agent) {
- if (isset($monthly_purchase_arr) && isset($monthly_purchase_arr[$agent['level']])) {
- $pre_sale_quantity += $monthly_purchase_arr[$agent['level']]['nums'];
- }
- }
- $add_data = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'goods_id' => $goods['goods_id'],
- 'pre_sale_quantity' => $pre_sale_quantity,
- ];
- $res = self::addData($add_data);
- if ($res) var_dump($res['id']);
- }
- }
- $i++;
- }
- return true;
- }
- /**
- * 更新增加额外限购数量和预售数量
- * @Author:hua
- * @Date:2024-06-14 15:21
- * @Copyright:copyright(c)2024 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function computeFillNums($params)
- {
- $mall_id = $params['mall_id'];
- $i = 1;
- $page_size = 1000;
- $goods_list = CloudStockGoods::lists([
- 'where' => [['mall_id' => $mall_id], ['status' => ['status' => StatusEnum::ENABLED]]],
- ]);
- $where = ['mall_id' => $mall_id, 'status' => ['status' => StatusEnum::ENABLED]];
- if (!empty($params['uid_arr'])) $where['user_id'] = $params['uid_arr'];
- while ($list = CloudStockAgent::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- foreach ($list as $item) {
- $user_id = $item['user_id'];
- $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id, 1);
- $child_agent_list = CloudStockAgent::find()
- ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
- // ->andWhere(['<','level',$item['level']])
- ->asArray()
- ->all();
- foreach ($goods_list as $goods) {
- $limit_nums = 0;
- if (!isset($goods['additional_purchase_restrictions'])) continue;
- $additional_purchase_restrictions_arr = json_decode($goods['additional_purchase_restrictions'], true);
- if (empty($additional_purchase_restrictions_arr) || !is_array($additional_purchase_restrictions_arr)) continue;
- $additional_purchase_restrictions_arr = array_column($additional_purchase_restrictions_arr, null, 'level');
- foreach ($child_agent_list as $child_agent) {
- if (!isset($additional_purchase_restrictions_arr[$child_agent['level']]['nums'])) continue;
- $limit_nums += $additional_purchase_restrictions_arr[$child_agent['level']]['nums'];
- }
- $add_data = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'goods_id' => $goods['goods_id'],
- 'limit_nums' => $limit_nums,
- ];
- $res = self::addData($add_data);
- if ($res) var_dump($res['id']);
- }
- }
- $i++;
- }
- $i = 1;
- while ($list = CloudStockAgent::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- foreach ($list as $item) {
- $user_id = $item['user_id'];
- $goods_data = self::getGoodsData($mall_id, $user_id, $item['level'], $goods_list);
- foreach ($goods_data as $goods_id => $nums) {
- $add_data = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'goods_id' => $goods_id,
- 'pre_sale_quantity' => $nums,
- ];
- $res = self::addData($add_data);
- if ($res) var_dump($res['id']);
- }
- }
- $i++;
- }
- return true;
- }
- /**
- * 获取商品=>预售数量
- * @Author:hua
- * @Date:2024-06-14 15:21
- * @Copyright:copyright(c)2024 广东七件事集团
- * @param $mall_id
- * @param $user_id
- * @param $level
- * @param $goods_list
- * @return array
- */
- protected static function getGoodsData($mall_id, $user_id, $level, $goods_list)
- {
- $j = 1;
- $goods_data = [];
- while (true) {
- $limit = 1000;
- $offset = ($j - 1) * $limit;
- $child_user_id_arr = UserPartitionRelationship::getChildIdArrByChunk($user_id, $offset, $limit);
- if (empty($child_user_id_arr)) {
- break;
- }
- $child_agent_list = CloudStockAgent::find()
- ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
- ->andWhere(['<', 'level', $level])
- ->asArray()
- ->all();
- $child_user_id_arr = [];
- if (!empty($child_agent_list)) {
- $child_user_id_arr = array_column($child_agent_list, 'user_id');
- }
- $limit_nums_list = self::lists([
- 'where' => [
- ['user_id' => $child_user_id_arr],
- ],
- 'select' => 'sum(limit_nums) as limit_nums,goods_id',
- 'group' => 'goods_id',
- 'index' => 'goods_id'
- ]);
- foreach ($goods_list as $goods) {
- $pre_sale_quantity = 0;
- if (isset($limit_nums_list[$goods['goods_id']])) {
- $pre_sale_quantity += $limit_nums_list[$goods['goods_id']]['limit_nums'];
- }
- $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
- if (!empty($monthly_purchase_arr)) {
- $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
- }
- foreach ($child_agent_list as $agent) {
- if (isset($monthly_purchase_arr) && isset($monthly_purchase_arr[$agent['level']])) {
- $pre_sale_quantity += $monthly_purchase_arr[$agent['level']]['nums'];
- }
- }
- if (isset($goods_data[$goods['goods_id']])) {
- $goods_data[$goods['goods_id']] += $pre_sale_quantity;
- } else {
- $goods_data[$goods['goods_id']] = $pre_sale_quantity;
- }
- }
- $j++;
- }
- return $goods_data;
- }
- public static function computeByGoodsDel($params)
- {
- $mall_id = $params['mall_id'];
- $goods_id = $params['goods_id'];
- self::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $mall_id, 'goods_id' => $goods_id]);
- CloudStockAgent::fixStockOne($mall_id);
- return true;
- }
- }
|