OrderService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/22
  6. * Time: 下午4:51
  7. */
  8. namespace app\common\service;
  9. use app\admin\model\PalmpaySubAccount;
  10. use app\api\library\OrderDeduction;
  11. use app\common\constants\Order;
  12. use app\common\model\OrdersCollect;
  13. use app\main\service\AdminKlUpdateService;
  14. use app\main\service\KlService;
  15. use app\main\constants\PayConstants;
  16. use app\main\service\OfficialAccountsService;
  17. use think\db\Query;
  18. use think\Log;
  19. /**
  20. * 订单信息处理
  21. * Class OrderService
  22. * @package app\common\service
  23. */
  24. class OrderService extends BaseService
  25. {
  26. /**
  27. * @var OrderService
  28. */
  29. private static $self;
  30. /**
  31. * @return OrderService
  32. */
  33. public static function instance()
  34. {
  35. if (self::$self == NULL) {
  36. self::$self = new self();
  37. }
  38. return self::$self;
  39. }
  40. /**
  41. * @return OrdersCollect
  42. */
  43. public function getOrderCollectModel()
  44. {
  45. return model('OrdersCollect');
  46. }
  47. public function getMonthReport($admin_id, $date_range, $offset, $limit,$business_line = PayConstants::BUSINESS_WECHAT)
  48. {
  49. $default = [
  50. 'code' => 1,
  51. 'data' => [
  52. 'total' => 0,
  53. 'rows' => [],
  54. ],
  55. ];
  56. try {
  57. $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id);
  58. if (!$channel_ids) {
  59. return $default;
  60. }
  61. $fields = [
  62. 'LEFT(createdate,6)' => 'create_month',
  63. 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money',
  64. 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money',
  65. 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count',
  66. 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders',
  67. 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money',
  68. 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders',
  69. 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit',
  70. 'o.type',
  71. 'o.flag',
  72. ];
  73. $total = $this->getRechargeOptions($date_range, $fields, false)
  74. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  75. ->where('o.business_line', $business_line)
  76. ->whereIn('o.admin_id', $channel_ids)
  77. ->group('o.type, o.flag, create_month')
  78. ->count();
  79. $all_list = $this->getRechargeOptions($date_range, $fields, false)
  80. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  81. ->where('o.business_line', $business_line)
  82. ->whereIn('o.admin_id', $channel_ids)
  83. ->field($fields)
  84. ->group('o.type, o.flag, create_month')
  85. ->limit($offset, $limit)
  86. ->select();
  87. $current_page_createdate = [];
  88. foreach ($all_list as $channel_order) {
  89. $current_page_createdate[] = $channel_order['create_month'];
  90. }
  91. $agent_list = $this->getRechargeOptions($date_range, $fields, false)
  92. ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH)
  93. ->where('o.business_line', $business_line)
  94. ->whereIn('LEFT(createdate,6)', $current_page_createdate)
  95. ->whereIn('o.admin_id', $channel_ids)
  96. ->order(['create_month' => 'asc'])
  97. ->field($fields)
  98. ->group('o.type, o.flag, create_month')
  99. ->select();
  100. $channel_list = $this->getRechargeOptions($date_range, $fields, false)
  101. ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF)
  102. ->where('o.business_line', $business_line)
  103. ->whereIn('LEFT(createdate,6)', $current_page_createdate)
  104. ->whereIn('o.admin_id', $channel_ids)
  105. ->order(['create_month' => 'asc'])
  106. ->field($fields)
  107. ->group('o.type, o.flag, create_month')
  108. ->select();
  109. $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list);
  110. return [
  111. 'code' => 0,
  112. 'data' => [
  113. 'total' => $total,
  114. 'rows' => $channel_data,
  115. ],
  116. ];
  117. } catch (\Exception $e) {
  118. Log::log($e->getMessage() . $e->getFile() . $e->getLine());
  119. return $default;
  120. }
  121. }
  122. public function getDayReport($admin_id, $date_range, $offset, $limit, $business_line = PayConstants::BUSINESS_WECHAT)
  123. {
  124. $default = [
  125. 'code' => 1,
  126. 'data' => [
  127. 'total' => 0,
  128. 'rows' => [],
  129. ],
  130. ];
  131. try {
  132. $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id);
  133. if (!$channel_ids) {
  134. return $default;
  135. }
  136. $fields = [
  137. 'createdate',
  138. 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money',
  139. 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money',
  140. 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count',
  141. 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders',
  142. 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money',
  143. 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders',
  144. 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit',
  145. 'o.type',
  146. 'o.flag',
  147. ];
  148. $total = $this->getRechargeOptions($date_range, $fields)
  149. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  150. ->where('o.business_line', $business_line)
  151. ->whereIn('o.admin_id', $channel_ids)
  152. ->group('o.type, o.flag, createdate')
  153. ->count();
  154. $all_list = $this->getRechargeOptions($date_range, $fields)
  155. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  156. ->where('o.business_line', $business_line)
  157. ->whereIn('o.admin_id', $channel_ids)
  158. ->field($fields)
  159. ->group('o.type, o.flag, createdate')
  160. ->limit($offset, $limit)
  161. ->select();
  162. $current_page_createdate = [];
  163. foreach ($all_list as $channel_order) {
  164. $current_page_createdate[] = $channel_order['createdate'];
  165. }
  166. $agent_list = $this->getRechargeOptions($date_range, $fields)
  167. ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH)
  168. ->where('o.business_line', $business_line)
  169. ->whereIn('o.createdate', $current_page_createdate)
  170. ->whereIn('o.admin_id', $channel_ids)
  171. ->order(['createdate' => 'asc'])
  172. ->field($fields)
  173. ->group('o.type, o.flag, createdate')
  174. ->select();
  175. $channel_list = $this->getRechargeOptions($date_range, $fields)
  176. ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF)
  177. ->where('o.business_line', $business_line)
  178. ->whereIn('o.createdate', $current_page_createdate)
  179. ->whereIn('o.admin_id', $channel_ids)
  180. ->order(['createdate' => 'asc'])
  181. ->field($fields)
  182. ->group('o.type, o.flag, createdate')
  183. ->select();
  184. $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list);
  185. return [
  186. 'code' => 0,
  187. 'data' => [
  188. 'total' => $total,
  189. 'rows' => $channel_data,
  190. ],
  191. ];
  192. } catch (\Exception $e) {
  193. Log::log($e->getMessage() . $e->getFile() . $e->getLine());
  194. return $default;
  195. }
  196. }
  197. /**
  198. * @param $admin_id
  199. * @param $date_range
  200. * @param $offset
  201. * @param $limit
  202. * @return array
  203. */
  204. public function getMonthServiceReport($admin_id, $date_range, $offset, $limit, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT)
  205. {
  206. $default = [
  207. 'code' => 1,
  208. 'data' => [
  209. 'total' => 0,
  210. 'rows' => [],
  211. ],
  212. ];
  213. try {
  214. $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id);
  215. if (!$channel_ids) {
  216. return $default;
  217. }
  218. $fields = [
  219. 'admin.username',
  220. 'admin.id',
  221. 'LEFT(createdate,6)' => 'create_month',
  222. 'IFNULL(SUM(o.recharge_money),0)' => 'recharge_money',
  223. 'IFNULL(SUM(o.normal_recharge_money),0)' => 'normal_recharge_money',
  224. 'IFNULL(SUM(o.vip_recharge_orders_count),0)' => 'vip_recharge_orders_count',
  225. 'IFNULL(SUM(o.normal_recharge_orders),0)' => 'normal_recharge_orders',
  226. 'IFNULL(SUM(o.vip_recharge_money),0)' => 'vip_recharge_money',
  227. 'IFNULL(SUM(o.vip_recharge_orders),0)' => 'vip_recharge_orders',
  228. 'IFNULL(SUM(o.recharge_money_benefit),0)' => 'recharge_money_benefit',
  229. 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname',
  230. 'o.type',
  231. 'o.flag',
  232. ];
  233. $total = $this->getRechargeOptions($date_range, $fields, false, $filter)
  234. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  235. ->where('o.business_line', $business_line)
  236. ->whereIn('o.admin_id', $channel_ids)
  237. ->group('admin.id, o.type, o.flag, create_month, wx_nickname')
  238. ->count();
  239. $all_list = $this->getRechargeOptions($date_range, $fields, false, $filter)
  240. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  241. ->where('o.business_line', $business_line)
  242. ->whereIn('o.admin_id', $channel_ids)
  243. ->order(['LEFT(createdate,6)' => 'asc'])
  244. ->field($fields)
  245. ->group('admin.id, o.type, o.flag, create_month, wx_nickname')
  246. ->limit($offset, $limit)
  247. ->select();
  248. $current_page_channel_ids = $current_page_createdate = [];
  249. foreach ($all_list as $channel_order) {
  250. $current_page_channel_ids[] = $channel_order['id'];
  251. $current_page_createdate[] = $channel_order['create_month'];
  252. }
  253. $agent_list = $this->getRechargeOptions($date_range, $fields, false, $filter)
  254. ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH)
  255. ->where('o.business_line', $business_line)
  256. ->whereIn('o.admin_id', $current_page_channel_ids)
  257. ->whereIn('LEFT(createdate,6)', $current_page_createdate)
  258. ->order(['create_month' => 'asc'])
  259. ->field($fields)
  260. ->group('admin.id, o.type, create_month, wx_nickname')
  261. ->select();
  262. $channel_list = $this->getRechargeOptions($date_range, $fields, false, $filter)
  263. ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF)
  264. ->where('o.business_line', $business_line)
  265. ->whereIn('o.admin_id', $current_page_channel_ids)
  266. ->whereIn('LEFT(createdate,6)', $current_page_createdate)
  267. ->order(['create_month' => 'asc'])
  268. ->field($fields)
  269. ->group('admin.id, o.type, create_month, wx_nickname')
  270. ->select();
  271. $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list);
  272. return [
  273. 'code' => 0,
  274. 'data' => [
  275. 'total' => $total,
  276. 'rows' => $channel_data,
  277. ],
  278. ];
  279. } catch (\Exception $e) {
  280. Log::log($e->getMessage());
  281. return $default;
  282. }
  283. }
  284. /**
  285. * @param $admin_id
  286. * @param $date_range
  287. * @param $offset
  288. * @param $limit
  289. * @return array
  290. */
  291. public function getDayServiceReport($admin_id, $date_range, $offset, $limit, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT)
  292. {
  293. $default = [
  294. 'code' => 1,
  295. 'data' => [
  296. 'total' => 0,
  297. 'rows' => [],
  298. ],
  299. ];
  300. try {
  301. $channel_ids = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id);
  302. if (!$channel_ids) {
  303. return $default;
  304. }
  305. $fields = [
  306. 'admin.username',
  307. 'admin.id',
  308. 'o.createdate',
  309. 'replace(JSON_EXTRACT(json,"$.authorizer_info.nick_name"),\'"\',\'\')' => 'wx_nickname',
  310. 'o.recharge_money',
  311. 'o.normal_recharge_orders',
  312. 'o.vip_recharge_orders',
  313. 'o.normal_recharge_money',
  314. 'o.vip_recharge_money',
  315. 'o.recharge_money',
  316. 'o.recharge_money_benefit',
  317. 'o.flag',
  318. 'o.type',
  319. 'o.createdate',
  320. ];
  321. $total = $this->getRechargeOptions($date_range, $fields, true, $filter )
  322. ->whereIn('o.admin_id', $channel_ids)
  323. ->where('o.business_line', $business_line)
  324. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)->count();
  325. $all_list = $this->getRechargeOptions($date_range, $fields, true, $filter)
  326. ->where('o.flag', Order::ORDER_COLLECT_FLAG_ALL)
  327. ->where('o.business_line', $business_line)
  328. ->whereIn('o.admin_id', $channel_ids)
  329. ->order(['o.createdate' => 'desc', 'recharge_money' => 'desc', 'o.admin_id' => 'asc'])
  330. ->limit($offset, $limit)
  331. ->select();
  332. $current_page_channel_ids = $current_page_createdate = [];
  333. foreach ($all_list as $channel_order) {
  334. $current_page_channel_ids[] = $channel_order['id'];
  335. $current_page_createdate[] = $channel_order['createdate'];
  336. }
  337. $agent_list = $this->getRechargeOptions($date_range, $fields, true, $filter)
  338. ->where('o.flag', Order::ORDER_COLLECT_FLAG_BRANCH)
  339. ->where('o.business_line', $business_line)
  340. ->whereIn('o.admin_id', $current_page_channel_ids)
  341. ->whereIn('o.createdate', $current_page_createdate)
  342. ->select();
  343. $channel_list = $this->getRechargeOptions($date_range, $fields,true, $filter)
  344. ->where('o.flag', Order::ORDER_COLLECT_FLAG_SELF)
  345. ->where('o.business_line', $business_line)
  346. ->whereIn('o.admin_id', $current_page_channel_ids)
  347. ->whereIn('o.createdate', $current_page_createdate)
  348. ->select();
  349. $channel_data = $this->processChannelResult($all_list, $channel_list, $agent_list);
  350. return [
  351. 'code' => 0,
  352. 'data' => [
  353. 'total' => $total,
  354. 'rows' => $channel_data,
  355. ],
  356. ];
  357. } catch (\Exception $e) {
  358. Log::alert($e->getMessage() . $e->getFile() . $e->getLine());
  359. return $default;
  360. }
  361. }
  362. /**
  363. * @param $item
  364. * @return string
  365. */
  366. public function processUniqueKey($item)
  367. {
  368. $item = $item->toArray();
  369. $return_keys = [];
  370. $keys = ['createdate', 'create_month', 'id'];
  371. foreach ($keys as $key) {
  372. if (array_key_exists($key, $item)) {
  373. $return_keys[] = $item[$key];
  374. }
  375. }
  376. return implode('-', $return_keys);
  377. }
  378. public function processChannelResult($all_list, $channel_list, $agent_list)
  379. {
  380. $result = [];
  381. if ($all_list) {
  382. $agent_data = [];
  383. //代理商
  384. foreach ($agent_list as $item) {
  385. $key = $this->processUniqueKey($item);
  386. $agent_data[$key] = [
  387. 'recharge_money' => $item['recharge_money'],
  388. 'recharge_money_benefit' => $item['recharge_money_benefit'],
  389. ];
  390. }
  391. $channel_data = [];
  392. //渠道商
  393. foreach ($channel_list as $item) {
  394. $key = $this->processUniqueKey($item);
  395. $channel_data[$key] = [
  396. 'recharge_money' => $item['recharge_money'],
  397. 'recharge_money_benefit' => $item['recharge_money_benefit'],
  398. ];
  399. }
  400. //统计结果
  401. foreach ($all_list as $item) {
  402. $key = $this->processUniqueKey($item);
  403. //self_recharge->渠道商本身的充值金额
  404. //self_recharge_benefit->渠道商的分成收益:渠道商本身的充值 * 渠道商分成比例1
  405. if(array_key_exists($key, $channel_data)){
  406. $item['self_recharge'] = $channel_data[$key]['recharge_money'];
  407. $item['self_recharge_benefit'] = $channel_data[$key]['recharge_money_benefit'];
  408. }else{
  409. $item['self_recharge'] = 0;
  410. $item['self_recharge_benefit'] = 0;
  411. }
  412. $item['total_benefit'] = $item['recharge_money_benefit'];
  413. if (array_key_exists($key, $agent_data)) {
  414. $item['total_benefit'] = $item['recharge_money_benefit'] - $agent_data[$key]['recharge_money_benefit'];
  415. $item['agent_benefit'] = $agent_data[$key]['recharge_money_benefit'];
  416. $item['agent_recharge'] = $agent_data[$key]['recharge_money'];
  417. } else {
  418. $item['agent_benefit'] = 0;
  419. $item['agent_recharge'] = 0;
  420. }
  421. $result[] = $item;
  422. }
  423. }
  424. return $result;
  425. }
  426. /**
  427. * @param $date_range
  428. * @param $fields
  429. * @param $day
  430. * @return Query
  431. */
  432. public function getRechargeOptions($date_range, $fields, $day = true, $filter=[], $business_line = PayConstants::BUSINESS_WECHAT)
  433. {
  434. $time = explode(',', $date_range);
  435. $where = [];
  436. if ( isset($filter['username']) && !empty($filter['username'])){
  437. $where['admin.username'] = $filter['username'];
  438. }
  439. if (!$day) {
  440. $time[0] .= '00';
  441. //获取前一天的日期格式
  442. if ($time[1] == date('Ym')) {
  443. $time[1] = $time[1] . str_pad(date('d') - 1, 2, '0', STR_PAD_LEFT);
  444. }else{
  445. $time[1] .= '31';
  446. }
  447. }
  448. $query = $this->getOrderCollectModel()
  449. ->alias('o')
  450. ->where('o.business_line', $business_line)
  451. ->join('admin', "o.admin_id=admin.id", "LEFT")
  452. ->join('admin_config', "admin.id=admin_config.admin_id", "LEFT")
  453. ->where("type", '=', Order::ORDER_COLLECT_TYPE_DAY)
  454. ->where($where)
  455. ->where("createdate>=$time[0]")
  456. ->where("createdate<=$time[1]")
  457. ->field($fields);
  458. return $query;
  459. }
  460. /**
  461. * @param $channelIds
  462. * @param $date
  463. * @return int|mixed
  464. */
  465. public function getVipDataRangeTime($channelIds, $date, $business_line = PayConstants::BUSINESS_WECHAT)
  466. {
  467. try{
  468. $result = $this->getOrderCollectModel()
  469. ->where('business_line', $business_line)
  470. ->field(['sum(recharge_money)' => 'recharge_money'])
  471. ->where(['type' => 1, 'flag' => 3, 'admin_id' => ['in', $channelIds], 'createdate' => $date])
  472. ->find();
  473. if($result){
  474. return $result['recharge_money'];
  475. }
  476. return 0;
  477. }catch (\Exception $e){
  478. Log::alert($e->getMessage() . $e->getFile() . $e->getLine());
  479. return 0;
  480. }
  481. }
  482. /**
  483. * 分账参数
  484. * @param $money
  485. * @param $channelId
  486. * @return \app\common\model\object\ReturnObject
  487. */
  488. public function getPalmPayCmd($money, $channelId)
  489. {
  490. if($money==0) return false;
  491. $dzAccount = $this->getDzAccount(get_host_no_port())->data;
  492. $default = ['dz_palmpay_sub_account_id'=>'','dz_amount'=>0,'channel_palmpay_sub_account_id'=>'','channel_amount'=>0];
  493. if ($dzAccount) {
  494. $cmd = [
  495. 'SFJOrdertype' => '1',
  496. 'remarktype' => 'JHS0100000',
  497. 'plantCode' => '4032',
  498. ];
  499. $channelAccount = $this->getChannelAccount($dzAccount['palmpay_company_id'], $channelId)->data;
  500. if ($channelAccount && $dzAccount['palmpay_company_id'] == $channelAccount['palmpay_company_id']) { //分账主体一致才分账
  501. $adminExtend = \app\main\service\AdminService::instance()->getAdminExtendModel()->getInfo($channelId);
  502. $rate = $adminExtend['benefit'];
  503. $channelMoney = number_format(floatval($rate) * floatval($money),2,'.','');
  504. $dzMoney = number_format(floatval($money) - $channelMoney,2,'.','');
  505. if($channelMoney > 0) {
  506. $default['channel_palmpay_sub_account_id'] = $channelAccount['sub_account_code'];
  507. $default['channel_amount'] = $channelMoney;
  508. $cmd['oderlist'][] = $this->getOrderItem($channelMoney, $channelAccount['sub_account_code'])->data;
  509. }
  510. if($dzMoney > 0) {
  511. $default['dz_palmpay_sub_account_id'] = $dzAccount['sub_account_code'];
  512. $default['dz_amount'] = $dzMoney;
  513. $cmd['oderlist'][] = $this->getOrderItem($dzMoney, $dzAccount['sub_account_code'])->data;
  514. }
  515. } else { //主体不一致,钱款打点众子账户
  516. $cmd['oderlist'][] = $this->getOrderItem($money, $dzAccount['sub_account_code'])->data;
  517. $default['dz_palmpay_sub_account_id'] = $dzAccount['sub_account_code'];
  518. $default['dz_amount'] = $money;
  519. }
  520. $json = json_encode([$cmd]);
  521. $return = urlencode($json);
  522. } else {
  523. $return = false;
  524. }
  525. return $this->setData(['cmd'=>$return,'default'=>$default])->getReturn();
  526. }
  527. /**
  528. * 获取分账单
  529. * @param $dzMoney
  530. * @param $SubAccNo
  531. * @return \app\common\model\object\ReturnObject
  532. */
  533. public function getOrderItem($dzMoney, $SubAccNo)
  534. {
  535. $tmp = [
  536. 'SubAccNo' => $SubAccNo,
  537. 'PayModel' => '1',
  538. 'TranFee' => '0.00',
  539. 'subamount' => (string)$dzMoney,
  540. 'suborderId' => $this->getUniqueOrder()->data,
  541. 'object' => '支付-' . $SubAccNo,
  542. ];
  543. return $this->setData($tmp)->getReturn();
  544. }
  545. /**
  546. * 获取分账订单唯一值
  547. * @return \app\common\model\object\ReturnObject
  548. */
  549. public function getUniqueOrder()
  550. {
  551. $data = substr(md5(substr_replace(uniqid() ,getmypid(),0,strlen(getmypid())). substr(microtime(),2,6) . uniqid()), 0, 22);
  552. return $this->setData($data)->getReturn();
  553. }
  554. /**
  555. * 获取点众账号子商户
  556. * @param $host
  557. * @return \app\common\model\object\ReturnObject
  558. */
  559. public function getDzAccount($host)
  560. {
  561. $dzAccount = OfficialAccountsService::instance()->getWxpayModel()
  562. ->alias('wx')
  563. ->join('palmpay_sub_account', 'palmpay_sub_account.id=wx.palmpay_sub_account_ids', 'LEFT')
  564. ->where('wx.pay_host', $host)
  565. ->whereNotNull('palmpay_sub_account.id')
  566. ->find();
  567. return $this->setData($dzAccount)->getReturn();
  568. }
  569. /**
  570. * 获取渠道账号子商户
  571. * @param $palmpay_company_id
  572. * @return \app\common\model\object\ReturnObject
  573. */
  574. public function getChannelAccount($palmpay_company_id, $channelId)
  575. {
  576. $adminConfig = \app\main\service\AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId);
  577. $channelAccount = false;
  578. if ($adminConfig && $adminConfig['palmpay_sub_account_ids']) {
  579. $subAccountMap = json_decode($adminConfig['palmpay_sub_account_ids'], true);
  580. $subAccountArr=array_column($subAccountMap,'palmpay_sub_account_id','company_id');
  581. if(array_key_exists($palmpay_company_id,$subAccountArr)){
  582. $subAccountId = $subAccountArr[$palmpay_company_id];
  583. $palmpaySubAccount = new PalmpaySubAccount();
  584. $channelAccount = $palmpaySubAccount->where('id', $subAccountId)->find();
  585. }
  586. }
  587. return $this->setData($channelAccount)->getReturn();
  588. }
  589. /**
  590. * 获取订单扣量账号
  591. * @param $groupId
  592. * @param $adminId
  593. * @param $channelId
  594. * @param $insertData
  595. * @param $isActivity
  596. * @return \app\common\model\object\ReturnObject
  597. * @throws \think\db\exception\DataNotFoundException
  598. * @throws \think\db\exception\ModelNotFoundException
  599. * @throws \think\exception\DbException
  600. */
  601. public function getKlAdminInfo($groupId, $adminId, $channelId, $insertData, $isActivity = false)
  602. {
  603. $orderDeduction = new OrderDeduction();
  604. $adminInfo = false;
  605. if ($channelId) {
  606. if ($rule_config = KlService::instance()->getKlRule($channelId)) {
  607. $adminInfo = AdminKlUpdateService::instance()->getKlAdmin($channelId, $insertData, $rule_config)->data;
  608. }else{
  609. $adminInfo = $orderDeduction->orderKL($adminId, $channelId, $insertData, $isActivity);
  610. }
  611. }
  612. //订单转移
  613. if ($groupId == 4 && !$adminInfo) {
  614. $adminInfo = $orderDeduction->orderTransfer($adminId, $channelId, $insertData, $isActivity);
  615. }
  616. if($adminInfo){
  617. $this->setData($adminInfo);
  618. }
  619. return $this->getReturn();
  620. }
  621. }