RestrictPurchases.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace addons\CloudStock\common\logic\order;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  4. use addons\CloudStock\common\models\CloudStockAgent;
  5. use addons\CloudStock\common\models\CloudStockAgentGuota;
  6. use addons\CloudStock\common\models\CloudStockGoods;
  7. use addons\CloudStock\common\models\CloudStockIncreaseDetail;
  8. use addons\CloudStock\common\models\CloudStockUpgradeBag;
  9. use common\enums\StatusEnum;
  10. class RestrictPurchases
  11. {
  12. public function orderPreview(array $params)
  13. {
  14. if(!empty($params['data'])&&!empty($params['mall_id'])&&!empty($params['user_id'])){
  15. $goods_ids = [];
  16. foreach ($params['data']['group_order_goods'] as $item){
  17. foreach ($item['goods'] as $goods_id=>$val){
  18. $goods_ids[]=$goods_id;
  19. }
  20. }
  21. self::check($params['mall_id'],$params['user_id'],$goods_ids,0,'buy');
  22. }
  23. }
  24. public function orderCreate(array $params)
  25. {
  26. if(!empty($params['goods_ids'])&&!empty($params['mall_id'])&&!empty($params['user_id'])){
  27. self::check($params['mall_id'],$params['user_id'],$params['goods_ids'],0,'buy');
  28. }
  29. }
  30. public static function check($mall_id, $user_id, $goods_ids, $num = 0, $type = 'fill',$source='1')
  31. {
  32. try {
  33. $month = date('Y-m');
  34. $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  35. if (empty($cloud_stock_agent)) return true;
  36. switch ($type) {
  37. case 'buy':
  38. $give_arr = [];
  39. $buy_goods_id_arr = $goods_ids;
  40. $upgrade_bag_list = CloudStockUpgradeBag::lists([
  41. 'where' => [
  42. ['mall_id' => $mall_id],
  43. ['status' => StatusEnum::ENABLED],
  44. ['<=','level',$cloud_stock_agent['level']],
  45. ],
  46. ]);
  47. foreach ($upgrade_bag_list as $item) {
  48. $goods_ids = [];
  49. if (!empty(intval($item['goods_id']))) {
  50. $goods_ids[] = intval($item['goods_id']);
  51. } else {
  52. $goods_ids = json_decode($item['goods_id'], true);
  53. }
  54. if (array_intersect($buy_goods_id_arr, $goods_ids)) {
  55. if (!empty($item['give_goods_num'])) {
  56. if (!empty(intval($item['give_goods_num']))) {
  57. $give_goods_num[] = intval($item['give_goods_num']);
  58. if (!empty($item['give_goods_id'])) {
  59. if (isset($give_arr[$item['give_goods_id']])) {
  60. $give_arr[$item['give_goods_id']] += $item['give_goods_num'];
  61. } else {
  62. $give_arr[$item['give_goods_id']] = $item['give_goods_num'];
  63. }
  64. }
  65. } else {
  66. $give_goods_num = json_decode($item['give_goods_num'], true);
  67. foreach ($give_goods_num as $give_goods) {
  68. if (isset($give_arr[$give_goods['goods_id']])) {
  69. $give_arr[$give_goods['goods_id']] += $give_goods['num'];
  70. } else {
  71. $give_arr[$give_goods['goods_id']] = $give_goods['num'];
  72. }
  73. }
  74. }
  75. }
  76. break;
  77. }
  78. }
  79. $goods_ids = array_keys($give_arr);
  80. if(empty($goods_ids)) return true;
  81. break;
  82. }
  83. $cloud_stock_agent_fill_num_list = CloudStockIncreaseDetail::find()
  84. ->where([
  85. 'mall_id' => $mall_id,
  86. 'user_id' => $user_id,
  87. 'status' => StatusEnum::ENABLED,
  88. 'goods_id' => $goods_ids,
  89. 'changes_type' => [CloudStockEnum::STOCK_CHANGE_TYPE100,CloudStockEnum::STOCK_CHANGE_TYPE101,],
  90. ])
  91. ->andWhere(['>=', 'created_at', strtotime($month . '-01 00:00:00')])
  92. ->select('sum(num) as num,goods_id')
  93. ->groupBy('goods_id')
  94. ->indexBy('goods_id')
  95. ->all();
  96. $cloud_stock_agent_guota_list = CloudStockAgentGuota::lists([
  97. 'where' => [
  98. ['mall_id' => $mall_id],
  99. ['user_id' => $user_id],
  100. ['goods_id' => $goods_ids],
  101. ['status' => StatusEnum::ENABLED],
  102. ],
  103. 'index' => 'goods_id'
  104. ]);
  105. $cloud_stock_goods = CloudStockGoods::lists([
  106. 'where' => [
  107. ['mall_id' => $mall_id],
  108. ['goods_id' => $goods_ids],
  109. ['status' => StatusEnum::ENABLED]
  110. ],
  111. 'index' => 'goods_id'
  112. ]);
  113. if (empty($cloud_stock_goods)) {
  114. return true;
  115. }
  116. foreach ($goods_ids as $goods_id) {
  117. if($type=='buy'){
  118. $num = 0;
  119. if(isset($give_arr[$goods_id])){
  120. $num+=$give_arr[$goods_id];
  121. }
  122. }
  123. if (!isset($cloud_stock_goods[$goods_id])) continue;
  124. $goods = $cloud_stock_goods[$goods_id];
  125. if (empty($goods['monthly_purchase'])) continue;
  126. $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
  127. if (empty($monthly_purchase_arr) || !is_array($monthly_purchase_arr)) continue;
  128. $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
  129. if (!isset($monthly_purchase_arr[$cloud_stock_agent['level']]) || $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'] <= 0) continue;
  130. $limit_nums = $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'];
  131. $fill_nums = 0;
  132. if (isset($cloud_stock_agent_fill_num_list[$goods_id])){
  133. $fill_nums = $cloud_stock_agent_fill_num_list[$goods_id]['num'];
  134. }
  135. if (isset($cloud_stock_agent_guota_list[$goods_id])) {
  136. $limit_nums += $cloud_stock_agent_guota_list[$goods_id]['limit_nums'];
  137. }
  138. if ($fill_nums + $num > $limit_nums) {
  139. if($source==1){
  140. throw new \Exception('您已达到当月补货上限');
  141. }else{
  142. throw new \Exception('该用户已经达到当月补货上限');
  143. }
  144. }
  145. }
  146. return true;
  147. } catch (\Exception $e) {
  148. throw new \Exception($e->getMessage());
  149. }
  150. }
  151. //限购数据,剩余数量
  152. public static function getLimitData($mall_id, $user_id, $goods_ids)
  153. {
  154. try {
  155. $month = date('Y-m');
  156. $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  157. if (empty($cloud_stock_agent)) return [];
  158. $cloud_stock_agent_fill_num_list = CloudStockIncreaseDetail::find()
  159. ->where([
  160. 'mall_id' => $mall_id,
  161. 'user_id' => $user_id,
  162. 'status' => StatusEnum::ENABLED,
  163. 'goods_id' => $goods_ids,
  164. 'changes_type' => [CloudStockEnum::STOCK_CHANGE_TYPE100,CloudStockEnum::STOCK_CHANGE_TYPE101,],
  165. ])
  166. ->andWhere(['>=', 'created_at', strtotime($month . '-01 00:00:00')])
  167. ->select('sum(num) as num,goods_id')
  168. ->groupBy('goods_id')
  169. ->indexBy('goods_id')
  170. ->all();
  171. $cloud_stock_agent_guota_list = CloudStockAgentGuota::lists([
  172. 'where' => [
  173. ['mall_id' => $mall_id],
  174. ['user_id' => $user_id],
  175. ['goods_id' => $goods_ids],
  176. ['status' => StatusEnum::ENABLED],
  177. ],
  178. 'index' => 'goods_id'
  179. ]);
  180. $cloud_stock_goods = CloudStockGoods::lists([
  181. 'where' => [
  182. ['mall_id' => $mall_id],
  183. ['goods_id' => $goods_ids],
  184. ['status' => StatusEnum::ENABLED]
  185. ],
  186. 'index' => 'goods_id'
  187. ]);
  188. if (empty($cloud_stock_goods)) return [];
  189. $data = [];
  190. foreach ($goods_ids as $goods_id) {
  191. if (!isset($cloud_stock_goods[$goods_id])) continue;
  192. $goods = $cloud_stock_goods[$goods_id];
  193. if (empty($goods['monthly_purchase'])) continue;
  194. $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
  195. if (empty($monthly_purchase_arr) || !is_array($monthly_purchase_arr)) continue;
  196. $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
  197. if (!isset($monthly_purchase_arr[$cloud_stock_agent['level']]) || $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'] <= 0) continue;
  198. $limit_nums = $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'];
  199. $fill_nums = 0;
  200. if (isset($cloud_stock_agent_fill_num_list[$goods_id])){
  201. $fill_nums = $cloud_stock_agent_fill_num_list[$goods_id]['num'];
  202. }
  203. if (isset($cloud_stock_agent_guota_list[$goods_id])) {
  204. $limit_nums += $cloud_stock_agent_guota_list[$goods_id]['limit_nums'];
  205. }
  206. $remaining_quantity = $limit_nums-$fill_nums;
  207. if($remaining_quantity<0) $remaining_quantity = 0;
  208. $data[$goods_id] = [
  209. 'month_quota'=>$limit_nums,
  210. 'remaining_quantity'=>$remaining_quantity,
  211. ];
  212. }
  213. return $data;
  214. } catch (\Exception $e) {
  215. \Yii::error('getLimitData:'.$e->getMessage());
  216. return [];
  217. }
  218. }
  219. }