CloudStockAgentGuota.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\user\UserPartitionRelationship;
  7. use addons\CloudStock\common\models\CloudStockAgent;
  8. /**
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $user_id 用户id
  13. * @property int $goods_id 商品id
  14. * @property int $limit_nums 增加额外限购数量
  15. * @property int $pre_sale_quantity 预售数量
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class CloudStockAgentGuota extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_cloud_stock_agent_guota}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'user_id', 'goods_id', 'limit_nums', 'pre_sale_quantity', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['goods_id'], 'required'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城id',
  47. 'user_id' => '用户id',
  48. 'goods_id' => '商品id',
  49. 'limit_nums' => '增加额外限购数量',
  50. 'pre_sale_quantity' => '预售数量',
  51. 'status' => '状态[-1:删除;0:禁用;1启用]',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public static function addData(array $params)
  57. {
  58. try {
  59. if (!empty($params['mall_id']) && !empty($params['user_id']) && !empty($params['goods_id'])) {
  60. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  61. if (empty($model)) {
  62. $model = new self();
  63. $model->loadDefaultValues();
  64. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  65. }
  66. if (isset($params['limit_nums'])) $model->limit_nums = $params['limit_nums'];
  67. if (isset($params['pre_sale_quantity'])) $model->pre_sale_quantity = $params['pre_sale_quantity'];
  68. } else {
  69. $model = new self();
  70. $model->loadDefaultValues();
  71. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  72. }
  73. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  74. return $model;
  75. } catch (\Exception $e) {
  76. self::$static_error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. public static function asynComputeFillNums($mall_id, $uid_arr = [])
  81. {
  82. $data = [
  83. 'mall_id' => $mall_id,
  84. 'uid_arr' => $uid_arr,
  85. ];
  86. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, $data, self::class, 'computeFillNums');
  87. }
  88. public static function computeFillNumsOld($params)
  89. {
  90. $mall_id = $params['mall_id'];
  91. $i = 1;
  92. $page_size = 1000;
  93. $goods_list = CloudStockGoods::lists([
  94. 'where' => [['mall_id' => $mall_id], ['status' => ['status' => StatusEnum::ENABLED]]],
  95. ]);
  96. $where = ['mall_id' => $mall_id, 'status' => ['status' => StatusEnum::ENABLED]];
  97. if (!empty($params['uid_arr'])) $where['user_id'] = $params['uid_arr'];
  98. while ($list = CloudStockAgent::find()
  99. ->where($where)
  100. ->offset(($i - 1) * $page_size)
  101. ->limit($page_size)->all()) {
  102. foreach ($list as $item) {
  103. $user_id = $item['user_id'];
  104. $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id, 1);
  105. $child_agent_list = CloudStockAgent::find()
  106. ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
  107. // ->andWhere(['<','level',$item['level']])
  108. ->asArray()
  109. ->all();
  110. foreach ($goods_list as $goods) {
  111. $limit_nums = 0;
  112. if (!isset($goods['additional_purchase_restrictions'])) continue;
  113. $additional_purchase_restrictions_arr = json_decode($goods['additional_purchase_restrictions'], true);
  114. if (empty($additional_purchase_restrictions_arr) || !is_array($additional_purchase_restrictions_arr)) continue;
  115. $additional_purchase_restrictions_arr = array_column($additional_purchase_restrictions_arr, null, 'level');
  116. foreach ($child_agent_list as $child_agent) {
  117. if (!isset($additional_purchase_restrictions_arr[$child_agent['level']])) continue;
  118. $limit_nums += $additional_purchase_restrictions_arr[$child_agent['level']]['nums'];
  119. }
  120. $add_data = [
  121. 'mall_id' => $mall_id,
  122. 'user_id' => $user_id,
  123. 'goods_id' => $goods['goods_id'],
  124. 'limit_nums' => $limit_nums,
  125. ];
  126. $res = self::addData($add_data);
  127. if ($res) var_dump($res['id']);
  128. }
  129. }
  130. $i++;
  131. }
  132. $i = 1;
  133. while ($list = CloudStockAgent::find()
  134. ->where($where)
  135. ->offset(($i - 1) * $page_size)
  136. ->limit($page_size)->all()) {
  137. foreach ($list as $item) {
  138. $user_id = $item['user_id'];
  139. $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id);
  140. $child_agent_list = CloudStockAgent::find()
  141. ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
  142. ->andWhere(['<', 'level', $item['level']])
  143. ->asArray()
  144. ->all();
  145. $child_user_id_arr = [];
  146. if (!empty($child_agent_list)) {
  147. $child_user_id_arr = array_column($child_agent_list, 'user_id');
  148. }
  149. $limit_nums_list = self::lists([
  150. 'where' => [
  151. ['user_id' => $child_user_id_arr],
  152. ],
  153. 'select' => 'sum(limit_nums) as limit_nums,goods_id',
  154. 'group' => 'goods_id',
  155. 'index' => 'goods_id'
  156. ]);
  157. foreach ($goods_list as $goods) {
  158. $pre_sale_quantity = 0;
  159. if (isset($limit_nums_list[$goods['goods_id']])) {
  160. $pre_sale_quantity += $limit_nums_list[$goods['goods_id']]['limit_nums'];
  161. }
  162. $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
  163. if (!empty($monthly_purchase_arr)) {
  164. $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
  165. }
  166. foreach ($child_agent_list as $agent) {
  167. if (isset($monthly_purchase_arr) && isset($monthly_purchase_arr[$agent['level']])) {
  168. $pre_sale_quantity += $monthly_purchase_arr[$agent['level']]['nums'];
  169. }
  170. }
  171. $add_data = [
  172. 'mall_id' => $mall_id,
  173. 'user_id' => $user_id,
  174. 'goods_id' => $goods['goods_id'],
  175. 'pre_sale_quantity' => $pre_sale_quantity,
  176. ];
  177. $res = self::addData($add_data);
  178. if ($res) var_dump($res['id']);
  179. }
  180. }
  181. $i++;
  182. }
  183. return true;
  184. }
  185. /**
  186. * 更新增加额外限购数量和预售数量
  187. * @Author:hua
  188. * @Date:2024-06-14 15:21
  189. * @Copyright:copyright(c)2024 广东七件事集团
  190. * @param $params
  191. * @return bool
  192. */
  193. public static function computeFillNums($params)
  194. {
  195. $mall_id = $params['mall_id'];
  196. $i = 1;
  197. $page_size = 1000;
  198. $goods_list = CloudStockGoods::lists([
  199. 'where' => [['mall_id' => $mall_id], ['status' => ['status' => StatusEnum::ENABLED]]],
  200. ]);
  201. $where = ['mall_id' => $mall_id, 'status' => ['status' => StatusEnum::ENABLED]];
  202. if (!empty($params['uid_arr'])) $where['user_id'] = $params['uid_arr'];
  203. while ($list = CloudStockAgent::find()
  204. ->where($where)
  205. ->offset(($i - 1) * $page_size)
  206. ->limit($page_size)->all()) {
  207. foreach ($list as $item) {
  208. $user_id = $item['user_id'];
  209. $child_user_id_arr = UserPartitionRelationship::getChildIdArr($user_id, 1);
  210. $child_agent_list = CloudStockAgent::find()
  211. ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
  212. // ->andWhere(['<','level',$item['level']])
  213. ->asArray()
  214. ->all();
  215. foreach ($goods_list as $goods) {
  216. $limit_nums = 0;
  217. if (!isset($goods['additional_purchase_restrictions'])) continue;
  218. $additional_purchase_restrictions_arr = json_decode($goods['additional_purchase_restrictions'], true);
  219. if (empty($additional_purchase_restrictions_arr) || !is_array($additional_purchase_restrictions_arr)) continue;
  220. $additional_purchase_restrictions_arr = array_column($additional_purchase_restrictions_arr, null, 'level');
  221. foreach ($child_agent_list as $child_agent) {
  222. if (!isset($additional_purchase_restrictions_arr[$child_agent['level']]['nums'])) continue;
  223. $limit_nums += $additional_purchase_restrictions_arr[$child_agent['level']]['nums'];
  224. }
  225. $add_data = [
  226. 'mall_id' => $mall_id,
  227. 'user_id' => $user_id,
  228. 'goods_id' => $goods['goods_id'],
  229. 'limit_nums' => $limit_nums,
  230. ];
  231. $res = self::addData($add_data);
  232. if ($res) var_dump($res['id']);
  233. }
  234. }
  235. $i++;
  236. }
  237. $i = 1;
  238. while ($list = CloudStockAgent::find()
  239. ->where($where)
  240. ->offset(($i - 1) * $page_size)
  241. ->limit($page_size)->all()) {
  242. foreach ($list as $item) {
  243. $user_id = $item['user_id'];
  244. $goods_data = self::getGoodsData($mall_id, $user_id, $item['level'], $goods_list);
  245. foreach ($goods_data as $goods_id => $nums) {
  246. $add_data = [
  247. 'mall_id' => $mall_id,
  248. 'user_id' => $user_id,
  249. 'goods_id' => $goods_id,
  250. 'pre_sale_quantity' => $nums,
  251. ];
  252. $res = self::addData($add_data);
  253. if ($res) var_dump($res['id']);
  254. }
  255. }
  256. $i++;
  257. }
  258. return true;
  259. }
  260. /**
  261. * 获取商品=>预售数量
  262. * @Author:hua
  263. * @Date:2024-06-14 15:21
  264. * @Copyright:copyright(c)2024 广东七件事集团
  265. * @param $mall_id
  266. * @param $user_id
  267. * @param $level
  268. * @param $goods_list
  269. * @return array
  270. */
  271. protected static function getGoodsData($mall_id, $user_id, $level, $goods_list)
  272. {
  273. $j = 1;
  274. $goods_data = [];
  275. while (true) {
  276. $limit = 1000;
  277. $offset = ($j - 1) * $limit;
  278. $child_user_id_arr = UserPartitionRelationship::getChildIdArrByChunk($user_id, $offset, $limit);
  279. if (empty($child_user_id_arr)) {
  280. break;
  281. }
  282. $child_agent_list = CloudStockAgent::find()
  283. ->where(['mall_id' => $mall_id, 'user_id' => $child_user_id_arr, 'status' => ['status' => StatusEnum::ENABLED]])
  284. ->andWhere(['<', 'level', $level])
  285. ->asArray()
  286. ->all();
  287. $child_user_id_arr = [];
  288. if (!empty($child_agent_list)) {
  289. $child_user_id_arr = array_column($child_agent_list, 'user_id');
  290. }
  291. $limit_nums_list = self::lists([
  292. 'where' => [
  293. ['user_id' => $child_user_id_arr],
  294. ],
  295. 'select' => 'sum(limit_nums) as limit_nums,goods_id',
  296. 'group' => 'goods_id',
  297. 'index' => 'goods_id'
  298. ]);
  299. foreach ($goods_list as $goods) {
  300. $pre_sale_quantity = 0;
  301. if (isset($limit_nums_list[$goods['goods_id']])) {
  302. $pre_sale_quantity += $limit_nums_list[$goods['goods_id']]['limit_nums'];
  303. }
  304. $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
  305. if (!empty($monthly_purchase_arr)) {
  306. $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
  307. }
  308. foreach ($child_agent_list as $agent) {
  309. if (isset($monthly_purchase_arr) && isset($monthly_purchase_arr[$agent['level']])) {
  310. $pre_sale_quantity += $monthly_purchase_arr[$agent['level']]['nums'];
  311. }
  312. }
  313. if (isset($goods_data[$goods['goods_id']])) {
  314. $goods_data[$goods['goods_id']] += $pre_sale_quantity;
  315. } else {
  316. $goods_data[$goods['goods_id']] = $pre_sale_quantity;
  317. }
  318. }
  319. $j++;
  320. }
  321. return $goods_data;
  322. }
  323. public static function computeByGoodsDel($params)
  324. {
  325. $mall_id = $params['mall_id'];
  326. $goods_id = $params['goods_id'];
  327. self::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $mall_id, 'goods_id' => $goods_id]);
  328. CloudStockAgent::fixStockOne($mall_id);
  329. return true;
  330. }
  331. }