CommissionCron.php 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. <?php
  2. namespace app\controller\api\cron;
  3. use app\common\model\store\order\StoreOrder;
  4. use app\common\repositories\user\UserBillRepository;
  5. use app\common\model\user\UserCertification;
  6. use AdaPaySdk\Drawcash;
  7. use AdaPaySdk\PaymentConfirm;
  8. use AdaPaySdk\SettleAccount;
  9. use app\common\dao\store\order\StoreOrderDao;
  10. use app\common\repositories\merchant\order\OrderGzcRepository;
  11. use app\common\repositories\merchant\order\OrderMerchantRepository;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\common\repositories\store\order\StoreOrderStatusRepository;
  14. use app\common\repositories\user\UserRepository;
  15. use app\controller\api\Common;
  16. use app\controller\merchant\finance\Reconciliation;
  17. use app\traits\GongApiRequest;
  18. use app\traits\ShouxinyiApiRequest;
  19. use Carbon\Carbon;
  20. use Exception;
  21. use PHPMailer\PHPMailer\PHPMailer;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. use think\facade\Cache;
  26. use think\facade\Db;
  27. use think\facade\Log;
  28. use function GuzzleHttp\Psr7\str;
  29. use app\traits\HuiPay;
  30. class CommissionCron
  31. {
  32. /**
  33. * @var false|string
  34. */
  35. private $startMonth;
  36. /**
  37. * @var false|string
  38. */
  39. private $endMonth;
  40. /**
  41. * @var false|string
  42. */
  43. private $month;
  44. public function __construct()
  45. {
  46. // $this->startMonth = date('Y-m-01 00:00:00', strtotime('-1 month'));
  47. // $this->endMonth = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
  48. // $this->month = date('Y-m', strtotime('-1 month'));
  49. $this->startMonth = date('Y-m-01 00:00:00');
  50. $this->endMonth = date("Y-m-d 23:59:59");
  51. $this->month = date('Y-m');
  52. }
  53. /**
  54. * 奖金
  55. */
  56. public function bonus()
  57. {
  58. //创客补贴
  59. $this->real_subsidy_money();
  60. // 跨店奖励
  61. $this->stride_shop_money();
  62. //推荐创客收益
  63. $this->invite_shop_money();
  64. //大V经理分享奖
  65. $this->big_vip_money();
  66. //大V升级奖
  67. $this->big_vip_up_money();
  68. echo '执行成功';
  69. }
  70. public function stride_shop_money()
  71. {
  72. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  73. $sql = "select stride_shop_mer_id,sum(stride_shop_money) stride_shop_money from rrx_store_order o
  74. left join rrx_store_order_extend od on od.order_id=o.order_id
  75. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.stride_shop_mer_id>0 and od.stride_shop_money>0
  76. group by od.stride_shop_mer_id";
  77. $mer_bonus_list = Db::query($sql);
  78. Db::name('log')->insert(['data' => "跨店奖励 mer_bonus_list:" . json_encode($mer_bonus_list)]);
  79. foreach ($mer_bonus_list as $mer_bonus) {
  80. $mer_id = $mer_bonus['stride_shop_mer_id'];
  81. $money = $mer_bonus['stride_shop_money'];
  82. if ($money < 0.01) {
  83. Db::name('log')->insert(['data' => "跨店奖励:mer_id:{$mer_id} 跨店奖励低于0.01无法入库 特此记录!"]);
  84. continue;
  85. }
  86. $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
  87. $mark = "跨店奖励" . $money;
  88. $title = "跨店奖励({$this->month}月)";
  89. $commission_type = 7;
  90. $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
  91. }
  92. }
  93. public function invite_shop_money()
  94. {
  95. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  96. $sql = "select od.share_uid,sum(invite_shop_money) invite_shop_money from rrx_store_order o
  97. left join rrx_store_order_extend od on od.order_id=o.order_id
  98. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.share_uid>0 and od.invite_shop_money>0 group by od.share_uid";
  99. $user_bonus_list = Db::query($sql);
  100. Db::name('log')->insert(['data' => "推荐创客奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
  101. foreach ($user_bonus_list as $user_bonus) {
  102. $bonus_uid = $user_bonus['share_shop_uid'];
  103. $money = $user_bonus['invite_shop_money'];
  104. if ($money < 0.01) {
  105. Db::name('log')->insert(['data' => "推荐创客:bonus_uid:{$bonus_uid} 推荐创客低于0.01无法入库 特此记录!"]);
  106. continue;
  107. }
  108. $mark = "推荐创客收益" . $money;
  109. $title = "推荐创客收益({$this->month}月)";
  110. $commission_type = 10;
  111. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  112. }
  113. }
  114. public function big_vip_money()
  115. {
  116. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  117. $sql = "select od.share_uid,sum(big_vip_money) big_vip_money from rrx_store_order o
  118. left join rrx_store_order_extend od on od.order_id=o.order_id
  119. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.share_uid>0 and od.big_vip_money>0 group by od.share_uid";
  120. $user_bonus_list = Db::query($sql);
  121. Db::name('log')->insert(['data' => "大v奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
  122. foreach ($user_bonus_list as $user_bonus) {
  123. $bonus_uid = $user_bonus['share_uid'];
  124. $money = $user_bonus['big_vip_money'];
  125. if ($money < 0.01) {
  126. Db::name('log')->insert(['data' => "大v分享奖:bonus_uid:{$bonus_uid} 大v分享奖低于0.01无法入库 特此记录!"]);
  127. continue;
  128. }
  129. $mark = "大V经理分享奖" . $money;
  130. $title = "大V经理分享奖({$this->month})";
  131. $commission_type = 16;
  132. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  133. }
  134. }
  135. public function real_subsidy_money()
  136. {
  137. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  138. $sql = "select o.mer_id mer_id,sum(real_subsidy_money) real_subsidy_money from rrx_store_order o
  139. left join rrx_store_order_extend od on od.order_id=o.order_id
  140. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.real_subsidy_money>0
  141. group by o.mer_id";
  142. $mer_bonus_list = Db::query($sql);
  143. Db::name('log')->insert(['data' => "创客补贴 mer_bonus_list:" . json_encode($mer_bonus_list)]);
  144. foreach ($mer_bonus_list as $mer_bonus) {
  145. $mer_id = $mer_bonus['mer_id'];
  146. $money = $mer_bonus['real_subsidy_money'];
  147. if ($money < 0.01) {
  148. Db::name('log')->insert(['data' => "创客补贴:mer_id:{$mer_id} 创客补贴低于0.01无法入库 特此记录!"]);
  149. continue;
  150. }
  151. $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
  152. $mark = "创客补贴" . $money;
  153. $title = "创客补贴({$this->month}月)";
  154. $commission_type = 19;
  155. $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
  156. }
  157. }
  158. /**
  159. * 大V升级奖
  160. * 1. 查询大V经理列表
  161. * 2. 查询团队列表
  162. * 3. 查询团队这个月的总流水的百分之一分给大V经理
  163. * @return void
  164. */
  165. public function big_vip_up_money()
  166. {
  167. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  168. $user_list = Db::name("user")->field('uid')->where("user_group", 3)->select()->toArray();
  169. foreach ($user_list as $user) {
  170. $mer_id = 0;
  171. $user_id = $user['uid'];
  172. $sql = "select getUserSpreadId($user_id) as uids";
  173. $spread_user_list = Db::query($sql);
  174. $spread_user_list = trim($spread_user_list[0]['uids'],'$,');
  175. //查询可以分到的钱
  176. $sql = "select sum(big_vip_up_money) big_vip_up_money from rrx_store_order o
  177. left join rrx_store_order_extend od on od.order_id=o.order_id
  178. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and o.uid in ($spread_user_list)";
  179. $big_vip_up = Db::query($sql)[0];
  180. $money = $big_vip_up['big_vip_up_money'] ?? 0;
  181. Db::name('log')->insert(['data' => "大V升级奖 big_vip_up_money {$user_id}:{$money}"]);
  182. if ($money < 0.01) {
  183. Db::name('log')->insert(['data' => "大v分享奖:big_vip_up_money:{$money} 大v分享奖低于0.01无法入库 特此记录!"]);
  184. continue;
  185. }
  186. $mark = "大V升级奖" . $money;
  187. $title = "大V升级奖({$this->month}月)";
  188. $commission_type = 10;
  189. $StoreOrderRepository->add_bill_gongfu($title, $money, $user_id, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  190. }
  191. }
  192. /**
  193. * 用户升级
  194. * @throws DbException
  195. */
  196. public function member_up()
  197. {
  198. $uid_list = Db::name('store_order')
  199. ->where('mer_id', systemConfig('mer_365'))
  200. ->where('paid', 1)
  201. ->whereIn('status', [0, 1, 2, 3])
  202. ->group('uid')
  203. ->column('uid');
  204. Log::info('升级:' . json_encode($uid_list));
  205. foreach ($uid_list as $uid) {
  206. $pay_price = Db::name('store_order')
  207. ->where('mer_id', systemConfig('mer_365'))
  208. ->where('uid', $uid)
  209. ->where('paid', 1)
  210. ->where('is_settlement', 1)
  211. ->whereIn('status', [0, 1, 2, 3])
  212. ->sum('pay_price');
  213. if ($pay_price >= 365) {
  214. // 有没有直推20个经理
  215. $invite_count = Db::name('user')->where('spread_uid', $uid)->field('uid,user_group')->where('user_group', 2)->count();
  216. print_r("uid:{$uid}," . Db::name('user')->where('uid', $uid)->value('phone') . "邀请365经理人数{$invite_count} <br>");
  217. if ($invite_count >= 20) {
  218. $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [4, 5, 6])->update([
  219. 'user_group' => 3
  220. ]);
  221. if ($res) var_dump("uid:{$uid},升级大V经理");
  222. } else {
  223. $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [3, 4, 5, 6])->update([
  224. 'user_group' => 2
  225. ]);
  226. if ($res) var_dump("uid:{$uid},升级365会员");
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * 创客合伙人收益
  233. */
  234. private function partner_award()
  235. {
  236. //1. 被创客设置成创客合伙人,获得该项收益
  237. //已结算订单金额的
  238. //2%
  239. //1. 按月统计并结算
  240. //2. 计算公式:
  241. //所属创客店补贴*2%/创客合伙人总数
  242. // 推荐创客收益设置
  243. $commission_rate = systemConfig('commission_rate');
  244. Db::name('log')->insert(['data' => 'commission_rate:' . json_encode($commission_rate)]);
  245. $rate = $commission_rate['chuangkehehuoren_rate'];
  246. // 查询全部订单
  247. $sql = "select mer_id,sum(pay_price) pay_price,sum(total_price) total_price from rrx_store_order where paid=1 and status in (2,3) and is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' group by mer_id";
  248. $mer_order_list = Db::query($sql);
  249. Db::name('log')->insert(['data' => "创客合伙人收益 user_order_list:" . json_encode($mer_order_list)]);
  250. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  251. $mer_out_list = [];
  252. foreach ($mer_order_list as $mer_order) {
  253. $mer_id = $mer_order['mer_id'];
  254. $hehuo_user_list = Db::name('user')->field('uid')->where('mer_id', $mer_id)->where('is_shop_partner', 1)->select()->toArray();
  255. $money = decimal2($mer_order['total_price'] * $rate / 100);
  256. foreach ($hehuo_user_list as $hehuo_user) {
  257. $user_money = decimal2($money / count($hehuo_user_list));
  258. Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客合伙人人数:" . count($hehuo_user_list) . " 创客补贴比例{$rate}% 创客店铺订单流水:{$mer_order['total_price']} 获取创客推荐收益:{$money}!"]);
  259. if ($money == 0) {
  260. Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客补贴低于0.01无法入库 特此记录!"]);
  261. continue;
  262. }
  263. $mark = "创客合伙人收益" . $user_money;
  264. $title = "创客合伙人收益({$this->month}月)";
  265. $commission_type = 17;
  266. $StoreOrderRepository->add_bill_gongfu($title, $money, $hehuo_user['uid'], $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  267. }
  268. $money = decimal2($mer_order['total_price'] * $rate / 100);
  269. $mer_out_list[] = [
  270. $mer_order['mer_id'] => $money
  271. ];
  272. }
  273. Db::name('log')->insert(['data' => '跨店奖励 商户支出:' . json_encode($mer_out_list)]);
  274. return $mer_out_list;
  275. }
  276. /**
  277. * 代理区域收益
  278. */
  279. public function area_manage_details()
  280. {
  281. //1. 代理区域内已结算订单总额的百分
  282. //比;
  283. //2. 按照收货地址统计;
  284. //3. 奖励对象:省代、区县代;
  285. //区县代 5%
  286. //省代 2%
  287. //1. 按月结算:每月 1 号 0 点
  288. //统计上月已结算的订单金额
  289. //并对应发放佣金
  290. //2. 计算公式:
  291. //(1)无运费:收货地为该区域的已结算订单金额*对应比例
  292. //(2)有运费:(收货地为该区域的已结算订单金额-运费)*对应比例
  293. set_time_limit(0);
  294. ini_set('memory_limit', '-1');
  295. $commission_rate = systemConfig('commission_rate');
  296. Db::name('log')->insert(['data' => '代理区域收益 commission_rate:' . json_encode($commission_rate)]);
  297. $area_agency_rate = $commission_rate['area_agency_rate'] ?? 0;
  298. $province_agency_rate = $commission_rate['province_agency_rate'] ?? 0;
  299. $bonus_list = [];
  300. $bonus_user_list = Db::name('user')->field('uid,user_group,manage_address,spread_uid')->whereIn('user_group', [5, 6])->select()->toArray();
  301. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  302. foreach ($bonus_user_list as $bonus_user) {
  303. $manage_address = $bonus_user['manage_address'];
  304. if (empty($manage_address))
  305. continue;
  306. $sql = "select sum(pay_price) pay_price,sum(total_price) total_price from rrx_store_order where paid=1 and status in (2,3) and is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and user_address_ids like '$manage_address%'";
  307. $total_price = Db::query($sql)[0]['total_price'] ?? 0;
  308. $rate = $bonus_user['user_group'] == 6 ? $province_agency_rate : $area_agency_rate;
  309. $money = decimal2($total_price * $rate / 100);
  310. Db::name('log')->insert(['data' => "代理区域收益:uid:{$bonus_user['uid']} 代理区域收益比例{$rate}% 流水:{$total_price} 收益:{$money} sql:" . $sql]);
  311. $mark = "代理区域收益" . $money;
  312. $title = "代理区域收益({$this->month}月)";
  313. $commission_type = 12;
  314. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_user['uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month);
  315. $bonus_list[] = [
  316. 'uid' => $bonus_user['uid'],
  317. 'money' => $money,
  318. 'spread_uid' => $bonus_user['spread_uid'],
  319. ];
  320. }
  321. $this->invite_agency_award($bonus_list);
  322. echo '执行成功';
  323. }
  324. /**
  325. * 推荐代理收益
  326. * @param $bonus_list
  327. */
  328. private function invite_agency_award($bonus_list)
  329. {
  330. // 1. 推荐的代理收益的百分比
  331. //2. 奖励对象:所有用户
  332. //1. 按月结算:每个月 1 号 0点统计上月的代理收益并对应发放佣金
  333. //2. 计算公式:
  334. //推荐代理总收益*推荐代理收益百分比
  335. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  336. $this->month = date('Y-m', strtotime('-1 month'));
  337. $commission_rate = systemConfig('commission_rate');
  338. $rate = $commission_rate['tuijiandaili_rate'] ?? 0;
  339. foreach ($bonus_list as $bonus) {
  340. $money = decimal2($bonus['money'] * $rate / 100);
  341. $mark = "推荐代理收益" . $money;
  342. $title = "推荐代理收益({$this->month}月)";
  343. $commission_type = 15;
  344. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus['spread_uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month, $this->month);
  345. }
  346. }
  347. /**
  348. * 生成商品图片缓存(最好用命令行执行)
  349. * @return \think\response\Json
  350. * @throws DataNotFoundException
  351. * @throws DbException
  352. * @throws ModelNotFoundException
  353. */
  354. public function make_product_share_cache()
  355. {
  356. set_time_limit(0);
  357. ini_set('memory_limit', '-1');
  358. $redis = Cache::store('redis')->handler();
  359. $key = 'make_product_share_cache';
  360. // $redis->del($key);
  361. // if (!$redis->setnx($key, 1))
  362. // return json(['code' => 400, 'msg' => '现在有正在执行的任务' ]);
  363. $redis->expire($key, 5 * 60);
  364. $product_share_ids = Db::name('product_share')
  365. // ->where('`url` is not null')
  366. ->group('product_id')
  367. ->column('product_id');
  368. $list = Db::name('store_product')
  369. ->whereNotIn('product_id', $product_share_ids)
  370. ->where('is_show', 1)
  371. ->limit(3)
  372. ->order('create_time desc')
  373. ->select()
  374. ->toArray();
  375. var_dump($list);
  376. $header = [
  377. 'X-Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0LmJqdGRiYS5jb20iLCJhdWQiOiJ0ZXN0LmJqdGRiYS5jb20iLCJpYXQiOjE2OTI0MjQ4MTEsIm5iZiI6MTY5MjQyNDgxMSwiZXhwIjoxNzAzMjI0ODExLCJqdGkiOls5NzIwLCJ1c2VyIl19.YtYf1ssMTz6fOO2rZVX-jb48L7AqXSKezQcbTHR3sCk'
  378. ];
  379. foreach ($list as $store_product) {
  380. $res1 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=1", null, 'get', $header);
  381. $res2 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=2", null, 'get', $header);
  382. var_dump($res1, $res2);
  383. }
  384. $redis->del($key);
  385. return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
  386. }
  387. /**
  388. * 梳理每个人的公排人数
  389. * @throws \think\db\exception\DataNotFoundException
  390. * @throws \think\db\exception\DbException
  391. * @throws \think\db\exception\ModelNotFoundException
  392. */
  393. public function tease_user_count()
  394. {
  395. $list = Db::name('user')->where('is_del', 0)->select()->toArray();
  396. foreach ($list as $item) {
  397. $level_number = Db::name('user')->where('level_id', $item['uid'])->count();
  398. $user_count = Db::name('user_count')->where('uid', $item['uid'])->find();
  399. if ($level_number >= 5) {
  400. $res = Db::name('user_count')->where('uid', $item['uid'])->delete();
  401. if ($res) echo "{$item['uid']}公排人数已删除\r\n";
  402. continue;
  403. }
  404. if ($user_count) {
  405. if ($level_number == $user_count['level_number']) continue;
  406. Db::name('user_count')->where('uid', $item['uid'])->update([
  407. 'level_number' => $level_number
  408. ]);
  409. } else {
  410. Db::name('user_count')->insert([
  411. 'uid' => $item['uid'],
  412. 'level_number' => 0
  413. ]);
  414. $user_count['level_number'] = 0;
  415. }
  416. echo "{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n";
  417. Log::info("{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n");
  418. }
  419. return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
  420. }
  421. /**
  422. * 下架微唯宝已下架的商品
  423. * @return mixed
  424. * @throws \think\db\exception\DbException
  425. */
  426. public function downGongProduct()
  427. {
  428. $start = input('start');
  429. $where = [
  430. 'is_used' => 1,
  431. 'mer_status' => 1,
  432. // 'type' => 5,
  433. // 'is_show' => 1,
  434. 'is_hide' => 0,
  435. // 'is_del' => 0
  436. ];
  437. if (!$start) {
  438. $product_data_count = Db::name('store_product')
  439. ->where($where)
  440. ->where("spu_id != '' or spu_id is not null")
  441. ->count();
  442. $start = mt_rand(0, $product_data_count);
  443. }
  444. $product_data = Db::name('store_product')
  445. ->where($where)
  446. ->where("spu_id != '' or spu_id is not null")
  447. ->where('product_id', '>', $start)
  448. ->order('product_id asc')
  449. ->limit(0, 50)
  450. ->select()
  451. ->toArray();
  452. foreach ($product_data as $item) {
  453. $spuIds = $item['spu_id'];
  454. $goods_status = GongApiRequest::goods_status($spuIds);
  455. $status = $goods_status[0]['status'] ?? 1;
  456. $goods_info = GongApiRequest::get_goods_info($spuIds);
  457. if ($status == 0 || (isset($goods_info['error_msg']) && $goods_info['error_msg'] == '没有查询到商品信息')) {
  458. Db::startTrans();
  459. try {
  460. // 删除选品库对应的数据
  461. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  462. 'status' => 9,
  463. 'update_time' => date('Y-m-d H:i:s')
  464. ]);
  465. // 删除商品管理对应的数据
  466. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  467. 'is_del' => 1,
  468. 'is_show' => 0
  469. ]);
  470. Db::commit();
  471. } catch (Exception $e) {
  472. Db::rollback();
  473. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  474. return app('json')->fail($e->getMessage());
  475. }
  476. } else if ($status == 1 || !isset($goods_info['error_msg'])) {
  477. Db::startTrans();
  478. try {
  479. // 选品库对应的数据
  480. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  481. 'status' => 1,
  482. 'update_time' => date('Y-m-d H:i:s')
  483. ]);
  484. // 商品管理对应的数据
  485. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  486. 'is_del' => 0,
  487. 'is_show' => 1
  488. ]);
  489. Db::commit();
  490. } catch (Exception $e) {
  491. Db::rollback();
  492. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  493. return app('json')->fail($e->getMessage());
  494. }
  495. }
  496. }
  497. return app('json')->success('操作成功');
  498. }
  499. /**
  500. * 定时获取 交易类型 1订单入账 2佣金提现支出 3养老金(红积分兑换订单)
  501. */
  502. public function platform_income_detail_get()
  503. {
  504. $merchant_list = Db::name('merchant')
  505. ->where('status', 1)
  506. ->where('mer_state', 1)
  507. ->where("mer_name != ''")
  508. ->column('mer_id');
  509. $Reconciliation = app()->make(Reconciliation::class);
  510. $unique_list = [];
  511. $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
  512. ->select()
  513. ->toArray();
  514. foreach ($enterprise_platform_income_detail_list as $item) {
  515. $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
  516. }
  517. foreach ($merchant_list as $mer_id) {
  518. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  519. $is_first = empty($enterprise_platform_income_detail_list);
  520. // 1自营订单入账金额=已结算订单金额-运费-养老金-汇付通道服务费; 订单结算的时候才统计
  521. $store_order_list = Db::name('store_order')
  522. ->where('mer_id', $mer_id)
  523. ->where('paid', 1)
  524. ->where('douhuomall', 0)
  525. ->where('is_settlement', 1)
  526. ->order('order_id', 'desc')
  527. // ->limit(500)
  528. ->select()
  529. ->toArray();
  530. foreach ($store_order_list as $store_order) {
  531. if (in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  532. $pay_type = $store_order['pay_type'];
  533. if (!in_array($pay_type, [0, 1])) continue;
  534. $group_order = Db::name('store_group_order')
  535. ->alias('sgo')
  536. ->where('sgo.group_order_id', $store_order['group_order_id'])
  537. ->find();
  538. if (empty($group_order)) continue;
  539. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  540. 'shop_bonus_rate' => 30,
  541. 'yanglao_rate' => 5,
  542. ];
  543. $yanglao = Db::name('user_bill')
  544. ->where('order_sn', $store_order['order_sn'])
  545. ->where('commission_type', 5)
  546. ->value('number');
  547. $ru_money = 0;
  548. if ($pay_type != 0) {
  549. $store_split_order = Db::name('store_split_order')
  550. ->where('group_order_id', $group_order['group_order_sn'])
  551. ->where('status', 1)
  552. ->find();
  553. if (empty($store_split_order)) continue;
  554. if ($pay_type == 1) {
  555. //微信
  556. if ($group_order['pay_wx'] == 3) {
  557. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  558. } else {
  559. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  560. }
  561. } else {
  562. //支付宝
  563. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  564. }
  565. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  566. $params['api_key'] = $api_key;
  567. //查询支付确认对象
  568. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  569. $res = $this->payment_confirm_query_traits($params);
  570. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  571. // 获取分账后一个工作日时间
  572. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  573. if (isset($res['status'])) {
  574. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  575. 'hf_fee_amt' => $res['fee_amt'],
  576. 'hf_clear_time' => $close_time
  577. ]);
  578. }
  579. if (isset($res['status']) && time() > strtotime($close_time)) {
  580. if ($store_order['mer_id'] == 496) {
  581. $ru_money = $store_order['pay_price'] - $yanglao - $res['fee_amt'];
  582. } else {
  583. $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) - $res['fee_amt'] + +$store_order['pay_postage'];
  584. }
  585. $store_order['pay_time'] = '';//线上支付的
  586. } else {
  587. continue;
  588. }
  589. } else {
  590. // 订单金额+运费
  591. if ($store_order['mer_id'] == 496) {
  592. $ru_money = $store_order['pay_price'] - $yanglao;
  593. } else {
  594. $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) + $store_order['pay_postage'];
  595. }
  596. }
  597. Db::name('enterprise_platform_income_detail')
  598. ->insert([
  599. 'mer_id' => $mer_id,
  600. 'uid' => $store_order['uid'],
  601. 'title' => '自营订单入账',
  602. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  603. 'deal_time' => isset($close_time) ? $close_time : ((isset($store_order['pay_time']) && $store_order['pay_time']) ? $store_order['pay_time'] : date('Y-m-d H:i:s')),
  604. 'money' => $ru_money,
  605. 'deal_type' => 1,
  606. 'order_id' => $store_order['order_id'],
  607. 'order_sn' => $store_order['order_sn'],
  608. 'user_extra_id' => '',
  609. 'pm' => 1,
  610. 'create_time' => date('Y-m-d H:i:s'),
  611. ]);
  612. unset($close_time);
  613. }
  614. //2佣金提现到账 提现成功统计
  615. // $user_extract_list = Db::name('user_extract')
  616. // ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
  617. // ->where('status', 1)
  618. // ->where('withdrawal_type', 3)
  619. // ->where('extract_type', '<>', 4)
  620. // ->when($uids, function ($query) use ($uids) {
  621. // $query->whereIn('uid', $uids);
  622. // })
  623. // ->order('extract_id', 'desc')
  624. //// ->limit(100)
  625. // ->select()
  626. // ->toArray();
  627. // foreach ($user_extract_list as $user_extract) {
  628. // if (in_array('2' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'], $unique_list)) continue;
  629. // $enterprise_platform_income_detail = Db::name('enterprise_platform_income_detail')
  630. // ->where('deal_type', 2)
  631. // ->where('user_extra_id', $user_extract['extract_id'])
  632. // ->find();
  633. // if (!$enterprise_platform_income_detail) {
  634. // Db::name('enterprise_platform_income_detail')
  635. // ->insert([
  636. // 'mer_id' => $mer_id,
  637. // 'uid' => $user_extract['uid'],
  638. // 'title' => '佣金提现支出',
  639. // 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  640. // 'deal_time' => $user_extract['status_time'],
  641. // 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
  642. // 'deal_type' => 2,
  643. // 'order_id' => '',
  644. // 'order_sn' => $user_extract['order_sn'],
  645. // 'user_extra_id' => $user_extract['extract_id'],
  646. // 'pm' => 1,
  647. // 'create_time' => date('Y-m-d H:i:s'),
  648. // ]);
  649. // }
  650. // }
  651. $start_time = '2024-04-01';
  652. for ($i = 0; $i < 1000; $i++) {
  653. $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
  654. if (strtotime($day) > time()) {
  655. break;
  656. }
  657. $Reconciliation->cron($day, $mer_id);
  658. }
  659. }
  660. // 计算平台
  661. //交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利
  662. $mer_id = 0;
  663. // 3 订单支付 供应链订单结算后入账到平台的金额
  664. $store_order_list = Db::name('store_order')
  665. ->where('paid', 1)
  666. ->order('order_id', 'desc')
  667. // ->limit(500)
  668. ->select()
  669. ->toArray();
  670. foreach ($store_order_list as $store_order) {
  671. if (in_array('3' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  672. $group_order = Db::name('store_group_order')
  673. ->alias('sgo')
  674. ->where('sgo.group_order_id', $store_order['group_order_id'])
  675. ->find();
  676. if (empty($group_order)) continue;
  677. $store_split_order = Db::name('store_split_order')
  678. ->where('group_order_id', $group_order['group_order_sn'])
  679. ->where('status', 1)
  680. ->find();
  681. if (empty($store_split_order)) continue;
  682. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  683. Db::name('enterprise_platform_income_detail')
  684. ->insert([
  685. 'mer_id' => $mer_id,
  686. 'uid' => $store_order['uid'],
  687. 'title' => '订单支付',
  688. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
  689. 'deal_time' => isset($close_time) ? $close_time : ((isset($store_order['pay_time']) && $store_order['pay_time']) ? $store_order['pay_time'] : date('Y-m-d H:i:s')),
  690. 'money' => $store_order['pay_price'] + $store_order['pay_postage'],
  691. 'deal_type' => 3,
  692. 'order_id' => $store_order['order_id'],
  693. 'order_sn' => $store_order['order_sn'],
  694. 'user_extra_id' => '',
  695. 'pm' => 1,
  696. 'create_time' => date('Y-m-d H:i:s'),
  697. ]);
  698. }
  699. //5用户佣金提现
  700. $user_extract_list = Db::name('user_extract')
  701. ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
  702. ->where('status', 1)
  703. ->order('extract_id', 'desc')
  704. // ->limit(100)
  705. ->select()
  706. ->toArray();
  707. foreach ($user_extract_list as $user_extract) {
  708. if (in_array('5' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'] . $mer_id, $unique_list)) continue;
  709. Db::name('enterprise_platform_income_detail')
  710. ->insert([
  711. 'mer_id' => $mer_id,
  712. 'uid' => $user_extract['uid'],
  713. 'title' => '用户佣金提现',
  714. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  715. 'deal_time' => $user_extract['status_time'],
  716. 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
  717. 'deal_type' => 5,
  718. 'order_id' => '',
  719. 'order_sn' => $user_extract['order_sn'],
  720. 'user_extra_id' => $user_extract['extract_id'],
  721. 'pm' => 0,
  722. 'create_time' => date('Y-m-d H:i:s'),
  723. ]);
  724. }
  725. // 6订单入账:支付成功的订单数据 供应链订单
  726. $store_order_list = Db::name('store_order')
  727. ->where('paid', 1)
  728. ->where('douhuomall', 1)
  729. ->where('is_settlement', 1)
  730. ->order('order_id', 'desc')
  731. // ->limit(500)
  732. ->select()
  733. ->toArray();
  734. foreach ($store_order_list as $store_order) {
  735. if (in_array('6' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  736. $pay_type = $store_order['pay_type'];
  737. if (!in_array($pay_type, [0, 1])) continue;
  738. $group_order = Db::name('store_group_order')
  739. ->alias('sgo')
  740. ->where('sgo.group_order_id', $store_order['group_order_id'])
  741. ->find();
  742. if (empty($group_order)) continue;
  743. $yanglao = Db::name('user_bill')
  744. ->where('order_sn', $store_order['order_sn'])
  745. ->where('commission_type', 5)
  746. ->value('number');
  747. $ru_money = 0;
  748. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  749. 'shop_bonus_rate' => 30,
  750. 'yanglao_rate' => 5,
  751. ];
  752. if ($pay_type != 0) {
  753. $store_split_order = Db::name('store_split_order')
  754. ->where('group_order_id', $group_order['group_order_sn'])
  755. ->find();
  756. if (empty($store_split_order)) continue;
  757. if (!$store_order['hf_fee_amt']) {
  758. if ($pay_type == 1) {
  759. //微信
  760. if ($group_order['pay_wx'] == 3) {
  761. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  762. } else {
  763. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  764. }
  765. } else {
  766. //支付宝
  767. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  768. }
  769. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  770. $params['api_key'] = $api_key;
  771. //查询支付确认对象
  772. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  773. $res = $this->payment_confirm_query_traits($params);
  774. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  775. if (isset($res['status'])) {
  776. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  777. 'hf_fee_amt' => $res['fee_amt'],
  778. 'hf_clear_time' => $close_time
  779. ]);
  780. $store_order['hf_fee_amt'] = $res['fee_amt'];
  781. }
  782. }
  783. // 获取分账后一个工作日时间
  784. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  785. if (isset($res['status']) && time() > strtotime($close_time)) {
  786. if ($store_order['mer_id'] == 496) {
  787. $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
  788. } else {
  789. $ru_money = $store_order['pay_price'] - $yanglao;
  790. }
  791. $store_order['pay_time'] = '';//线上支付的
  792. } else {
  793. continue;
  794. }
  795. } else {
  796. // 订单金额+运费
  797. if ($store_order['mer_id'] == 496) {
  798. $ru_money = $store_order['pay_price'] - $yanglao;
  799. } else {
  800. $ru_money = $store_order['pay_price'] - $yanglao;
  801. }
  802. }
  803. Db::name('enterprise_platform_income_detail')
  804. ->insert([
  805. 'mer_id' => $mer_id,
  806. 'uid' => $store_order['uid'],
  807. 'title' => '订单入账',
  808. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  809. 'deal_time' => isset($close_time) ? $close_time : ((isset($store_order['pay_time']) && $store_order['pay_time']) ? $store_order['pay_time'] : date('Y-m-d H:i:s')),
  810. 'money' => $ru_money,
  811. 'deal_type' => 6,
  812. 'order_id' => $store_order['order_id'],
  813. 'order_sn' => $store_order['order_sn'],
  814. 'user_extra_id' => '',
  815. 'pm' => 1,
  816. 'create_time' => date('Y-m-d H:i:s'),
  817. ]);
  818. unset($close_time);
  819. }
  820. //7 创客自营订单让利=创客自营订单金额*分润比例-(创客自营订单金额-运费)*养老金比例
  821. $store_order_list = Db::name('store_order')
  822. ->where('paid', 1)
  823. ->where('is_settlement', 1)
  824. ->where('douhuomall', 0)
  825. ->order('order_id', 'desc')
  826. // ->limit(500)
  827. ->select()
  828. ->toArray();
  829. foreach ($store_order_list as $store_order) {
  830. if (in_array('7' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  831. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  832. 'shop_bonus_rate' => 30,
  833. 'yanglao_rate' => 5,
  834. ];
  835. $pay_type = $store_order['pay_type'];
  836. if ($pay_type != 0) {
  837. $group_order = Db::name('store_group_order')
  838. ->alias('sgo')
  839. ->where('sgo.group_order_id', $store_order['group_order_id'])
  840. ->find();
  841. if (empty($group_order)) continue;
  842. $store_split_order = Db::name('store_split_order')
  843. ->where('group_order_id', $group_order['group_order_sn'])
  844. ->find();
  845. if (empty($store_split_order)) continue;
  846. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  847. if (!$store_order['hf_fee_amt']) {
  848. $store_split_order = Db::name('store_split_order')
  849. ->where('group_order_id', $group_order['group_order_sn'])
  850. ->where('status', 1)
  851. ->find();
  852. if (empty($store_split_order)) continue;
  853. if ($pay_type == 1) {
  854. //微信
  855. if ($group_order['pay_wx'] == 3) {
  856. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  857. } else {
  858. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  859. }
  860. } else {
  861. //支付宝
  862. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  863. }
  864. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  865. $params['api_key'] = $api_key;
  866. //查询支付确认对象
  867. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  868. $res = $this->payment_confirm_query_traits($params);
  869. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  870. // 获取分账后一个工作日时间
  871. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  872. if (isset($res['status'])) {
  873. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  874. 'hf_fee_amt' => $res['fee_amt'],
  875. 'hf_clear_time' => $close_time
  876. ]);
  877. $store_order['hf_fee_amt'] = $res['fee_amt'];
  878. }
  879. }
  880. $yanglao = Db::name('user_bill')
  881. ->where('order_sn', $store_order['order_sn'])
  882. ->where('commission_type', 5)
  883. ->value('number');
  884. if (isset($res['status']) && time() > strtotime($close_time)) {
  885. if ($store_order['mer_id'] == 496) {
  886. $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
  887. } else {
  888. $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
  889. }
  890. $store_order['pay_time'] = '';//线上支付的
  891. } else {
  892. continue;
  893. }
  894. }else{
  895. if ($store_order['mer_id'] == 496) {
  896. $ru_money = $store_order['pay_price'] - $yanglao;
  897. } else {
  898. $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
  899. }
  900. }
  901. Db::name('enterprise_platform_income_detail')
  902. ->insert([
  903. 'mer_id' => $mer_id,
  904. 'uid' => $store_order['uid'],
  905. 'title' => '创客自营订单让利',
  906. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
  907. 'deal_time' => isset($close_time) ? $close_time : ((isset($store_order['pay_time']) && $store_order['pay_time']) ? $store_order['pay_time'] : date('Y-m-d H:i:s')),
  908. 'money' => $ru_money,
  909. 'deal_type' => 7,
  910. 'order_id' => $store_order['order_id'],
  911. 'order_sn' => $store_order['order_sn'],
  912. 'user_extra_id' => '',
  913. 'pm' => 1,
  914. 'create_time' => date('Y-m-d H:i:s'),
  915. ]);
  916. unset($close_time);
  917. }
  918. $start_time = '2024-04-01';
  919. for ($i = 0; $i < 1000; $i++) {
  920. $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
  921. if (strtotime($day) > time()) {
  922. break;
  923. }
  924. $Reconciliation->cron($day, 0);
  925. }
  926. return app('json')->success('获取平台收支明细成功');
  927. }
  928. public function clear_platform_income_detail()
  929. {
  930. Db::name('enterprise_platform_income_detail')->where('id', '>', 0)
  931. ->delete();
  932. }
  933. function getNextWorkday($date)
  934. {
  935. // 设置工作日数组
  936. $workdays = array(1, 2, 3, 4, 5); // 默认为周一到周五
  937. // 将日期字符串转换为时间戳
  938. $timestamp = strtotime($date);
  939. // 获取该日期的星期几
  940. $weekday = date('N', $timestamp);
  941. // 如果不是工作日,则增加一天直到是工作日
  942. while (!in_array($weekday, $workdays)) {
  943. $timestamp += (24 * 3600); // 增加一天
  944. $weekday = date('N', $timestamp); // 更新星期几
  945. }
  946. // 返回下一个工作日的日期字符串
  947. return date('Y-m-d H:i:s', $timestamp);
  948. }
  949. /**
  950. * @param $paymnet_params
  951. * @return array
  952. * 查询支付确认对象
  953. */
  954. public
  955. function payment_confirm_query_traits($paymnet_params)
  956. {
  957. $merber = new PaymentConfirm();
  958. $merber->query($paymnet_params);
  959. # 返回查询结果
  960. $merber->isError();
  961. return $merber->result;
  962. }
  963. //服务器配置,一般是默认配置
  964. private $CharSet = "UTF-8"; //设定邮件编码
  965. private $SMTPDebug = 0; // 调试模式输出
  966. private $isSMTP = true; // 使用SMTP
  967. private $Host = 'smtp.163.com'; // SMTP服务器
  968. private $SMTPAuth = true; // 允许 SMTP 认证
  969. private $Username = 'username'; // SMTP 用户名 即邮箱的用户名
  970. private $Password = 'MHIKZOXVPTGNVOMU'; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
  971. private $SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议
  972. private $Port = 465; // 服务器端口 25 或者465 具体要看邮箱服务器支持
  973. //修改成自己的邮箱
  974. private $From = 'formalverification@163.com'; //发件人邮箱
  975. private $FromName = 'makalo'; //发件人的用户昵称
  976. //邮件对象
  977. private $mail;
  978. //一般只有用户名和密码还有端口会变动, 所以这三个参数可以创建对象动态指定,也可以直接写死
  979. public function mallInit($Username = '', $Password = '', $Port = '')
  980. {
  981. if (!empty($Username)) {
  982. $this->Username = $Username;
  983. }
  984. if (!empty($Password)) {
  985. $this->Password = $Password;
  986. }
  987. if (!empty($Port)) {
  988. $this->Port = $Port;
  989. }
  990. //为true表示启用异常
  991. $mail = new PHPMailer(true);
  992. //服务器配置
  993. $mail->CharSet = $this->CharSet; //设定邮件编码
  994. $mail->SMTPDebug = $this->SMTPDebug; // 调试模121212 1式输出
  995. if ($this->isSMTP === true) {
  996. $mail->isSMTP(); // 使用SMTP
  997. }
  998. $mail->Host = $this->Host; // SMTP服务器
  999. $mail->SMTPAuth = $this->SMTPAuth; // 允许 SMTP 认证
  1000. $mail->Username = $this->Username; // SMTP 用户名 即邮箱的用户名
  1001. $mail->Password = $this->Password; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
  1002. $mail->SMTPSecure = $this->SMTPSecure; // 允许 TLS 或者ssl协议
  1003. $mail->Port = $this->Port; // 服务器端口 25 或者465 具体要看邮箱服务器支持
  1004. $mail->setFrom($this->From, $this->FromName); //发件人
  1005. $this->mail = $mail;
  1006. }
  1007. /**
  1008. * 发送邮件
  1009. * @param array $sendAddress 收件人地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1010. * @param array $Attachment 附件数组,如果为单值数组则为添加附件,如果为键值数组则添加附件并且重命名
  1011. * @param bool $isHtml 是否以html文档格式发送
  1012. * @param string $Subject 邮件主题
  1013. * @param string $Body 邮件内容,如果以html格式可以写入html
  1014. * @param string $AltBody 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1015. * @param array $CC 抄送地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1016. * @param array $BCC 密送地址数组,如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1017. * @return bool 是否发送成功
  1018. */
  1019. public function sendEMail($sendAddress = [], $Subject = '', $Body = '', $AltBody = '', $Attachment = [], $isHtml = true, $CC = [], $BCC = [])
  1020. {
  1021. try {
  1022. //必填参数校验
  1023. if (empty($sendAddress) || empty($Subject) || empty($Body)) {
  1024. return false;
  1025. }
  1026. //收件人
  1027. foreach ($sendAddress as $key => $value) {
  1028. if (is_int($key)) {
  1029. // 处理单值
  1030. $this->mail->addAddress($value);
  1031. } else {
  1032. // 处理键值
  1033. $this->mail->addAddress($value, $key);
  1034. }
  1035. }
  1036. //附件
  1037. if (!empty($Attachment)) {
  1038. foreach ($Attachment as $key => $value) {
  1039. if (is_int($key)) {
  1040. // 处理单值
  1041. $this->mail->addAttachment($value);
  1042. } else {
  1043. // 处理键值
  1044. $this->mail->addAttachment($value, $key);
  1045. }
  1046. }
  1047. }
  1048. //抄送
  1049. if (!empty($CC)) {
  1050. //抄送不为空
  1051. foreach ($CC as $key => $value) {
  1052. if (is_int($key)) {
  1053. // 处理单值
  1054. $this->mail->addCC($value);
  1055. } else {
  1056. // 处理键值
  1057. $this->mail->addCC($value, $key);
  1058. }
  1059. }
  1060. }
  1061. //密送
  1062. if (!empty($BCC)) {
  1063. //密送不为空
  1064. foreach ($BCC as $key => $value) {
  1065. if (is_int($key)) {
  1066. // 处理单值
  1067. $this->mail->addBCC($value);
  1068. } else {
  1069. // 处理键值
  1070. $this->mail->addBCC($value, $key);
  1071. }
  1072. }
  1073. }
  1074. //回复的时候回复给哪个邮箱 建议和发件人一致
  1075. $this->mail->addReplyTo($this->From, $this->FromName);
  1076. //Content
  1077. // 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
  1078. $this->mail->isHTML($isHtml);
  1079. //邮件主题
  1080. $this->mail->Subject = $Subject;
  1081. //邮件内容
  1082. $this->mail->Body = $Body;
  1083. //如果邮件客户端不支持HTML则显示此内容
  1084. $this->mail->AltBody = $AltBody;
  1085. //发送
  1086. $this->mail->send();
  1087. return [
  1088. 'code' => 200,
  1089. ];
  1090. } catch (\Exception $e) {
  1091. return [
  1092. 'code' => 400,
  1093. 'message' => $this->mail->ErrorInfo
  1094. ];
  1095. }
  1096. }
  1097. public function take()
  1098. {
  1099. set_time_limit(0);
  1100. $this->mallInit('formalverification@163.com', 'SYECAWFLLTUPMDRP');
  1101. $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "开始执行" . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
  1102. $mallStr = '';
  1103. // Log::info('执行一次1 '.date('H:i') );
  1104. // return ;
  1105. // $storeOrderRepository = app()->make(StoreOrderRepository::class);
  1106. $key_list = [
  1107. 1 => 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9',
  1108. 2 => 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f',
  1109. 3 => 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'
  1110. ];
  1111. $appid_list = [
  1112. 1 => 'app_44108546-d27a-48ee-88c1-84b45b75114f',
  1113. 2 => 'app_0827b960-b932-45ec-b800-4e850091b419',
  1114. 3 => 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0'
  1115. ];
  1116. //
  1117. // $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
  1118. // $insertData = [
  1119. // [
  1120. // 'mid' => 0,
  1121. // 'requestId' => $payment_id,
  1122. // 'group_order_id' => 'WX171955791230553863',
  1123. // 'splitAmount' => 0.25,
  1124. // 'status' => 0
  1125. // ],
  1126. // ];
  1127. // $div_members[] = [
  1128. // 'member_id' => 'member_personal_16704641278422',
  1129. // 'amount' => '1.00',
  1130. // 'fee_flag' => 'Y'
  1131. // ];
  1132. // //////分账表
  1133. // Db::name("store_split_order")->insertAll($insertData);
  1134. // /// 分账表end
  1135. //
  1136. // $params = [
  1137. // 'payment_id' => '002212024062814583210652657343702200320',
  1138. // 'order_no' => $payment_id,
  1139. // 'confirm_amt' => '1.00',
  1140. // 'div_members' => $div_members
  1141. // ];
  1142. //
  1143. // $params['api_key'] = $key_list[3];
  1144. //
  1145. // var_dump($params);
  1146. // Db::name("log")->insert(array('data' => '汇付延时分账参数-----' . json_encode($params)));//日志记录
  1147. // $res = $storeOrderRepository->payment_confirm_create_traits($params);
  1148. // Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res)));//日志记录
  1149. // var_dump($res);
  1150. // // 分账
  1151. // return;
  1152. $merchant_member_list = Db::name('merchant_member')
  1153. // ->where('member_id', 'member_personal_16704641278422')
  1154. ->column('mer_id,type,member_id,settle_account_id');
  1155. foreach ($merchant_member_list as $merchant_member) {
  1156. $mer_id = $merchant_member['mer_id'];
  1157. if (empty($mer_id))
  1158. continue;
  1159. if (empty($merchant_member['settle_account_id']))
  1160. continue;
  1161. $params = [
  1162. 'app_id' => $appid_list[$merchant_member['type']],
  1163. 'member_id' => $merchant_member['member_id'],
  1164. 'settle_account_id' => $merchant_member['settle_account_id'],
  1165. 'api_key' => $key_list[$merchant_member['type']]
  1166. ];
  1167. $merber = new SettleAccount();
  1168. $merber->balance($params);
  1169. $merber->result;
  1170. // var_dump($merber->result);
  1171. if (isset($merber->result[1])) {
  1172. $result = json_decode($merber->result[1], true);
  1173. $result = json_decode($result['data'], true);
  1174. $result['avl_balance'] = $result['avl_balance'] ?? 0;
  1175. if ($result['avl_balance'] > 0) {
  1176. $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
  1177. $params = [
  1178. 'order_no' => $payment_id,
  1179. 'app_id' => $appid_list[$merchant_member['type']],
  1180. 'cash_type' => 'T1',
  1181. 'cash_amt' => sprintf("%01.2f", $result['avl_balance']),
  1182. 'member_id' => $merchant_member['member_id'],
  1183. 'api_key' => $key_list[$merchant_member['type']]
  1184. ];
  1185. $yanglao_member_ids = ['jsdk_member_wx21710232183139', 'jsdk_member_ali1710232187326'];
  1186. if (in_array($merchant_member['member_id'], $yanglao_member_ids)) {
  1187. $params['remark'] = '天地博爱科技~养老金';
  1188. }
  1189. Db::name("log")->insert(array('data' => '汇付取现参数-----' . json_encode($params)));//日志记录
  1190. $merber = new Drawcash();
  1191. $merber->create($params);
  1192. # 返回查询结果
  1193. $merber->isError();
  1194. $res = $merber->result;
  1195. Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res))); //日志记录
  1196. Db::name("log")->insert(array('data' => "mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----"));//日志记录
  1197. var_dump("mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----");
  1198. $mallStr = $mallStr . "-----mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----<br/>";
  1199. }
  1200. }
  1201. }
  1202. echo date('Y-m-d H:i:s') . '执行结束' . "\r\n";
  1203. $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "执行结束<br/>" . $mallStr . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
  1204. }
  1205. use HuiPay;
  1206. public function cesexx(){
  1207. // $store_product = Db::name('store_product')->where('mer_id', 439)->select();
  1208. // foreach ($store_product as $key=>$online) {
  1209. // $spu_id = $online['spu_id'];
  1210. // $product_id = $online['product_id'];
  1211. // $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
  1212. // if(!$data) continue;
  1213. // //商品主图
  1214. // $spuId_info=json_decode($data['spuId_info'],true);
  1215. // $slider_image=$spuId_info['detail_img_list']??'';
  1216. // $image=$spuId_info['cover_url']??'';
  1217. // try {
  1218. // $slider_image = implode(',',json_decode($slider_image,true));
  1219. // }catch (\Exception $exception){
  1220. // $slider_image = $image;
  1221. // }
  1222. // //更新产品图片数据
  1223. // $product_insert_data=[
  1224. // 'image'=>$image,
  1225. // 'slider_image'=>$slider_image,
  1226. // ];
  1227. // Db::name('store_product')->where('product_id',$product_id)->update($product_insert_data);
  1228. // echo '更新产品图片数据成功'.json_encode($product_insert_data);
  1229. // echo '产品id:'.$product_id;
  1230. // $detail = $spuId_info['detail'];
  1231. // if(!is_null(json_decode($detail))){
  1232. // $detail_list = json_decode($detail);
  1233. // $detail = '';
  1234. // foreach($detail_list as $value){
  1235. // $detail .= "<img src='".$value."' referrerpolicy='no-referrer' /></img>";
  1236. // }
  1237. // }
  1238. // Db::name('store_product_content')->where('product_id',$product_id)->update(['content'=>$detail]);
  1239. // }
  1240. // $cursor = Db::name('store_order')->where('mer_id', 439)->select();
  1241. // foreach ($cursor as $key=>$online) {
  1242. // // if($key != 0) continue;
  1243. // $order_id = $online['order_id'];
  1244. // $orderProduct = Db::name('store_order')->alias('so')->where('so.order_id',$order_id)->leftJoin('store_order_product sop','so.order_id=sop.order_id')->field('sop.*')->select()->toArray();
  1245. // foreach ($orderProduct as $_k=>&$_vv){
  1246. // $_vv['cart_info'] = json_decode($_vv['cart_info'],true);
  1247. // if(isset($_vv['cart_info']['product']['product_id'])){
  1248. // $image = Db::name('store_product')->where('product_id', $_vv['cart_info']['product']['product_id'])->value('image');
  1249. // if($image){
  1250. // $_vv['cart_info']['product']['image'] = $image;
  1251. // }
  1252. // }
  1253. // Db::name('store_order_product')->where('order_id', $order_id)->update(['cart_info'=> json_encode($_vv['cart_info'], true)]);
  1254. // }
  1255. // }
  1256. // 普通订单
  1257. // $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('gzc', 0)->where('mer_id', 496)->where('pay_type', 'in', [0,1,2,4])->select();
  1258. // $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  1259. // $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  1260. // $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  1261. // foreach ($cursor as $online) {
  1262. // if ($online['mer_id'] == 496) {
  1263. // if (!in_array($online['status'], [0, 1, 2, 3])) {
  1264. // continue;
  1265. // }
  1266. // }
  1267. // $certification = UserCertification::getInstance()->where('uid', $online['uid'])->find();
  1268. // if (!$certification) {
  1269. // Log::info($online['uid'] . '没有实名认证');
  1270. // // echo '没有实名认证';
  1271. // continue;
  1272. // }
  1273. // //查询养老金金额
  1274. // $UserBillRepository = app()->make(UserBillRepository::class);
  1275. // $bill = $UserBillRepository->getWhere(['uid' => $online['uid'], 'link_id' => $online['order_id'], 'commission_type' => 5]);
  1276. // $pension = $bill->number ?? 0;
  1277. // if ($pension <= 0) {
  1278. // echo '金额为为0:'.$online['order_sn'];
  1279. // Log::info($online['order_sn'] . 'pension' . $pension);
  1280. // continue;
  1281. // }
  1282. // Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository) {
  1283. // //新增入账明细记录
  1284. // $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  1285. // $create = [
  1286. // 'uid' => $online['uid'],
  1287. // 'platform_no' => $orderId,
  1288. // 'account_type' => $online['pay_type'] == 1 ? 3 : 2,
  1289. // 'user_name' => $certification['user_name'],
  1290. // 'mobile' => $certification['mobile'],
  1291. // 'user_card' => $certification['user_card'],
  1292. // 'pension' => $pension,
  1293. // 'order_type' => 2,
  1294. // 'out_trade_no' => $online['order_sn']
  1295. // ];
  1296. // $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
  1297. // $check = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
  1298. // if ($test_user == 0 && $check <= 0) {
  1299. // $OrderGzcRepository->create($create);
  1300. // }
  1301. // $order = StoreOrder::getInstance()->find($online['order_id']);
  1302. // $order->gzc = 1;
  1303. // $order->save();
  1304. // });
  1305. // $bill->gzc = 1;
  1306. // $bill->is_gzc = 1;
  1307. // $bill->save();
  1308. // }
  1309. //18995126502
  1310. //会员直推荐佣金补1800,复购金补200,贡献值补4900,积分补4900,养老金补350
  1311. // $user = Db::name("user")->where('uid', 910)->find();
  1312. // $order_sn = 0;
  1313. // $order_id = 0;
  1314. // $order["mer_id"] = 0;
  1315. // //sat存养老金
  1316. // $tuiguanyanglaojin = '350';
  1317. // $tuimark = "推荐消费获得养老金" . $tuiguanyanglaojin;
  1318. // $tuititle = "养老金";
  1319. // $commission_type = 5;
  1320. // if ($tuiguanyanglaojin >= 0.01) {
  1321. // $this->add_bill($tuititle, $tuiguanyanglaojin, $user["uid"], $tuimark, $commission_type, $order_sn, $order_id, $order["mer_id"], 0, 0, 0);
  1322. // } else {
  1323. // Db::name('log')->insert(['data' => '订单号:' . $order_sn . '付款额度较低,推荐养老金低于0.01无法入库 特此记录!']);
  1324. // }
  1325. // $tuiguanyongjin = '1800';
  1326. // $tuiYongmark = "推荐消费获得佣金" . $tuiguanyongjin;
  1327. // $tuiYongzhoutitle = "推广佣金";
  1328. // $commission_yong_type = 3;
  1329. // if ($tuiguanyongjin >= 0.01) {
  1330. // $this->add_bill($tuiYongzhoutitle, $tuiguanyongjin, $user["uid"], $tuiYongmark, $commission_yong_type, $order_sn, $order_id, $order["mer_id"], 0, 0, 0);
  1331. // } else {
  1332. // Db::name('log')->insert(['data' => '订单号:' . $order_sn . '付款额度较低或 商家、平台设置的返佣比例较低,佣金低于0.01无法入库 特此记录!']);
  1333. // }
  1334. // $merchant_member_list = Db::name('order_merchant')
  1335. // ->where('status', 0)->where('paid', 0)->where('create_time', '>', '2025-01-01 00:00:00')
  1336. // ->column('id,mer_id,uid,out_trade_no,hf_pay_id,create_time');
  1337. // $out_trade_no_list = [];
  1338. // foreach ($merchant_member_list as $key => $value) {
  1339. // if (strpos($value['out_trade_no'], 'ALI') !== false) {
  1340. // //支付宝
  1341. // $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  1342. // } else {
  1343. // //微信
  1344. // $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  1345. // }
  1346. // $params = [
  1347. // 'api_key' => $api_key,
  1348. // 'payment_id' => $value['hf_pay_id'],
  1349. // ];
  1350. // $res = $this->Payment_query_traits($params);
  1351. // if (isset($res['status']) && $res['status'] == 'succeeded') {
  1352. // $params['out_trade_no'] = $value['out_trade_no'];
  1353. // // echo json_encode($params);
  1354. // $out_trade_no_list[] = $value['out_trade_no'];
  1355. // }
  1356. // echo json_encode($res);
  1357. // $params['out_trade_no'] = $value['out_trade_no'];
  1358. // echo json_encode($params);
  1359. // }
  1360. // echo json_encode($out_trade_no_list);
  1361. // $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  1362. // $params = [
  1363. // 'api_key' => $api_key,
  1364. // 'payment_id' => '002212025031014345810745060317006163968'
  1365. // ];
  1366. // $res = $this->Payment_query_traits($params);
  1367. // echo json_encode($res);
  1368. // $params['out_trade_no'] = $value['out_trade_no'];
  1369. // echo json_encode($params);
  1370. // echo json_encode($merchant_member_list);
  1371. // echo 'xxx';
  1372. }
  1373. /**
  1374. * 佣金加入账单
  1375. * @param $title
  1376. * @param $number
  1377. * @param $uid
  1378. * @param $mark
  1379. * @param $commission_type
  1380. * @param $order_sn
  1381. */
  1382. public function add_bill($title, $number, $uid, $mark, $commission_type, $order_sn, $order_id = 0, $mer_id = 0, $source = 0, $status = 1, $type_shop = 0)
  1383. {
  1384. log::info("====进入add——bill");
  1385. log::info("==uid==={$uid}");
  1386. if ($uid == 0 || $number == 0) {
  1387. return;
  1388. }
  1389. $number = sprintf('%.2f', $number);
  1390. $data = [
  1391. 'link_id' => $order_id,
  1392. "uid" => $uid,
  1393. "pm" => 1,
  1394. "title" => $title,
  1395. "category" => "now_money",
  1396. "type" => "commission",
  1397. "number" => $number,
  1398. "balance" => 0,
  1399. "mark" => $mark,
  1400. // "create_time"=>date("Y-m-d H:i:s"),
  1401. "status" => $status,
  1402. "commission_type" => $commission_type,
  1403. "order_sn" => $order_sn,
  1404. "mer_id" => $mer_id,
  1405. "source" => $source,
  1406. "type_shop" => $type_shop
  1407. ];
  1408. log::info("开始添加user——bills");
  1409. log::info($data);
  1410. Db::name("user_bill")->insert($data);
  1411. if ($type_shop == 0) {
  1412. if ($commission_type == 5) {
  1413. Db::name("user")->where("uid", $uid)->inc('annuity', $number)->update();
  1414. Db::name("user")->where("uid", $uid)->inc('annuity_num', 1)->update();
  1415. } else {
  1416. Db::name("user")->where("uid", $uid)->inc('brokerage_price', $number)->update();
  1417. }
  1418. }
  1419. }
  1420. //定时任务写日账单,月账单
  1421. public function cron($day,$mer_id=496)
  1422. {
  1423. Db::name('enterprise_day_bill')->where('mer_id', $mer_id)->where('day', $day)->delete();
  1424. $day_flow_query = Db::name('enterprise_platform_income_detail')
  1425. ->where('mer_id', $mer_id)
  1426. ->when($mer_id, function ($query) use ($mer_id) {
  1427. $query->where('deal_type', -1);//没有流水
  1428. }, function ($query) use ($mer_id) {
  1429. $query->whereIn('deal_type', [3]);
  1430. })
  1431. ->where('deal_type', 4)
  1432. ->where('pm', 1);
  1433. $day_flow_query_month = clone $day_flow_query;
  1434. // 日订单流水
  1435. $day_flow = $day_flow_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
  1436. $accounting_query = Db::name('enterprise_platform_income_detail')
  1437. ->where('mer_id', $mer_id)
  1438. ->when($mer_id, function ($query) use ($mer_id) {
  1439. $query->where('deal_type', 1);
  1440. }, function ($query) use ($mer_id) {
  1441. $query->whereIn('deal_type', [6,7]);
  1442. })
  1443. ->where('pm', 1);
  1444. $accounting_query_month = clone $accounting_query;
  1445. // 入账金额
  1446. $day_accounting = $accounting_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
  1447. $expenditure_query = Db::name('enterprise_platform_income_detail')
  1448. ->when($mer_id, function ($query) use ($mer_id) {
  1449. $query->where('mer_id', $mer_id)
  1450. ->where('deal_type', -1);//没有流水
  1451. }, function ($query) use ($mer_id) {
  1452. $query->whereIn('deal_type', [4,5]);
  1453. });
  1454. $expenditure_query_month = clone $expenditure_query;
  1455. // 日支出总额
  1456. $day_expenditure = 0;
  1457. if($mer_id==0){
  1458. $day_expenditure = $expenditure_query->where("DATE(deal_time) = '" . $day . "'")
  1459. ->whereIn('deal_type', [4,5])
  1460. ->where('mer_id',0)
  1461. ->sum('money');
  1462. }
  1463. $insert = [];
  1464. $insert['mer_id'] = $mer_id;
  1465. $insert['day'] = $day;
  1466. $insert['flow'] = $day_flow;
  1467. $insert['expenditure'] = $day_expenditure;
  1468. $insert['accounting'] = $day_accounting;
  1469. $previousDate = date('Y-m-d', strtotime($day . ' -1 day'));
  1470. $balance_zuotian = Db::name('enterprise_day_bill')->where('day', $previousDate)->where('mer_id', $mer_id)->value('balance');
  1471. $balance = $balance_zuotian ? $balance_zuotian : 0;
  1472. if($mer_id){
  1473. $tixian = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('extract_price');
  1474. $service_money = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('service_money');
  1475. $tixian -= $service_money;
  1476. $ylj = Db::name('user_bill')->where('mer_id', $mer_id)->where('status', 1)->where("DATE(take_time) = '" . $day . "'")->sum('number');
  1477. $insert['balance'] = $day_accounting - $tixian + $balance - $ylj;
  1478. }else{
  1479. $tixian = Db::name('enterprise_platform_income_detail')
  1480. ->where("deal_time >'$day'")
  1481. ->where("deal_time <'$day 23:59:59'")
  1482. ->where('deal_type', 5)
  1483. ->sum('money');
  1484. $insert['balance'] = $day_accounting - $tixian + $balance ;
  1485. }
  1486. Db::name('enterprise_day_bill')->insertGetId($insert);
  1487. $month_list[] = date('Y-m', strtotime('-4 month'));
  1488. $month_list[] = date('Y-m', strtotime('-3 month'));
  1489. $month_list[] = date('Y-m', strtotime('-2 month'));
  1490. $month_list[] = date('Y-m', strtotime('-1 month'));
  1491. $month_list[] = date('Y-m');
  1492. foreach ($month_list as $month) {
  1493. Db::name('enterprise_month_bill')->where('mer_id',$mer_id)->where('month', $month)->delete();
  1494. $flow_query_month_clone = clone $day_flow_query_month;
  1495. $accounting_query_month_clone = clone $accounting_query_month;
  1496. $expenditure_query_month_clone = clone $expenditure_query_month;
  1497. $startDate = date('Y-m-01 00:00:00', strtotime($month));
  1498. $endDate = date("Y-m-d 00:00:00", strtotime('+1 month', strtotime($month)));
  1499. // 月订单流水
  1500. $money_flow = $flow_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
  1501. // 月入账总额
  1502. $money_accounting = $accounting_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
  1503. // 月支出总额
  1504. $money_expenditure = $expenditure_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
  1505. $insert2 = [];
  1506. $insert2['month'] = $month;
  1507. $insert2['mer_id'] = $mer_id;
  1508. $insert2['flow'] = $money_flow;
  1509. $insert2['expenditure'] = $money_expenditure;
  1510. $insert2['accounting'] = $money_accounting;
  1511. Db::name('enterprise_month_bill')->insertGetId($insert2);
  1512. }
  1513. }
  1514. /**
  1515. * 生成平台收支明细,从而生成 日账单 月账单
  1516. * 平台收入:线上订单表、线下订单表、话费表
  1517. * 平台支出:用户提现、商户分账
  1518. * @return mixed
  1519. * @throws DataNotFoundException
  1520. * @throws DbException
  1521. * @throws ModelNotFoundException
  1522. */
  1523. public function platform_income_detail_get_new()
  1524. {
  1525. // 1、获取所有 商户 商户名称非空, 状态为正常、商户状态为开启
  1526. $merchant_list = Db::name('merchant')
  1527. ->where('status', 1)
  1528. ->where('mer_state', 1)
  1529. ->where("mer_name != ''")
  1530. ->column('mer_id');
  1531. // 2、获取 已有的 平台收支明细,并创建 唯一的 key
  1532. $unique_list = [];
  1533. // $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
  1534. // ->select()
  1535. // ->toArray();
  1536. // foreach ($enterprise_platform_income_detail_list as $item) {
  1537. // // deal_type : '交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利 '
  1538. // // user_extra_id : 提现表id
  1539. // $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
  1540. // }
  1541. // 3、获取订单信息
  1542. // 查询 该商户 所有的 平台订单(已支付、平台订单、已结算)
  1543. $store_order_list = Db::name('store_order')
  1544. ->whereIn('mer_id', $merchant_list)
  1545. ->where('paid', 1)
  1546. // ->where('douhuomall', 0)
  1547. ->where('is_settlement', 1)
  1548. ->order('order_id', 'desc')
  1549. ->select()
  1550. ->toArray();
  1551. $store_order_map = array_reduce($store_order_list, function($result, $order) {
  1552. // 根据 mer_id 进行分组
  1553. $result[$order['mer_id']][] = $order;
  1554. return $result;
  1555. }, []);
  1556. // 京豆
  1557. $jingdou_list_by_online = Db::name("user_sign_jingdou")
  1558. ->where('order_type', '=','0')
  1559. ->where('status', '=',1)
  1560. ->whereNotNull('settlement_time')
  1561. ->whereIn('order_id', array_column($store_order_list,'order_id'))
  1562. ->select()
  1563. ->toArray();
  1564. $jingdou_map_by_online = array_reduce($jingdou_list_by_online, function($result, $jingdou_by_online) {
  1565. // 根据 mer_id 进行分组
  1566. $result[$jingdou_by_online['order_id']][] = $jingdou_by_online;
  1567. return $result;
  1568. }, []);
  1569. // 线下扫码
  1570. $order_merchant_list = Db::name('order_merchant')
  1571. ->whereIn('mer_id', $merchant_list)
  1572. ->where('status', 1) // '0待支付,1已支付,2申请退款,3退款完成 5 已失效',
  1573. // ->where('arrive', 1) // 是否到账,系统应该是没对该字段进行维护 全是0
  1574. ->where('paid', 1)
  1575. ->where('binds', 0)
  1576. ->whereNotNull('pay_time')
  1577. ->order('id', 'desc')
  1578. ->select()
  1579. ->toArray();
  1580. $order_merchant_map = array_reduce($order_merchant_list, function($result, $order) {
  1581. // 根据 mer_id 进行分组
  1582. $result[$order['mer_id']][] = $order;
  1583. return $result;
  1584. }, []);
  1585. // 京豆
  1586. $jingdou_list_by_offline = Db::name("user_sign_jingdou")
  1587. ->where('order_type', '=','10')
  1588. ->where('status', '=',1)
  1589. ->whereNotNull('settlement_time')
  1590. ->whereIn('order_id', array_column($order_merchant_list,'id'))
  1591. ->select()
  1592. ->toArray();
  1593. $jingdou_map_by_offline = array_reduce($jingdou_list_by_offline, function($result, $jingdou_by_offline) {
  1594. // 根据 mer_id 进行分组
  1595. $result[$jingdou_by_offline['order_id']][] = $jingdou_by_offline;
  1596. return $result;
  1597. }, []);
  1598. // 话费
  1599. $order_huafei_list = Db::name('order_huafei')
  1600. ->where('status', 2) // '订单状态 0 待支付 1 已付 充值中 2充值成功 3充值失败 需要退款 4退款成功 5已超时 6待充值 7 已匹配 8已存单 9 已取消 10返销 11部分到账 12取消中'
  1601. // ->where('arrive', 1) // 是否到账,系统应该是没对该字段进行维护 全是0
  1602. // ->whereNotNull('finish_time')
  1603. ->order('order_huafei_id', 'desc')
  1604. ->select()
  1605. ->toArray();
  1606. // 清空数据表
  1607. Db::execute("TRUNCATE TABLE `rrx_enterprise_platform_income_detail`");
  1608. // 4、循环所有商户ID
  1609. /** @var OrderMerchantRepository $OrderMerchantRepository */
  1610. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  1611. foreach ($merchant_list as $mer_id) {
  1612. // 线上订单
  1613. if (!empty($store_order_map[$mer_id])) {
  1614. $online_data_list = [];
  1615. foreach ($store_order_map[$mer_id] as $store_order) {
  1616. // 判断该笔订单是否已 存在 平台收支明细表 的记录中
  1617. if (!in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) {
  1618. // 经过和领导确认,入账 金额为 订单金额 - 让利金额 - 手续费
  1619. $money = 0.00;
  1620. $vr = 0.00;
  1621. $jd = 0.00;
  1622. $fg = 0.00;
  1623. $fenrun = $store_order['con_pri'] ?: 0.00;
  1624. if (!empty($store_order['fzone_pay_type'])) {
  1625. $cost = bcsub($store_order['total_price'], $fenrun, 2);
  1626. if (in_array($store_order['fzone_pay_type'], ['a2', 'a3', 'b2', 'b4', 'b6'])) {
  1627. $money = $cost;
  1628. } else if (in_array($store_order['fzone_pay_type'], ['a1', 'b1', 'b5'])) {
  1629. $vr = $cost;
  1630. } else if (in_array($store_order['fzone_pay_type'], ['c1'])) {
  1631. $fg = $cost;
  1632. }
  1633. }
  1634. if (!empty($jingdou_map_by_online[$store_order['order_id']])) {
  1635. $jd = array_sum(array_column($jingdou_map_by_online[$store_order['order_id']], 'number'));
  1636. }
  1637. $online_data_list[] = [
  1638. 'mer_id' => $mer_id,
  1639. 'uid' => $store_order['uid'],
  1640. 'title' => '订单入账',
  1641. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  1642. 'deal_time' => $store_order['pay_time'] ?: date('Y-m-d H:i:s'),
  1643. 'money' => $money,
  1644. 'deal_type' => 1,
  1645. 'order_type' => 0,
  1646. 'order_id' => $store_order['order_id'],
  1647. 'order_sn' => $store_order['order_sn'],
  1648. 'user_extra_id' => '',
  1649. 'pm' => 1,
  1650. 'create_time' => date('Y-m-d H:i:s'),
  1651. 'vr' => $vr,
  1652. 'jingdou' => $jd,
  1653. 'fg' => $fg,
  1654. 'fenrun' => $fenrun, // 让利
  1655. ];
  1656. }
  1657. }
  1658. if (!empty($online_data_list)) {
  1659. Db::name('enterprise_platform_income_detail')
  1660. ->insertAll($online_data_list);
  1661. }
  1662. }
  1663. // 线下订单 0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 9话费 10 线下
  1664. if (!empty($order_merchant_map[$mer_id])) {
  1665. $offline_data_list = [];
  1666. foreach ($order_merchant_map[$mer_id] as $order_merchant) {
  1667. // 判断该笔订单是否已 存在 平台收支明细表 的记录中
  1668. if (!in_array('1' . $order_merchant['uid'] . $order_merchant['out_trade_no'] . $order_merchant['id'] . '0' . $mer_id, $unique_list)) {
  1669. // 经过和领导确认,入账 金额为 订单金额 - 让利金额 - 手续费
  1670. $money = $order_merchant['mer_money'] ?: 0.00;
  1671. $vr = 0.00;
  1672. $jd = 0.00;
  1673. $fg = 0.00;
  1674. $fenrun = $order_merchant['mer_fanyong'] ?: 0.00;
  1675. if (!empty($jingdou_map_by_offline[$order_merchant['id']])) {
  1676. $jd = array_sum(array_column($jingdou_map_by_offline[$order_merchant['id']], 'number'));
  1677. }
  1678. $offline_data_list[] = [
  1679. 'mer_id' => $mer_id,
  1680. 'uid' => $order_merchant['uid'],
  1681. 'title' => '订单入账',
  1682. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  1683. 'deal_time' => $order_merchant['pay_time'] ?: date('Y-m-d H:i:s'),
  1684. 'money' => $money,
  1685. 'deal_type' => 1,
  1686. 'order_type' => 10,
  1687. 'order_id' => $order_merchant['id'],
  1688. 'order_sn' => $order_merchant['out_trade_no'],
  1689. 'user_extra_id' => '',
  1690. 'pm' => 1,
  1691. 'create_time' => date('Y-m-d H:i:s'),
  1692. 'vr' => $vr,
  1693. 'jingdou' => $jd,
  1694. 'fg' => $fg,
  1695. 'fenrun' => $fenrun,
  1696. ];
  1697. }
  1698. }
  1699. if (!empty($offline_data_list)) {
  1700. Db::name('enterprise_platform_income_detail')
  1701. ->insertAll($offline_data_list);
  1702. }
  1703. }
  1704. }
  1705. $mer_id = 0;
  1706. $order_huafei_data_list = [];
  1707. foreach ($order_huafei_list as $order_huafei) {
  1708. if (!in_array('1' . $order_huafei['uid'] . $order_huafei['order_sn'] . $order_huafei['order_huafei_id'] . '0' . $mer_id, $unique_list)) {
  1709. $order_huafei_data_list[] = [
  1710. 'mer_id' => $mer_id,
  1711. 'uid' => $order_huafei['uid'],
  1712. 'title' => '订单入账',
  1713. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  1714. 'deal_time' => $order_huafei['finish_time'] ?: date('Y-m-d H:i:s'),
  1715. 'money' => $order_huafei['cost_price'],
  1716. 'deal_type' => 1,
  1717. 'order_type' => 9,
  1718. 'order_id' => $order_huafei['order_huafei_id'],
  1719. 'order_sn' => $order_huafei['order_sn'],
  1720. 'user_extra_id' => '',
  1721. 'pm' => 1,
  1722. 'create_time' => date('Y-m-d H:i:s'),
  1723. 'vr' => 0.00,
  1724. 'jingdou' => 0.00,
  1725. 'fg' => 0.00,
  1726. 'fenrun' => $order_huafei['fanli_price'] // 让利
  1727. ];
  1728. }
  1729. }
  1730. if (!empty($order_huafei_data_list)) {
  1731. Db::name('enterprise_platform_income_detail')
  1732. ->insertAll($order_huafei_data_list);
  1733. }
  1734. // 5用户佣金提现
  1735. $user_extract_list = Db::name('user_extract')
  1736. ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
  1737. ->where('status', 1)
  1738. ->order('extract_id', 'desc')
  1739. ->select()
  1740. ->toArray();
  1741. foreach ($user_extract_list as $user_extract) {
  1742. if (in_array('5' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'] . $mer_id, $unique_list)) continue;
  1743. Db::name('enterprise_platform_income_detail')
  1744. ->insert([
  1745. 'mer_id' => $mer_id,
  1746. 'uid' => $user_extract['uid'],
  1747. 'title' => '用户佣金提现',
  1748. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  1749. 'deal_time' => $user_extract['status_time'],
  1750. 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
  1751. 'deal_type' => 5,
  1752. 'order_id' => '',
  1753. 'order_sn' => $user_extract['order_sn'],
  1754. 'user_extra_id' => $user_extract['extract_id'],
  1755. 'pm' => 0,
  1756. 'create_time' => date('Y-m-d H:i:s'),
  1757. ]);
  1758. }
  1759. // // 查询分账信息
  1760. // $store_split_order = Db::name('store_split_order')
  1761. // ->where('status', 1)
  1762. // ->select()
  1763. // ->toArray();
  1764. // $store_split_order_map = array_column($store_split_order, null, 'group_order_id');
  1765. // 待完善
  1766. $this->set_enterprise_bill();
  1767. return app('json')->success('获取平台收支明细成功');
  1768. }
  1769. public function set_enterprise_bill()
  1770. {
  1771. // 1、查出 时间范围内的 平台收支明细
  1772. $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
  1773. // deal_time格式 2025-03-10 10:44:34
  1774. // ->whereBetween('deal_time', [$start_time, $end_time])
  1775. ->select()
  1776. ->toArray();
  1777. // 2、根据时间进行分组
  1778. $enterprise_platform_income_detail_map = array_reduce($enterprise_platform_income_detail_list, function ($result, $enterprise_platform_income_detail) {
  1779. // 根据 mer_id 进行分组
  1780. $result[date('Y-m', strtotime($enterprise_platform_income_detail['deal_time']))][date('Y-m-d', strtotime($enterprise_platform_income_detail['deal_time']))][] = $enterprise_platform_income_detail;
  1781. return $result;
  1782. }, []);
  1783. // 3、清空表数据
  1784. Db::execute("TRUNCATE TABLE `rrx_enterprise_day_bill`");
  1785. Db::execute("TRUNCATE TABLE `rrx_enterprise_month_bill`");
  1786. // 4、循环计算平台当天内的 收支流水
  1787. $month_bill_map = [];
  1788. $day_bill_map = [];
  1789. foreach ($enterprise_platform_income_detail_map as $month => $enterprise_platform_income_detail_list_by_month) {
  1790. // 平台的日账单
  1791. $month_bill_map[$month . '-0'] = [
  1792. 'mer_id' => 0,
  1793. 'month' => $month,
  1794. 'flow' => 0.00,
  1795. 'expenditure' => 0.00,
  1796. 'accounting' => 0.00,
  1797. 'balance' => 0.00,
  1798. 'jingdou' => 0.00,
  1799. 'fenrun' => 0.00,
  1800. 'vr' => 0.00,
  1801. 'fg' => 0.00
  1802. ];
  1803. foreach ($enterprise_platform_income_detail_list_by_month as $day => $enterprise_platform_income_detail_list_by_day) {
  1804. // 平台的日账单
  1805. $day_bill_map[$day . '-0'] = [
  1806. 'mer_id' => 0,
  1807. 'day' => $day,
  1808. 'flow' => 0.00,
  1809. 'expenditure' => 0.00,
  1810. 'accounting' => 0.00,
  1811. 'balance' => 0.00,
  1812. 'jingdou' => 0.00,
  1813. 'fenrun' => 0.00,
  1814. 'vr' => 0.00,
  1815. 'fg' => 0.00
  1816. ];
  1817. foreach ($enterprise_platform_income_detail_list_by_day as $enterprise_platform_income_detail) {
  1818. if (!isset($day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']])) {
  1819. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']] = [
  1820. 'mer_id' => $enterprise_platform_income_detail['mer_id'],
  1821. 'day' => $day,
  1822. 'flow' => 0.00,
  1823. 'expenditure' => 0.00,
  1824. 'accounting' => 0.00,
  1825. 'balance' => 0.00,
  1826. 'jingdou' => 0.00,
  1827. 'fenrun' => 0.00,
  1828. 'vr' => 0.00,
  1829. 'fg' => 0.00
  1830. ];
  1831. }
  1832. if (!isset($month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']])) {
  1833. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']] = [
  1834. 'mer_id' => $enterprise_platform_income_detail['mer_id'],
  1835. 'month' => $month,
  1836. 'flow' => 0.00,
  1837. 'expenditure' => 0.00,
  1838. 'accounting' => 0.00,
  1839. 'balance' => 0.00,
  1840. 'jingdou' => 0.00,
  1841. 'fenrun' => 0.00,
  1842. 'vr' => 0.00,
  1843. 'fg' => 0.00
  1844. ];
  1845. }
  1846. if($enterprise_platform_income_detail['pm'] == 1) {
  1847. // 订单总价
  1848. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['flow'] += ($enterprise_platform_income_detail['money'] + $enterprise_platform_income_detail['vr'] + $enterprise_platform_income_detail['fg'] + $enterprise_platform_income_detail['fenrun']);
  1849. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['flow'] += ($enterprise_platform_income_detail['money'] + $enterprise_platform_income_detail['vr'] + $enterprise_platform_income_detail['fg'] + $enterprise_platform_income_detail['fenrun']);
  1850. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1851. $day_bill_map[$day . '-0']['flow'] += ($enterprise_platform_income_detail['money'] + $enterprise_platform_income_detail['vr'] + $enterprise_platform_income_detail['fg'] + $enterprise_platform_income_detail['fenrun']);
  1852. $month_bill_map[$month . '-0']['flow'] += ($enterprise_platform_income_detail['money'] + $enterprise_platform_income_detail['vr'] + $enterprise_platform_income_detail['fg'] + $enterprise_platform_income_detail['fenrun']);
  1853. }
  1854. // 入账现金
  1855. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['accounting'] += $enterprise_platform_income_detail['money'];
  1856. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['accounting'] += $enterprise_platform_income_detail['money'];
  1857. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1858. $day_bill_map[$day . '-0']['accounting'] += $enterprise_platform_income_detail['money'];
  1859. $month_bill_map[$month . '-0']['accounting'] += $enterprise_platform_income_detail['money'];
  1860. }
  1861. // VR
  1862. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] += $enterprise_platform_income_detail['vr'];
  1863. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] += $enterprise_platform_income_detail['vr'];
  1864. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1865. $day_bill_map[$day . '-0']['vr'] += $enterprise_platform_income_detail['vr'];
  1866. $month_bill_map[$month . '-0']['vr'] += $enterprise_platform_income_detail['vr'];
  1867. }
  1868. // 复购
  1869. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] += $enterprise_platform_income_detail['fg'];
  1870. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] += $enterprise_platform_income_detail['fg'];
  1871. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1872. $day_bill_map[$day . '-0']['fg'] += $enterprise_platform_income_detail['fg'];
  1873. $month_bill_map[$month . '-0']['fg'] += $enterprise_platform_income_detail['fg'];
  1874. }
  1875. // 分润
  1876. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] += $enterprise_platform_income_detail['fenrun'];
  1877. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] += $enterprise_platform_income_detail['fenrun'];
  1878. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1879. $day_bill_map[$day . '-0']['fenrun'] += $enterprise_platform_income_detail['fenrun'];
  1880. $month_bill_map[$month . '-0']['fenrun'] += $enterprise_platform_income_detail['fenrun'];
  1881. }
  1882. // 获得的京豆
  1883. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] += $enterprise_platform_income_detail['jingdou'];
  1884. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] += $enterprise_platform_income_detail['jingdou'];
  1885. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1886. $day_bill_map[$day . '-0']['jingdou'] += $enterprise_platform_income_detail['jingdou'];
  1887. $month_bill_map[$month . '-0']['jingdou'] += $enterprise_platform_income_detail['jingdou'];
  1888. }
  1889. // 现金结余
  1890. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] += $enterprise_platform_income_detail['money'];
  1891. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] += $enterprise_platform_income_detail['money'];
  1892. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1893. $day_bill_map[$day . '-0']['balance'] += $enterprise_platform_income_detail['money'];
  1894. $month_bill_map[$month . '-0']['balance'] += $enterprise_platform_income_detail['money'];
  1895. }
  1896. }
  1897. if($enterprise_platform_income_detail['pm'] == 0) {
  1898. // 支出现金
  1899. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['expenditure'] += $enterprise_platform_income_detail['money'];
  1900. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['expenditure'] += $enterprise_platform_income_detail['money'];
  1901. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1902. $day_bill_map[$day . '-0']['expenditure'] += $enterprise_platform_income_detail['money'];
  1903. $month_bill_map[$month . '-0']['expenditure'] += $enterprise_platform_income_detail['money'];
  1904. }
  1905. // VR
  1906. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] -= $enterprise_platform_income_detail['vr'];
  1907. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] -= $enterprise_platform_income_detail['vr'];
  1908. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1909. $day_bill_map[$day . '-0']['vr'] -= $enterprise_platform_income_detail['vr'];
  1910. $month_bill_map[$month . '-0']['vr'] -= $enterprise_platform_income_detail['vr'];
  1911. }
  1912. // 复购
  1913. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] -= $enterprise_platform_income_detail['fg'];
  1914. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] -= $enterprise_platform_income_detail['fg'];
  1915. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1916. $day_bill_map[$day . '-0']['fg'] -= $enterprise_platform_income_detail['fg'];
  1917. $month_bill_map[$month . '-0']['fg'] -= $enterprise_platform_income_detail['fg'];
  1918. }
  1919. // 分润
  1920. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
  1921. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
  1922. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1923. $day_bill_map[$day . '-0']['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
  1924. $month_bill_map[$month . '-0']['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
  1925. }
  1926. // 获得的京豆
  1927. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
  1928. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
  1929. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1930. $day_bill_map[$day . '-0']['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
  1931. $month_bill_map[$month . '-0']['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
  1932. }
  1933. // 现金结余
  1934. $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] -= $enterprise_platform_income_detail['money'];
  1935. $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] -= $enterprise_platform_income_detail['money'];
  1936. if ($enterprise_platform_income_detail['mer_id'] != 0) {
  1937. $day_bill_map[$day . '-0']['balance'] -= $enterprise_platform_income_detail['money'];
  1938. $month_bill_map[$month . '-0']['balance'] -= $enterprise_platform_income_detail['money'];
  1939. }
  1940. }
  1941. }
  1942. }
  1943. }
  1944. // 5、写入数据
  1945. Db::name('enterprise_day_bill')->insertAll(array_values($day_bill_map));
  1946. Db::name('enterprise_month_bill')->insertAll(array_values($month_bill_map));
  1947. }
  1948. }