| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145 |
- <?php
- namespace app\controller\api\cron;
- use app\common\model\store\order\StoreOrder;
- use app\common\repositories\user\UserBillRepository;
- use app\common\model\user\UserCertification;
- use AdaPaySdk\Drawcash;
- use AdaPaySdk\PaymentConfirm;
- use AdaPaySdk\SettleAccount;
- use app\common\dao\store\order\StoreOrderDao;
- use app\common\repositories\merchant\order\OrderGzcRepository;
- use app\common\repositories\merchant\order\OrderMerchantRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\common\repositories\store\order\StoreOrderStatusRepository;
- use app\common\repositories\user\UserRepository;
- use app\controller\api\Common;
- use app\controller\merchant\finance\Reconciliation;
- use app\traits\GongApiRequest;
- use app\traits\ShouxinyiApiRequest;
- use Carbon\Carbon;
- use Exception;
- use PHPMailer\PHPMailer\PHPMailer;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Cache;
- use think\facade\Db;
- use think\facade\Log;
- use function GuzzleHttp\Psr7\str;
- use app\traits\HuiPay;
- class CommissionCron
- {
- /**
- * @var false|string
- */
- private $startMonth;
- /**
- * @var false|string
- */
- private $endMonth;
- /**
- * @var false|string
- */
- private $month;
- public function __construct()
- {
- // $this->startMonth = date('Y-m-01 00:00:00', strtotime('-1 month'));
- // $this->endMonth = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
- // $this->month = date('Y-m', strtotime('-1 month'));
- $this->startMonth = date('Y-m-01 00:00:00');
- $this->endMonth = date("Y-m-d 23:59:59");
- $this->month = date('Y-m');
- }
- /**
- * 奖金
- */
- public function bonus()
- {
- //创客补贴
- $this->real_subsidy_money();
- // 跨店奖励
- $this->stride_shop_money();
- //推荐创客收益
- $this->invite_shop_money();
- //大V经理分享奖
- $this->big_vip_money();
- //大V升级奖
- $this->big_vip_up_money();
- echo '执行成功';
- }
- public function stride_shop_money()
- {
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $sql = "select stride_shop_mer_id,sum(stride_shop_money) stride_shop_money from rrx_store_order o
- left join rrx_store_order_extend od on od.order_id=o.order_id
- 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
- group by od.stride_shop_mer_id";
- $mer_bonus_list = Db::query($sql);
- Db::name('log')->insert(['data' => "跨店奖励 mer_bonus_list:" . json_encode($mer_bonus_list)]);
- foreach ($mer_bonus_list as $mer_bonus) {
- $mer_id = $mer_bonus['stride_shop_mer_id'];
- $money = $mer_bonus['stride_shop_money'];
- if ($money < 0.01) {
- Db::name('log')->insert(['data' => "跨店奖励:mer_id:{$mer_id} 跨店奖励低于0.01无法入库 特此记录!"]);
- continue;
- }
- $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
- $mark = "跨店奖励" . $money;
- $title = "跨店奖励({$this->month}月)";
- $commission_type = 7;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
- }
- }
- public function invite_shop_money()
- {
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $sql = "select od.share_uid,sum(invite_shop_money) invite_shop_money from rrx_store_order o
- left join rrx_store_order_extend od on od.order_id=o.order_id
- 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";
- $user_bonus_list = Db::query($sql);
- Db::name('log')->insert(['data' => "推荐创客奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
- foreach ($user_bonus_list as $user_bonus) {
- $bonus_uid = $user_bonus['share_shop_uid'];
- $money = $user_bonus['invite_shop_money'];
- if ($money < 0.01) {
- Db::name('log')->insert(['data' => "推荐创客:bonus_uid:{$bonus_uid} 推荐创客低于0.01无法入库 特此记录!"]);
- continue;
- }
- $mark = "推荐创客收益" . $money;
- $title = "推荐创客收益({$this->month}月)";
- $commission_type = 10;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
- }
- }
- public function big_vip_money()
- {
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $sql = "select od.share_uid,sum(big_vip_money) big_vip_money from rrx_store_order o
- left join rrx_store_order_extend od on od.order_id=o.order_id
- 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";
- $user_bonus_list = Db::query($sql);
- Db::name('log')->insert(['data' => "大v奖励 user_bonus_list:" . json_encode($user_bonus_list)]);
- foreach ($user_bonus_list as $user_bonus) {
- $bonus_uid = $user_bonus['share_uid'];
- $money = $user_bonus['big_vip_money'];
- if ($money < 0.01) {
- Db::name('log')->insert(['data' => "大v分享奖:bonus_uid:{$bonus_uid} 大v分享奖低于0.01无法入库 特此记录!"]);
- continue;
- }
- $mark = "大V经理分享奖" . $money;
- $title = "大V经理分享奖({$this->month})";
- $commission_type = 16;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_uid, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
- }
- }
- public function real_subsidy_money()
- {
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $sql = "select o.mer_id mer_id,sum(real_subsidy_money) real_subsidy_money from rrx_store_order o
- left join rrx_store_order_extend od on od.order_id=o.order_id
- where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMonth' and '$this->endMonth' and od.real_subsidy_money>0
- group by o.mer_id";
- $mer_bonus_list = Db::query($sql);
- Db::name('log')->insert(['data' => "创客补贴 mer_bonus_list:" . json_encode($mer_bonus_list)]);
- foreach ($mer_bonus_list as $mer_bonus) {
- $mer_id = $mer_bonus['mer_id'];
- $money = $mer_bonus['real_subsidy_money'];
- if ($money < 0.01) {
- Db::name('log')->insert(['data' => "创客补贴:mer_id:{$mer_id} 创客补贴低于0.01无法入库 特此记录!"]);
- continue;
- }
- $merchant = Db::name('merchant')->where('mer_id', $mer_id)->find();
- $mark = "创客补贴" . $money;
- $title = "创客补贴({$this->month}月)";
- $commission_type = 19;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $merchant['uid'] ?? 0, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month, true);
- }
- }
- /**
- * 大V升级奖
- * 1. 查询大V经理列表
- * 2. 查询团队列表
- * 3. 查询团队这个月的总流水的百分之一分给大V经理
- * @return void
- */
- public function big_vip_up_money()
- {
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $user_list = Db::name("user")->field('uid')->where("user_group", 3)->select()->toArray();
- foreach ($user_list as $user) {
- $mer_id = 0;
- $user_id = $user['uid'];
- $sql = "select getUserSpreadId($user_id) as uids";
- $spread_user_list = Db::query($sql);
- $spread_user_list = trim($spread_user_list[0]['uids'],'$,');
- //查询可以分到的钱
- $sql = "select sum(big_vip_up_money) big_vip_up_money from rrx_store_order o
- left join rrx_store_order_extend od on od.order_id=o.order_id
- 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)";
- $big_vip_up = Db::query($sql)[0];
- $money = $big_vip_up['big_vip_up_money'] ?? 0;
- Db::name('log')->insert(['data' => "大V升级奖 big_vip_up_money {$user_id}:{$money}"]);
- if ($money < 0.01) {
- Db::name('log')->insert(['data' => "大v分享奖:big_vip_up_money:{$money} 大v分享奖低于0.01无法入库 特此记录!"]);
- continue;
- }
- $mark = "大V升级奖" . $money;
- $title = "大V升级奖({$this->month}月)";
- $commission_type = 10;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $user_id, $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
- }
- }
- /**
- * 用户升级
- * @throws DbException
- */
- public function member_up()
- {
- $uid_list = Db::name('store_order')
- ->where('mer_id', systemConfig('mer_365'))
- ->where('paid', 1)
- ->whereIn('status', [0, 1, 2, 3])
- ->group('uid')
- ->column('uid');
- Log::info('升级:' . json_encode($uid_list));
- foreach ($uid_list as $uid) {
- $pay_price = Db::name('store_order')
- ->where('mer_id', systemConfig('mer_365'))
- ->where('uid', $uid)
- ->where('paid', 1)
- ->where('is_settlement', 1)
- ->whereIn('status', [0, 1, 2, 3])
- ->sum('pay_price');
- if ($pay_price >= 365) {
- // 有没有直推20个经理
- $invite_count = Db::name('user')->where('spread_uid', $uid)->field('uid,user_group')->where('user_group', 2)->count();
- print_r("uid:{$uid}," . Db::name('user')->where('uid', $uid)->value('phone') . "邀请365经理人数{$invite_count} <br>");
- if ($invite_count >= 20) {
- $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [4, 5, 6])->update([
- 'user_group' => 3
- ]);
- if ($res) var_dump("uid:{$uid},升级大V经理");
- } else {
- $res = Db::name('user')->where('uid', $uid)->whereNotIn('user_group', [3, 4, 5, 6])->update([
- 'user_group' => 2
- ]);
- if ($res) var_dump("uid:{$uid},升级365会员");
- }
- }
- }
- }
- /**
- * 创客合伙人收益
- */
- private function partner_award()
- {
- //1. 被创客设置成创客合伙人,获得该项收益
- //已结算订单金额的
- //2%
- //1. 按月统计并结算
- //2. 计算公式:
- //所属创客店补贴*2%/创客合伙人总数
- // 推荐创客收益设置
- $commission_rate = systemConfig('commission_rate');
- Db::name('log')->insert(['data' => 'commission_rate:' . json_encode($commission_rate)]);
- $rate = $commission_rate['chuangkehehuoren_rate'];
- // 查询全部订单
- $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";
- $mer_order_list = Db::query($sql);
- Db::name('log')->insert(['data' => "创客合伙人收益 user_order_list:" . json_encode($mer_order_list)]);
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $mer_out_list = [];
- foreach ($mer_order_list as $mer_order) {
- $mer_id = $mer_order['mer_id'];
- $hehuo_user_list = Db::name('user')->field('uid')->where('mer_id', $mer_id)->where('is_shop_partner', 1)->select()->toArray();
- $money = decimal2($mer_order['total_price'] * $rate / 100);
- foreach ($hehuo_user_list as $hehuo_user) {
- $user_money = decimal2($money / count($hehuo_user_list));
- Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客合伙人人数:" . count($hehuo_user_list) . " 创客补贴比例{$rate}% 创客店铺订单流水:{$mer_order['total_price']} 获取创客推荐收益:{$money}!"]);
- if ($money == 0) {
- Db::name('log')->insert(['data' => "创客补贴:uid:{$hehuo_user['uid']} 创客补贴低于0.01无法入库 特此记录!"]);
- continue;
- }
- $mark = "创客合伙人收益" . $user_money;
- $title = "创客合伙人收益({$this->month}月)";
- $commission_type = 17;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $hehuo_user['uid'], $mark, $commission_type, null, 0, $mer_id, 0, 1, 8, 'commission', $this->month);
- }
- $money = decimal2($mer_order['total_price'] * $rate / 100);
- $mer_out_list[] = [
- $mer_order['mer_id'] => $money
- ];
- }
- Db::name('log')->insert(['data' => '跨店奖励 商户支出:' . json_encode($mer_out_list)]);
- return $mer_out_list;
- }
- /**
- * 代理区域收益
- */
- public function area_manage_details()
- {
- //1. 代理区域内已结算订单总额的百分
- //比;
- //2. 按照收货地址统计;
- //3. 奖励对象:省代、区县代;
- //区县代 5%
- //省代 2%
- //1. 按月结算:每月 1 号 0 点
- //统计上月已结算的订单金额
- //并对应发放佣金
- //2. 计算公式:
- //(1)无运费:收货地为该区域的已结算订单金额*对应比例
- //(2)有运费:(收货地为该区域的已结算订单金额-运费)*对应比例
- set_time_limit(0);
- ini_set('memory_limit', '-1');
- $commission_rate = systemConfig('commission_rate');
- Db::name('log')->insert(['data' => '代理区域收益 commission_rate:' . json_encode($commission_rate)]);
- $area_agency_rate = $commission_rate['area_agency_rate'] ?? 0;
- $province_agency_rate = $commission_rate['province_agency_rate'] ?? 0;
- $bonus_list = [];
- $bonus_user_list = Db::name('user')->field('uid,user_group,manage_address,spread_uid')->whereIn('user_group', [5, 6])->select()->toArray();
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- foreach ($bonus_user_list as $bonus_user) {
- $manage_address = $bonus_user['manage_address'];
- if (empty($manage_address))
- continue;
- $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%'";
- $total_price = Db::query($sql)[0]['total_price'] ?? 0;
- $rate = $bonus_user['user_group'] == 6 ? $province_agency_rate : $area_agency_rate;
- $money = decimal2($total_price * $rate / 100);
- Db::name('log')->insert(['data' => "代理区域收益:uid:{$bonus_user['uid']} 代理区域收益比例{$rate}% 流水:{$total_price} 收益:{$money} sql:" . $sql]);
- $mark = "代理区域收益" . $money;
- $title = "代理区域收益({$this->month}月)";
- $commission_type = 12;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus_user['uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month);
- $bonus_list[] = [
- 'uid' => $bonus_user['uid'],
- 'money' => $money,
- 'spread_uid' => $bonus_user['spread_uid'],
- ];
- }
- $this->invite_agency_award($bonus_list);
- echo '执行成功';
- }
- /**
- * 推荐代理收益
- * @param $bonus_list
- */
- private function invite_agency_award($bonus_list)
- {
- // 1. 推荐的代理收益的百分比
- //2. 奖励对象:所有用户
- //1. 按月结算:每个月 1 号 0点统计上月的代理收益并对应发放佣金
- //2. 计算公式:
- //推荐代理总收益*推荐代理收益百分比
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
- $this->month = date('Y-m', strtotime('-1 month'));
- $commission_rate = systemConfig('commission_rate');
- $rate = $commission_rate['tuijiandaili_rate'] ?? 0;
- foreach ($bonus_list as $bonus) {
- $money = decimal2($bonus['money'] * $rate / 100);
- $mark = "推荐代理收益" . $money;
- $title = "推荐代理收益({$this->month}月)";
- $commission_type = 15;
- $StoreOrderRepository->add_bill_gongfu($title, $money, $bonus['spread_uid'], $mark, $commission_type, null, 0, 0, 0, 1, 8, 'commission', $this->month, $this->month);
- }
- }
- /**
- * 生成商品图片缓存(最好用命令行执行)
- * @return \think\response\Json
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function make_product_share_cache()
- {
- set_time_limit(0);
- ini_set('memory_limit', '-1');
- $redis = Cache::store('redis')->handler();
- $key = 'make_product_share_cache';
- // $redis->del($key);
- // if (!$redis->setnx($key, 1))
- // return json(['code' => 400, 'msg' => '现在有正在执行的任务' ]);
- $redis->expire($key, 5 * 60);
- $product_share_ids = Db::name('product_share')
- // ->where('`url` is not null')
- ->group('product_id')
- ->column('product_id');
- $list = Db::name('store_product')
- ->whereNotIn('product_id', $product_share_ids)
- ->where('is_show', 1)
- ->limit(3)
- ->order('create_time desc')
- ->select()
- ->toArray();
- var_dump($list);
- $header = [
- 'X-Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0LmJqdGRiYS5jb20iLCJhdWQiOiJ0ZXN0LmJqdGRiYS5jb20iLCJpYXQiOjE2OTI0MjQ4MTEsIm5iZiI6MTY5MjQyNDgxMSwiZXhwIjoxNzAzMjI0ODExLCJqdGkiOls5NzIwLCJ1c2VyIl19.YtYf1ssMTz6fOO2rZVX-jb48L7AqXSKezQcbTHR3sCk'
- ];
- foreach ($list as $store_product) {
- $res1 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=1", null, 'get', $header);
- $res2 = Common::sendCurl("http://127.0.0.1:8324/api/product_share?pid={$store_product['product_id']}&type=2", null, 'get', $header);
- var_dump($res1, $res2);
- }
- $redis->del($key);
- return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
- }
- /**
- * 梳理每个人的公排人数
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function tease_user_count()
- {
- $list = Db::name('user')->where('is_del', 0)->select()->toArray();
- foreach ($list as $item) {
- $level_number = Db::name('user')->where('level_id', $item['uid'])->count();
- $user_count = Db::name('user_count')->where('uid', $item['uid'])->find();
- if ($level_number >= 5) {
- $res = Db::name('user_count')->where('uid', $item['uid'])->delete();
- if ($res) echo "{$item['uid']}公排人数已删除\r\n";
- continue;
- }
- if ($user_count) {
- if ($level_number == $user_count['level_number']) continue;
- Db::name('user_count')->where('uid', $item['uid'])->update([
- 'level_number' => $level_number
- ]);
- } else {
- Db::name('user_count')->insert([
- 'uid' => $item['uid'],
- 'level_number' => 0
- ]);
- $user_count['level_number'] = 0;
- }
- echo "{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n";
- Log::info("{$item['uid']}公排人数从{$user_count['level_number']}调整到{$level_number}人\r\n");
- }
- return json(['code' => 200, 'msg' => '生成商品分享缓存定时任务执行成功']);
- }
- /**
- * 下架微唯宝已下架的商品
- * @return mixed
- * @throws \think\db\exception\DbException
- */
- public function downGongProduct()
- {
- $start = input('start');
- $where = [
- 'is_used' => 1,
- 'mer_status' => 1,
- // 'type' => 5,
- // 'is_show' => 1,
- 'is_hide' => 0,
- // 'is_del' => 0
- ];
- if (!$start) {
- $product_data_count = Db::name('store_product')
- ->where($where)
- ->where("spu_id != '' or spu_id is not null")
- ->count();
- $start = mt_rand(0, $product_data_count);
- }
- $product_data = Db::name('store_product')
- ->where($where)
- ->where("spu_id != '' or spu_id is not null")
- ->where('product_id', '>', $start)
- ->order('product_id asc')
- ->limit(0, 50)
- ->select()
- ->toArray();
- foreach ($product_data as $item) {
- $spuIds = $item['spu_id'];
- $goods_status = GongApiRequest::goods_status($spuIds);
- $status = $goods_status[0]['status'] ?? 1;
- $goods_info = GongApiRequest::get_goods_info($spuIds);
- if ($status == 0 || (isset($goods_info['error_msg']) && $goods_info['error_msg'] == '没有查询到商品信息')) {
- Db::startTrans();
- try {
- // 删除选品库对应的数据
- Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
- 'status' => 9,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- // 删除商品管理对应的数据
- Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
- 'is_del' => 1,
- 'is_show' => 0
- ]);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
- return app('json')->fail($e->getMessage());
- }
- } else if ($status == 1 || !isset($goods_info['error_msg'])) {
- Db::startTrans();
- try {
- // 选品库对应的数据
- Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
- 'status' => 1,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- // 商品管理对应的数据
- Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
- 'is_del' => 0,
- 'is_show' => 1
- ]);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
- return app('json')->fail($e->getMessage());
- }
- }
- }
- return app('json')->success('操作成功');
- }
- /**
- * 定时获取 交易类型 1订单入账 2佣金提现支出 3养老金(红积分兑换订单)
- */
- public function platform_income_detail_get()
- {
- $merchant_list = Db::name('merchant')
- ->where('status', 1)
- ->where('mer_state', 1)
- ->where("mer_name != ''")
- ->column('mer_id');
- $Reconciliation = app()->make(Reconciliation::class);
- $unique_list = [];
- $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
- ->select()
- ->toArray();
- foreach ($enterprise_platform_income_detail_list as $item) {
- $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
- }
- foreach ($merchant_list as $mer_id) {
- $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
- $is_first = empty($enterprise_platform_income_detail_list);
- // 1自营订单入账金额=已结算订单金额-运费-养老金-汇付通道服务费; 订单结算的时候才统计
- $store_order_list = Db::name('store_order')
- ->where('mer_id', $mer_id)
- ->where('paid', 1)
- ->where('douhuomall', 0)
- ->where('is_settlement', 1)
- ->order('order_id', 'desc')
- // ->limit(500)
- ->select()
- ->toArray();
- foreach ($store_order_list as $store_order) {
- if (in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
- $pay_type = $store_order['pay_type'];
- if (!in_array($pay_type, [0, 1])) continue;
- $group_order = Db::name('store_group_order')
- ->alias('sgo')
- ->where('sgo.group_order_id', $store_order['group_order_id'])
- ->find();
- if (empty($group_order)) continue;
- $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
- 'shop_bonus_rate' => 30,
- 'yanglao_rate' => 5,
- ];
- $yanglao = Db::name('user_bill')
- ->where('order_sn', $store_order['order_sn'])
- ->where('commission_type', 5)
- ->value('number');
- $ru_money = 0;
- if ($pay_type != 0) {
- $store_split_order = Db::name('store_split_order')
- ->where('group_order_id', $group_order['group_order_sn'])
- ->where('status', 1)
- ->find();
- if (empty($store_split_order)) continue;
- if ($pay_type == 1) {
- //微信
- if ($group_order['pay_wx'] == 3) {
- $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- } else {
- $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
- }
- } else {
- //支付宝
- $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
- }
- $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
- $params['api_key'] = $api_key;
- //查询支付确认对象
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
- $res = $this->payment_confirm_query_traits($params);
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
- // 获取分账后一个工作日时间
- $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
- if (isset($res['status'])) {
- Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
- 'hf_fee_amt' => $res['fee_amt'],
- 'hf_clear_time' => $close_time
- ]);
- }
- if (isset($res['status']) && time() > strtotime($close_time)) {
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao - $res['fee_amt'];
- } else {
- $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) - $res['fee_amt'] + +$store_order['pay_postage'];
- }
- $store_order['pay_time'] = '';//线上支付的
- } else {
- continue;
- }
- } else {
- // 订单金额+运费
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao;
- } else {
- $ru_money = $store_order['pay_price'] * (1 - $shop_bonus_setting['shop_bonus_rate'] / 100) + $store_order['pay_postage'];
- }
- }
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $store_order['uid'],
- 'title' => '自营订单入账',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- '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')),
- 'money' => $ru_money,
- 'deal_type' => 1,
- 'order_id' => $store_order['order_id'],
- 'order_sn' => $store_order['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- unset($close_time);
- }
- //2佣金提现到账 提现成功统计
- // $user_extract_list = Db::name('user_extract')
- // ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
- // ->where('status', 1)
- // ->where('withdrawal_type', 3)
- // ->where('extract_type', '<>', 4)
- // ->when($uids, function ($query) use ($uids) {
- // $query->whereIn('uid', $uids);
- // })
- // ->order('extract_id', 'desc')
- //// ->limit(100)
- // ->select()
- // ->toArray();
- // foreach ($user_extract_list as $user_extract) {
- // if (in_array('2' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'], $unique_list)) continue;
- // $enterprise_platform_income_detail = Db::name('enterprise_platform_income_detail')
- // ->where('deal_type', 2)
- // ->where('user_extra_id', $user_extract['extract_id'])
- // ->find();
- // if (!$enterprise_platform_income_detail) {
- // Db::name('enterprise_platform_income_detail')
- // ->insert([
- // 'mer_id' => $mer_id,
- // 'uid' => $user_extract['uid'],
- // 'title' => '佣金提现支出',
- // 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- // 'deal_time' => $user_extract['status_time'],
- // 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
- // 'deal_type' => 2,
- // 'order_id' => '',
- // 'order_sn' => $user_extract['order_sn'],
- // 'user_extra_id' => $user_extract['extract_id'],
- // 'pm' => 1,
- // 'create_time' => date('Y-m-d H:i:s'),
- // ]);
- // }
- // }
- $start_time = '2024-04-01';
- for ($i = 0; $i < 1000; $i++) {
- $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
- if (strtotime($day) > time()) {
- break;
- }
- $Reconciliation->cron($day, $mer_id);
- }
- }
- // 计算平台
- //交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利
- $mer_id = 0;
- // 3 订单支付 供应链订单结算后入账到平台的金额
- $store_order_list = Db::name('store_order')
- ->where('paid', 1)
- ->order('order_id', 'desc')
- // ->limit(500)
- ->select()
- ->toArray();
- foreach ($store_order_list as $store_order) {
- if (in_array('3' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
- $group_order = Db::name('store_group_order')
- ->alias('sgo')
- ->where('sgo.group_order_id', $store_order['group_order_id'])
- ->find();
- if (empty($group_order)) continue;
- $store_split_order = Db::name('store_split_order')
- ->where('group_order_id', $group_order['group_order_sn'])
- ->where('status', 1)
- ->find();
- if (empty($store_split_order)) continue;
- $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $store_order['uid'],
- 'title' => '订单支付',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
- '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')),
- 'money' => $store_order['pay_price'] + $store_order['pay_postage'],
- 'deal_type' => 3,
- 'order_id' => $store_order['order_id'],
- 'order_sn' => $store_order['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- }
- //5用户佣金提现
- $user_extract_list = Db::name('user_extract')
- ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
- ->where('status', 1)
- ->order('extract_id', 'desc')
- // ->limit(100)
- ->select()
- ->toArray();
- foreach ($user_extract_list as $user_extract) {
- if (in_array('5' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'] . $mer_id, $unique_list)) continue;
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $user_extract['uid'],
- 'title' => '用户佣金提现',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- 'deal_time' => $user_extract['status_time'],
- 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
- 'deal_type' => 5,
- 'order_id' => '',
- 'order_sn' => $user_extract['order_sn'],
- 'user_extra_id' => $user_extract['extract_id'],
- 'pm' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- }
- // 6订单入账:支付成功的订单数据 供应链订单
- $store_order_list = Db::name('store_order')
- ->where('paid', 1)
- ->where('douhuomall', 1)
- ->where('is_settlement', 1)
- ->order('order_id', 'desc')
- // ->limit(500)
- ->select()
- ->toArray();
- foreach ($store_order_list as $store_order) {
- if (in_array('6' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
- $pay_type = $store_order['pay_type'];
- if (!in_array($pay_type, [0, 1])) continue;
- $group_order = Db::name('store_group_order')
- ->alias('sgo')
- ->where('sgo.group_order_id', $store_order['group_order_id'])
- ->find();
- if (empty($group_order)) continue;
- $yanglao = Db::name('user_bill')
- ->where('order_sn', $store_order['order_sn'])
- ->where('commission_type', 5)
- ->value('number');
- $ru_money = 0;
- $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
- 'shop_bonus_rate' => 30,
- 'yanglao_rate' => 5,
- ];
- if ($pay_type != 0) {
- $store_split_order = Db::name('store_split_order')
- ->where('group_order_id', $group_order['group_order_sn'])
- ->find();
- if (empty($store_split_order)) continue;
- if (!$store_order['hf_fee_amt']) {
- if ($pay_type == 1) {
- //微信
- if ($group_order['pay_wx'] == 3) {
- $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- } else {
- $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
- }
- } else {
- //支付宝
- $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
- }
- $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
- $params['api_key'] = $api_key;
- //查询支付确认对象
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
- $res = $this->payment_confirm_query_traits($params);
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
- if (isset($res['status'])) {
- Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
- 'hf_fee_amt' => $res['fee_amt'],
- 'hf_clear_time' => $close_time
- ]);
- $store_order['hf_fee_amt'] = $res['fee_amt'];
- }
- }
- // 获取分账后一个工作日时间
- $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
- if (isset($res['status']) && time() > strtotime($close_time)) {
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
- } else {
- $ru_money = $store_order['pay_price'] - $yanglao;
- }
- $store_order['pay_time'] = '';//线上支付的
- } else {
- continue;
- }
- } else {
- // 订单金额+运费
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao;
- } else {
- $ru_money = $store_order['pay_price'] - $yanglao;
- }
- }
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $store_order['uid'],
- 'title' => '订单入账',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- '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')),
- 'money' => $ru_money,
- 'deal_type' => 6,
- 'order_id' => $store_order['order_id'],
- 'order_sn' => $store_order['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- unset($close_time);
- }
- //7 创客自营订单让利=创客自营订单金额*分润比例-(创客自营订单金额-运费)*养老金比例
- $store_order_list = Db::name('store_order')
- ->where('paid', 1)
- ->where('is_settlement', 1)
- ->where('douhuomall', 0)
- ->order('order_id', 'desc')
- // ->limit(500)
- ->select()
- ->toArray();
- foreach ($store_order_list as $store_order) {
- if (in_array('7' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
- $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
- 'shop_bonus_rate' => 30,
- 'yanglao_rate' => 5,
- ];
- $pay_type = $store_order['pay_type'];
- if ($pay_type != 0) {
- $group_order = Db::name('store_group_order')
- ->alias('sgo')
- ->where('sgo.group_order_id', $store_order['group_order_id'])
- ->find();
- if (empty($group_order)) continue;
- $store_split_order = Db::name('store_split_order')
- ->where('group_order_id', $group_order['group_order_sn'])
- ->find();
- if (empty($store_split_order)) continue;
- $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
- if (!$store_order['hf_fee_amt']) {
- $store_split_order = Db::name('store_split_order')
- ->where('group_order_id', $group_order['group_order_sn'])
- ->where('status', 1)
- ->find();
- if (empty($store_split_order)) continue;
- if ($pay_type == 1) {
- //微信
- if ($group_order['pay_wx'] == 3) {
- $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- } else {
- $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
- }
- } else {
- //支付宝
- $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
- }
- $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
- $params['api_key'] = $api_key;
- //查询支付确认对象
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象参数-----' . json_encode($params)));//日志记录
- $res = $this->payment_confirm_query_traits($params);
- Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
- // 获取分账后一个工作日时间
- $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
- if (isset($res['status'])) {
- Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
- 'hf_fee_amt' => $res['fee_amt'],
- 'hf_clear_time' => $close_time
- ]);
- $store_order['hf_fee_amt'] = $res['fee_amt'];
- }
- }
- $yanglao = Db::name('user_bill')
- ->where('order_sn', $store_order['order_sn'])
- ->where('commission_type', 5)
- ->value('number');
- if (isset($res['status']) && time() > strtotime($close_time)) {
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao - $store_order['hf_fee_amt'];
- } else {
- $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
- }
- $store_order['pay_time'] = '';//线上支付的
- } else {
- continue;
- }
- }else{
- if ($store_order['mer_id'] == 496) {
- $ru_money = $store_order['pay_price'] - $yanglao;
- } else {
- $ru_money = ($store_order['pay_price'] * $shop_bonus_setting['shop_bonus_rate'] / 100) - $yanglao;
- }
- }
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $store_order['uid'],
- 'title' => '创客自营订单让利',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy4'),
- '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')),
- 'money' => $ru_money,
- 'deal_type' => 7,
- 'order_id' => $store_order['order_id'],
- 'order_sn' => $store_order['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- unset($close_time);
- }
- $start_time = '2024-04-01';
- for ($i = 0; $i < 1000; $i++) {
- $day = date('Y-m-d', strtotime("+{$i} days", strtotime($start_time)));
- if (strtotime($day) > time()) {
- break;
- }
- $Reconciliation->cron($day, 0);
- }
- return app('json')->success('获取平台收支明细成功');
- }
- public function clear_platform_income_detail()
- {
- Db::name('enterprise_platform_income_detail')->where('id', '>', 0)
- ->delete();
- }
- function getNextWorkday($date)
- {
- // 设置工作日数组
- $workdays = array(1, 2, 3, 4, 5); // 默认为周一到周五
- // 将日期字符串转换为时间戳
- $timestamp = strtotime($date);
- // 获取该日期的星期几
- $weekday = date('N', $timestamp);
- // 如果不是工作日,则增加一天直到是工作日
- while (!in_array($weekday, $workdays)) {
- $timestamp += (24 * 3600); // 增加一天
- $weekday = date('N', $timestamp); // 更新星期几
- }
- // 返回下一个工作日的日期字符串
- return date('Y-m-d H:i:s', $timestamp);
- }
- /**
- * @param $paymnet_params
- * @return array
- * 查询支付确认对象
- */
- public
- function payment_confirm_query_traits($paymnet_params)
- {
- $merber = new PaymentConfirm();
- $merber->query($paymnet_params);
- # 返回查询结果
- $merber->isError();
- return $merber->result;
- }
- //服务器配置,一般是默认配置
- private $CharSet = "UTF-8"; //设定邮件编码
- private $SMTPDebug = 0; // 调试模式输出
- private $isSMTP = true; // 使用SMTP
- private $Host = 'smtp.163.com'; // SMTP服务器
- private $SMTPAuth = true; // 允许 SMTP 认证
- private $Username = 'username'; // SMTP 用户名 即邮箱的用户名
- private $Password = 'MHIKZOXVPTGNVOMU'; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
- private $SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议
- private $Port = 465; // 服务器端口 25 或者465 具体要看邮箱服务器支持
- //修改成自己的邮箱
- private $From = 'formalverification@163.com'; //发件人邮箱
- private $FromName = 'makalo'; //发件人的用户昵称
- //邮件对象
- private $mail;
- //一般只有用户名和密码还有端口会变动, 所以这三个参数可以创建对象动态指定,也可以直接写死
- public function mallInit($Username = '', $Password = '', $Port = '')
- {
- if (!empty($Username)) {
- $this->Username = $Username;
- }
- if (!empty($Password)) {
- $this->Password = $Password;
- }
- if (!empty($Port)) {
- $this->Port = $Port;
- }
- //为true表示启用异常
- $mail = new PHPMailer(true);
- //服务器配置
- $mail->CharSet = $this->CharSet; //设定邮件编码
- $mail->SMTPDebug = $this->SMTPDebug; // 调试模121212 1式输出
- if ($this->isSMTP === true) {
- $mail->isSMTP(); // 使用SMTP
- }
- $mail->Host = $this->Host; // SMTP服务器
- $mail->SMTPAuth = $this->SMTPAuth; // 允许 SMTP 认证
- $mail->Username = $this->Username; // SMTP 用户名 即邮箱的用户名
- $mail->Password = $this->Password; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
- $mail->SMTPSecure = $this->SMTPSecure; // 允许 TLS 或者ssl协议
- $mail->Port = $this->Port; // 服务器端口 25 或者465 具体要看邮箱服务器支持
- $mail->setFrom($this->From, $this->FromName); //发件人
- $this->mail = $mail;
- }
- /**
- * 发送邮件
- * @param array $sendAddress 收件人地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
- * @param array $Attachment 附件数组,如果为单值数组则为添加附件,如果为键值数组则添加附件并且重命名
- * @param bool $isHtml 是否以html文档格式发送
- * @param string $Subject 邮件主题
- * @param string $Body 邮件内容,如果以html格式可以写入html
- * @param string $AltBody 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
- * @param array $CC 抄送地址数组, 如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
- * @param array $BCC 密送地址数组,如果为单值数组则只写入收件地址,如果为键值数组则还写入收件人名称
- * @return bool 是否发送成功
- */
- public function sendEMail($sendAddress = [], $Subject = '', $Body = '', $AltBody = '', $Attachment = [], $isHtml = true, $CC = [], $BCC = [])
- {
- try {
- //必填参数校验
- if (empty($sendAddress) || empty($Subject) || empty($Body)) {
- return false;
- }
- //收件人
- foreach ($sendAddress as $key => $value) {
- if (is_int($key)) {
- // 处理单值
- $this->mail->addAddress($value);
- } else {
- // 处理键值
- $this->mail->addAddress($value, $key);
- }
- }
- //附件
- if (!empty($Attachment)) {
- foreach ($Attachment as $key => $value) {
- if (is_int($key)) {
- // 处理单值
- $this->mail->addAttachment($value);
- } else {
- // 处理键值
- $this->mail->addAttachment($value, $key);
- }
- }
- }
- //抄送
- if (!empty($CC)) {
- //抄送不为空
- foreach ($CC as $key => $value) {
- if (is_int($key)) {
- // 处理单值
- $this->mail->addCC($value);
- } else {
- // 处理键值
- $this->mail->addCC($value, $key);
- }
- }
- }
- //密送
- if (!empty($BCC)) {
- //密送不为空
- foreach ($BCC as $key => $value) {
- if (is_int($key)) {
- // 处理单值
- $this->mail->addBCC($value);
- } else {
- // 处理键值
- $this->mail->addBCC($value, $key);
- }
- }
- }
- //回复的时候回复给哪个邮箱 建议和发件人一致
- $this->mail->addReplyTo($this->From, $this->FromName);
- //Content
- // 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
- $this->mail->isHTML($isHtml);
- //邮件主题
- $this->mail->Subject = $Subject;
- //邮件内容
- $this->mail->Body = $Body;
- //如果邮件客户端不支持HTML则显示此内容
- $this->mail->AltBody = $AltBody;
- //发送
- $this->mail->send();
- return [
- 'code' => 200,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 400,
- 'message' => $this->mail->ErrorInfo
- ];
- }
- }
- public function take()
- {
- set_time_limit(0);
- $this->mallInit('formalverification@163.com', 'SYECAWFLLTUPMDRP');
- $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "开始执行" . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
- $mallStr = '';
- // Log::info('执行一次1 '.date('H:i') );
- // return ;
- // $storeOrderRepository = app()->make(StoreOrderRepository::class);
- $key_list = [
- 1 => 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9',
- 2 => 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f',
- 3 => 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'
- ];
- $appid_list = [
- 1 => 'app_44108546-d27a-48ee-88c1-84b45b75114f',
- 2 => 'app_0827b960-b932-45ec-b800-4e850091b419',
- 3 => 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0'
- ];
- //
- // $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
- // $insertData = [
- // [
- // 'mid' => 0,
- // 'requestId' => $payment_id,
- // 'group_order_id' => 'WX171955791230553863',
- // 'splitAmount' => 0.25,
- // 'status' => 0
- // ],
- // ];
- // $div_members[] = [
- // 'member_id' => 'member_personal_16704641278422',
- // 'amount' => '1.00',
- // 'fee_flag' => 'Y'
- // ];
- // //////分账表
- // Db::name("store_split_order")->insertAll($insertData);
- // /// 分账表end
- //
- // $params = [
- // 'payment_id' => '002212024062814583210652657343702200320',
- // 'order_no' => $payment_id,
- // 'confirm_amt' => '1.00',
- // 'div_members' => $div_members
- // ];
- //
- // $params['api_key'] = $key_list[3];
- //
- // var_dump($params);
- // Db::name("log")->insert(array('data' => '汇付延时分账参数-----' . json_encode($params)));//日志记录
- // $res = $storeOrderRepository->payment_confirm_create_traits($params);
- // Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res)));//日志记录
- // var_dump($res);
- // // 分账
- // return;
- $merchant_member_list = Db::name('merchant_member')
- // ->where('member_id', 'member_personal_16704641278422')
- ->column('mer_id,type,member_id,settle_account_id');
- foreach ($merchant_member_list as $merchant_member) {
- $mer_id = $merchant_member['mer_id'];
- if (empty($mer_id))
- continue;
- if (empty($merchant_member['settle_account_id']))
- continue;
- $params = [
- 'app_id' => $appid_list[$merchant_member['type']],
- 'member_id' => $merchant_member['member_id'],
- 'settle_account_id' => $merchant_member['settle_account_id'],
- 'api_key' => $key_list[$merchant_member['type']]
- ];
- $merber = new SettleAccount();
- $merber->balance($params);
- $merber->result;
- // var_dump($merber->result);
- if (isset($merber->result[1])) {
- $result = json_decode($merber->result[1], true);
- $result = json_decode($result['data'], true);
- $result['avl_balance'] = $result['avl_balance'] ?? 0;
- if ($result['avl_balance'] > 0) {
- $payment_id = ShouxinyiApiRequest::getOrderNumber('split');
- $params = [
- 'order_no' => $payment_id,
- 'app_id' => $appid_list[$merchant_member['type']],
- 'cash_type' => 'T1',
- 'cash_amt' => sprintf("%01.2f", $result['avl_balance']),
- 'member_id' => $merchant_member['member_id'],
- 'api_key' => $key_list[$merchant_member['type']]
- ];
- $yanglao_member_ids = ['jsdk_member_wx21710232183139', 'jsdk_member_ali1710232187326'];
- if (in_array($merchant_member['member_id'], $yanglao_member_ids)) {
- $params['remark'] = '天地博爱科技~养老金';
- }
- Db::name("log")->insert(array('data' => '汇付取现参数-----' . json_encode($params)));//日志记录
- $merber = new Drawcash();
- $merber->create($params);
- # 返回查询结果
- $merber->isError();
- $res = $merber->result;
- Db::name("log")->insert(array('data' => '汇付延时分账-----' . json_encode($res))); //日志记录
- Db::name("log")->insert(array('data' => "mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----"));//日志记录
- var_dump("mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----");
- $mallStr = $mallStr . "-----mer_id {$mer_id} 请求分账{$result['avl_balance']}元-----<br/>";
- }
- }
- }
- echo date('Y-m-d H:i:s') . '执行结束' . "\r\n";
- $this->sendEMail(['389123492@qq.com', '420132441@qq.com'], '结算定时', "执行结束<br/>" . $mallStr . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');
- }
- use HuiPay;
- public function cesexx(){
- // $store_product = Db::name('store_product')->where('mer_id', 439)->select();
- // foreach ($store_product as $key=>$online) {
- // $spu_id = $online['spu_id'];
- // $product_id = $online['product_id'];
- // $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
- // if(!$data) continue;
- // //商品主图
- // $spuId_info=json_decode($data['spuId_info'],true);
- // $slider_image=$spuId_info['detail_img_list']??'';
- // $image=$spuId_info['cover_url']??'';
- // try {
- // $slider_image = implode(',',json_decode($slider_image,true));
- // }catch (\Exception $exception){
- // $slider_image = $image;
- // }
- // //更新产品图片数据
- // $product_insert_data=[
- // 'image'=>$image,
- // 'slider_image'=>$slider_image,
- // ];
- // Db::name('store_product')->where('product_id',$product_id)->update($product_insert_data);
- // echo '更新产品图片数据成功'.json_encode($product_insert_data);
- // echo '产品id:'.$product_id;
- // $detail = $spuId_info['detail'];
- // if(!is_null(json_decode($detail))){
- // $detail_list = json_decode($detail);
- // $detail = '';
- // foreach($detail_list as $value){
- // $detail .= "<img src='".$value."' referrerpolicy='no-referrer' /></img>";
- // }
- // }
- // Db::name('store_product_content')->where('product_id',$product_id)->update(['content'=>$detail]);
- // }
- // $cursor = Db::name('store_order')->where('mer_id', 439)->select();
- // foreach ($cursor as $key=>$online) {
- // // if($key != 0) continue;
- // $order_id = $online['order_id'];
- // $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();
- // foreach ($orderProduct as $_k=>&$_vv){
- // $_vv['cart_info'] = json_decode($_vv['cart_info'],true);
- // if(isset($_vv['cart_info']['product']['product_id'])){
- // $image = Db::name('store_product')->where('product_id', $_vv['cart_info']['product']['product_id'])->value('image');
- // if($image){
- // $_vv['cart_info']['product']['image'] = $image;
- // }
- // }
- // Db::name('store_order_product')->where('order_id', $order_id)->update(['cart_info'=> json_encode($_vv['cart_info'], true)]);
- // }
- // }
- // 普通订单
- // $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();
- // $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
- // $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
- // $OrderGzcRepository = app()->make(OrderGzcRepository::class);
- // foreach ($cursor as $online) {
- // if ($online['mer_id'] == 496) {
- // if (!in_array($online['status'], [0, 1, 2, 3])) {
- // continue;
- // }
- // }
- // $certification = UserCertification::getInstance()->where('uid', $online['uid'])->find();
- // if (!$certification) {
- // Log::info($online['uid'] . '没有实名认证');
- // // echo '没有实名认证';
- // continue;
- // }
- // //查询养老金金额
- // $UserBillRepository = app()->make(UserBillRepository::class);
- // $bill = $UserBillRepository->getWhere(['uid' => $online['uid'], 'link_id' => $online['order_id'], 'commission_type' => 5]);
- // $pension = $bill->number ?? 0;
- // if ($pension <= 0) {
- // echo '金额为为0:'.$online['order_sn'];
- // Log::info($online['order_sn'] . 'pension' . $pension);
- // continue;
- // }
- // Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository) {
- // //新增入账明细记录
- // $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
- // $create = [
- // 'uid' => $online['uid'],
- // 'platform_no' => $orderId,
- // 'account_type' => $online['pay_type'] == 1 ? 3 : 2,
- // 'user_name' => $certification['user_name'],
- // 'mobile' => $certification['mobile'],
- // 'user_card' => $certification['user_card'],
- // 'pension' => $pension,
- // 'order_type' => 2,
- // 'out_trade_no' => $online['order_sn']
- // ];
- // $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
- // $check = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
- // if ($test_user == 0 && $check <= 0) {
- // $OrderGzcRepository->create($create);
- // }
- // $order = StoreOrder::getInstance()->find($online['order_id']);
- // $order->gzc = 1;
- // $order->save();
- // });
- // $bill->gzc = 1;
- // $bill->is_gzc = 1;
- // $bill->save();
- // }
- //18995126502
- //会员直推荐佣金补1800,复购金补200,贡献值补4900,积分补4900,养老金补350
- // $user = Db::name("user")->where('uid', 910)->find();
- // $order_sn = 0;
- // $order_id = 0;
- // $order["mer_id"] = 0;
- // //sat存养老金
- // $tuiguanyanglaojin = '350';
- // $tuimark = "推荐消费获得养老金" . $tuiguanyanglaojin;
- // $tuititle = "养老金";
- // $commission_type = 5;
- // if ($tuiguanyanglaojin >= 0.01) {
- // $this->add_bill($tuititle, $tuiguanyanglaojin, $user["uid"], $tuimark, $commission_type, $order_sn, $order_id, $order["mer_id"], 0, 0, 0);
- // } else {
- // Db::name('log')->insert(['data' => '订单号:' . $order_sn . '付款额度较低,推荐养老金低于0.01无法入库 特此记录!']);
- // }
- // $tuiguanyongjin = '1800';
- // $tuiYongmark = "推荐消费获得佣金" . $tuiguanyongjin;
- // $tuiYongzhoutitle = "推广佣金";
- // $commission_yong_type = 3;
- // if ($tuiguanyongjin >= 0.01) {
- // $this->add_bill($tuiYongzhoutitle, $tuiguanyongjin, $user["uid"], $tuiYongmark, $commission_yong_type, $order_sn, $order_id, $order["mer_id"], 0, 0, 0);
- // } else {
- // Db::name('log')->insert(['data' => '订单号:' . $order_sn . '付款额度较低或 商家、平台设置的返佣比例较低,佣金低于0.01无法入库 特此记录!']);
- // }
- // $merchant_member_list = Db::name('order_merchant')
- // ->where('status', 0)->where('paid', 0)->where('create_time', '>', '2025-01-01 00:00:00')
- // ->column('id,mer_id,uid,out_trade_no,hf_pay_id,create_time');
- // $out_trade_no_list = [];
- // foreach ($merchant_member_list as $key => $value) {
- // if (strpos($value['out_trade_no'], 'ALI') !== false) {
- // //支付宝
- // $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
- // } else {
- // //微信
- // $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- // }
- // $params = [
- // 'api_key' => $api_key,
- // 'payment_id' => $value['hf_pay_id'],
- // ];
- // $res = $this->Payment_query_traits($params);
- // if (isset($res['status']) && $res['status'] == 'succeeded') {
- // $params['out_trade_no'] = $value['out_trade_no'];
- // // echo json_encode($params);
- // $out_trade_no_list[] = $value['out_trade_no'];
- // }
- // echo json_encode($res);
- // $params['out_trade_no'] = $value['out_trade_no'];
- // echo json_encode($params);
- // }
- // echo json_encode($out_trade_no_list);
- // $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
- // $params = [
- // 'api_key' => $api_key,
- // 'payment_id' => '002212025031014345810745060317006163968'
- // ];
- // $res = $this->Payment_query_traits($params);
- // echo json_encode($res);
- // $params['out_trade_no'] = $value['out_trade_no'];
- // echo json_encode($params);
- // echo json_encode($merchant_member_list);
- // echo 'xxx';
- }
- /**
- * 佣金加入账单
- * @param $title
- * @param $number
- * @param $uid
- * @param $mark
- * @param $commission_type
- * @param $order_sn
- */
- 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)
- {
- log::info("====进入add——bill");
- log::info("==uid==={$uid}");
- if ($uid == 0 || $number == 0) {
- return;
- }
- $number = sprintf('%.2f', $number);
- $data = [
- 'link_id' => $order_id,
- "uid" => $uid,
- "pm" => 1,
- "title" => $title,
- "category" => "now_money",
- "type" => "commission",
- "number" => $number,
- "balance" => 0,
- "mark" => $mark,
- // "create_time"=>date("Y-m-d H:i:s"),
- "status" => $status,
- "commission_type" => $commission_type,
- "order_sn" => $order_sn,
- "mer_id" => $mer_id,
- "source" => $source,
- "type_shop" => $type_shop
- ];
- log::info("开始添加user——bills");
- log::info($data);
- Db::name("user_bill")->insert($data);
- if ($type_shop == 0) {
- if ($commission_type == 5) {
- Db::name("user")->where("uid", $uid)->inc('annuity', $number)->update();
- Db::name("user")->where("uid", $uid)->inc('annuity_num', 1)->update();
- } else {
- Db::name("user")->where("uid", $uid)->inc('brokerage_price', $number)->update();
- }
- }
- }
- //定时任务写日账单,月账单
- public function cron($day,$mer_id=496)
- {
- Db::name('enterprise_day_bill')->where('mer_id', $mer_id)->where('day', $day)->delete();
- $day_flow_query = Db::name('enterprise_platform_income_detail')
- ->where('mer_id', $mer_id)
- ->when($mer_id, function ($query) use ($mer_id) {
- $query->where('deal_type', -1);//没有流水
- }, function ($query) use ($mer_id) {
- $query->whereIn('deal_type', [3]);
- })
- ->where('deal_type', 4)
- ->where('pm', 1);
- $day_flow_query_month = clone $day_flow_query;
- // 日订单流水
- $day_flow = $day_flow_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
- $accounting_query = Db::name('enterprise_platform_income_detail')
- ->where('mer_id', $mer_id)
- ->when($mer_id, function ($query) use ($mer_id) {
- $query->where('deal_type', 1);
- }, function ($query) use ($mer_id) {
- $query->whereIn('deal_type', [6,7]);
- })
- ->where('pm', 1);
- $accounting_query_month = clone $accounting_query;
- // 入账金额
- $day_accounting = $accounting_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
- $expenditure_query = Db::name('enterprise_platform_income_detail')
- ->when($mer_id, function ($query) use ($mer_id) {
- $query->where('mer_id', $mer_id)
- ->where('deal_type', -1);//没有流水
- }, function ($query) use ($mer_id) {
- $query->whereIn('deal_type', [4,5]);
- });
- $expenditure_query_month = clone $expenditure_query;
- // 日支出总额
- $day_expenditure = 0;
- if($mer_id==0){
- $day_expenditure = $expenditure_query->where("DATE(deal_time) = '" . $day . "'")
- ->whereIn('deal_type', [4,5])
- ->where('mer_id',0)
- ->sum('money');
- }
- $insert = [];
- $insert['mer_id'] = $mer_id;
- $insert['day'] = $day;
- $insert['flow'] = $day_flow;
- $insert['expenditure'] = $day_expenditure;
- $insert['accounting'] = $day_accounting;
- $previousDate = date('Y-m-d', strtotime($day . ' -1 day'));
- $balance_zuotian = Db::name('enterprise_day_bill')->where('day', $previousDate)->where('mer_id', $mer_id)->value('balance');
- $balance = $balance_zuotian ? $balance_zuotian : 0;
- if($mer_id){
- $tixian = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('extract_price');
- $service_money = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('service_money');
- $tixian -= $service_money;
- $ylj = Db::name('user_bill')->where('mer_id', $mer_id)->where('status', 1)->where("DATE(take_time) = '" . $day . "'")->sum('number');
- $insert['balance'] = $day_accounting - $tixian + $balance - $ylj;
- }else{
- $tixian = Db::name('enterprise_platform_income_detail')
- ->where("deal_time >'$day'")
- ->where("deal_time <'$day 23:59:59'")
- ->where('deal_type', 5)
- ->sum('money');
- $insert['balance'] = $day_accounting - $tixian + $balance ;
- }
- Db::name('enterprise_day_bill')->insertGetId($insert);
- $month_list[] = date('Y-m', strtotime('-4 month'));
- $month_list[] = date('Y-m', strtotime('-3 month'));
- $month_list[] = date('Y-m', strtotime('-2 month'));
- $month_list[] = date('Y-m', strtotime('-1 month'));
- $month_list[] = date('Y-m');
- foreach ($month_list as $month) {
- Db::name('enterprise_month_bill')->where('mer_id',$mer_id)->where('month', $month)->delete();
- $flow_query_month_clone = clone $day_flow_query_month;
- $accounting_query_month_clone = clone $accounting_query_month;
- $expenditure_query_month_clone = clone $expenditure_query_month;
- $startDate = date('Y-m-01 00:00:00', strtotime($month));
- $endDate = date("Y-m-d 00:00:00", strtotime('+1 month', strtotime($month)));
- // 月订单流水
- $money_flow = $flow_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
- // 月入账总额
- $money_accounting = $accounting_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
- // 月支出总额
- $money_expenditure = $expenditure_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
- $insert2 = [];
- $insert2['month'] = $month;
- $insert2['mer_id'] = $mer_id;
- $insert2['flow'] = $money_flow;
- $insert2['expenditure'] = $money_expenditure;
- $insert2['accounting'] = $money_accounting;
- Db::name('enterprise_month_bill')->insertGetId($insert2);
- }
- }
- /**
- * 生成平台收支明细,从而生成 日账单 月账单
- * 平台收入:线上订单表、线下订单表、话费表
- * 平台支出:用户提现、商户分账
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function platform_income_detail_get_new()
- {
- // 1、获取所有 商户 商户名称非空, 状态为正常、商户状态为开启
- $merchant_list = Db::name('merchant')
- ->where('status', 1)
- ->where('mer_state', 1)
- ->where("mer_name != ''")
- ->column('mer_id');
- // 2、获取 已有的 平台收支明细,并创建 唯一的 key
- $unique_list = [];
- // $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
- // ->select()
- // ->toArray();
- // foreach ($enterprise_platform_income_detail_list as $item) {
- // // deal_type : '交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利 '
- // // user_extra_id : 提现表id
- // $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
- // }
- // 3、获取订单信息
- // 查询 该商户 所有的 平台订单(已支付、平台订单、已结算)
- $store_order_list = Db::name('store_order')
- ->whereIn('mer_id', $merchant_list)
- ->where('paid', 1)
- ->where('douhuomall', 0)
- ->where('is_settlement', 1)
- ->order('order_id', 'desc')
- ->select()
- ->toArray();
- $store_order_map = array_reduce($store_order_list, function($result, $order) {
- // 根据 mer_id 进行分组
- $result[$order['mer_id']][] = $order;
- return $result;
- }, []);
- // 京豆
- $jingdou_list_by_online = Db::name("user_sign_jingdou")
- ->where('order_type', '=','0')
- ->where('status', '=',1)
- ->whereIn('order_id', array_column($store_order_list,'order_id'))
- ->select()
- ->toArray();
- $jingdou_map_by_online = array_reduce($jingdou_list_by_online, function($result, $jingdou_by_online) {
- // 根据 mer_id 进行分组
- $result[$jingdou_by_online['order_id']][] = $jingdou_by_online;
- return $result;
- }, []);
- // 线下扫码
- $order_merchant_list = Db::name('order_merchant')
- ->whereIn('mer_id', $merchant_list)
- ->where('status', 1) // '0待支付,1已支付,2申请退款,3退款完成 5 已失效',
- // ->where('arrive', 1) // 是否到账,系统应该是没对该字段进行维护 全是0
- ->where('paid', 1)
- ->where('binds', 0)
- ->whereNotNull('pay_time')
- ->order('id', 'desc')
- ->select()
- ->toArray();
- $order_merchant_map = array_reduce($order_merchant_list, function($result, $order) {
- // 根据 mer_id 进行分组
- $result[$order['mer_id']][] = $order;
- return $result;
- }, []);
- // 京豆
- $jingdou_list_by_offline = Db::name("user_sign_jingdou")
- ->where('order_type', '=','10')
- ->where('status', '=',1)
- ->whereIn('order_id', array_column($order_merchant_list,'id'))
- ->select()
- ->toArray();
- $jingdou_map_by_offline = array_reduce($jingdou_list_by_offline, function($result, $jingdou_by_offline) {
- // 根据 mer_id 进行分组
- $result[$jingdou_by_offline['order_id']][] = $jingdou_by_offline;
- return $result;
- }, []);
- // 话费
- $order_huafei_list = Db::name('order_huafei')
- ->where('status', 2) // '订单状态 0 待支付 1 已付 充值中 2充值成功 3充值失败 需要退款 4退款成功 5已超时 6待充值 7 已匹配 8已存单 9 已取消 10返销 11部分到账 12取消中'
- // ->where('arrive', 1) // 是否到账,系统应该是没对该字段进行维护 全是0
- // ->whereNotNull('finish_time')
- ->order('order_huafei_id', 'desc')
- ->select()
- ->toArray();
- // 清空数据表
- Db::execute("TRUNCATE TABLE `rrx_enterprise_platform_income_detail`");
- // 4、循环所有商户ID
- /** @var OrderMerchantRepository $OrderMerchantRepository */
- $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
- foreach ($merchant_list as $mer_id) {
- // 线上订单
- if (!empty($store_order_map[$mer_id])) {
- $online_data_list = [];
- foreach ($store_order_map[$mer_id] as $store_order) {
- // 判断该笔订单是否已 存在 平台收支明细表 的记录中
- if (!in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) {
- // 经过和领导确认,入账 金额为 订单金额 - 让利金额 - 手续费
- $money = 0.00;
- $vr = 0.00;
- $jd = 0.00;
- $fg = 0.00;
- $fenrun = $store_order['con_pri'] ?: 0.00;
- if (!empty($store_order['fzone_pay_type'])) {
- $cost = bcsub($store_order['total_price'], $fenrun, 2);
- if (in_array($store_order['fzone_pay_type'], ['a2', 'a3', 'b2', 'b4', 'b6'])) {
- $money = $cost;
- } else if (in_array($store_order['fzone_pay_type'], ['a1', 'b1', 'b5'])) {
- $vr = $cost;
- } else if (in_array($store_order['fzone_pay_type'], ['c1'])) {
- $fg = $cost;
- }
- }
- if (!empty($jingdou_map_by_online[$store_order['order_id']])) {
- $jd = array_sum(array_column($jingdou_map_by_online[$store_order['order_id']], 'number'));
- }
- $online_data_list[] = [
- 'mer_id' => $mer_id,
- 'uid' => $store_order['uid'],
- 'title' => '订单入账',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- 'deal_time' => $store_order['pay_time'] ?: date('Y-m-d H:i:s'),
- 'money' => $money,
- 'deal_type' => 1,
- 'order_type' => 0,
- 'order_id' => $store_order['order_id'],
- 'order_sn' => $store_order['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- 'vr' => $vr,
- 'jingdou' => $jd,
- 'fg' => $fg,
- 'fenrun' => $fenrun, // 让利
- ];
- }
- }
- if (!empty($online_data_list)) {
- Db::name('enterprise_platform_income_detail')
- ->insertAll($online_data_list);
- }
- }
- // 线下订单 0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 9话费 10 线下
- if (!empty($order_merchant_map[$mer_id])) {
- $offline_data_list = [];
- foreach ($order_merchant_map[$mer_id] as $order_merchant) {
- // 判断该笔订单是否已 存在 平台收支明细表 的记录中
- if (!in_array('1' . $order_merchant['uid'] . $order_merchant['out_trade_no'] . $order_merchant['id'] . '0' . $mer_id, $unique_list)) {
- // 经过和领导确认,入账 金额为 订单金额 - 让利金额 - 手续费
- $money = $order_merchant['mer_money'] ?: 0.00;
- $vr = 0.00;
- $jd = 0.00;
- $fg = 0.00;
- $fenrun = $order_merchant['mer_fanyong'] ?: 0.00;
- if (!empty($jingdou_map_by_offline[$order_merchant['id']])) {
- $jd = array_sum(array_column($jingdou_map_by_offline[$order_merchant['id']], 'number'));
- }
- $offline_data_list[] = [
- 'mer_id' => $mer_id,
- 'uid' => $order_merchant['uid'],
- 'title' => '订单入账',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- 'deal_time' => $order_merchant['pay_time'] ?: date('Y-m-d H:i:s'),
- 'money' => $money,
- 'deal_type' => 1,
- 'order_type' => 10,
- 'order_id' => $order_merchant['id'],
- 'order_sn' => $order_merchant['out_trade_no'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- 'vr' => $vr,
- 'jingdou' => $jd,
- 'fg' => $fg,
- 'fenrun' => $fenrun,
- ];
- }
- }
- if (!empty($offline_data_list)) {
- Db::name('enterprise_platform_income_detail')
- ->insertAll($offline_data_list);
- }
- }
- }
- $mer_id = 0;
- $order_huafei_data_list = [];
- foreach ($order_huafei_list as $order_huafei) {
- if (!in_array('1' . $order_huafei['uid'] . $order_huafei['order_sn'] . $order_huafei['order_huafei_id'] . '0' . $mer_id, $unique_list)) {
- $order_huafei_data_list[] = [
- 'mer_id' => $mer_id,
- 'uid' => $order_huafei['uid'],
- 'title' => '订单入账',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- 'deal_time' => $order_huafei['finish_time'] ?: date('Y-m-d H:i:s'),
- 'money' => $order_huafei['cost_price'],
- 'deal_type' => 1,
- 'order_type' => 9,
- 'order_id' => $order_huafei['order_huafei_id'],
- 'order_sn' => $order_huafei['order_sn'],
- 'user_extra_id' => '',
- 'pm' => 1,
- 'create_time' => date('Y-m-d H:i:s'),
- 'vr' => 0.00,
- 'jingdou' => 0.00,
- 'fg' => 0.00,
- 'fenrun' => $order_huafei['fanli_price'] // 让利
- ];
- }
- }
- if (!empty($order_huafei_data_list)) {
- Db::name('enterprise_platform_income_detail')
- ->insertAll($order_huafei_data_list);
- }
- // 5用户佣金提现
- $user_extract_list = Db::name('user_extract')
- ->field('extract_id,uid,extract_type,extract_price,service_money,order_sn,status_time')
- ->where('status', 1)
- ->order('extract_id', 'desc')
- ->select()
- ->toArray();
- foreach ($user_extract_list as $user_extract) {
- if (in_array('5' . $user_extract['uid'] . $user_extract['order_sn'] . '0' . $user_extract['extract_id'] . $mer_id, $unique_list)) continue;
- Db::name('enterprise_platform_income_detail')
- ->insert([
- 'mer_id' => $mer_id,
- 'uid' => $user_extract['uid'],
- 'title' => '用户佣金提现',
- 'deal_order_sn' => $OrderMerchantRepository->getNewOrderId('jy2'),
- 'deal_time' => $user_extract['status_time'],
- 'money' => bcsub($user_extract['extract_price'], $user_extract['service_money'], 2),
- 'deal_type' => 5,
- 'order_id' => '',
- 'order_sn' => $user_extract['order_sn'],
- 'user_extra_id' => $user_extract['extract_id'],
- 'pm' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- }
- // // 查询分账信息
- // $store_split_order = Db::name('store_split_order')
- // ->where('status', 1)
- // ->select()
- // ->toArray();
- // $store_split_order_map = array_column($store_split_order, null, 'group_order_id');
- // 待完善
- $this->set_enterprise_bill();
- return app('json')->success('获取平台收支明细成功');
- }
- public function set_enterprise_bill()
- {
- // 1、查出 时间范围内的 平台收支明细
- $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
- // deal_time格式 2025-03-10 10:44:34
- // ->whereBetween('deal_time', [$start_time, $end_time])
- ->select()
- ->toArray();
- // 2、根据时间进行分组
- $enterprise_platform_income_detail_map = array_reduce($enterprise_platform_income_detail_list, function ($result, $enterprise_platform_income_detail) {
- // 根据 mer_id 进行分组
- $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;
- return $result;
- }, []);
- // 3、清空表数据
- Db::execute("TRUNCATE TABLE `rrx_enterprise_day_bill`");
- Db::execute("TRUNCATE TABLE `rrx_enterprise_month_bill`");
- // 4、循环计算平台当天内的 收支流水
- $month_bill_map = [];
- $day_bill_map = [];
- foreach ($enterprise_platform_income_detail_map as $month => $enterprise_platform_income_detail_list_by_month) {
- // 平台的日账单
- $month_bill_map[$month . '-0'] = [
- 'mer_id' => 0,
- 'month' => $month,
- 'flow' => 0.00,
- 'expenditure' => 0.00,
- 'accounting' => 0.00,
- 'balance' => 0.00,
- 'jingdou' => 0.00,
- 'fenrun' => 0.00,
- 'vr' => 0.00,
- 'fg' => 0.00
- ];
- foreach ($enterprise_platform_income_detail_list_by_month as $day => $enterprise_platform_income_detail_list_by_day) {
- // 平台的日账单
- $day_bill_map[$day . '-0'] = [
- 'mer_id' => 0,
- 'day' => $day,
- 'flow' => 0.00,
- 'expenditure' => 0.00,
- 'accounting' => 0.00,
- 'balance' => 0.00,
- 'jingdou' => 0.00,
- 'fenrun' => 0.00,
- 'vr' => 0.00,
- 'fg' => 0.00
- ];
- foreach ($enterprise_platform_income_detail_list_by_day as $enterprise_platform_income_detail) {
- if (!isset($day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']])) {
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']] = [
- 'mer_id' => $enterprise_platform_income_detail['mer_id'],
- 'day' => $day,
- 'flow' => 0.00,
- 'expenditure' => 0.00,
- 'accounting' => 0.00,
- 'balance' => 0.00,
- 'jingdou' => 0.00,
- 'fenrun' => 0.00,
- 'vr' => 0.00,
- 'fg' => 0.00
- ];
- }
- if (!isset($month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']])) {
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']] = [
- 'mer_id' => $enterprise_platform_income_detail['mer_id'],
- 'month' => $month,
- 'flow' => 0.00,
- 'expenditure' => 0.00,
- 'accounting' => 0.00,
- 'balance' => 0.00,
- 'jingdou' => 0.00,
- 'fenrun' => 0.00,
- 'vr' => 0.00,
- 'fg' => 0.00
- ];
- }
- if($enterprise_platform_income_detail['pm'] == 1) {
- // 订单总价
- $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']);
- $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']);
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $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']);
- $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']);
- }
- // 入账现金
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['accounting'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['accounting'] += $enterprise_platform_income_detail['money'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['accounting'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-0']['accounting'] += $enterprise_platform_income_detail['money'];
- }
- // VR
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] += $enterprise_platform_income_detail['vr'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] += $enterprise_platform_income_detail['vr'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['vr'] += $enterprise_platform_income_detail['vr'];
- $month_bill_map[$month . '-0']['vr'] += $enterprise_platform_income_detail['vr'];
- }
- // 复购
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] += $enterprise_platform_income_detail['fg'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] += $enterprise_platform_income_detail['fg'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['fg'] += $enterprise_platform_income_detail['fg'];
- $month_bill_map[$month . '-0']['fg'] += $enterprise_platform_income_detail['fg'];
- }
- // 分润
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] += $enterprise_platform_income_detail['fenrun'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] += $enterprise_platform_income_detail['fenrun'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['fenrun'] += $enterprise_platform_income_detail['fenrun'];
- $month_bill_map[$month . '-0']['fenrun'] += $enterprise_platform_income_detail['fenrun'];
- }
- // 获得的京豆
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] += $enterprise_platform_income_detail['jingdou'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] += $enterprise_platform_income_detail['jingdou'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['jingdou'] += $enterprise_platform_income_detail['jingdou'];
- $month_bill_map[$month . '-0']['jingdou'] += $enterprise_platform_income_detail['jingdou'];
- }
- // 现金结余
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] += $enterprise_platform_income_detail['money'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['balance'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-0']['balance'] += $enterprise_platform_income_detail['money'];
- }
- }
- if($enterprise_platform_income_detail['pm'] == 0) {
- // 支出现金
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['expenditure'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['expenditure'] += $enterprise_platform_income_detail['money'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['expenditure'] += $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-0']['expenditure'] += $enterprise_platform_income_detail['money'];
- }
- // VR
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] -= $enterprise_platform_income_detail['vr'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['vr'] -= $enterprise_platform_income_detail['vr'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['vr'] -= $enterprise_platform_income_detail['vr'];
- $month_bill_map[$month . '-0']['vr'] -= $enterprise_platform_income_detail['vr'];
- }
- // 复购
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] -= $enterprise_platform_income_detail['fg'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fg'] -= $enterprise_platform_income_detail['fg'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['fg'] -= $enterprise_platform_income_detail['fg'];
- $month_bill_map[$month . '-0']['fg'] -= $enterprise_platform_income_detail['fg'];
- }
- // 分润
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
- $month_bill_map[$month . '-0']['fenrun'] -= $enterprise_platform_income_detail['fenrun'];
- }
- // 获得的京豆
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
- $month_bill_map[$month . '-0']['jingdou'] -= $enterprise_platform_income_detail['jingdou'];
- }
- // 现金结余
- $day_bill_map[$day . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] -= $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-' . $enterprise_platform_income_detail['mer_id']]['balance'] -= $enterprise_platform_income_detail['money'];
- if ($enterprise_platform_income_detail['mer_id'] != 0) {
- $day_bill_map[$day . '-0']['balance'] -= $enterprise_platform_income_detail['money'];
- $month_bill_map[$month . '-0']['balance'] -= $enterprise_platform_income_detail['money'];
- }
- }
- }
- }
- }
- // 5、写入数据
- Db::name('enterprise_day_bill')->insertAll(array_values($day_bill_map));
- Db::name('enterprise_month_bill')->insertAll(array_values($month_bill_map));
- }
- }
|