| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace addons\CloudStock\common\logic\order;
- use addons\CloudStock\common\enums\CloudStockEnum;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockAgentGuota;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\models\CloudStockIncreaseDetail;
- use addons\CloudStock\common\models\CloudStockUpgradeBag;
- use common\enums\StatusEnum;
- class RestrictPurchases
- {
- public function orderPreview(array $params)
- {
- if(!empty($params['data'])&&!empty($params['mall_id'])&&!empty($params['user_id'])){
- $goods_ids = [];
- foreach ($params['data']['group_order_goods'] as $item){
- foreach ($item['goods'] as $goods_id=>$val){
- $goods_ids[]=$goods_id;
- }
- }
- self::check($params['mall_id'],$params['user_id'],$goods_ids,0,'buy');
- }
- }
- public function orderCreate(array $params)
- {
- if(!empty($params['goods_ids'])&&!empty($params['mall_id'])&&!empty($params['user_id'])){
- self::check($params['mall_id'],$params['user_id'],$params['goods_ids'],0,'buy');
- }
- }
- public static function check($mall_id, $user_id, $goods_ids, $num = 0, $type = 'fill',$source='1')
- {
- try {
- $month = date('Y-m');
- $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_agent)) return true;
- switch ($type) {
- case 'buy':
- $give_arr = [];
- $buy_goods_id_arr = $goods_ids;
- $upgrade_bag_list = CloudStockUpgradeBag::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['<=','level',$cloud_stock_agent['level']],
- ],
- ]);
- foreach ($upgrade_bag_list as $item) {
- $goods_ids = [];
- if (!empty(intval($item['goods_id']))) {
- $goods_ids[] = intval($item['goods_id']);
- } else {
- $goods_ids = json_decode($item['goods_id'], true);
- }
- if (array_intersect($buy_goods_id_arr, $goods_ids)) {
- if (!empty($item['give_goods_num'])) {
- if (!empty(intval($item['give_goods_num']))) {
- $give_goods_num[] = intval($item['give_goods_num']);
- if (!empty($item['give_goods_id'])) {
- if (isset($give_arr[$item['give_goods_id']])) {
- $give_arr[$item['give_goods_id']] += $item['give_goods_num'];
- } else {
- $give_arr[$item['give_goods_id']] = $item['give_goods_num'];
- }
- }
- } else {
- $give_goods_num = json_decode($item['give_goods_num'], true);
- foreach ($give_goods_num as $give_goods) {
- if (isset($give_arr[$give_goods['goods_id']])) {
- $give_arr[$give_goods['goods_id']] += $give_goods['num'];
- } else {
- $give_arr[$give_goods['goods_id']] = $give_goods['num'];
- }
- }
- }
- }
- break;
- }
- }
- $goods_ids = array_keys($give_arr);
- if(empty($goods_ids)) return true;
- break;
- }
- $cloud_stock_agent_fill_num_list = CloudStockIncreaseDetail::find()
- ->where([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'status' => StatusEnum::ENABLED,
- 'goods_id' => $goods_ids,
- 'changes_type' => [CloudStockEnum::STOCK_CHANGE_TYPE100,CloudStockEnum::STOCK_CHANGE_TYPE101,],
- ])
- ->andWhere(['>=', 'created_at', strtotime($month . '-01 00:00:00')])
- ->select('sum(num) as num,goods_id')
- ->groupBy('goods_id')
- ->indexBy('goods_id')
- ->all();
- $cloud_stock_agent_guota_list = CloudStockAgentGuota::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['user_id' => $user_id],
- ['goods_id' => $goods_ids],
- ['status' => StatusEnum::ENABLED],
- ],
- 'index' => 'goods_id'
- ]);
- $cloud_stock_goods = CloudStockGoods::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['goods_id' => $goods_ids],
- ['status' => StatusEnum::ENABLED]
- ],
- 'index' => 'goods_id'
- ]);
- if (empty($cloud_stock_goods)) {
- return true;
- }
- foreach ($goods_ids as $goods_id) {
- if($type=='buy'){
- $num = 0;
- if(isset($give_arr[$goods_id])){
- $num+=$give_arr[$goods_id];
- }
- }
- if (!isset($cloud_stock_goods[$goods_id])) continue;
- $goods = $cloud_stock_goods[$goods_id];
- if (empty($goods['monthly_purchase'])) continue;
- $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
- if (empty($monthly_purchase_arr) || !is_array($monthly_purchase_arr)) continue;
- $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
- if (!isset($monthly_purchase_arr[$cloud_stock_agent['level']]) || $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'] <= 0) continue;
- $limit_nums = $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'];
- $fill_nums = 0;
- if (isset($cloud_stock_agent_fill_num_list[$goods_id])){
- $fill_nums = $cloud_stock_agent_fill_num_list[$goods_id]['num'];
- }
- if (isset($cloud_stock_agent_guota_list[$goods_id])) {
- $limit_nums += $cloud_stock_agent_guota_list[$goods_id]['limit_nums'];
- }
- if ($fill_nums + $num > $limit_nums) {
- if($source==1){
- throw new \Exception('您已达到当月补货上限');
- }else{
- throw new \Exception('该用户已经达到当月补货上限');
- }
- }
- }
- return true;
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- //限购数据,剩余数量
- public static function getLimitData($mall_id, $user_id, $goods_ids)
- {
- try {
- $month = date('Y-m');
- $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_agent)) return [];
- $cloud_stock_agent_fill_num_list = CloudStockIncreaseDetail::find()
- ->where([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'status' => StatusEnum::ENABLED,
- 'goods_id' => $goods_ids,
- 'changes_type' => [CloudStockEnum::STOCK_CHANGE_TYPE100,CloudStockEnum::STOCK_CHANGE_TYPE101,],
- ])
- ->andWhere(['>=', 'created_at', strtotime($month . '-01 00:00:00')])
- ->select('sum(num) as num,goods_id')
- ->groupBy('goods_id')
- ->indexBy('goods_id')
- ->all();
- $cloud_stock_agent_guota_list = CloudStockAgentGuota::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['user_id' => $user_id],
- ['goods_id' => $goods_ids],
- ['status' => StatusEnum::ENABLED],
- ],
- 'index' => 'goods_id'
- ]);
- $cloud_stock_goods = CloudStockGoods::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['goods_id' => $goods_ids],
- ['status' => StatusEnum::ENABLED]
- ],
- 'index' => 'goods_id'
- ]);
- if (empty($cloud_stock_goods)) return [];
- $data = [];
- foreach ($goods_ids as $goods_id) {
- if (!isset($cloud_stock_goods[$goods_id])) continue;
- $goods = $cloud_stock_goods[$goods_id];
- if (empty($goods['monthly_purchase'])) continue;
- $monthly_purchase_arr = json_decode($goods['monthly_purchase'], true);
- if (empty($monthly_purchase_arr) || !is_array($monthly_purchase_arr)) continue;
- $monthly_purchase_arr = array_column($monthly_purchase_arr, null, 'level');
- if (!isset($monthly_purchase_arr[$cloud_stock_agent['level']]) || $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'] <= 0) continue;
- $limit_nums = $monthly_purchase_arr[$cloud_stock_agent['level']]['nums'];
- $fill_nums = 0;
- if (isset($cloud_stock_agent_fill_num_list[$goods_id])){
- $fill_nums = $cloud_stock_agent_fill_num_list[$goods_id]['num'];
- }
- if (isset($cloud_stock_agent_guota_list[$goods_id])) {
- $limit_nums += $cloud_stock_agent_guota_list[$goods_id]['limit_nums'];
- }
- $remaining_quantity = $limit_nums-$fill_nums;
- if($remaining_quantity<0) $remaining_quantity = 0;
- $data[$goods_id] = [
- 'month_quota'=>$limit_nums,
- 'remaining_quantity'=>$remaining_quantity,
- ];
- }
- return $data;
- } catch (\Exception $e) {
- \Yii::error('getLimitData:'.$e->getMessage());
- return [];
- }
- }
- }
|