SharingReceiverForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Admin;
  4. use app\models\Option;
  5. use app\models\SaasCategory;
  6. use app\models\SaasUser;
  7. use app\models\Salesman;
  8. use app\models\SharingReceiver;
  9. use app\models\SharingReceiverCustom;
  10. use app\models\Store;
  11. use app\models\User;
  12. use yii\base\Model;
  13. class SharingReceiverForm extends Model
  14. {
  15. public $id;
  16. public $ids;
  17. /**
  18. * 分账名称
  19. */
  20. public $sharing_name;
  21. /**
  22. * 用户名称
  23. */
  24. public $user_name;
  25. /**
  26. * 商城名称
  27. */
  28. public $store_name;
  29. /**
  30. * 用户手机号
  31. */
  32. public $mobile;
  33. /**
  34. * 分账类型
  35. */
  36. public $sharing_type;
  37. /**
  38. * 分账类型ID:用户ID 商户ID
  39. */
  40. public $sharing_type_id;
  41. /**
  42. * 分账方式
  43. */
  44. public $sharing_way;
  45. /**
  46. * 分账方式基于店铺:店铺ID
  47. */
  48. public $sharing_store_id;
  49. /**
  50. * 分账比例
  51. */
  52. public $sharing_profit;
  53. /**
  54. * 起止时间-开始时间
  55. */
  56. public $start_time;
  57. /**
  58. * 起止时间-结束时间
  59. */
  60. public $end_time;
  61. /**
  62. * 额外联盟用户ID
  63. */
  64. public $extra_saas_user_id;
  65. /**
  66. * 商城分类ID
  67. */
  68. public $store_category_id;
  69. public $key;
  70. public function rules()
  71. {
  72. return [
  73. [['sharing_name', 'user_name', 'mobile', 'start_time', 'end_time', 'ids', 'sharing_type_id', 'store_name', 'key'], 'string'],
  74. [['id', 'sharing_type', 'sharing_way', 'sharing_store_id', 'extra_saas_user_id', 'store_category_id'], 'integer'],
  75. [['sharing_name', 'user_name', 'mobile'], 'trim'],
  76. [['sharing_profit'], 'number']
  77. ];
  78. }
  79. public function list() {
  80. try {
  81. $sharing_name = $this->sharing_name;
  82. $user_name = $this->user_name;
  83. $store_name = $this->store_name;
  84. $mobile = $this->mobile;
  85. $start_time = $this->start_time;
  86. $end_time = $this->end_time;
  87. $sharing_type = $this->sharing_type;
  88. $sharing_way = $this->sharing_way;
  89. $query = SharingReceiverCustom::find()->alias('src')
  90. ->leftJoin(['su' => SaasUser::tableName()], 'su.id = src.type_id AND src.sharing_type = ' . SharingReceiverCustom::SHARING_TYPE_USER)
  91. ->leftJoin(['su_' => SaasUser::tableName()], 'su.id = src.extra_saas_user_id AND src.sharing_type = ' . SharingReceiverCustom::SHARING_TYPE_MERCHANT)
  92. ->leftJoin(['s' => Store::tableName()], 's.id = src.sharing_store_id AND src.sharing_way = ' . SharingReceiverCustom::SHARING_WAY_STORE)
  93. ->where(['src.is_delete' => SharingReceiverCustom::IS_DELETE_NO]);
  94. if (isset($sharing_name) && !empty($sharing_name)) {
  95. $query->andWhere(['LIKE', 'src.name', $sharing_name]);
  96. }
  97. if (isset($user_name) && !empty($user_name)) {
  98. $query->andWhere(['OR', ['LIKE', 'su.name', $user_name], ['LIKE', 'su_.name', $user_name], ['LIKE', 'src.type_id', $user_name]]);
  99. }
  100. if (isset($store_name) && !empty($store_name)) {
  101. $query->andWhere(['LIKE', 's.name', $store_name]);
  102. }
  103. if (isset($mobile) && !empty($mobile)) {
  104. $query->andWhere(['OR', ['LIKE', 'su.mobile', $mobile], ['LIKE', 'su_.mobile', $mobile]]);
  105. }
  106. if (isset($start_time) && !empty($start_time)) {
  107. $query->andWhere(['>=', 'src.created_at', strtotime($start_time)]);
  108. }
  109. if (isset($end_time) && !empty($end_time)) {
  110. $query->andWhere(['<=', 'src.created_at', strtotime($end_time)]);
  111. }
  112. if (isset($sharing_type) && in_array($sharing_type, SharingReceiverCustom::SHARING_TYPE)) {
  113. $query->andWhere(['src.sharing_type' => $sharing_type]);
  114. }
  115. if (isset($sharing_way) && in_array($sharing_way, SharingReceiverCustom::SHARING_WAY)) {
  116. $query->andWhere(['src.sharing_way' => $sharing_way]);
  117. }
  118. $query->select('src.id, src.name, src.sharing_type, src.type_id, src.sharing_way, s.name store_name,
  119. src.sharing_profit, src.amount, src.created_at, src.sharing_store_id, src.extra_saas_user_id')
  120. ->orderBy('src.id DESC');
  121. $pagination = pagination_make($query);
  122. foreach ($pagination['list'] as &$item) {
  123. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  124. $item['sharing_type'] = intval($item['sharing_type']);
  125. $item['sharing_way'] = intval($item['sharing_way']);
  126. $item['store_name'] = $item['store_name'] ?: '';
  127. $saasUser = null;
  128. if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_USER) {
  129. $saasUser = SaasUser::findOne($item['type_id']);
  130. }
  131. if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_MERCHANT) {
  132. $saasUser = SaasUser::findOne($item['extra_saas_user_id']);
  133. }
  134. $item['user_name'] = $saasUser['name'] ?: '';
  135. $item['avatar'] = $saasUser['avatar'] ?: '';
  136. $item['mobile'] = $saasUser['mobile'] ?: '';
  137. }
  138. return [
  139. 'code' => 0,
  140. 'msg' => 'success',
  141. 'data' => $pagination
  142. ];
  143. } catch (\Exception $e) {
  144. return [
  145. 'code' => 1,
  146. 'msg' => $e->getMessage()
  147. ];
  148. }
  149. }
  150. public function save() {
  151. try {
  152. $id = $this->id;
  153. $sharing_name = $this->sharing_name;
  154. $sharing_type_id = $this->sharing_type_id;
  155. $sharing_type = intval($this->sharing_type);
  156. $sharing_way = intval($this->sharing_way);
  157. $sharing_store_id = $this->sharing_store_id;
  158. $sharing_profit = $this->sharing_profit;
  159. $extra_saas_user_id = $this->extra_saas_user_id;
  160. if (!in_array($sharing_type, SharingReceiverCustom::SHARING_TYPE)) {
  161. throw new \Exception('分账类型枚举值错误');
  162. }
  163. if (!in_array($sharing_way, SharingReceiverCustom::SHARING_WAY)) {
  164. throw new \Exception('分账方式枚举值错误');
  165. }
  166. if ($sharing_type === SharingReceiverCustom::SHARING_TYPE_USER) {
  167. $saasUser = SaasUser::findOne(['id' => $sharing_type_id, 'is_delete' => 0]);
  168. if (!$saasUser) {
  169. throw new \Exception('用户不存在');
  170. }
  171. } else {
  172. $saasUser = SaasUser::findOne(['id' => $extra_saas_user_id, 'is_delete' => 0]);
  173. if (!$saasUser) {
  174. throw new \Exception('商户绑定用户不存在');
  175. }
  176. }
  177. if ($sharing_way == SharingReceiverCustom::SHARING_WAY_STORE) {
  178. $store_id = $sharing_store_id;
  179. $store = Store::findOne($store_id);
  180. if (bcadd(union_share_scale($store, false, $id), $sharing_profit, 2) > 100) {
  181. throw new \Exception('保存失败,分销比例超过100%');
  182. }
  183. }
  184. $sharingReceiverCustom = SharingReceiverCustom::findOne(['id' => $id, 'is_delete' => 0]);
  185. if (!$sharingReceiverCustom) {
  186. $is_edit = SharingReceiverCustom::find()->where([
  187. 'sharing_type' => $sharing_type,
  188. 'type_id' => $sharing_type_id,
  189. 'sharing_way' => $sharing_way,
  190. 'sharing_store_id' => $sharing_store_id,
  191. 'is_delete' => 0
  192. ])->asArray()->one();
  193. $sharingReceiverCustom = SharingReceiverCustom::findOne($is_edit['id']);
  194. if (!$sharingReceiverCustom) {
  195. $sharingReceiverCustom = new SharingReceiverCustom();
  196. }
  197. }
  198. $sharingReceiverCustom->name = $sharing_name;
  199. $sharingReceiverCustom->sharing_type = $sharing_type;
  200. $sharingReceiverCustom->type_id = $sharing_type_id;
  201. $sharingReceiverCustom->sharing_way = $sharing_way;
  202. $sharingReceiverCustom->sharing_store_id = $sharing_store_id;
  203. $sharingReceiverCustom->sharing_profit = $sharing_profit;
  204. $sharingReceiverCustom->extra_saas_user_id = $extra_saas_user_id;
  205. if (!$sharingReceiverCustom->save()) {
  206. throw new \Exception(implode(';', array_values($sharingReceiverCustom->firstErrors)));
  207. };
  208. return [
  209. 'code' => 0,
  210. 'msg' => '操作成功'
  211. ];
  212. } catch (\Exception $e) {
  213. return [
  214. 'code' => 1,
  215. 'msg' => $e->getMessage()
  216. ];
  217. }
  218. }
  219. public function del() {
  220. try {
  221. $ids = $this->ids;
  222. $ids = explode(',', $ids ?: '');
  223. if (!$ids) {
  224. throw new \Exception('请选择要删除的数据');
  225. }
  226. foreach ($ids as $id) {
  227. $sharingReceiverCustom = SharingReceiverCustom::findOne($id);
  228. if (!$sharingReceiverCustom) {
  229. throw new \Exception('数据不存在');
  230. }
  231. $sharingReceiverCustom->is_delete = SharingReceiverCustom::IS_DELETE_YES;
  232. if (!$sharingReceiverCustom->save()) {
  233. throw new \Exception(implode(';', array_values($sharingReceiverCustom->firstErrors)));
  234. }
  235. }
  236. return [
  237. 'code' => 0,
  238. 'msg' => '操作成功'
  239. ];
  240. } catch (\Exception $e) {
  241. return [
  242. 'code' => 1,
  243. 'msg' => $e->getMessage()
  244. ];
  245. }
  246. }
  247. //让利分配明细
  248. public function profitList() {
  249. try {
  250. $store_name = $this->store_name;
  251. $store_category_id = $this->store_category_id;
  252. $query = Store::find()->where(['is_delete' => 0]);
  253. if ($store_name) {
  254. $query->andWhere(['like', 'name', $store_name]);
  255. }
  256. if ($store_category_id) {
  257. $query->andWhere(['category_id' => $store_category_id]);
  258. }
  259. $query->orderBy('id DESC')->select('id, name, logo, category_id, self_rebate_value');
  260. $list = pagination_make($query);
  261. foreach ($list['list'] as &$item) {
  262. //分类名称
  263. $saasCategory = SaasCategory::findOne(['is_delete' => 0, 'id' => $item['category_id']]);
  264. $item['category_name'] = $saasCategory->name ?: '';
  265. //省级佣金
  266. $item['provider_profit'] = SharingReceiver::find()->alias('sr')
  267. ->where([
  268. 'sr.store_id' => $item['id'],
  269. 'sr.from' => SharingReceiver::FROM_AREA_AGENT_PROVIDER
  270. ])->sum('amount') ?: '0.00';
  271. //市级佣金
  272. $item['city_profit'] = SharingReceiver::find()->alias('sr')
  273. ->where([
  274. 'sr.store_id' => $item['id'],
  275. 'sr.from' => SharingReceiver::FROM_AREA_AGENT_CITY
  276. ])->sum('amount') ?: '0.00';
  277. //区级佣金
  278. $item['district_profit'] = SharingReceiver::find()->alias('sr')
  279. ->where([
  280. 'sr.store_id' => $item['id'],
  281. 'sr.from' => SharingReceiver::FROM_AREA_AGENT_DISTRICT
  282. ])->sum('amount') ?: '0.00';
  283. //推广代理佣金
  284. $item['bd_profit'] = SharingReceiver::find()->alias('sr')
  285. ->where([
  286. 'sr.store_id' => $item['id'],
  287. 'sr.from' => SharingReceiver::FROM_BD_AGENT
  288. ])->sum('amount') ?: '0.00';
  289. //店铺分销佣金
  290. $item['store_profit'] = SharingReceiver::find()->alias('sr')
  291. ->where([
  292. 'sr.store_id' => $item['id'],
  293. 'sr.from' => SharingReceiver::FROM_STORE_TO_STORE
  294. ])->sum('amount') ?: '0.00';
  295. //分销一级佣金
  296. $item['first_profit'] = SharingReceiver::find()->alias('sr')
  297. ->where([
  298. 'sr.store_id' => $item['id'],
  299. 'sr.from' => SharingReceiver::FROM_PLATFORM
  300. ])->andWhere(['LIKE', 'remark', '一级分销'])->sum('amount') ?: '0.00';
  301. //分销二级佣金
  302. $item['second_profit'] = SharingReceiver::find()->alias('sr')
  303. ->where([
  304. 'sr.store_id' => $item['id'],
  305. 'sr.from' => SharingReceiver::FROM_PLATFORM
  306. ])->andWhere(['LIKE', 'remark', '二级分销'])->sum('amount') ?: '0.00';
  307. //分销三级佣金
  308. $item['third_profit'] = SharingReceiver::find()->alias('sr')
  309. ->where([
  310. 'sr.store_id' => $item['id'],
  311. 'sr.from' => SharingReceiver::FROM_PLATFORM
  312. ])->andWhere(['LIKE', 'remark', '三级分销'])->sum('amount') ?: '0.00';
  313. //灵活分润比例
  314. $item['custom_profit'] = SharingReceiverCustom::find()->where([
  315. 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
  316. ])->andWhere(['OR', [
  317. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
  318. 'sharing_store_id' => $item['id'],
  319. ], [
  320. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_PLATFORM,
  321. ]])->sum('sharing_profit') ?: '0.00';
  322. }
  323. return [
  324. 'code' => 0,
  325. 'msg' => 'success',
  326. 'data' => $list
  327. ];
  328. } catch (\Exception $e) {
  329. return [
  330. 'code' => 1,
  331. 'msg' => $e->getMessage()
  332. ];
  333. }
  334. }
  335. //商城比例详情
  336. public function storeProfitDetail() {
  337. try {
  338. $store_id = $this->id;
  339. $store = Store::findOne($store_id);
  340. //分类名称
  341. $saasCategory = SaasCategory::findOne(['is_delete' => 0, 'id' => $store->category_id]);
  342. $admin = Admin::findOne([
  343. 'type' => 'store',
  344. 'type_id' => $store_id
  345. ]);
  346. $admin_saas_user = SaasUser::findOne($store->store_admin);
  347. //显示比例汇总
  348. $ratio = union_share_scale($store);
  349. $custom_profit = SharingReceiverCustom::find()->where([
  350. 'is_delete' => SharingReceiverCustom::IS_DELETE_NO,
  351. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_PLATFORM,
  352. ])->sum('sharing_profit') ?: 0;
  353. $ratio = bcadd($ratio, $custom_profit, 2);
  354. $agency_price_config = Option::get('agency_price_config', 0, 'saas')['value'];
  355. $agency_price_config = json_decode($agency_price_config, true) ?: [];
  356. $province_percent = $agency_price_config['province_percent'] ?: 0;
  357. $ratio = bcadd($ratio, $province_percent, 2);
  358. $city_percent = $agency_price_config['city_percent'] ?: 0;
  359. $ratio = bcadd($ratio, $city_percent, 2);
  360. $district_percent = $agency_price_config['district_percent'] ?: 0;
  361. $ratio = bcadd($ratio, $district_percent, 2);
  362. $bd_agency_price_config = Option::get('bd_agency_price_config', 0, 'saas')['value'];
  363. $bd_agency_price_config = json_decode($bd_agency_price_config, true) ?: [];
  364. $bd_agent_percent = $bd_agency_price_config['bd_agent_percent'] ?: 0;
  365. $ratio = bcadd($ratio, $bd_agent_percent, 2);
  366. $transfer_profit = $store->transfer_profit;
  367. $store_info = [
  368. 'store_name' => $store->name,
  369. 'logo' => $store->logo,
  370. 'category_name' => $saasCategory->name,
  371. 'admin_user_name' => $admin_saas_user->name ?? '',
  372. 'contact_tel' => $store->contact_tel ?: ($admin_saas_user->mobile ?: ''),
  373. 'store_ratio' => ($ratio ?: '0.00') . '%',
  374. 'real_ratio' => (bcsub(100, $ratio ?: '0.00', 2)) . '%'
  375. ];
  376. return [
  377. 'code' => 0,
  378. 'msg' => '',
  379. 'data' => [
  380. 'store_info' => $store_info,
  381. 'tab_list' => [
  382. [
  383. 'key' => 'provider_profit',
  384. 'value' => '省级比例',
  385. ],
  386. [
  387. 'key' => 'city_profit',
  388. 'value' => '市级比例'
  389. ],
  390. [
  391. 'key' => 'district_profit',
  392. 'value' => '区级比例'
  393. ],
  394. [
  395. 'key' => 'bd_profit',
  396. 'value' => '推广代理比例'
  397. ],
  398. [
  399. 'key' => 'store_profit',
  400. 'value' => '店铺分销比例'
  401. ],
  402. [
  403. 'key' => 'first_profit',
  404. 'value' => '一级分销比例'
  405. ],
  406. [
  407. 'key' => 'second_profit',
  408. 'value' => '二级分销比例'
  409. ],
  410. [
  411. 'key' => 'third_profit',
  412. 'value' => '三级分销比例'
  413. ],
  414. [
  415. 'key' => 'self_profit',
  416. 'value' => '消费返利比例'
  417. ],
  418. [
  419. 'key' => 'platform_custom_profit',
  420. 'value' => '平台灵活分润比例'
  421. ],
  422. [
  423. 'key' => 'store_custom_profit',
  424. 'value' => '店铺灵活分润比例'
  425. ],
  426. ]
  427. ]
  428. ];
  429. } catch (\Exception $e) {
  430. return [
  431. 'code' => 1,
  432. 'msg' => $e->getMessage()
  433. ];
  434. }
  435. }
  436. //商城比例详情
  437. public function storeProfitList() {
  438. try {
  439. $key = $this->key;
  440. $store_id = $this->id;
  441. $store = Store::findOne($store_id);
  442. // 联盟分销
  443. $union_option = json_decode($store->share_setting, true);
  444. $list = [];
  445. $totalCount = 1;
  446. $pageNo = 1;
  447. $pageSize = 10;
  448. if (in_array($key, ['provider_profit', 'city_profit', 'district_profit'])) {
  449. $agency_price_config = Option::get('agency_price_config', 0, 'saas')['value'];
  450. $agency_price_config = json_decode($agency_price_config, true) ?: [];
  451. $percent = 0;
  452. $where = [
  453. 'is_delete' => 0,
  454. 'type' => Admin::ADMIN_TYPE_DEFAULT
  455. ];
  456. if ($key === 'provider_profit') {
  457. $percent = $agency_price_config['province_percent'] ?: 0;
  458. $where['area_level'] = 3;
  459. $where['province_id'] = $store->province_id;
  460. }
  461. if ($key === 'city_profit') {
  462. $percent = $agency_price_config['city_percent'] ?: 0;
  463. $where['area_level'] = 2;
  464. $where['city_id'] = $store->city_id;
  465. }
  466. if ($key === 'district_profit') {
  467. $percent = $agency_price_config['district_percent'] ?: 0;
  468. $where['area_level'] = 1;
  469. $where['district_id'] = $store->district_id;
  470. }
  471. $admin = Admin::findOne($where);
  472. if ($admin) {
  473. $admin_saas_user = SaasUser::findOne($admin->saas_user_id);
  474. }
  475. $list[] = [
  476. 'name' => $admin_saas_user->name ?? '',
  477. 'mobile' => $admin_saas_user->mobile ?? '',
  478. 'avatar' => $admin_saas_user->avatar ?? '',
  479. 'profit' => $percent . '%'
  480. ];
  481. }
  482. if ($key === 'bd_profit') {
  483. $bd_agency_price_config = Option::get('bd_agency_price_config', 0, 'saas')['value'];
  484. $bd_agency_price_config = json_decode($bd_agency_price_config, true) ?: [];
  485. $percent = $bd_agency_price_config['bd_agent_percent'] ?: 0;
  486. $admin_id = 0;
  487. //查询推广代理
  488. if ($store->salesman_id) {
  489. $salesman = Salesman::findOne($store->salesman_id);
  490. $admin_id = $salesman->admin_id;
  491. }
  492. if (empty($admin_id) && $store->admin_id) {
  493. $admin_id = $store->admin_id;
  494. }
  495. $admin = Admin::findOne([
  496. 'id' => $admin_id,
  497. 'is_delete' => 0,
  498. 'type' => Admin::ADMIN_TYPE_BD_AGENT
  499. ]);
  500. if ($admin) {
  501. $admin_saas_user = SaasUser::findOne($admin->saas_user_id);
  502. }
  503. $list[] = [
  504. 'name' => $admin_saas_user->name ?? '',
  505. 'mobile' => $admin_saas_user->mobile ?? '',
  506. 'avatar' => $admin_saas_user->avatar ?? '',
  507. 'profit' => $percent. '%'
  508. ];
  509. }
  510. if (in_array($key, ['store_profit', 'first_profit', 'second_profit', 'third_profit', 'self_profit'])) {
  511. $percent = 0;
  512. if ($key === 'store_profit') {
  513. $percent = $store->store_share_value;
  514. }
  515. if ($key === 'first_profit') {
  516. $percent = $union_option['level_one'] ?: '0.00';
  517. }
  518. if ($key === 'second_profit') {
  519. $percent = $union_option['level_two'] ?: '0.00';
  520. }
  521. if ($key === 'third_profit') {
  522. $percent = $union_option['level_three'] ?: '0.00';
  523. }
  524. if ($key === 'self_profit') {
  525. $percent = $union_option['self_profit'] ?: '0.00';
  526. }
  527. $list[] = [
  528. 'name' => $store->name,
  529. 'mobile' => $store->contact_tel ?: '',
  530. 'avatar' => $store->logo ?: '',
  531. 'profit' => $percent. '%'
  532. ];
  533. }
  534. if (in_array($key, ['platform_custom_profit', 'store_custom_profit'])) {
  535. $where = [
  536. 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
  537. ];
  538. if ($key === 'platform_custom_profit') {
  539. $where['sharing_way'] = SharingReceiverCustom::SHARING_WAY_PLATFORM;
  540. }
  541. if ($key === 'store_custom_profit') {
  542. $where['sharing_way'] = SharingReceiverCustom::SHARING_WAY_STORE;
  543. $where['sharing_store_id'] = $store->id;
  544. }
  545. $query = SharingReceiverCustom::find()
  546. ->where($where)
  547. ->orderBy('id DESC')
  548. ->select('id, name, sharing_type, type_id, extra_saas_user_id, sharing_profit');
  549. $data = pagination_make($query);
  550. foreach ($data['list'] as $item) {
  551. $saas_user = null;
  552. $item['sharing_type'] = intval($item['sharing_type']);
  553. if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_USER) {
  554. $saas_user = SaasUser::findOne($item['type_id']);
  555. }
  556. if ($item['sharing_type'] === SharingReceiverCustom::SHARING_TYPE_MERCHANT) {
  557. $saas_user = SaasUser::findOne($item['extra_saas_user_id']);
  558. if (!$saas_user) {
  559. $item['name'] = $item['name'] . "({$item['type_id']})";
  560. }
  561. }
  562. $list[] = [
  563. 'name' => $saas_user->name ?: $item['name'],
  564. 'mobile' => $saas_user->mobile ?: '',
  565. 'avatar' => $saas_user->avatar ?: '',
  566. 'profit' => $item['sharing_profit']. '%'
  567. ];
  568. }
  569. $totalCount = $data['totalCount'];
  570. $pageNo = $data['pageNo'];
  571. $pageSize = $data['pageSize'];
  572. }
  573. return [
  574. 'code' => 0,
  575. 'msg' => '',
  576. 'data' => [
  577. 'totalCount' => $totalCount,
  578. 'list' => $list,
  579. 'pageNo' => $pageNo,
  580. 'pageSize' => $pageSize,
  581. ]
  582. ];
  583. } catch (\Exception $e) {
  584. return [
  585. 'code' => 1,
  586. 'msg' => $e->getMessage()
  587. ];
  588. }
  589. }
  590. }