MemberRelation.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2018/10/22
  6. * Time: 上午11:52
  7. */
  8. namespace app\common\services\member;
  9. use app\backend\modules\member\models\Member;
  10. use app\backend\modules\member\models\MemberShopInfo;
  11. use app\common\exceptions\ShopException;
  12. use app\common\models\member\ChildrenOfMember;
  13. use app\common\models\member\ParentOfMember;
  14. use Illuminate\Support\Facades\DB;
  15. use app\backend\modules\member\models\MemberRelation as Relation;
  16. use app\backend\modules\member\models\MemberParent;
  17. use app\common\models\notice\MessageTemp;
  18. use app\common\events\MessageEvent;
  19. use app\common\services\finance\PointService;
  20. class MemberRelation
  21. {
  22. public $parent;
  23. public $child;
  24. public $map_relaton = [];
  25. public $map_parent = [];
  26. public $map_parent_total = 0;
  27. public function __construct()
  28. {
  29. $this->parent = new ParentOfMember();
  30. $this->child = new ChildrenOfMember();
  31. }
  32. /**
  33. * 批量统计会员父级
  34. *
  35. */
  36. public function createParentOfMember($uniacid = null)
  37. {
  38. ini_set("memory_limit","-1");
  39. \Log::debug('------queue parent start-----');
  40. if (is_null($uniacid)) {
  41. $uniacid = \YunShop::app()->uniacid;
  42. }
  43. $pageSize = 2000;
  44. $total = Member::getAllMembersInfosByQueue($uniacid)->distinct()->count();
  45. $total_page = ceil($total/$pageSize);
  46. \Log::debug('------total-----', $total);
  47. \Log::debug('------total_page-----', $total_page);
  48. for ($curr_page = 1; $curr_page <= $total_page; $curr_page++) {
  49. \Log::debug('------curr_page-----', $curr_page);
  50. $offset = ($curr_page - 1) * $pageSize;
  51. $job = (new \app\Jobs\memberParentOfMemberJob($uniacid, $pageSize, $offset));
  52. dispatch($job);
  53. }
  54. }
  55. /**
  56. * 批量统计会员子级
  57. *
  58. */
  59. public function createChildOfMember()
  60. {
  61. \Log::debug('------queue child start-----');
  62. $job = (new \app\Jobs\memberChildOfMemberJob(\YunShop::app()->uniacid));
  63. dispatch($job);
  64. }
  65. /**
  66. * 获取会员指定层级的子级
  67. *
  68. * @param $uid
  69. * @param $depth
  70. * @return mixed
  71. */
  72. public function getMemberByDepth($uid, $depth)
  73. {
  74. $this->child->getMemberByDepth($uid, $depth);
  75. }
  76. /**
  77. * 添加会员关系
  78. *
  79. */
  80. public function addMemberOfRelation($uid, $parent_id)
  81. {
  82. try {
  83. DB::transaction(function() use ($uid, $parent_id) {
  84. $this->parent->addNewParentData($uid, $parent_id);
  85. $this->child-> addNewChildData($this->parent, $uid, $parent_id);
  86. });
  87. return true;
  88. } catch (\Exception $e) {
  89. \Log::debug('-------member relation add error-----', [$e->getMessage()]);
  90. return false;
  91. }
  92. }
  93. /**
  94. * 删除会员关系
  95. *
  96. * @param $uid
  97. * @throws \Exception
  98. * @throws \Throwable
  99. */
  100. public function delMemberOfRelation($uid, $n_parent_id)
  101. {
  102. DB::transaction(function() use ($uid, $n_parent_id) {
  103. \Log::debug('--------setp5-------');
  104. $this->child->delMemberOfRelation($this->parent, $uid, $n_parent_id);
  105. $this->parent->delMemberOfRelation($this->child, $uid, $n_parent_id);
  106. });
  107. }
  108. /**
  109. * 修改后重新添加
  110. *
  111. * @param $uid
  112. * @param $n_parent_id
  113. */
  114. public function reAddMemberOfRelation()
  115. {
  116. foreach ($this->map_relaton as $reData) {
  117. $this->addMemberOfRelation($reData[1], $reData[0]);
  118. }
  119. }
  120. /**
  121. * 修改会员关系
  122. *
  123. * @param $uid
  124. * @param $o_parent_id
  125. * @param $n_parent_id
  126. * @throws \Exception
  127. * @throws \Throwable
  128. */
  129. public function changeMemberOfRelation($uid, $n_parent_id)
  130. {
  131. try {
  132. DB::transaction(function() use ($uid, $n_parent_id) {
  133. $this->delMemberOfRelation($uid, $n_parent_id);
  134. if ($n_parent_id) {
  135. \Log::debug('------step4-------', $n_parent_id);
  136. $this->reAddMemberOfRelation();
  137. }
  138. });
  139. return true;
  140. } catch (\Exception $e) {
  141. \Log::debug('------修改会员关系error----', [$e->getMessage()]);
  142. return false;
  143. }
  144. }
  145. public function hasRelationOfParent($uid, $depth)
  146. {
  147. return $this->parent->hasRelationOfParent($uid, $depth);
  148. }
  149. public function hasRelationOfChild($uid)
  150. {
  151. return $this->child->hasRelationOfChild($uid);
  152. }
  153. public function build($member_id, $parent_id, $model = null)
  154. {
  155. $parent_relation = $this->hasRelationOfParent($member_id, 1);
  156. if ($parent_relation->isEmpty() && intval($parent_id) > 0) {
  157. \Log::debug('------step1-------');
  158. if ($this->addMemberOfRelation($member_id, $parent_id)) {
  159. return ['status' => 1];
  160. }
  161. } else {
  162. if ($parent_relation[0]->parent_id != $parent_id) {
  163. return $this->change($member_id, $parent_id);
  164. }
  165. }
  166. return ['status' => 0];
  167. }
  168. public function change($member_id, $parent_id)
  169. {
  170. $ids = [];
  171. if ($member_id != $parent_id) {
  172. $parent_relation = $this->hasRelationOfParent($member_id, 1);
  173. $child_relation = $this->hasRelationOfChild($member_id);
  174. \Log::debug('------step2-------');
  175. $this->map_relaton[] = [$parent_id, $member_id];
  176. foreach ($child_relation as $rows) {
  177. $ids[] = $rows['child_id'];
  178. }
  179. $ids = array_unique($ids) ?: [];
  180. $memberInfo = MemberShopInfo::getParentOfMembeWithTrashed($ids);
  181. if (count($ids) != count($memberInfo)) {
  182. $filter = [];
  183. $reids = [];
  184. foreach ($memberInfo as $val) {
  185. if (in_array($val['member_id'], $filter)) {
  186. $reids[] = $val['member_id'];
  187. } else {
  188. $filter[] = $val['member_id'];
  189. }
  190. }
  191. \Log::debug('-------重复id-----', $reids);
  192. \Log::debug('------关系链修改-数据异常-------', [count($ids), count($memberInfo)]);
  193. throw new ShopException('关系链修改-数据异常');
  194. }
  195. foreach ($ids as $rows) {
  196. foreach ($memberInfo as $val) {
  197. if ($rows == $val['member_id']) {
  198. $this->map_relaton[] = [$val['parent_id'], $val['member_id']];
  199. break;
  200. }
  201. }
  202. }
  203. file_put_contents(storage_path("logs/" . date('Y-m-d') . "_changerelation.log"), print_r($member_id . '-'. $parent_relation[0]->parent_id . '-'. $parent_id . PHP_EOL, 1), FILE_APPEND);
  204. if ($this->changeMemberOfRelation($member_id, $parent_id)) {
  205. \Log::debug('------修改完成------', [$member_id, $parent_id]);
  206. // //绑定下线成功赠送积分
  207. // $this->rewardPoint($parent_id,$member_id);
  208. return ['status' => 1];
  209. }
  210. }
  211. return ['status' => 0];
  212. }
  213. //成为下线奖励积分
  214. public function rewardPoint($parent_id,$member_id){
  215. $relation = Relation::getSetInfo()->first();
  216. //奖励积分
  217. $reward_points = $relation->reward_points;
  218. //最大奖励人数
  219. $maxinum_number = $relation->maximum_number;
  220. $total = MemberParent::where([
  221. ['parent_id','=',$parent_id],
  222. ['level','=',1],
  223. ])->count();
  224. \Log::debug('会员成为下线奖励积分',$reward_points.'--'.$maxinum_number.'---'.$total);
  225. if( $total <= $maxinum_number || empty($maxinum_number)){
  226. //团队下线小于设置的最大奖励人数就奖励积分
  227. $memberModel = Member::where('uid',$parent_id)->first();
  228. $pointData = array(
  229. 'uniacid' => \YunShop::app()->get('uniacid'),
  230. 'point_income_type' => PointService::POINT_INCOME_GET,
  231. 'member_id' => $memberModel->uid,
  232. 'point_mode' => PointService::POINT_MODE_PRESENTATION,
  233. 'point' => $reward_points,
  234. 'remark' => '------会员ID为----'.$member_id.'成为会员ID为'.$parent_id.'的下线奖励积分'.$reward_points.'个',
  235. );
  236. try {
  237. $pointService = new PointService($pointData);
  238. $pointService->changePoint();
  239. \Log::debug('------会员ID为----'.$member_id.'成为会员ID为'.$parent_id.'的下线奖励积分'.$reward_points);
  240. } catch (\Exception $e) {
  241. \Log::error('成为下线积分奖励出错:' . $e->getMessage());
  242. }
  243. }
  244. }
  245. /**
  246. * 修复会员关系
  247. *
  248. * @param $uid
  249. * @param $parent_id
  250. * @throws \Exception
  251. * @throws \Throwable
  252. */
  253. public function fixMemberOfRelation($uid, $parent_id)
  254. {
  255. DB::transaction(function() use ($uid, $parent_id) {
  256. $this->parent->fixParentData($uid, $parent_id);
  257. $this->child-> fixChildData($this->parent, $uid, $parent_id);
  258. });
  259. }
  260. public function fixData($member_id, $parent_id)
  261. {
  262. $this->fixMemberOfRelation($member_id, $parent_id);
  263. }
  264. }