CommissionCron.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. <?php
  2. namespace app\controller\api\cron;
  3. use AdaPaySdk\Drawcash;
  4. use AdaPaySdk\PaymentConfirm;
  5. use AdaPaySdk\SettleAccount;
  6. use app\common\dao\store\order\StoreOrderDao;
  7. use app\common\repositories\merchant\order\OrderGzcRepository;
  8. use app\common\repositories\merchant\order\OrderMerchantRepository;
  9. use app\common\repositories\store\order\StoreOrderRepository;
  10. use app\common\repositories\store\order\StoreOrderStatusRepository;
  11. use app\common\repositories\user\UserRepository;
  12. use app\controller\api\Common;
  13. use app\controller\merchant\finance\Reconciliation;
  14. use app\traits\GongApiRequest;
  15. use app\traits\ShouxinyiApiRequest;
  16. use Carbon\Carbon;
  17. use Exception;
  18. use PHPMailer\PHPMailer\PHPMailer;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Cache;
  23. use think\facade\Db;
  24. use think\facade\Log;
  25. use function GuzzleHttp\Psr7\str;
  26. class CommissionCron
  27. {
  28. /**
  29. * @var false|string
  30. */
  31. private $startMonth;
  32. /**
  33. * @var false|string
  34. */
  35. private $endMonth;
  36. /**
  37. * @var false|string
  38. */
  39. private $month;
  40. public function __construct()
  41. {
  42. // $this->startMonth = date('Y-m-01 00:00:00', strtotime('-1 month'));
  43. // $this->endMonth = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
  44. // $this->month = date('Y-m', strtotime('-1 month'));
  45. $this->startMonth = date('Y-m-01 00:00:00');
  46. $this->endMonth = date("Y-m-d 23:59:59");
  47. $this->month = date('Y-m');
  48. }
  49. /**
  50. * 奖金
  51. */
  52. public function bonus()
  53. {
  54. //创客补贴
  55. $this->real_subsidy_money();
  56. // 跨店奖励
  57. $this->stride_shop_money();
  58. //推荐创客收益
  59. $this->invite_shop_money();
  60. //大V经理分享奖
  61. $this->big_vip_money();
  62. //大V升级奖
  63. $this->big_vip_up_money();
  64. echo '执行成功';
  65. }
  66. public function stride_shop_money()
  67. {
  68. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  69. $sql = "select stride_shop_mer_id,sum(stride_shop_money) stride_shop_money from rrx_store_order o
  70. left join rrx_store_order_extend od on od.order_id=o.order_id
  71. 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
  72. group by od.stride_shop_mer_id";
  73. $mer_bonus_list = Db::query($sql);
  74. Db::name('log')->insert(['data' => "跨店奖励 mer_bonus_list:" . json_encode($mer_bonus_list)]);
  75. foreach ($mer_bonus_list as $mer_bonus) {
  76. $mer_id = $mer_bonus['stride_shop_mer_id'];
  77. $money = $mer_bonus['stride_shop_money'];
  78. if ($money < 0.01) {
  79. Db::name('log')->insert(['data' => "跨店奖励:mer_id:{$mer_id} 跨店奖励低于0.01无法入库 特此记录!"]);
  80. continue;
  81. }
  82. $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
  83. $mark = "跨店奖励" . $money;
  84. $title = "跨店奖励({$this->month}月)";
  85. $commission_type = 7;
  86. $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
  87. }
  88. }
  89. public function invite_shop_money()
  90. {
  91. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  92. $sql = "select od.share_uid,sum(invite_shop_money) invite_shop_money from rrx_store_order o
  93. left join rrx_store_order_extend od on od.order_id=o.order_id
  94. 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";
  95. $user_bonus_list = Db::query($sql);
  96. Db::name('log')->insert(['data' => "推荐创客奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
  97. foreach ($user_bonus_list as $user_bonus) {
  98. $bonus_uid = $user_bonus['share_shop_uid'];
  99. $money = $user_bonus['invite_shop_money'];
  100. if ($money < 0.01) {
  101. Db::name('log')->insert(['data' => "推荐创客:bonus_uid:{$bonus_uid} 推荐创客低于0.01无法入库 特此记录!"]);
  102. continue;
  103. }
  104. $mark = "推荐创客收益" . $money;
  105. $title = "推荐创客收益({$this->month}月)";
  106. $commission_type = 10;
  107. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  108. }
  109. }
  110. public function big_vip_money()
  111. {
  112. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  113. $sql = "select od.share_uid,sum(big_vip_money) big_vip_money from rrx_store_order o
  114. left join rrx_store_order_extend od on od.order_id=o.order_id
  115. 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";
  116. $user_bonus_list = Db::query($sql);
  117. Db::name('log')->insert(['data' => "大v奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
  118. foreach ($user_bonus_list as $user_bonus) {
  119. $bonus_uid = $user_bonus['share_uid'];
  120. $money = $user_bonus['big_vip_money'];
  121. if ($money < 0.01) {
  122. Db::name('log')->insert(['data' => "大v分享奖:bonus_uid:{$bonus_uid} 大v分享奖低于0.01无法入库 特此记录!"]);
  123. continue;
  124. }
  125. $mark = "大V经理分享奖" . $money;
  126. $title = "大V经理分享奖({$this->month})";
  127. $commission_type = 16;
  128. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  129. }
  130. }
  131. public function real_subsidy_money()
  132. {
  133. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  134. $sql = "select o.mer_id mer_id,sum(real_subsidy_money) real_subsidy_money from rrx_store_order o
  135. left join rrx_store_order_extend od on od.order_id=o.order_id
  136. where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.real_subsidy_money>0
  137. group by o.mer_id";
  138. $mer_bonus_list = Db::query($sql);
  139. Db::name('log')->insert(['data' => "创客补贴 mer_bonus_list:" . json_encode($mer_bonus_list)]);
  140. foreach ($mer_bonus_list as $mer_bonus) {
  141. $mer_id = $mer_bonus['mer_id'];
  142. $money = $mer_bonus['real_subsidy_money'];
  143. if ($money < 0.01) {
  144. Db::name('log')->insert(['data' => "创客补贴:mer_id:{$mer_id} 创客补贴低于0.01无法入库 特此记录!"]);
  145. continue;
  146. }
  147. $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
  148. $mark = "创客补贴" . $money;
  149. $title = "创客补贴({$this->month}月)";
  150. $commission_type = 19;
  151. $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
  152. }
  153. }
  154. /**
  155. * 大V升级奖
  156. * 1. 查询大V经理列表
  157. * 2. 查询团队列表
  158. * 3. 查询团队这个月的总流水的百分之一分给大V经理
  159. * @return void
  160. */
  161. public function big_vip_up_money()
  162. {
  163. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  164. $user_list = Db::name("user")->field('uid')->where("user_group", 3)->select()->toArray();
  165. foreach ($user_list as $user) {
  166. $mer_id = 0;
  167. $user_id = $user['uid'];
  168. $sql = "select getUserSpreadId($user_id) as uids";
  169. $spread_user_list = Db::query($sql);
  170. $spread_user_list = trim($spread_user_list[0]['uids'],'$,');
  171. //查询可以分到的钱
  172. $sql = "select sum(big_vip_up_money) big_vip_up_money from rrx_store_order o
  173. left join rrx_store_order_extend od on od.order_id=o.order_id
  174. 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)";
  175. $big_vip_up = Db::query($sql)[0];
  176. $money = $big_vip_up['big_vip_up_money'] ?? 0;
  177. Db::name('log')->insert(['data' => "大V升级奖 big_vip_up_money {$user_id}:{$money}"]);
  178. if ($money < 0.01) {
  179. Db::name('log')->insert(['data' => "大v分享奖:big_vip_up_money:{$money} 大v分享奖低于0.01无法入库 特此记录!"]);
  180. continue;
  181. }
  182. $mark = "大V升级奖" . $money;
  183. $title = "大V升级奖({$this->month}月)";
  184. $commission_type = 10;
  185. $StoreOrderRepository->add_bill_gongfu($title, $money, $user_id, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  186. }
  187. }
  188. /**
  189. * 用户升级
  190. * @throws DbException
  191. */
  192. public function member_up()
  193. {
  194. $uid_list = Db::name('store_order')
  195. ->where('mer_id', systemConfig('mer_365'))
  196. ->where('paid', 1)
  197. ->whereIn('status', [0, 1, 2, 3])
  198. ->group('uid')
  199. ->column('uid');
  200. Log::info('升级:' . json_encode($uid_list));
  201. foreach ($uid_list as $uid) {
  202. $pay_price = Db::name('store_order')
  203. ->where('mer_id', systemConfig('mer_365'))
  204. ->where('uid', $uid)
  205. ->where('paid', 1)
  206. ->where('is_settlement', 1)
  207. ->whereIn('status', [0, 1, 2, 3])
  208. ->sum('pay_price');
  209. if ($pay_price >= 365) {
  210. // 有没有直推20个经理
  211. $invite_count = Db::name('user')->where('spread_uid', $uid)->field('uid,user_group')->where('user_group', 2)->count();
  212. print_r("uid:{$uid}," . Db::name('user')->where('uid', $uid)->value('phone') . "邀请365经理人数{$invite_count} <br>");
  213. if ($invite_count >= 20) {
  214. $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [4, 5, 6])->update([
  215. 'user_group' => 3
  216. ]);
  217. if ($res) var_dump("uid:{$uid},升级大V经理");
  218. } else {
  219. $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [3, 4, 5, 6])->update([
  220. 'user_group' => 2
  221. ]);
  222. if ($res) var_dump("uid:{$uid},升级365会员");
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * 创客合伙人收益
  229. */
  230. private function partner_award()
  231. {
  232. //1. 被创客设置成创客合伙人,获得该项收益
  233. //已结算订单金额的
  234. //2%
  235. //1. 按月统计并结算
  236. //2. 计算公式:
  237. //所属创客店补贴*2%/创客合伙人总数
  238. // 推荐创客收益设置
  239. $commission_rate = systemConfig('commission_rate');
  240. Db::name('log')->insert(['data' => 'commission_rate:' . json_encode($commission_rate)]);
  241. $rate = $commission_rate['chuangkehehuoren_rate'];
  242. // 查询全部订单
  243. $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";
  244. $mer_order_list = Db::query($sql);
  245. Db::name('log')->insert(['data' => "创客合伙人收益 user_order_list:" . json_encode($mer_order_list)]);
  246. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  247. $mer_out_list = [];
  248. foreach ($mer_order_list as $mer_order) {
  249. $mer_id = $mer_order['mer_id'];
  250. $hehuo_user_list = Db::name('user')->field('uid')->where('mer_id', $mer_id)->where('is_shop_partner', 1)->select()->toArray();
  251. $money = decimal2($mer_order['total_price'] * $rate / 100);
  252. foreach ($hehuo_user_list as $hehuo_user) {
  253. $user_money = decimal2($money / count($hehuo_user_list));
  254. Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客合伙人人数:" . count($hehuo_user_list) . " 创客补贴比例{$rate}% 创客店铺订单流水:{$mer_order['total_price']} 获取创客推荐收益:{$money}!"]);
  255. if ($money == 0) {
  256. Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客补贴低于0.01无法入库 特此记录!"]);
  257. continue;
  258. }
  259. $mark = "创客合伙人收益" . $user_money;
  260. $title = "创客合伙人收益({$this->month}月)";
  261. $commission_type = 17;
  262. $StoreOrderRepository->add_bill_gongfu($title, $money, $hehuo_user['uid'], $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
  263. }
  264. $money = decimal2($mer_order['total_price'] * $rate / 100);
  265. $mer_out_list[] = [
  266. $mer_order['mer_id'] => $money
  267. ];
  268. }
  269. Db::name('log')->insert(['data' => '跨店奖励 商户支出:' . json_encode($mer_out_list)]);
  270. return $mer_out_list;
  271. }
  272. /**
  273. * 代理区域收益
  274. */
  275. public function area_manage_details()
  276. {
  277. //1. 代理区域内已结算订单总额的百分
  278. //比;
  279. //2. 按照收货地址统计;
  280. //3. 奖励对象:省代、区县代;
  281. //区县代 5%
  282. //省代 2%
  283. //1. 按月结算:每月 1 号 0 点
  284. //统计上月已结算的订单金额
  285. //并对应发放佣金
  286. //2. 计算公式:
  287. //(1)无运费:收货地为该区域的已结算订单金额*对应比例
  288. //(2)有运费:(收货地为该区域的已结算订单金额-运费)*对应比例
  289. set_time_limit(0);
  290. ini_set('memory_limit', '-1');
  291. $commission_rate = systemConfig('commission_rate');
  292. Db::name('log')->insert(['data' => '代理区域收益 commission_rate:' . json_encode($commission_rate)]);
  293. $area_agency_rate = $commission_rate['area_agency_rate'] ?? 0;
  294. $province_agency_rate = $commission_rate['province_agency_rate'] ?? 0;
  295. $bonus_list = [];
  296. $bonus_user_list = Db::name('user')->field('uid,user_group,manage_address,spread_uid')->whereIn('user_group', [5, 6])->select()->toArray();
  297. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  298. foreach ($bonus_user_list as $bonus_user) {
  299. $manage_address = $bonus_user['manage_address'];
  300. if (empty($manage_address))
  301. continue;
  302. $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%'";
  303. $total_price = Db::query($sql)[0]['total_price'] ?? 0;
  304. $rate = $bonus_user['user_group'] == 6 ? $province_agency_rate : $area_agency_rate;
  305. $money = decimal2($total_price * $rate / 100);
  306. Db::name('log')->insert(['data' => "代理区域收益:uid:{$bonus_user['uid']} 代理区域收益比例{$rate}% 流水:{$total_price} 收益:{$money} sql:" . $sql]);
  307. $mark = "代理区域收益" . $money;
  308. $title = "代理区域收益({$this->month}月)";
  309. $commission_type = 12;
  310. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_user['uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month);
  311. $bonus_list[] = [
  312. 'uid' => $bonus_user['uid'],
  313. 'money' => $money,
  314. 'spread_uid' => $bonus_user['spread_uid'],
  315. ];
  316. }
  317. $this->invite_agency_award($bonus_list);
  318. echo '执行成功';
  319. }
  320. /**
  321. * 推荐代理收益
  322. * @param $bonus_list
  323. */
  324. private function invite_agency_award($bonus_list)
  325. {
  326. // 1. 推荐的代理收益的百分比
  327. //2. 奖励对象:所有用户
  328. //1. 按月结算:每个月 1 号 0点统计上月的代理收益并对应发放佣金
  329. //2. 计算公式:
  330. //推荐代理总收益*推荐代理收益百分比
  331. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  332. $this->month = date('Y-m', strtotime('-1 month'));
  333. $commission_rate = systemConfig('commission_rate');
  334. $rate = $commission_rate['tuijiandaili_rate'] ?? 0;
  335. foreach ($bonus_list as $bonus) {
  336. $money = decimal2($bonus['money'] * $rate / 100);
  337. $mark = "推荐代理收益" . $money;
  338. $title = "推荐代理收益({$this->month}月)";
  339. $commission_type = 15;
  340. $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus['spread_uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month, $this->month);
  341. }
  342. }
  343. /**
  344. * 生成商品图片缓存(最好用命令行执行)
  345. * @return \think\response\Json
  346. * @throws DataNotFoundException
  347. * @throws DbException
  348. * @throws ModelNotFoundException
  349. */
  350. public function make_product_share_cache()
  351. {
  352. set_time_limit(0);
  353. ini_set('memory_limit', '-1');
  354. $redis = Cache::store('redis')->handler();
  355. $key = 'make_product_share_cache';
  356. // $redis->del($key);
  357. // if (!$redis->setnx($key, 1))
  358. // return json(['code' => 400, 'msg' => '现在有正在执行的任务' ]);
  359. $redis->expire($key, 5 * 60);
  360. $product_share_ids = Db::name('product_share')
  361. // ->where('`url` is not null')
  362. ->group('product_id')
  363. ->column('product_id');
  364. $list = Db::name('store_product')
  365. ->whereNotIn('product_id', $product_share_ids)
  366. ->where('is_show', 1)
  367. ->limit(3)
  368. ->order('create_time desc')
  369. ->select()
  370. ->toArray();
  371. var_dump($list);
  372. $header = [
  373. 'X-Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0LmJqdGRiYS5jb20iLCJhdWQiOiJ0ZXN0LmJqdGRiYS5jb20iLCJpYXQiOjE2OTI0MjQ4MTEsIm5iZiI6MTY5MjQyNDgxMSwiZXhwIjoxNzAzMjI0ODExLCJqdGkiOls5NzIwLCJ1c2VyIl19.YtYf1ssMTz6fOO2rZVX-jb48L7AqXSKezQcbTHR3sCk'
  374. ];
  375. foreach ($list as $store_product) {
  376. $res1 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=1", null, 'get', $header);
  377. $res2 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=2", null, 'get', $header);
  378. var_dump($res1, $res2);
  379. }
  380. $redis->del($key);
  381. return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
  382. }
  383. /**
  384. * 梳理每个人的公排人数
  385. * @throws \think\db\exception\DataNotFoundException
  386. * @throws \think\db\exception\DbException
  387. * @throws \think\db\exception\ModelNotFoundException
  388. */
  389. public function tease_user_count()
  390. {
  391. $list = Db::name('user')->where('is_del', 0)->select()->toArray();
  392. foreach ($list as $item) {
  393. $level_number = Db::name('user')->where('level_id', $item['uid'])->count();
  394. $user_count = Db::name('user_count')->where('uid', $item['uid'])->find();
  395. if ($level_number >= 5) {
  396. $res = Db::name('user_count')->where('uid', $item['uid'])->delete();
  397. if ($res) echo "{$item['uid']}公排人数已删除\r\n";
  398. continue;
  399. }
  400. if ($user_count) {
  401. if ($level_number == $user_count['level_number']) continue;
  402. Db::name('user_count')->where('uid', $item['uid'])->update([
  403. 'level_number' => $level_number
  404. ]);
  405. } else {
  406. Db::name('user_count')->insert([
  407. 'uid' => $item['uid'],
  408. 'level_number' => 0
  409. ]);
  410. $user_count['level_number'] = 0;
  411. }
  412. echo "{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n";
  413. Log::info("{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n");
  414. }
  415. return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
  416. }
  417. /**
  418. * 下架微唯宝已下架的商品
  419. * @return mixed
  420. * @throws \think\db\exception\DbException
  421. */
  422. public function downGongProduct()
  423. {
  424. $start = input('start');
  425. $where = [
  426. 'is_used' => 1,
  427. 'mer_status' => 1,
  428. // 'type' => 5,
  429. // 'is_show' => 1,
  430. 'is_hide' => 0,
  431. // 'is_del' => 0
  432. ];
  433. if (!$start) {
  434. $product_data_count = Db::name('store_product')
  435. ->where($where)
  436. ->where("spu_id != '' or spu_id is not null")
  437. ->count();
  438. $start = mt_rand(0, $product_data_count);
  439. }
  440. $product_data = Db::name('store_product')
  441. ->where($where)
  442. ->where("spu_id != '' or spu_id is not null")
  443. ->where('product_id', '>', $start)
  444. ->order('product_id asc')
  445. ->limit(0, 50)
  446. ->select()
  447. ->toArray();
  448. foreach ($product_data as $item) {
  449. $spuIds = $item['spu_id'];
  450. $goods_status = GongApiRequest::goods_status($spuIds);
  451. $status = $goods_status[0]['status'] ?? 1;
  452. $goods_info = GongApiRequest::get_goods_info($spuIds);
  453. if ($status == 0 || (isset($goods_info['error_msg']) && $goods_info['error_msg'] == '没有查询到商品信息')) {
  454. Db::startTrans();
  455. try {
  456. // 删除选品库对应的数据
  457. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  458. 'status' => 9,
  459. 'update_time' => date('Y-m-d H:i:s')
  460. ]);
  461. // 删除商品管理对应的数据
  462. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  463. 'is_del' => 1,
  464. 'is_show' => 0
  465. ]);
  466. Db::commit();
  467. } catch (Exception $e) {
  468. Db::rollback();
  469. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  470. return app('json')->fail($e->getMessage());
  471. }
  472. } else if ($status == 1 || !isset($goods_info['error_msg'])) {
  473. Db::startTrans();
  474. try {
  475. // 选品库对应的数据
  476. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  477. 'status' => 1,
  478. 'update_time' => date('Y-m-d H:i:s')
  479. ]);
  480. // 商品管理对应的数据
  481. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  482. 'is_del' => 0,
  483. 'is_show' => 1
  484. ]);
  485. Db::commit();
  486. } catch (Exception $e) {
  487. Db::rollback();
  488. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  489. return app('json')->fail($e->getMessage());
  490. }
  491. }
  492. }
  493. return app('json')->success('操作成功');
  494. }
  495. /**
  496. * 定时获取 交易类型 1订单入账 2佣金提现支出 3养老金(红积分兑换订单)
  497. */
  498. public function platform_income_detail_get()
  499. {
  500. $merchant_list = Db::name('merchant')
  501. ->where('status', 1)
  502. ->where('mer_state', 1)
  503. ->where("mer_name != ''")
  504. ->column('mer_id');
  505. $Reconciliation = app()->make(Reconciliation::class);
  506. $unique_list = [];
  507. $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
  508. ->select()
  509. ->toArray();
  510. foreach ($enterprise_platform_income_detail_list as $item) {
  511. $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
  512. }
  513. foreach ($merchant_list as $mer_id) {
  514. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  515. $is_first = empty($enterprise_platform_income_detail_list);
  516. // 1自营订单入账金额=已结算订单金额-运费-养老金-汇付通道服务费; 订单结算的时候才统计
  517. $store_order_list = Db::name('store_order')
  518. ->where('mer_id', $mer_id)
  519. ->where('paid', 1)
  520. ->where('douhuomall', 0)
  521. ->where('is_settlement', 1)
  522. ->order('order_id', 'desc')
  523. // ->limit(500)
  524. ->select()
  525. ->toArray();
  526. foreach ($store_order_list as $store_order) {
  527. if (in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  528. $pay_type = $store_order['pay_type'];
  529. if (!in_array($pay_type, [0, 1])) continue;
  530. $group_order = Db::name('store_group_order')
  531. ->alias('sgo')
  532. ->where('sgo.group_order_id', $store_order['group_order_id'])
  533. ->find();
  534. if (empty($group_order)) continue;
  535. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  536. 'shop_bonus_rate' => 30,
  537. 'yanglao_rate' => 5,
  538. ];
  539. $yanglao = Db::name('user_bill')
  540. ->where('order_sn', $store_order['order_sn'])
  541. ->where('commission_type', 5)
  542. ->value('number');
  543. $ru_money = 0;
  544. if ($pay_type != 0) {
  545. $store_split_order = Db::name('store_split_order')
  546. ->where('group_order_id', $group_order['group_order_sn'])
  547. ->where('status', 1)
  548. ->find();
  549. if (empty($store_split_order)) continue;
  550. if ($pay_type == 1) {
  551. //微信
  552. if ($group_order['pay_wx'] == 3) {
  553. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  554. } else {
  555. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  556. }
  557. } else {
  558. //支付宝
  559. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  560. }
  561. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  562. $params['api_key'] = $api_key;
  563. //查询支付确认对象
  564. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  565. $res = $this->payment_confirm_query_traits($params);
  566. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  567. // 获取分账后一个工作日时间
  568. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  569. if (isset($res['status'])) {
  570. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  571. 'hf_fee_amt' => $res['fee_amt'],
  572. 'hf_clear_time' => $close_time
  573. ]);
  574. }
  575. if (isset($res['status']) && time() > strtotime($close_time)) {
  576. if ($store_order['mer_id'] == 496) {
  577. $ru_money = $store_order['pay_price'] - $yanglao - $res['fee_amt'];
  578. } else {
  579. $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) - $res['fee_amt'] + +$store_order['pay_postage'];
  580. }
  581. $store_order['pay_time'] = '';//线上支付的
  582. } else {
  583. continue;
  584. }
  585. } else {
  586. // 订单金额+运费
  587. if ($store_order['mer_id'] == 496) {
  588. $ru_money = $store_order['pay_price'] - $yanglao;
  589. } else {
  590. $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) + $store_order['pay_postage'];
  591. }
  592. }
  593. Db::name('enterprise_platform_income_detail')
  594. ->insert([
  595. 'mer_id' => $mer_id,
  596. 'uid' => $store_order['uid'],
  597. 'title' => '自营订单入账',
  598. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  599. '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')),
  600. 'money' => $ru_money,
  601. 'deal_type' => 1,
  602. 'order_id' => $store_order['order_id'],
  603. 'order_sn' => $store_order['order_sn'],
  604. 'user_extra_id' => '',
  605. 'pm' => 1,
  606. 'create_time' => date('Y-m-d H:i:s'),
  607. ]);
  608. unset($close_time);
  609. }
  610. //2佣金提现到账 提现成功统计
  611. // $user_extract_list = Db::name('user_extract')
  612. // ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
  613. // ->where('status', 1)
  614. // ->where('withdrawal_type', 3)
  615. // ->where('extract_type', '<>', 4)
  616. // ->when($uids, function ($query) use ($uids) {
  617. // $query->whereIn('uid', $uids);
  618. // })
  619. // ->order('extract_id', 'desc')
  620. //// ->limit(100)
  621. // ->select()
  622. // ->toArray();
  623. // foreach ($user_extract_list as $user_extract) {
  624. // if (in_array('2' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'], $unique_list)) continue;
  625. // $enterprise_platform_income_detail = Db::name('enterprise_platform_income_detail')
  626. // ->where('deal_type', 2)
  627. // ->where('user_extra_id', $user_extract['extract_id'])
  628. // ->find();
  629. // if (!$enterprise_platform_income_detail) {
  630. // Db::name('enterprise_platform_income_detail')
  631. // ->insert([
  632. // 'mer_id' => $mer_id,
  633. // 'uid' => $user_extract['uid'],
  634. // 'title' => '佣金提现支出',
  635. // 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  636. // 'deal_time' => $user_extract['status_time'],
  637. // 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
  638. // 'deal_type' => 2,
  639. // 'order_id' => '',
  640. // 'order_sn' => $user_extract['order_sn'],
  641. // 'user_extra_id' => $user_extract['extract_id'],
  642. // 'pm' => 1,
  643. // 'create_time' => date('Y-m-d H:i:s'),
  644. // ]);
  645. // }
  646. // }
  647. $start_time = '2024-04-01';
  648. for ($i = 0; $i < 1000; $i++) {
  649. $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
  650. if (strtotime($day) > time()) {
  651. break;
  652. }
  653. $Reconciliation->cron($day, $mer_id);
  654. }
  655. }
  656. // 计算平台
  657. //交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利
  658. $mer_id = 0;
  659. // 3 订单支付 供应链订单结算后入账到平台的金额
  660. $store_order_list = Db::name('store_order')
  661. ->where('paid', 1)
  662. ->order('order_id', 'desc')
  663. // ->limit(500)
  664. ->select()
  665. ->toArray();
  666. foreach ($store_order_list as $store_order) {
  667. if (in_array('3' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  668. $group_order = Db::name('store_group_order')
  669. ->alias('sgo')
  670. ->where('sgo.group_order_id', $store_order['group_order_id'])
  671. ->find();
  672. if (empty($group_order)) continue;
  673. $store_split_order = Db::name('store_split_order')
  674. ->where('group_order_id', $group_order['group_order_sn'])
  675. ->where('status', 1)
  676. ->find();
  677. if (empty($store_split_order)) continue;
  678. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  679. Db::name('enterprise_platform_income_detail')
  680. ->insert([
  681. 'mer_id' => $mer_id,
  682. 'uid' => $store_order['uid'],
  683. 'title' => '订单支付',
  684. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
  685. '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')),
  686. 'money' => $store_order['pay_price'] + $store_order['pay_postage'],
  687. 'deal_type' => 3,
  688. 'order_id' => $store_order['order_id'],
  689. 'order_sn' => $store_order['order_sn'],
  690. 'user_extra_id' => '',
  691. 'pm' => 1,
  692. 'create_time' => date('Y-m-d H:i:s'),
  693. ]);
  694. }
  695. //5用户佣金提现
  696. $user_extract_list = Db::name('user_extract')
  697. ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
  698. ->where('status', 1)
  699. ->order('extract_id', 'desc')
  700. // ->limit(100)
  701. ->select()
  702. ->toArray();
  703. foreach ($user_extract_list as $user_extract) {
  704. if (in_array('5' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'] . $mer_id, $unique_list)) continue;
  705. Db::name('enterprise_platform_income_detail')
  706. ->insert([
  707. 'mer_id' => $mer_id,
  708. 'uid' => $user_extract['uid'],
  709. 'title' => '用户佣金提现',
  710. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  711. 'deal_time' => $user_extract['status_time'],
  712. 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
  713. 'deal_type' => 5,
  714. 'order_id' => '',
  715. 'order_sn' => $user_extract['order_sn'],
  716. 'user_extra_id' => $user_extract['extract_id'],
  717. 'pm' => 0,
  718. 'create_time' => date('Y-m-d H:i:s'),
  719. ]);
  720. }
  721. // 6订单入账:支付成功的订单数据 供应链订单
  722. $store_order_list = Db::name('store_order')
  723. ->where('paid', 1)
  724. ->where('douhuomall', 1)
  725. ->where('is_settlement', 1)
  726. ->order('order_id', 'desc')
  727. // ->limit(500)
  728. ->select()
  729. ->toArray();
  730. foreach ($store_order_list as $store_order) {
  731. if (in_array('6' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  732. $pay_type = $store_order['pay_type'];
  733. if (!in_array($pay_type, [0, 1])) continue;
  734. $group_order = Db::name('store_group_order')
  735. ->alias('sgo')
  736. ->where('sgo.group_order_id', $store_order['group_order_id'])
  737. ->find();
  738. if (empty($group_order)) continue;
  739. $yanglao = Db::name('user_bill')
  740. ->where('order_sn', $store_order['order_sn'])
  741. ->where('commission_type', 5)
  742. ->value('number');
  743. $ru_money = 0;
  744. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  745. 'shop_bonus_rate' => 30,
  746. 'yanglao_rate' => 5,
  747. ];
  748. if ($pay_type != 0) {
  749. $store_split_order = Db::name('store_split_order')
  750. ->where('group_order_id', $group_order['group_order_sn'])
  751. ->find();
  752. if (empty($store_split_order)) continue;
  753. if (!$store_order['hf_fee_amt']) {
  754. if ($pay_type == 1) {
  755. //微信
  756. if ($group_order['pay_wx'] == 3) {
  757. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  758. } else {
  759. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  760. }
  761. } else {
  762. //支付宝
  763. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  764. }
  765. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  766. $params['api_key'] = $api_key;
  767. //查询支付确认对象
  768. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  769. $res = $this->payment_confirm_query_traits($params);
  770. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  771. if (isset($res['status'])) {
  772. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  773. 'hf_fee_amt' => $res['fee_amt'],
  774. 'hf_clear_time' => $close_time
  775. ]);
  776. $store_order['hf_fee_amt'] = $res['fee_amt'];
  777. }
  778. }
  779. // 获取分账后一个工作日时间
  780. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  781. if (isset($res['status']) && time() > strtotime($close_time)) {
  782. if ($store_order['mer_id'] == 496) {
  783. $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
  784. } else {
  785. $ru_money = $store_order['pay_price'] - $yanglao;
  786. }
  787. $store_order['pay_time'] = '';//线上支付的
  788. } else {
  789. continue;
  790. }
  791. } else {
  792. // 订单金额+运费
  793. if ($store_order['mer_id'] == 496) {
  794. $ru_money = $store_order['pay_price'] - $yanglao;
  795. } else {
  796. $ru_money = $store_order['pay_price'] - $yanglao;
  797. }
  798. }
  799. Db::name('enterprise_platform_income_detail')
  800. ->insert([
  801. 'mer_id' => $mer_id,
  802. 'uid' => $store_order['uid'],
  803. 'title' => '订单入账',
  804. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
  805. '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')),
  806. 'money' => $ru_money,
  807. 'deal_type' => 6,
  808. 'order_id' => $store_order['order_id'],
  809. 'order_sn' => $store_order['order_sn'],
  810. 'user_extra_id' => '',
  811. 'pm' => 1,
  812. 'create_time' => date('Y-m-d H:i:s'),
  813. ]);
  814. unset($close_time);
  815. }
  816. //7 创客自营订单让利=创客自营订单金额*分润比例-(创客自营订单金额-运费)*养老金比例
  817. $store_order_list = Db::name('store_order')
  818. ->where('paid', 1)
  819. ->where('is_settlement', 1)
  820. ->where('douhuomall', 0)
  821. ->order('order_id', 'desc')
  822. // ->limit(500)
  823. ->select()
  824. ->toArray();
  825. foreach ($store_order_list as $store_order) {
  826. if (in_array('7' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
  827. $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
  828. 'shop_bonus_rate' => 30,
  829. 'yanglao_rate' => 5,
  830. ];
  831. $pay_type = $store_order['pay_type'];
  832. if ($pay_type != 0) {
  833. $group_order = Db::name('store_group_order')
  834. ->alias('sgo')
  835. ->where('sgo.group_order_id', $store_order['group_order_id'])
  836. ->find();
  837. if (empty($group_order)) continue;
  838. $store_split_order = Db::name('store_split_order')
  839. ->where('group_order_id', $group_order['group_order_sn'])
  840. ->find();
  841. if (empty($store_split_order)) continue;
  842. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  843. if (!$store_order['hf_fee_amt']) {
  844. $store_split_order = Db::name('store_split_order')
  845. ->where('group_order_id', $group_order['group_order_sn'])
  846. ->where('status', 1)
  847. ->find();
  848. if (empty($store_split_order)) continue;
  849. if ($pay_type == 1) {
  850. //微信
  851. if ($group_order['pay_wx'] == 3) {
  852. $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  853. } else {
  854. $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  855. }
  856. } else {
  857. //支付宝
  858. $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  859. }
  860. $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
  861. $params['api_key'] = $api_key;
  862. //查询支付确认对象
  863. Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
  864. $res = $this->payment_confirm_query_traits($params);
  865. Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
  866. // 获取分账后一个工作日时间
  867. $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
  868. if (isset($res['status'])) {
  869. Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
  870. 'hf_fee_amt' => $res['fee_amt'],
  871. 'hf_clear_time' => $close_time
  872. ]);
  873. $store_order['hf_fee_amt'] = $res['fee_amt'];
  874. }
  875. }
  876. $yanglao = Db::name('user_bill')
  877. ->where('order_sn', $store_order['order_sn'])
  878. ->where('commission_type', 5)
  879. ->value('number');
  880. if (isset($res['status']) && time() > strtotime($close_time)) {
  881. if ($store_order['mer_id'] == 496) {
  882. $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
  883. } else {
  884. $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
  885. }
  886. $store_order['pay_time'] = '';//线上支付的
  887. } else {
  888. continue;
  889. }
  890. }else{
  891. if ($store_order['mer_id'] == 496) {
  892. $ru_money = $store_order['pay_price'] - $yanglao;
  893. } else {
  894. $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
  895. }
  896. }
  897. Db::name('enterprise_platform_income_detail')
  898. ->insert([
  899. 'mer_id' => $mer_id,
  900. 'uid' => $store_order['uid'],
  901. 'title' => '创客自营订单让利',
  902. 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
  903. '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')),
  904. 'money' => $ru_money,
  905. 'deal_type' => 7,
  906. 'order_id' => $store_order['order_id'],
  907. 'order_sn' => $store_order['order_sn'],
  908. 'user_extra_id' => '',
  909. 'pm' => 1,
  910. 'create_time' => date('Y-m-d H:i:s'),
  911. ]);
  912. unset($close_time);
  913. }
  914. $start_time = '2024-04-01';
  915. for ($i = 0; $i < 1000; $i++) {
  916. $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
  917. if (strtotime($day) > time()) {
  918. break;
  919. }
  920. $Reconciliation->cron($day, 0);
  921. }
  922. return app('json')->success('获取平台收支明细成功');
  923. }
  924. public function clear_platform_income_detail()
  925. {
  926. Db::name('enterprise_platform_income_detail')->where('id', '>', 0)
  927. ->delete();
  928. }
  929. function getNextWorkday($date)
  930. {
  931. // 设置工作日数组
  932. $workdays = array(1, 2, 3, 4, 5); // 默认为周一到周五
  933. // 将日期字符串转换为时间戳
  934. $timestamp = strtotime($date);
  935. // 获取该日期的星期几
  936. $weekday = date('N', $timestamp);
  937. // 如果不是工作日,则增加一天直到是工作日
  938. while (!in_array($weekday, $workdays)) {
  939. $timestamp += (24 * 3600); // 增加一天
  940. $weekday = date('N', $timestamp); // 更新星期几
  941. }
  942. // 返回下一个工作日的日期字符串
  943. return date('Y-m-d H:i:s', $timestamp);
  944. }
  945. /**
  946. * @param $paymnet_params
  947. * @return array
  948. * 查询支付确认对象
  949. */
  950. public
  951. function payment_confirm_query_traits($paymnet_params)
  952. {
  953. $merber = new PaymentConfirm();
  954. $merber->query($paymnet_params);
  955. # 返回查询结果
  956. $merber->isError();
  957. return $merber->result;
  958. }
  959. //服务器配置,一般是默认配置
  960. private $CharSet = "UTF-8"; //设定邮件编码
  961. private $SMTPDebug = 0; // 调试模式输出
  962. private $isSMTP = true; // 使用SMTP
  963. private $Host = 'smtp.163.com'; // SMTP服务器
  964. private $SMTPAuth = true; // 允许 SMTP 认证
  965. private $Username = 'username'; // SMTP 用户名 即邮箱的用户名
  966. private $Password = 'MHIKZOXVPTGNVOMU'; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
  967. private $SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议
  968. private $Port = 465; // 服务器端口 25 或者465 具体要看邮箱服务器支持
  969. //修改成自己的邮箱
  970. private $From = 'formalverification@163.com'; //发件人邮箱
  971. private $FromName = 'makalo'; //发件人的用户昵称
  972. //邮件对象
  973. private $mail;
  974. //一般只有用户名和密码还有端口会变动, 所以这三个参数可以创建对象动态指定,也可以直接写死
  975. public function mallInit($Username = '', $Password = '', $Port = '')
  976. {
  977. if (!empty($Username)) {
  978. $this->Username = $Username;
  979. }
  980. if (!empty($Password)) {
  981. $this->Password = $Password;
  982. }
  983. if (!empty($Port)) {
  984. $this->Port = $Port;
  985. }
  986. //为true表示启用异常
  987. $mail = new PHPMailer(true);
  988. //服务器配置
  989. $mail->CharSet = $this->CharSet; //设定邮件编码
  990. $mail->SMTPDebug = $this->SMTPDebug; // 调试模121212 1式输出
  991. if ($this->isSMTP === true) {
  992. $mail->isSMTP(); // 使用SMTP
  993. }
  994. $mail->Host = $this->Host; // SMTP服务器
  995. $mail->SMTPAuth = $this->SMTPAuth; // 允许 SMTP 认证
  996. $mail->Username = $this->Username; // SMTP 用户名 即邮箱的用户名
  997. $mail->Password = $this->Password; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
  998. $mail->SMTPSecure = $this->SMTPSecure; // 允许 TLS 或者ssl协议
  999. $mail->Port = $this->Port; // 服务器端口 25 或者465 具体要看邮箱服务器支持
  1000. $mail->setFrom($this->From, $this->FromName); //发件人
  1001. $this->mail = $mail;
  1002. }
  1003. /**
  1004. * 发送邮件
  1005. * @param array $sendAddress 收件人地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1006. * @param array $Attachment 附件数组,如果为单值数组则为添加附件,如果为键值数组则添加附件并且重命名
  1007. * @param bool $isHtml 是否以html文档格式发送
  1008. * @param string $Subject 邮件主题
  1009. * @param string $Body 邮件内容,如果以html格式可以写入html
  1010. * @param string $AltBody 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1011. * @param array $CC 抄送地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1012. * @param array $BCC 密送地址数组,如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
  1013. * @return bool 是否发送成功
  1014. */
  1015. public function sendEMail($sendAddress = [], $Subject = '', $Body = '', $AltBody = '', $Attachment = [], $isHtml = true, $CC = [], $BCC = [])
  1016. {
  1017. try {
  1018. //必填参数校验
  1019. if (empty($sendAddress) || empty($Subject) || empty($Body)) {
  1020. return false;
  1021. }
  1022. //收件人
  1023. foreach ($sendAddress as $key => $value) {
  1024. if (is_int($key)) {
  1025. // 处理单值
  1026. $this->mail->addAddress($value);
  1027. } else {
  1028. // 处理键值
  1029. $this->mail->addAddress($value, $key);
  1030. }
  1031. }
  1032. //附件
  1033. if (!empty($Attachment)) {
  1034. foreach ($Attachment as $key => $value) {
  1035. if (is_int($key)) {
  1036. // 处理单值
  1037. $this->mail->addAttachment($value);
  1038. } else {
  1039. // 处理键值
  1040. $this->mail->addAttachment($value, $key);
  1041. }
  1042. }
  1043. }
  1044. //抄送
  1045. if (!empty($CC)) {
  1046. //抄送不为空
  1047. foreach ($CC as $key => $value) {
  1048. if (is_int($key)) {
  1049. // 处理单值
  1050. $this->mail->addCC($value);
  1051. } else {
  1052. // 处理键值
  1053. $this->mail->addCC($value, $key);
  1054. }
  1055. }
  1056. }
  1057. //密送
  1058. if (!empty($BCC)) {
  1059. //密送不为空
  1060. foreach ($BCC as $key => $value) {
  1061. if (is_int($key)) {
  1062. // 处理单值
  1063. $this->mail->addBCC($value);
  1064. } else {
  1065. // 处理键值
  1066. $this->mail->addBCC($value, $key);
  1067. }
  1068. }
  1069. }
  1070. //回复的时候回复给哪个邮箱 建议和发件人一致
  1071. $this->mail->addReplyTo($this->From, $this->FromName);
  1072. //Content
  1073. // 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
  1074. $this->mail->isHTML($isHtml);
  1075. //邮件主题
  1076. $this->mail->Subject = $Subject;
  1077. //邮件内容
  1078. $this->mail->Body = $Body;
  1079. //如果邮件客户端不支持HTML则显示此内容
  1080. $this->mail->AltBody = $AltBody;
  1081. //发送
  1082. $this->mail->send();
  1083. return [
  1084. 'code' => 200,
  1085. ];
  1086. } catch (\Exception $e) {
  1087. return [
  1088. 'code' => 400,
  1089. 'message' => $this->mail->ErrorInfo
  1090. ];
  1091. }
  1092. }
  1093. public function take()
  1094. {
  1095. set_time_limit(0);
  1096. $this->mallInit('formalverification@163.com', 'SYECAWFLLTUPMDRP');
  1097. $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "开始执行" . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
  1098. $mallStr = '';
  1099. // Log::info('执行一次1 '.date('H:i') );
  1100. // return ;
  1101. // $storeOrderRepository = app()->make(StoreOrderRepository::class);
  1102. $key_list = [
  1103. 1 => 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9',
  1104. 2 => 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f',
  1105. 3 => 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'
  1106. ];
  1107. $appid_list = [
  1108. 1 => 'app_44108546-d27a-48ee-88c1-84b45b75114f',
  1109. 2 => 'app_0827b960-b932-45ec-b800-4e850091b419',
  1110. 3 => 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0'
  1111. ];
  1112. //
  1113. // $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
  1114. // $insertData = [
  1115. // [
  1116. // 'mid' => 0,
  1117. // 'requestId' => $payment_id,
  1118. // 'group_order_id' => 'WX171955791230553863',
  1119. // 'splitAmount' => 0.25,
  1120. // 'status' => 0
  1121. // ],
  1122. // ];
  1123. // $div_members[] = [
  1124. // 'member_id' => 'member_personal_16704641278422',
  1125. // 'amount' => '1.00',
  1126. // 'fee_flag' => 'Y'
  1127. // ];
  1128. // //////分账表
  1129. // Db::name("store_split_order")->insertAll($insertData);
  1130. // /// 分账表end
  1131. //
  1132. // $params = [
  1133. // 'payment_id' => '002212024062814583210652657343702200320',
  1134. // 'order_no' => $payment_id,
  1135. // 'confirm_amt' => '1.00',
  1136. // 'div_members' => $div_members
  1137. // ];
  1138. //
  1139. // $params['api_key'] = $key_list[3];
  1140. //
  1141. // var_dump($params);
  1142. // Db::name("log")->insert(array('data' => '汇付延时分账参数-----' . json_encode($params)));//日志记录
  1143. // $res = $storeOrderRepository->payment_confirm_create_traits($params);
  1144. // Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res)));//日志记录
  1145. // var_dump($res);
  1146. // // 分账
  1147. // return;
  1148. $merchant_member_list = Db::name('merchant_member')
  1149. // ->where('member_id', 'member_personal_16704641278422')
  1150. ->column('mer_id,type,member_id,settle_account_id');
  1151. foreach ($merchant_member_list as $merchant_member) {
  1152. $mer_id = $merchant_member['mer_id'];
  1153. if (empty($mer_id))
  1154. continue;
  1155. if (empty($merchant_member['settle_account_id']))
  1156. continue;
  1157. $params = [
  1158. 'app_id' => $appid_list[$merchant_member['type']],
  1159. 'member_id' => $merchant_member['member_id'],
  1160. 'settle_account_id' => $merchant_member['settle_account_id'],
  1161. 'api_key' => $key_list[$merchant_member['type']]
  1162. ];
  1163. $merber = new SettleAccount();
  1164. $merber->balance($params);
  1165. $merber->result;
  1166. // var_dump($merber->result);
  1167. if (isset($merber->result[1])) {
  1168. $result = json_decode($merber->result[1], true);
  1169. $result = json_decode($result['data'], true);
  1170. $result['avl_balance'] = $result['avl_balance'] ?? 0;
  1171. if ($result['avl_balance'] > 0) {
  1172. $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
  1173. $params = [
  1174. 'order_no' => $payment_id,
  1175. 'app_id' => $appid_list[$merchant_member['type']],
  1176. 'cash_type' => 'T1',
  1177. 'cash_amt' => sprintf("%01.2f", $result['avl_balance']),
  1178. 'member_id' => $merchant_member['member_id'],
  1179. 'api_key' => $key_list[$merchant_member['type']]
  1180. ];
  1181. $yanglao_member_ids = ['jsdk_member_wx21710232183139', 'jsdk_member_ali1710232187326'];
  1182. if (in_array($merchant_member['member_id'], $yanglao_member_ids)) {
  1183. $params['remark'] = '天地博爱科技~养老金';
  1184. }
  1185. Db::name("log")->insert(array('data' => '汇付取现参数-----' . json_encode($params)));//日志记录
  1186. $merber = new Drawcash();
  1187. $merber->create($params);
  1188. # 返回查询结果
  1189. $merber->isError();
  1190. $res = $merber->result;
  1191. Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res))); //日志记录
  1192. Db::name("log")->insert(array('data' => "mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----"));//日志记录
  1193. var_dump("mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----");
  1194. $mallStr = $mallStr . "-----mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----<br/>";
  1195. }
  1196. }
  1197. }
  1198. echo date('Y-m-d H:i:s') . '执行结束' . "\r\n";
  1199. $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "执行结束<br/>" . $mallStr . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
  1200. }
  1201. public function cesexx(){
  1202. // $storeOrderRepository = app()->make(StoreOrderRepository::class);
  1203. // $storeOrderRepository->douhuomall_delivery();
  1204. }
  1205. }