Fenxiao.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\v3tov4\model;
  11. use think\facade\Cache;
  12. /**
  13. * 迁移分销相关数据(分销商、分销商等级)
  14. */
  15. class Fenxiao extends Upgrade
  16. {
  17. /**
  18. * 同步分销商数据
  19. * @param $page_index
  20. * @param $page_size
  21. */
  22. public function getFenxiaoList($page_index, $page_size)
  23. {
  24. try {
  25. model('fenxiao')->startTrans();
  26. $field = 'np.promoter_id,np.promoter_shop_name,np.uid,np.promoter_level,np.parent_promoter,np.regidter_time,np.audit_time,np.lock_time,npl.level_name';
  27. $join = [
  28. [ 'nfx_promoter_level npl', 'np.promoter_level = npl.level_id', 'left' ],
  29. ];
  30. // 查询v3分销商表
  31. $list = $this->getPageList('nfx_promoter', [ [ 'is_audit', '=', '1' ] ], $field, $page_index, $page_size, 'np', $join);
  32. $data = [];
  33. if (!empty($list)) {
  34. if ($page_index == 1) {
  35. // 首次清空分销商表
  36. $prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
  37. model('fenxiao')->execute("TRUNCATE TABLE {$prefix}fenxiao");
  38. }
  39. foreach ($list as $item) {
  40. // 分销商编号
  41. $time_str = date('YmdHi');
  42. $max_no = Cache::get("fenxiao_no_" . $time_str);
  43. if (!isset($max_no) || empty($max_no)) {
  44. $max_no = 1;
  45. } else {
  46. $max_no += 1;
  47. }
  48. $fenxiao_no = $time_str . sprintf("%04d", $max_no);
  49. // 上上级分销商id
  50. $grand_parent = 0;
  51. if (!empty($item[ 'parent_promoter' ])) {
  52. $parent_promoter_info = $this->getInfo('nfx_promoter', [ [ 'promoter_id', '=', $item[ 'parent_promoter' ] ] ], 'parent_promoter');
  53. if (!empty($parent_promoter_info) && !empty($parent_promoter_info[ 'parent_promoter' ])) {
  54. $grand_parent = $parent_promoter_info[ 'parent_promoter' ];
  55. }
  56. }
  57. // 查询分销商账户数据
  58. $account_info = $this->getInfo('nfx_user_account', [ [ 'uid', '=', $item[ 'uid' ] ] ], 'commission,commission_cash,commission_withdraw');
  59. array_push($data, [
  60. 'fenxiao_id' => $item[ 'promoter_id' ],
  61. 'site_id' => 1,
  62. 'fenxiao_no' => $fenxiao_no,
  63. 'fenxiao_name' => $item[ 'promoter_shop_name' ],
  64. 'member_id' => $item[ 'uid' ],
  65. 'level_id' => $item[ 'promoter_level' ],
  66. 'level_name' => $item[ 'level_name' ],
  67. 'parent' => $item[ 'parent_promoter' ],
  68. 'grand_parent' => $grand_parent,
  69. 'account' => $account_info[ 'commission_cash' ] ?? 0,
  70. 'account_withdraw' => $account_info[ 'commission_withdraw' ] ?? 0,
  71. 'total_commission' => $account_info[ 'commission' ] ?? 0,
  72. 'create_time' => $item[ 'regidter_time' ],
  73. 'audit_time' => $item[ 'audit_time' ],
  74. 'lock_time' => $item[ 'lock_time' ]
  75. ]);
  76. }
  77. }
  78. // 添加到v4分销商表
  79. model('fenxiao')->addList($data);
  80. model('fenxiao')->commit();
  81. return $this->success();
  82. } catch (\Exception $e) {
  83. model('fenxiao')->rollback();
  84. return $this->error('', $e->getMessage());
  85. }
  86. }
  87. /**
  88. * 获取需要同步的分销商的数量
  89. */
  90. public function getFenxiaoCount()
  91. {
  92. return $this->getCount('nfx_promoter', [ [ 'is_audit', '=', '1' ] ], 'promoter_id');
  93. }
  94. /**
  95. * 同步待审核分销商数据
  96. * @param $page_index
  97. * @param $page_size
  98. */
  99. public function getFenxiaoApplyList($page_index, $page_size)
  100. {
  101. try {
  102. model('fenxiao_apply')->startTrans();
  103. $field = 'np.promoter_shop_name,np.parent_promoter,np.uid,np.promoter_level,np.regidter_time,npl.level_name,su.user_tel,su.nick_name,su.user_headimg,su.reg_time,su.user_name';
  104. $join = [
  105. [ 'nfx_promoter_level npl', 'np.promoter_level = npl.level_id', 'left' ],
  106. [ 'sys_user su', 'su.uid = np.uid', 'left' ]
  107. ];
  108. // 查询v3分销商表
  109. $list = $this->getPageList('nfx_promoter', [ [ 'is_audit', '=', '0' ] ], $field, $page_index, $page_size, 'np', $join);
  110. $data = [];
  111. if (!empty($list)) {
  112. if ($page_index == 1) {
  113. // 首次清空分销商申请表
  114. $prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
  115. model('fenxiao_apply')->execute("TRUNCATE TABLE {$prefix}fenxiao_apply");
  116. }
  117. foreach ($list as $item) {
  118. $user_info = $this->getInfo('sys_user', [ [ 'uid', '=', $item[ 'uid' ] ] ]);
  119. if ($user_info) {
  120. array_push($data, [
  121. 'site_id' => 1,
  122. 'fenxiao_name' => $item[ 'promoter_shop_name' ],
  123. 'parent' => $item[ 'parent_promoter' ],
  124. 'member_id' => $item[ 'uid' ],
  125. 'mobile' => $item[ 'user_tel' ] ?? 0,
  126. 'nickname' => $item[ 'nick_name' ] ?? $item[ 'user_name' ],
  127. 'headimg' => $item[ 'user_headimg' ],
  128. 'level_id' => $item[ 'promoter_level' ],
  129. 'level_name' => $item[ 'level_name' ],
  130. 'reg_time' => $item[ 'reg_time' ],
  131. 'create_time' => $item[ 'regidter_time' ]
  132. ]);
  133. }
  134. }
  135. }
  136. // 添加到v4分销商申请表
  137. model('fenxiao_apply')->addList($data);
  138. model('fenxiao_apply')->commit();
  139. return $this->success();
  140. } catch (\Exception $e) {
  141. model('fenxiao_apply')->rollback();
  142. return $this->error('', $e->getMessage());
  143. }
  144. }
  145. /**
  146. * 获取需要同步的待审核分销商的数量
  147. */
  148. public function getFenxiaoApplyCount()
  149. {
  150. return $this->getCount('nfx_promoter', [ [ 'is_audit', '=', '0' ] ], 'promoter_id');
  151. }
  152. /**
  153. * 同步分销商等级数据
  154. */
  155. public function getFenxiaoLevelList($page_index, $page_size)
  156. {
  157. try {
  158. model('fenxiao_level')->startTrans();
  159. if ($page_index == 1) {
  160. $field = 'level_id,level_name,level_0,level_1,level_2,level_money,create_time';
  161. // 查询v3分销商表
  162. $list = $this->getList('nfx_promoter_level', [], $field, 'level_money asc');
  163. $data = [];
  164. if (!empty($list)) {
  165. // 首次清空分销商申请表
  166. $prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
  167. model('fenxiao_level')->execute("TRUNCATE TABLE {$prefix}fenxiao_level");
  168. foreach ($list as $key => $item) {
  169. array_push($data, [
  170. 'level_id' => $item[ 'level_id' ],
  171. 'level_num' => ( $key + 1 ),
  172. 'site_id' => 1,
  173. 'level_name' => $item[ 'level_name' ],
  174. 'one_rate' => $item[ 'level_0' ],
  175. 'two_rate' => $item[ 'level_1' ],
  176. 'three_rate' => $item[ 'level_2' ],
  177. 'upgrade_type' => 1,
  178. 'order_money' => $item[ 'level_money' ],
  179. 'create_time' => $item[ 'create_time' ],
  180. 'status' => 1
  181. ]);
  182. }
  183. // 添加到v4分销商等级表
  184. model('fenxiao_level')->addList($data);
  185. }
  186. // 添加默认分销商等级
  187. $default_level = [
  188. 'site_id' => 1,
  189. 'level_name' => '默认等级',
  190. 'level_num' => 0,
  191. 'one_rate' => 0,
  192. 'two_rate' => 0,
  193. 'three_rate' => 0,
  194. 'upgrade_type' => 1,
  195. 'order_money' => 0,
  196. 'create_time' => time(),
  197. 'status' => 1,
  198. 'is_default' => 1
  199. ];
  200. model('fenxiao_level')->add($default_level);
  201. }
  202. model('fenxiao_level')->commit();
  203. return $this->success();
  204. } catch (\Exception $e) {
  205. model('fenxiao_level')->rollback();
  206. return $this->error('', $e->getMessage());
  207. }
  208. }
  209. /**
  210. * 获取需要同步的分销商等级的数量
  211. */
  212. public function getFenxiaoLevelCount()
  213. {
  214. return $this->getCount('nfx_promoter_level', [], 'level_id');
  215. }
  216. }