WeightRuleLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace addons\BossWeight\common\logic;
  3. use common\traits\ErrorTrait;
  4. use Yii;
  5. use Exception;
  6. use yii\helpers\Json;
  7. use common\models\mall\Mall;
  8. use addons\BossWeight\common\enums\BossEnum;
  9. use addons\BossWeight\common\models\AddonsBoss;
  10. use common\enums\StatusEnum;
  11. use addons\BossWeight\common\models\AddonsBossLevelSetting;
  12. use common\enums\OrderStatusEnum;
  13. use common\models\order\Order;
  14. use common\models\order\OrderDetail;
  15. use common\models\user\UserPartitionRelationship;
  16. use addons\BossWeight\common\models\AddonsBossPrice;
  17. use common\enums\UpgradeEnum;
  18. /**
  19. * 股东权重状态执行
  20. * @Author lun
  21. * @DateTime 2021-11-16 19:07:32
  22. * @copyright: Copyright (c) 2020 广东七件事集团
  23. */
  24. class WeightRuleLogic{
  25. use ErrorTrait;
  26. public function execute()
  27. {
  28. $mall_list = Mall::getEnableMallList(true);
  29. if(!empty($mall_list)){
  30. foreach ($mall_list as $mall){
  31. $mallId = $mall['id'];
  32. // 获取基础配置
  33. $config = Yii::$app->services->setting->addons->get($mallId, 'BossWeight', 'basic');
  34. if ($config['is_enable'] == StatusEnum::ENABLED) {
  35. // 获取所有股东列表
  36. $boss_list = AddonsBoss::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])->asArray()->all();
  37. if (empty($boss_list)) continue;
  38. foreach ($boss_list as $k => $boss){
  39. if(!$this->weightRule($boss,1)) continue;
  40. }
  41. }
  42. }
  43. }
  44. return true;
  45. }
  46. /**
  47. * 权重限制规则
  48. */
  49. public function weightRule($boss,$type = 0){
  50. $levelSetting = AddonsBossLevelSetting::find()->where([
  51. 'status' => 1,
  52. 'mall_id'=> $boss['mall_id'],
  53. 'level' => $boss['level']
  54. ])->one();
  55. if(!$levelSetting || !$levelSetting->weight_turnover_ratio || !$levelSetting->weight_level_ratio || !$levelSetting->weight_status || !$levelSetting->weight_is_limit){
  56. return false;
  57. }
  58. $checked_weight_values = json_decode($levelSetting->checked_weight_values,true);
  59. if(empty($checked_weight_values)){
  60. return false;
  61. }
  62. //股东权重
  63. $weight = CommissionLogLogic::getBossWeight($boss);
  64. foreach($checked_weight_values as $key => $value){
  65. $boss_update = AddonsBoss::findOne(['id'=>$boss['id']]);
  66. if($value['key'] == 0 && $value['checked'] ==1){
  67. //无权重不执行该条件
  68. if(!$weight && !$boss_update->boss_weight_status){
  69. continue;
  70. }
  71. $bossPrice = AddonsBossPrice::find()->where([
  72. 'user_id' => $boss['user_id'],
  73. 'level' => $boss['level']
  74. ])->one();
  75. if(!$bossPrice){
  76. continue;
  77. }
  78. //如果boss用户的分红金额达到设置的上限金额 boss权重失效
  79. if($levelSetting->weight_max_price && $bossPrice->price > $levelSetting->weight_max_price){
  80. if(!$boss_update->boss_weight_status && $type){
  81. $boss_update->boss_weight_status = 1;
  82. if(!$boss_update->save()){
  83. \Yii::error('股东:'.$boss['id'].'权重失效状态1更新失败');
  84. }
  85. }
  86. //如果规定时间内复购商品 boss权重恢复
  87. if(isset($value['value']['val']) && isset($value['value']['val1']) && !empty($value['value']['val']) && $value['value']['val1'] && $boss_update->boss_weight_status == 1){
  88. $end_time = strtotime('+'.$value['value']['val'].' day', $bossPrice->updated_at);
  89. $count = OrderDetail::find()->where([
  90. 'status' => 1,
  91. 'user_id' => $boss['user_id'],
  92. 'mall_id' => $boss['mall_id'],
  93. ])->andWhere(['in','goods_id',$value['value']['val1']])
  94. ->andWhere(['>','order_status',0])
  95. ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
  96. ->andWhere(['>','created_at',$bossPrice->updated_at])
  97. ->andWhere(['<=','created_at',$end_time])
  98. ->count();
  99. if($count > 0){
  100. $boss_update->boss_weight_status = 0;
  101. if(!$boss_update->save()){
  102. \Yii::error('股东:'.$boss['id'].'权重正常使用状态更新失败');
  103. }
  104. //恢复权重后恢复分红金额
  105. $bossPrice->price = 0;
  106. if(!$bossPrice->save()){
  107. \Yii::error('股东:'.$boss['id'].'分红额度更新失败');
  108. }
  109. }
  110. }
  111. }
  112. }
  113. if($value['key'] == 1 && $value['checked'] ==1){
  114. //无权重不执行该条件
  115. if(!$weight && !$boss_update->boss_weight_status){
  116. continue;
  117. }
  118. if(isset($value['value']['val']) && !empty($value['value']['val'])){
  119. //根据最近一次获得加权分红时间判断
  120. $bossPrice = AddonsBossPrice::find()->where([
  121. 'user_id' => $boss['user_id'],
  122. 'level' => $boss['level']
  123. ])->one();
  124. if(!$bossPrice){
  125. continue;
  126. }
  127. $start_time = time();
  128. $end_time = strtotime('-'.$value['value']['val'].' day', $start_time);
  129. //直推会员
  130. $userChilds = UserPartitionRelationship::getChildIdArr($boss['user_id'],1);
  131. //直推会员数量
  132. $userCounts = UserPartitionRelationship::getChildCounts($boss['user_id'],1);
  133. //直推会员达成条件数量
  134. $finished = 0;
  135. foreach ($userChilds as $k=>&$child){
  136. $count = $this->getOrderCounts($child,$boss['mall_id'],$end_time,$start_time);
  137. $childrenCount = $this->getChildCounts($child,$end_time,$start_time);
  138. if(!$count && !$childrenCount){
  139. //如果已经有了失效状态,则不再执行更新
  140. if(!$boss_update->boss_weight_status && $type){
  141. $boss_update->boss_weight_status = 2;
  142. if(!$boss_update->save()){
  143. \Yii::error('股东:'.$boss['id'].'权重失效状态2更新失败');
  144. }
  145. }
  146. }
  147. if(isset($value['value']['val1']) && !empty($value['value']['val1']) && $boss_update->boss_weight_status = 2){
  148. $boss_child = AddonsBoss::find()->where(['mall_id' => $boss['mall_id'], 'status' => StatusEnum::ENABLED,'user_id'=>$child])->asArray()->one();
  149. if($boss_child){
  150. $weight_child = CommissionLogLogic::getBossWeight($boss_child);
  151. if($weight_child){
  152. $userCounts--;
  153. continue;
  154. }
  155. }
  156. $update_count = Order::find()->where([
  157. 'pay_status' => [OrderStatusEnum::PAY],
  158. 'mall_id' => $boss['mall_id'],
  159. 'status' => 1,
  160. 'user_id' => $child,
  161. ])->andWhere(['>','created_at',$bossPrice->updated_at])
  162. ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
  163. ->count();
  164. if($update_count >= $value['value']['val1']){
  165. $finished++;
  166. }
  167. }
  168. }
  169. if($finished == $userCounts){
  170. $boss_update->boss_weight_status = 0;
  171. if(!$boss_update->save()){
  172. \Yii::error('股东:'.$boss['id'].'权重正常使用状态更新失败');
  173. }
  174. }
  175. }
  176. }
  177. if($value['key'] == 2 && $value['checked'] ==1){
  178. if(empty($value['value']['val']) || !$type){
  179. continue;
  180. }
  181. $time = $boss['created_at'];
  182. $weight_new = CommissionLogLogic::getBossWeightOutStatus($boss);
  183. if($weight_new || strtotime('+'.$value['value']['val'].' day', $time) > time() || $boss_update->upgrade_status != UpgradeEnum::UPGRADE_STATUS_GOODS){
  184. continue;
  185. }
  186. \Yii::error('股东:'.$boss['id'].'股东身份取消');
  187. $boss_update->status = -1;
  188. if(!$boss_update->save()){
  189. \Yii::error('股东:'.$boss['id'].'股东身份取消失败');
  190. }
  191. }
  192. }
  193. return true;
  194. }
  195. /**
  196. * 查询下级数组
  197. * @param $user_id
  198. * @param int $deep
  199. * @return array
  200. */
  201. public function getChildCounts($user_id,$start_at,$end_time)
  202. {
  203. $model = UserPartitionRelationship::find()->where(['user_id' => $user_id])->orderBy('part desc')->asArray()->one();
  204. $tree = $model['tree'];
  205. $parent_deep = $model['deep'];
  206. $query = UserPartitionRelationship::find()
  207. ->where(['<>', 'user_id', $user_id])
  208. ->andWhere(['like', 'tree', "{$tree},%", false])
  209. ->andWhere(['>','created_at',$start_at])
  210. ->andWhere(['<=','created_at',$end_time]);
  211. return $query->count();
  212. }
  213. /**
  214. * 查询会员订单数
  215. */
  216. public function getOrderCounts($user_id,$mall_id,$start_at,$end_time)
  217. {
  218. $count = Order::find()->where([
  219. 'pay_status' => [ OrderStatusEnum::PAY],
  220. 'mall_id' => $mall_id,
  221. 'status' => 1,
  222. 'user_id' => $user_id,
  223. ])->andWhere(['>','created_at',$start_at])
  224. ->andWhere(['=', 'store_id', StatusEnum::DISABLED]) // 过滤门店订单
  225. ->andWhere(['<=','created_at',$end_time])
  226. ->count();
  227. return $count;
  228. }
  229. }