| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\jobs\LgCashJob;
- use app\models\AccountLog;
- use app\models\Cash;
- use app\models\District;
- use app\models\Goods;
- use app\models\Lg;
- use app\models\Md;
- use app\models\MdStaff;
- use app\models\OldUserTreePath;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\ReOrder;
- use app\models\SaasUser;
- use app\models\ShareDetail;
- use app\models\ShareHolder;
- use app\models\ShareHolderAreaAgentApply;
- use app\models\ShareHolderLevel;
- use app\models\ShareHolderOutLog;
- use app\models\Store;
- use app\models\User;
- use app\models\UserTreePath;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\ShareQrcodeForm;
- use app\utils\OrderNo;
- use app\utils\Share\BonusPool;
- use yii\base\BaseObject;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use app\models\OrderRefund;
- use app\models\ShareHolderFrostLog;
- use app\models\StoreMoneyLog;
- use app\models\UserShareMoney;
- use app\utils\FuGou;
- use app\models\FugouCart;
- use app\models\ShareHolderFugouFrostLog;
- use app\utils\Alipay\Alipay;
- use app\utils\LgApi;
- class ShareHolderController extends BaseController
- {
- /**
- * 股东申请
- */
- public function actionApply() {
- $user_id = get_user_id();
- $store_id = get_store_id();
- $name = post_params('name');
- $mobile = post_params('mobile');
- $province = post_params('province');
- $province_name = post_params('province_name');
- $city = post_params('city');
- $city_name = post_params('city_name');
- $district = post_params('district');
- $district_name = post_params('district_name');
- $address = post_params('address');
- if (empty($name) || empty($mobile) || empty($province) || empty($city) || empty($district) || empty($address)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- $province_name = District::findOne($province)->name ?: '';
- $city_name = District::findOne($city)->name ?: '';
- $district_name = District::findOne($district)->name ?: '';
- if (empty($province_name) || empty($city_name) || empty($district_name)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- // TODO: 申请股东的条件
- $check_res = BonusPool::checkApply($store_id, $user_id);
- if ($check_res['code'] > 0) {
- return $this->asJson($check_res);
- }
- $admin = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['in', 'status', [1, 0]])->one();
- if ($admin) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前已成为股东或存在待审核记录'
- ]);
- }
- $share_level = ShareHolderLevel::findOne(['is_default' => 1, 'store_id' => get_store_id(), 'is_delete' => 0]);
- $share_holder = new ShareHolder();
- $share_holder->level_id = $share_level->id;
- $share_holder->store_id = get_store_id();
- $share_holder->user_id = $user_id;
- $share_holder->name = $name;
- $share_holder->address = $address;
- $share_holder->mobile = $mobile;
- $share_holder->province = $province;
- $share_holder->city = $city;
- $share_holder->district = $district;
- $share_holder->province_name = $province_name;
- $share_holder->city_name = $city_name;
- $share_holder->district_name = $district_name;
- if ($share_holder->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '提交成功'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => $share_holder->errors[0]
- ]);
- }
- /**
- * 等级
- * @return \yii\web\Response
- */
- public function actionHolderLevel() {
- $user_id = get_user_id();
- $store_id = get_store_id();
- $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]);
- if (!$share_holder) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '不好意思,您不是股东',
- ]);
- }
- $share_holder_level = ShareHolderLevel::findOne($share_holder->level_id);
- if (!$share_holder_level) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前数据不存在',
- ]);
- }
- $share_holder_level_arr = ShareHolderLevel::find()->where(['is_delete' => 0, 'store_id' => $store_id])->orderBy('level asc')->asArray()->all();
- foreach ($share_holder_level_arr as &$level) {
- $tmp = [];
- $condition = Json::decode($level['condition']);
- //佣金总额
- $amount_total = $condition['amount_total'];
- if (intval($amount_total['is_open'])) {
- $msg = "佣金总额达到";
- $title = "佣金总额";
- $total_price = get_user()->total_price;
- if ($total_price < $amount_total['value']) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'amount_total',
- 'require' => $amount_total['value'],
- 'value' => $total_price ?: 0
- ];
- }
- $man = $condition['man'];
- if ((bool)$man['is_open']) {
- $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
- foreach ((array)$man['value'] as $m) {
- $man_data = OldUserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->andWhere([
- 'AND', [
- '>', 'child_level', $share_user_parent_tree->parent_level
- ], [
- '<=', 'child_level', ($share_user_parent_tree->parent_level + (int)$m['level'])
- ]
- ])->asArray()->all();
- $man_data_count = count($man_data);
- // $msg = $m['level'] . '级团队总人数达到' . $m['count'];
- // $man_data = OldUserTreePath::find()->where([
- // 'parent_id' => $user_id
- // ])->andWhere([
- // '!=',
- // 'child_id',
- // $user_id
- // ])->asArray()->all();
- $man_data_count = $man_data_count ?: 0;
- $msg = "累计人数";
- $title = "团队人数";
- if ($man_data_count < $m['count']) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'man',
- 'require' => $m['count'],
- 'value' => $man_data_count,
- ];
- if (isset($m['money'])) {
- //消费达到$m['money']以上允许升级
- $child_id = array_column($man_data, 'child_id');
- $pay_price = Order::find()->where(
- [
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'user_id' => $child_id
- ])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
- $is_ok = 1;
- if ($m['money'] > $pay_price) {
- $is_ok = 0;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $m['level'] . '级内团队人数' . $m['count'] . '且团队消费',
- 'title' => '团队人数&消费金额',
- 'icon' => 'man',
- 'require' => $m['money'],
- 'value' => $pay_price ?: 0,
- ];
- }
- }
- }
- // 团队消费金额
- $order = $condition['order'];
- if ((bool)$order['is_open']) {
- $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
- foreach ($order['value'] as $o) {
- $man_data = OldUserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->andWhere([
- 'AND', [
- '>=', 'child_level', $share_user_parent_tree->parent_level
- ], [
- '<=', 'child_level', ($share_user_parent_tree->parent_level + ($o['level'] ?: 0))
- ]
- ])->asArray()->all();
- // $man_user_id = array_column($man_data, 'id');
- // 删除自己会员ID
- // $man_user_id = array_diff($man_user_id, [$user_id]);
- $man_user_id = array_column($man_data, 'child_id');
- $man_user_id[] = $user_id;
- $order_money = Order::find()
- ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM])
- ->andWhere(['in', 'user_id', $man_user_id])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
- ->sum('pay_price');
- $msg = $o['level'] . '级内团队总订单金额';
- // $msg = "总订单累计";
- $title = "团队订单";
- if ($order_money < $o['count']) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'order',
- 'require' => $o['count'],
- 'value' => $order_money ?: 0
- ];
- }
- }
- // 自身消费金额
- $self = $condition['self'];
- if ((bool)$self['is_open']) {
- // 兼容老版本
- if (!isset($self['value']['order_number'])) {
- $self_price = $self['value'];
- $self['value'] = [];
- $self['value']['order_number'] = 0;
- $self['value']['price'] = $self_price;
- }
- if ($self['value']['order_number'] == 1) {
- $order = Order::find()
- ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
- ->andWhere(['>=' , 'pay_price', $self['value']['price']])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
- ->one();
- // $msg = '自己消费单笔金额达到' . $self['value']['price'] . '元';
- $msg = "自己单笔消费金额";
- $title = "账户单笔消费";
- $max_order = Order::find()
- ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
- ->orderBy('pay_price desc')->asArray()->one();
- if (!$order) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $self['value']['price'] == 0 ? 1 : $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'self',
- 'require' => $self['value']['price'],
- 'value' => $max_order ? $max_order['pay_price'] : 0,
- ];
- } else {
- $order_money = Order::find()
- ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
- ->sum('pay_price');
- // $msg = '自己消费金额达到' . $self['value']['price'] . '元';
- $msg = "自己消费金额";
- $title = "账户消费";
- if ($order_money < $self['value']['price']) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'self',
- 'require' => $self['value']['price'],
- 'value' => $order_money ?: 0
- ];
- }
- }
- // 自身充值金额
- $recharge = $condition['recharge'];
- if ((bool)$recharge['is_open']) {
- $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price');
- // $msg = '充值金额达到' . $recharge['value'] . '元';
- $msg = "充值金额";
- $title = "账户充值";
- if ($recharge_money < $recharge['value']) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- $tmp[] = [
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'recharge',
- 'require' => $recharge['value'],
- 'value' => $recharge_money ?: 0
- ];
- }
- // 购买指定商品
- $goods = $condition['goods'];
- if ((bool)$goods['is_open']) {
- // 查找该用户是否买过此商品
- $goods_model = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
- ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM])
- ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
- ->andWhere(['od.goods_id' => $goods['value']['id']])->one();
- $goods_order_count = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
- ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM])
- ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
- ->andWhere(['od.goods_id' => $goods['value']['id']])->count();
- $msg = '购买指定商品';
- $title = "购买商品";
- // 是否过了售后期
- $pay_time = $goods_model->pay_time;
- if (!$goods_model) {
- $is_ok = 0;
- } else {
- if (time() < ($pay_time + $goods['day'] * 24 * 3600)) {
- $is_ok = 0;
- } else {
- $is_ok = 1;
- }
- }
- $tmp[] = [
- 'goods_id' => $goods['value']['id'],
- 'status' => $is_ok,
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'goods',
- 'require' => 1,
- 'value' => $goods_order_count ?: 0
- ];
- }
- // 等级升级
- $shareholder = $condition['shareholder'];
- if ($shareholder['is_open']) {
- $num = User::find()->alias('u')
- ->leftJoin(['h' => ShareHolder::tableName()], 'h.user_id = u.id')
- ->where([
- 'u.old_parent_id' => $user_id,
- 'h.status' => 1,
- 'h.is_delete' => 0,
- ])
- ->andWhere(['>=', 'h.level_id', $condition->shareholder->to_level_id])
- ->limit($condition->shareholder->num)//感觉不用加这个 下面有数组截取 但是不加怕查询太慢
- ->orderBy('h.created_at ASC')
- ->select('u.id')
- ->count();
- $share_level = ShareHolderLevel::findOne(['id' => $shareholder['to_level_id'], 'store_id' => $store_id, 'is_delete' => 0]);
- // $msg = '您在本商城发展的股东达到' . $shareholder['num'] . '位等级为'.$share_level->name;
- $msg = "升级为" . $share_level->name;
- $title = "团队下级账户升级";
- $tmp[] = [
- 'status' => $num >= $shareholder['num'],
- 'msg' => $msg,
- 'title' => $title,
- 'icon' => 'man',
- 'require' => $shareholder['num'],
- 'value' => $num
- ];
- }
- $level['text'] = $tmp;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'level_id' => $share_holder->level_id,
- 'level_res' => $share_holder_level_arr
- ]
- ]);
- }
- /**
- * 团队
- * @return \yii\web\Response
- */
- public function actionGroup() {
- $user_id = get_user_id();
- $store_id = get_store_id();
- $type = (int)get_params('type');
- $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
- \Yii::warning([$user_id, $store_id]);
- if ($type === 1) {//团队数据/团队中心—链动
- $share_user_parent_tree = UserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
- $man_data = UserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->andWhere([
- 'AND', [
- '>=', 'child_level', $share_user_parent_tree->parent_level
- ], [
- '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1))
- ]
- ])->asArray()->all();
- $child_user_id = array_column($man_data, 'child_id');
- $list = User::find()->where(['id' => $child_user_id])
- ->select('id, binding, nickname, avatar_url, store_id, parent_id')
- ->asArray()->all();
- // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.status' => 1])
- // ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
- // ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
- // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')
- // ->asArray()->all();
- } elseif ($type === 2) {
- $list = User::find()->alias('u')->where(['u.old_parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.is_out' => 1])->andWhere(['IS NOT', 'sh.id', NULL])
- ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id')
- // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')
- ->groupBy('u.id')->asArray()->all();
- foreach ($list as &$item) {
- $item['parent_id'] = 0;
- }
- $user_id = 0;
- } else {//股东中心-原推荐关系
- $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
- $man_data = OldUserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->andWhere([
- 'AND', [
- '>=', 'child_level', $share_user_parent_tree->parent_level
- ], [
- '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1))
- ]
- ])->asArray()->all();
- $child_user_id = array_column($man_data, 'child_id');
- $list = User::find()->where(['id' => $child_user_id])->select('id, binding, nickname, avatar_url, store_id, old_parent_id parent_id')
- ->asArray()->all();
- }
- $tree = UserTreePath::formatChildren($list, $user_id);
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'res' => $tree,
- 'count' => UserTreePath::$team_count ?: 0
- ]
- ]);
- }
- /**
- * 极差分润订单
- * @return \yii\web\Response
- */
- public function actionShareOrder() {
- $order_no = get_params('order_no');
- $name = get_params('name');
- $time = get_params('time');
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $type = get_params('type');
- // $query = ShareDetail::find()->alias('sd')->leftJoin(['o' => Order::tableName()], 'o.id=sd.type_id')
- // ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
- // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- // ->where(['sd.store_id' => get_store_id(), 'sd.is_delete' => 0, 'sd.type' => [ShareDetail::TYPE_RANGE_PROFIT, ShareDetail::TYPE_RANGE_PROFIT_OLD, ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL], 'sd.user_id' => get_user_id()]);
- // if ($name) {
- // $query->andWhere(['like', 'su.name', $name]);
- // }
- // if ($order_no) {
- // $query->andWhere(['o.order_no' => $order_no]);
- // }
- // if ($type) {
- // $query->andWhere(['sd.type' => [2, 5, 6, 9]]);
- // }
- // if ($time) {
- // $query->andWhere([
- // 'and',
- // ['>=', 'o.created_at', strtotime($time . '-01')],
- // ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
- // ]);
- // }
- // $total = $query->sum('sd.money');
- //获取团队订单金额数据
- $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
- $team_list = User::find()->childs(get_user_id(), true, $team_num)->andWhere(['store_id' => get_store_id()])->select('id')->column();
- $query = Order::find()->alias('o')
- ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['o.store_id' => get_store_id(), 'o.user_id' => $team_list, 'o.is_delete' => 0, 'o.trade_status' => [Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]]);
- if ($name) {
- $query->andWhere(['like', 'su.name', $name]);
- }
- if ($order_no) {
- $query->andWhere(['o.order_no' => $order_no]);
- }
- if ($time) {
- $query->andWhere([
- 'and',
- ['>=', 'o.created_at', strtotime($time . '-01')],
- ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
- ]);
- }
- $total = $query->sum('o.total_price');
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('su.name nickname, su.avatar, o.id, o.pay_time, o.order_no, o.pay_price, o.total_price range_profit, o.is_pay, o.trade_status')->orderBy('o.created_at desc')->groupBy('o.id')
- ->limit($pagination->limit)
- ->offset($pagination->offset)->asArray()->all();
- foreach ($list as &$value) {
- $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all();
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'total' => $total,
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ]
- ]);
- }
- /**
- * 极差分润订单
- * @return \yii\web\Response
- */
- public function actionShareOrderList()
- {
- $order_no = input_params('order_no');
- $name = input_params('name');
- $type = input_params('type');// 区分链动
- $is_area = input_params('is_area', 1); //只显示区域分红
- $trade_status = input_params('trade_status');
- $query = Order::find()->alias('o')->leftJoin(['sd' => ShareDetail::tableName()], 'sd.type_id = o.id')
- ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
- ->where(['o.store_id' => get_store_id()])
- ->andWhere(['<>', 'o.trade_status', 1]);
- $query->andWhere(['sd.user_id' => get_user_id()]);
- if($trade_status > -1){
- $query->andWhere(['o.trade_status' => $trade_status]);
- }
- if ($name) {
- $query->andWhere(['like', 'u.nickname', $name]);
- }
- $type_where = ['in', 'sd.type', [2, 5, 6, 7, 8, 9]];
- if ($type) {
- $type_where = ['in', 'sd.type', [2, 5, 6, 9]];
- }
- if ($is_area) {
- $type_where = ['AND', ['sd.type' => 3], ['>', 'sd.money', '0']];
- }
- $type_where = ['OR', $type_where, ['OR', ['>', 'holder_first_price', 0], ['>', 'holder_second_price', 0], ['>', 'holder_third_price', 0],
- ['>', 'old_holder_first_price', 0], ['>', 'old_holder_second_price', 0], ['>', 'old_holder_third_price', 0]]];
- $query->andWhere($type_where);
- if ($order_no) {
- $query->andWhere(['o.order_no' => $order_no]);
- }
- $query->select('o.id, o.pay_time created_at, o.trade_status, sd.money, o.id type_id, u.nickname, u.avatar_url, o.pay_time, o.order_no, o.id order_id, o.holder_first_price,
- o.holder_second_price, o.holder_third_price, o.old_holder_first_price, o.old_holder_second_price, o.old_holder_third_price,o.express_price')
- ->orderBy('o.id desc')->groupBy('o.id');
- $list = pagination_make($query);
- foreach ($list['list'] as &$value) {
- $value['order_pay_price'] = 0;
- $order = Order::find()->select('pay_price')->where(['order_no' => $value['order_no']])->one();
- if ($order) {
- $value['order_pay_price'] = $order->pay_price;
- }
- $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['type_id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all();
- if ($type) {
- $value['range_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_RANGE_PROFIT, 'type_id' => $value['type_id']])->sum('money');
- } else {
- $value['range_profit'] = ShareDetail::find()->where(['type' => [ShareDetail::TYPE_RANGE_PROFIT, ShareDetail::TYPE_RANGE_PROFIT_OLD, ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL], 'type_id' => $value['type_id']])->sum('money');
- }
- $value['agent_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_AREA_PROFIT, 'type_id' => $value['type_id']])->sum('money');
- $share_holder_price = bcadd($value['old_holder_first_price'],
- bcadd($value['old_holder_second_price'],
- bcadd($value['old_holder_third_price'],
- bcadd($value['holder_first_price'],
- bcadd($value['holder_second_price'], $value['holder_third_price'], 2)
- , 2)
- , 2)
- , 2)
- , 2);
- $value['share_holder_price'] = '¥' . $share_holder_price;
- }
- return $this->asJson([
- 'q' => $query->createCommand()->getRawSql(),
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ]);
- }
- /**
- * 股东分销
- * @return \yii\web\Response
- */
- public function actionHolderShareOrder() {
- $time = get_params('time');
- $page = get_params('page', 1);
- $limit = get_params('limit', 10);
- $user_id = get_user_id();
- $query = Order::find()->where(['store_id' => get_store_id(), 'trade_status' => [
- Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM
- ]])->andWhere(['OR',
- ['AND', ['old_parent_id' => $user_id], ['>', 'old_holder_first_price', '0']],
- ['AND', ['old_parent_id_1' => $user_id], ['>', 'old_holder_second_price', '0']],
- ['AND', ['old_parent_id_2' => $user_id], ['>', 'old_holder_third_price', '0']],
- ['AND', ['parent_id' => $user_id], ['>', 'holder_first_price', '0']],
- ['AND', ['parent_id_1' => $user_id], ['>', 'holder_second_price', '0']],
- ['AND', ['parent_id_2' => $user_id], ['>', 'holder_third_price', '0']]
- ]);
- if ($time) {
- $query->andWhere([
- 'and',
- ['>=', 'created_at', strtotime($time . '-01')],
- ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
- ]);
- }
- $total_data = (clone $query)->select('old_parent_id, old_holder_first_price, old_parent_id_1, old_holder_second_price,
- old_parent_id_2, old_holder_third_price, parent_id, holder_first_price, parent_id_1, holder_second_price, parent_id_2,
- holder_third_price')->asArray()->all();
- $total = 0;
- foreach ($total_data as $total_item) {
- if ($total_item['old_parent_id'] == $user_id) {
- $total = bcadd($total, $total_item['old_holder_first_price'], 2);
- }
- if ($total_item['old_parent_id_1'] == $user_id) {
- $total = bcadd($total, $total_item['old_holder_second_price'], 2);
- }
- if ($total_item['old_parent_id_2'] == $user_id) {
- $total = bcadd($total, $total_item['old_holder_third_price'], 2);
- }
- if ($total_item['parent_id'] == $user_id) {
- $total = bcadd($total, $total_item['holder_first_price'], 2);
- }
- if ($total_item['parent_id_1'] == $user_id) {
- $total = bcadd($total, $total_item['holder_second_price'], 2);
- }
- if ($total_item['parent_id_2'] == $user_id) {
- $total = bcadd($total, $total_item['holder_third_price'], 2);
- }
- }
- $query->select('id, pay_time, order_no, pay_price, is_pay, trade_status, old_parent_id, old_holder_first_price,
- old_parent_id_1, old_holder_second_price, old_parent_id_2, old_holder_third_price, parent_id, holder_first_price,
- parent_id_1, holder_second_price, parent_id_2, holder_third_price')->orderBy('created_at desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$value) {
- $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']])
- ->select('goods_name, num, attr, pic, total_price')->asArray()->all();
- $value['money'] = 0;
- if ($value['old_parent_id'] == $user_id) {
- $value['source'] = 1;
- $value['money'] = bcadd($value['money'], $value['old_holder_first_price'], 2);
- }
- if ($value['old_parent_id_1'] == $user_id) {
- $value['source'] = 2;
- $value['money'] = bcadd($value['money'], $value['old_holder_second_price'], 2);
- }
- if ($value['old_parent_id_2'] == $user_id) {
- $value['source'] = 3;
- $value['money'] = bcadd($value['money'], $value['old_holder_third_price'], 2);
- }
- if ($value['parent_id'] == $user_id) {
- $value['source'] = 1;
- $value['money'] = bcadd($value['money'], $value['holder_first_price'], 2);
- }
- if ($value['parent_id_1'] == $user_id) {
- $value['source'] = 2;
- $value['money'] = bcadd($value['money'], $value['holder_second_price'], 2);
- }
- if ($value['parent_id_2'] == $user_id) {
- $value['source'] = 3;
- $value['money'] = bcadd($value['money'], $value['holder_third_price'], 2);
- }
- $is_send = UserShareMoney::find()->where([
- 'type' => 2,
- 'user_id' => $user_id,
- 'order_id' => $value['id'],
- 'source' => $value['source']
- ])->asArray()->one();
- $is_send_text = '(未发放)';
- if ($is_send) {
- $is_send_text = '(已发放)';
- }
- $value['order_no'] .= $is_send_text;
- }
- $list['total'] = $total;
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list
- ]);
- }
- // public function actionAuditStatus() {
- // $user_id = get_user_id();
- // $share_holder = ShareHolder::find()->where(['user_id' => $user_id, 'store_id' => get_store_id(), 'is_delete' => 0])->orderBy('id desc')->limit(1)->one();
- // if (!$share_holder) {
- // return $this->asJson([
- // 'code' => 0,
- // 'msg' => '没有审核记录',
- // 'data' => ['status' => -1]
- // ]);
- // }
- // return $this->asJson([
- // 'code' => 0,
- // 'data' => ['status' => $share_holder->status]
- // ]);
- // }
- /**
- * 股东中心
- * @return \yii\web\Response
- */
- public function actionIndex() {
- $user_id = get_user_id();
- $store_id = get_store_id();
- $type = get_params('type');
- $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
- if (!$share_holder && $user_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未有权限,请先申请成为股东'
- ]);
- }
- $total_price = get_user()->total_price;
- $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]);
- $share_detail_query = ShareDetail::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'user_id' => $user_id]);
- $share_detail_query_1 = ShareDetail::find()->alias('sd')->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id])
- // ->leftJoin(OrderRefund::tableName() . ' r', "r.order_id = sd.type_id and r.is_delete = 0")
- // ->andWhere([
- // 'or',
- // 'isnull(r.id)',
- // ['r.type' => 2],
- // ['in', 'r.status', [2, 3]]
- // ]);
- ->leftJoin(['o' => Order::tableName()], 'sd.type_id = o.id')->andWhere(['!=', 'o.trade_status', 1]);
- if ($type) {
- $share_detail_query->andWhere(['type' => [2, 5, 6, 9]]);
- $share_detail_query_1->andWhere(['in', 'sd.type', [2, 5, 6, 9]]);
- }
- $share_detail_query->andWhere(['<=', 'type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]);
- $share_detail_query_1->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]);
- $order_count_q = clone $share_detail_query;
- $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
- $team_list = User::find()->alias('u')->childs(get_user_id(), true, $team_num)->andWhere(['u.store_id' => get_store_id()])->select('u.id')->column();
- $order_count = Order::find()->alias('o')
- ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['o.store_id' => get_store_id(), 'o.user_id' => $team_list, 'o.is_delete' => 0, 'o.trade_status' => [Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]])->count();
- // $total_price = $order_count_q->sum('money');
- // $cash_profit = $order_count_q->andWhere(['is_send' => 1])->sum('money');
- $cash_profit = get_user()->price;
- if ($user_id) {
- $cash_price = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id])->andWhere(['in', 'status', [2, 4]])->sum('price');
- $cash_price_apply = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'cash_type' => 3])->andWhere(['in', 'status', [0, 1]])->sum('price');
- // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id])->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all();
- $team_count = User::find()->alias('u')
- ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
- ->where(['sh.is_delete' => 0, 'sh.status' => 1])
- ->andWhere([
- 'u.parent_id' => $user_id
- ])
- ->count();
- }
- // $cash = get_user()->price;
- $agent_data = [];
- $district = '';
- if ($share_holder) {
- if ((int)$share_holder->agent_type === 1) {
- $agent_data = [$share_holder->province_id];
- } elseif ((int)$share_holder->agent_type === 2) {
- $agent_data = [$share_holder->province_id, $share_holder->city_id];
- } elseif ((int)$share_holder->agent_type === 3) {
- $agent_data = [$share_holder->province_id, $share_holder->city_id, $share_holder->district_id];
- }
- }
- $district_ = District::find()->where(['id' => $agent_data])->select('name')->column();
- if (!empty($district_)) {
- $district = implode('', $district_);
- }
- $data = [
- 'total_price' => $total_price,
- 'is_send' => $share_detail_query->andWhere(['is_send' => 1])->sum('money') ?: 0,
- 'no_send' => $share_detail_query_1->andWhere(['is_send' => 0])->sum('money') ?: 0,
- 'cash_price' => $cash_price ?? 0,
- 'cash_price_apply' => $cash_price_apply ?? '0.00',
- 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url,
- 'name' => $saas_user ? $saas_user->name : (get_user()->nickname ?: ''),
- 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: '',
- 'cash_profit' => sprintf("%.2f", $cash_profit),
- 'team_count' => $team_count ?? 0,
- 'id' => $user_id,
- 'order_count' => $order_count,
- 'agent_type' => (int)$share_holder->agent_type ?: 0,
- 'district' => $district
- ];
- return $this->asJson([
- 'code' => 0,
- 'data' => $data
- ]);
- }
- //团队数据
- public function actionGetTeamInfo() {
- try {
- //
- $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]);
- $user_id = get_user_id();
- $store_id = get_store_id();
- if ($user_id) {
- $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
- if (!$share_holder) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未有权限,请先申请成为股东'
- ]);
- }
- // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id])
- // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all();
- $team_count = User::find()->alias('u')
- ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
- ->where(['sh.is_delete' => 0, 'sh.status' => 1])
- ->andWhere([
- 'u.parent_id' => $user_id
- ])
- ->count();
- $list = User::findOld()->alias('u')->childs($user_id, false)->andWhere(['u.store_id' => $store_id])->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all();
- UserTreePath::formatChildren($list, $user_id);
- $team_count = UserTreePath::$team_count;
- $shareHolderOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.parent_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
- ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->select('su.name, su.avatar')
- ->asArray()->all();
- // $parentOutList = User::find()->alias('u')->where(['u.old_parent_id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL])
- // ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id')
- // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
- // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- // ->select('su.name, su.avatar')
- // ->groupBy('u.id')
- // ->asArray()->all();
- $parentOutList = User::find()->alias('u')->where(['u.old_parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.is_out' => 1])->andWhere(['IS NOT', 'sh.id', NULL])
- ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id')
- // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->select('su.name, su.avatar')
- ->groupBy('u.id')
- ->asArray()->all();
- // $parentOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
- // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
- // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- // ->select('su.name, su.avatar')
- // ->asArray()->all();
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url,
- 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '',
- 'id' => $user_id,
- 'team_count' => $team_count ?? '',
- 'parent_count' => count($parentOutList ?? []),
- 'team' => $shareHolderOutList ?? [],
- 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: ''
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- /**
- * 佣金列表
- * @return \yii\web\Response
- */
- public function actionProfitList() {
- $store_id = get_store_id();
- $user_id = get_user_id();
- $time = get_params('time');
- $type = get_params('type');
- $status = get_params('status');
- $page = get_params('page', 1);
- $limit = get_params('limit', 20);
- //区域代理
- $is_area = get_params('is_area');
- $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
- if (!$share_holder && $user_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未有权限,请先申请成为股东'
- ]);
- }
- $query = ShareDetail::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['>','money',0]);
- if ($type) {
- $query->andWhere(['type' => [2, 5, 6, 9]]);
- }
- if ($is_area) {
- $query->andWhere(['type' => 3]);
- }
- //今日奖励
- $t_query = clone $query;
- $today_money = $t_query->andWhere(['AND',
- ['>=', 'created_at', strtotime(date('Y-m-d'))],
- ['<', 'created_at', strtotime(date('Y-m-d') . '+1 day')]
- ])->select('money')->sum('money');
- //总待结算金额
- $no_send_query = clone $query;
- $no_send_money = $no_send_query->andWhere(['is_send' => 0])->select('money')->sum('money');
- //总已结算金额
- $send_query = clone $query;
- $send_money = $send_query->andWhere(['is_send' => 1])->select('money')->sum('money');
- if (in_array($status, [0, 1]) && !is_null($status)) {
- $query->andWhere(['is_send' => $status]);
- }
- if ($time) {
- $query->andWhere([
- 'and',
- ['>=', 'created_at', strtotime($time . '-01')],
- ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
- ]);
- }
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->orderBy('created_at desc')
- ->limit($pagination->limit)
- ->offset($pagination->offset)->asArray()->all();
- $range_name = Option::get('range_name', get_store_id(), 'bonus_pool', '')['value'];
- $holder_name = Option::get('holder_name', get_store_id(), 'bonus_pool', '')['value'];
- $area_name = Option::get('area_name', get_store_id(), 'bonus_pool', '')['value'];
- $old_range_name = Option::get('old_range_name', get_store_id(), 'bonus_pool', '')['value'];
- $direct_range_name = Option::get('direct_range_name', get_store_id(), 'bonus_pool', '')['value'];
- $old_add_holder_name = Option::get('old_add_holder_name', get_store_id(), 'bonus_pool', '')['value'];
- $saas_user = get_saas_user();
- foreach ($list as &$value) {
- $value['is_refund'] = $value['type'] == ShareDetail::TYPE_HOLDER_PROFIT ? 0 : Order::is_refund($value['store_id'], $value['type_id']);
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['send_time'] = $value['send_time'] ? date('Y-m-d H:i:s', $value['send_time']) : '';
- $value['profit_name'] = '分红';
- if ($value['type'] == ShareDetail::TYPE_HOLDER_PROFIT) {
- // $value['profit_name'] = $holder_name ?: '股东分红';
- $value['profit_name'] = $holder_name ?: '股东分红';
- }
- if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT) {
- // $value['profit_name'] = $range_name ?: '级差分红';
- $value['profit_name'] = $range_name ?: '级差分红';
- }
- if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD) {
- // $value['profit_name'] = $range_name ?: '级差分红(原关系)';
- $value['profit_name'] = $old_range_name ?: '级差分红(原关系)';
- }
- if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT_OLD) {
- // $value['profit_name'] = $range_name ?: '直属团队收益分成(原关系)';
- $value['profit_name'] = $old_add_holder_name ?: '直属团队收益分成(原关系)';
- }
- if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT) {
- // $value['profit_name'] = $range_name ?: '直属团队收益分成';
- $value['profit_name'] = $direct_range_name ?: '直属团队收益分成';
- }
- if ($value['type'] == ShareDetail::TYPE_AREA_PROFIT) {
- // $value['profit_name'] = $area_name ?: '区域分红';
- $value['profit_name'] = $area_name ?: '区域分红';
- }
- if ($value['type'] == ShareDetail::TYPE_FROST_PROFIT) {
- // $value['profit_name'] = $range_name ?: '冻结金额释放';
- $value['profit_name'] = '冻结金额释放';
- }
- if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL) {
- // $value['profit_name'] = $range_name ?: '冻结金额释放';
- $value['profit_name'] = '商城会员等级分红';
- }
- $value['trade_status'] = 0;
- if ((int)$value['type'] !== 1) {
- $order = Order::findOne($value['type_id']);
- if ($order) {
- $value['trade_status'] = (int)$order->trade_status;
- }
- }
- }
- $send_type_where = [];
- if ($type) {
- $send_type_where = ['sd.type' => [2, 5, 6, 9]];
- }
- if ($is_area) {
- $send_type_where = ['sd.type' => 3];
- }
- $total = ShareDetail::find()->alias('sd')
- ->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id])
- ->leftJoin(['o' => Order::tableName()], 'o.id = sd.type_id')
- ->andWhere(['or', ['!=', 'o.trade_status', 1], ['sd.type_id' => 0]])
- ->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL])
- ->andWhere($send_type_where)
- ->sum('money') ?? '0.00';
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'total' => $total,
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- 'today_money' => $today_money ?? '0.00',
- 'no_send_money' => $no_send_money ?? '0.00',
- 'send_money' => $send_money ?? '0.00',
- 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url ?? '',
- 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '',
- 'id' => $user_id,
- 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?? '',
- ]
- ]);
- }
- /**
- * 获取佣金相关
- */
- public function actionGetPrice()
- {
- $store_id = get_store_id();
- $user_id = get_user_id();
- $is_area = input_params('is_area');
- $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
- if (!$share_holder) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- $cash = get_user()->price;
- //区分区域代理
- $groups = 'bonus_pool';
- if ($is_area) {
- $groups = 'bonus_pool_area_agent';
- if (intval($share_holder->agent_type) === 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- }
- $shareHolderCashProfit = Option::get('shareHolderCashProfit', $this->store_id, $groups, 0)['value'];
- $res = [
- 'cash_profit' => $cash,
- 'shareHolderCashProfit' => $shareHolderCashProfit
- ];
- // 新提现方式
- $res['cash_method_list'] = [];
- $saas_user = get_saas_user();
- $cash_method = [];
- $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one();
- if (!empty($saas_user->withdraw_method)) {
- $cash_method = json_decode($saas_user->withdraw_method, true);
- if ($lg){
- //灵工
- $cash_method = array_merge($cash_method, [[
- 'type' => 'lg'
- ]]);
- }
- if (is_array($cash_method)) {
- foreach($cash_method as $i => $item){
- if ($item['type'] === 'wechat' && Option::get('pay_wechat', $this->store_id, $groups, 0)['value'] == 0) {
- unset($cash_method[$i]);
- }
- if ($item['type'] === 'alipay' && Option::get('pay_alipay', $this->store_id, $groups, 0)['value'] == 0) {
- unset($cash_method[$i]);
- }
- if ($item['type'] === 'bank_card' && Option::get('bank', $this->store_id, $groups, 0)['value'] == 0) {
- unset($cash_method[$i]);
- }
- if ($item['type'] === 'lg' && Option::get('lg', $this->store_id, $groups, 0)['value'] == 0) {
- unset($cash_method[$i]);
- }
- }
- }
- }else{
- if($lg){
- //灵工
- $cash_method[]['type'] = 'lg';
- $res['cash_method_list'] = array_values($cash_method);
- }
- }
- if(Option::get('remaining_sum', $this->store_id, $groups, 0)['value'] == 1){
- $cash_method = array_merge($cash_method, [['type' => 'money']]);
- }
- $res['cash_method_list'] = array_values($cash_method);
- return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]);
- }
- /**
- * 获取提现方式
- */
- public function actionGetCashMethod()
- {
- $saas_user = get_saas_user();
- $cash_method = [];
- $is_area = input_params('is_area');
- $groups = 'bonus_pool';
- //区分区域代理
- if ($is_area) {
- $groups = 'bonus_pool_area_agent';
- }
- $payTypeList = [
- [
- 'name' => '微信',
- 'is_show' => intval(Option::get('pay_wechat', $this->store_id, $groups, 0)['value']),
- 'type' => 'wechat',
- 'is_bind' => false
- ],
- [
- 'name' => '支付宝',
- 'is_show' => intval(Option::get('pay_alipay', $this->store_id, $groups, 0)['value']),
- 'type' => 'alipay',
- 'is_bind' => false
- ],
- [
- 'name' => '银行卡',
- 'is_show' => intval(Option::get('bank', $this->store_id, $groups, 0)['value']),
- 'type' => 'bank_card',
- 'is_bind' => false
- ],
- [
- 'name' => '灵工',
- 'is_show' => intval(Option::get('lg', $this->store_id, $groups, 0)['value']),
- 'type' => 'lg',
- 'is_bind' => false
- ],
- ];
- $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one();
- if (!empty($saas_user->withdraw_method)) {
- $decode = json_decode($saas_user->withdraw_method, true);
- if ($lg){
- //灵工
- $decode = array_merge($decode, [[
- 'type' => 'lg'
- ]]);
- }
- if (is_array($decode)) {
- $cash_method = $decode;
- foreach ($payTypeList as &$pay_item) {
- foreach ($cash_method as $item) {
- if ($item['type'] === $pay_item['type']) {
- $pay_item['is_bind'] = true;
- $pay_item = array_merge($pay_item, $item);
- }
- }
- }
- }
- }else{
- if($lg){
- //灵工
- $cash_method[]['type'] = 'lg';
- $res['cash_method_list'] = array_values($cash_method);
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $payTypeList,
- ],
- ]);
- }
- /**
- * 获取提现方式
- */
- public function actionDelCashMethod()
- {
- $saas_user = get_saas_user();
- $type = post_params('type');
- if (empty($type)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- if ($type == 'lg'){
- $lg = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
- if ($lg){
- $lg->is_delete = 1;
- $lg->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- }
- if (empty($saas_user->withdraw_method)) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- $cash_method = json_decode($saas_user->withdraw_method, true);
- if (is_array($cash_method)) {
- foreach ($cash_method as $key => $value) {
- if ($value['type'] == $type) {
- unset($cash_method[$key]);
- }
- }
- $saas_user->withdraw_method = json_encode(array_values($cash_method));
- } else {
- $saas_user->withdraw_method = '';
- }
- if ($saas_user->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作失败',
- ]);
- }
- /**
- * 添加提现方式
- */
- public function actionAddCashMethod()
- {
- $type = post_params('type');
- if (empty($type)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误',
- ]);
- }
- $name = post_params('name');
- $account = post_params('account');
- $bank = post_params('bank');
- $cash_img = post_params('cash_img');
- if (empty($name) || empty($account)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '账号和姓名不能为空',
- ]);
- }
- if ($type == 'bank_card' && empty($bank)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '银行不能为空',
- ]);
- }
- $data = [
- 'type' => $type,
- 'name' => $name,
- 'account' => $account,
- 'cash_img' => $cash_img,
- ];
- if ($type == 'bank_card') {
- $data['bank'] = $bank;
- }
- $saas_user = get_saas_user();
- if (empty($saas_user->withdraw_method)) {
- $saas_user->withdraw_method = json_encode(array_values([$data]));
- } else {
- $decode = json_decode($saas_user->withdraw_method, true);
- if (!$decode) {
- $saas_user->withdraw_method = json_encode(array_values([$data]));
- } else {
- foreach ($decode as $key => $value) {
- if ($value['type'] == $type) {
- unset($decode[$key]);
- break;
- }
- }
- $decode[] = $data;
- $saas_user->withdraw_method = json_encode(array_values($decode));
- }
- }
- if ($saas_user->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败',
- ]);
- }
- public function actionCashApply() {
- checkReplay([get_user_id()]);
- $price = post_params('cash');
- $type = post_params('type');
- $is_area = post_params('is_area');
- if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat', 'money','lg'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数非法',
- ]);
- }
- $user_id = get_user_id();
- $user = get_user();
- $store_id = get_store_id();
- $groups = 'bonus_pool';
- $cash_type = 3;
- if ($is_area) {
- $groups = 'bonus_pool_area_agent';
- $cash_type = Cash::IS_CASH_TYPE_AREA_AGENT;
- }
- $cash_max_day = Option::get('cash_max_day', $store_id, $groups, 0)['value'];
- $cash_max_single_day = Option::get('cash_max_single_day', $store_id, $groups, 0)['value'];
- $min_money = Option::get('min_money', $store_id, $groups, 0)['value'];
- if($price < $min_money){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '最小提现'. $min_money .'元'
- ]);
- }
- $cash_sum = Cash::find()->where([
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5, 4],
- 'cash_type' => $cash_type,
- 'user_id' => $user_id
- ])->andWhere([
- 'AND',
- ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
- ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
- ])->sum('price');
- if ($cash_max_day) {
- $cash_max_day = $cash_max_day - ($cash_sum ?: 0);
- if ($price > $cash_max_day) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现金额不能超过' . $cash_max_day . '元'
- ]);
- }
- }
- if ($cash_max_single_day) {
- $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
- if ($price > $cash_max_single_day_) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元'
- ]);
- }
- }
- //提现手续费金额
- $shareHolderCashProfit = Option::get('shareHolderCashProfit', get_store_id(), $groups, 0)['value'];
- $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
- if (!$share_holder) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- if ($is_area) {
- if (intval($share_holder->agent_type) === 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- }
- //计算提现手续费金额
- // $shareHolderCashPrice = $price * ($shareHolderCashProfit / 100);
- if ($price > $user->price) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前大于可提现金额'
- ]);
- }
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $user_id,
- 'store_id' => $store_id, 'cash_type' => $cash_type])->exists();
- if ($exit) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '尚有未完成的提现申请'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- $cash = new Cash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
- $cash->is_delete = 0;
- $cash->status = 0;
- $cash->price = $price;
- $cash->created_at = time();
- $cash->user_id = $user_id;
- $cash->store_id = $store_id;
- $saas_user = get_saas_user();
- if (!empty($saas_user->withdraw_method)) {
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column($withdraw_method, null, 'type');
- if ($type == 'wechat') {
- $cash->type = 0;
- $cash->name = $withdraw_method['wechat']['name'];
- $cash->mobile = $withdraw_method['wechat']['account'];
- }
- if ($type == 'alipay') {
- $cash->type = 1;
- $cash->name = $withdraw_method['alipay']['name'];
- $cash->mobile = $withdraw_method['alipay']['account'];
- }
- if ($type == 'bank_card') {
- $cash->type = 2;
- $cash->name = $withdraw_method['bank_card']['name'];
- $cash->mobile = $withdraw_method['bank_card']['account'];
- $cash->bank_name = $withdraw_method['bank_card']['bank'];
- $cash->bank_branch = $withdraw_method['bank_card']['branch'];
- }
- if ($type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- }else{
- if ($type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- }
- if ($type == 'money') {
- $cash->type = 3;
- }
- $cash->pay_time = 0;
- $cash->service_charge = $type == 'money' ? 0 : $shareHolderCashProfit;
- $cash->cash_type = $cash_type;
- if ($cash->save()) {
- $user->price -= $cash->price;
- if (!$user->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $user->errors
- ]);
- }
- $auto_money = Option::get('auto_money', $store_id, $groups, 0)['value'];
- if ($cash_type == 3 && ($cash->type == Cash::TYPE_RECHARGE || ($price <= $auto_money && $auto_money > 0))) {
- // TODO 在设置金额内,股东 / 2+1佣金提现自动打款
- $this->cashConfirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $store_id);
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '申请成功'
- ]);
- } else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cash->errors
- ]);
- }
- }
- /**
- * 打款
- * @return \yii\web\Response
- */
- public function cashConfirm($id, $status, $store_id)
- {
- $store = Store::findOne($store_id);
- $cash = Cash::findOne([
- 'id' => $id,
- 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if($status == Cash::STATUS_GIVEN){
- $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true) ?: [];
- $val = 0;
- foreach ($store_wechat_cash as $value) {
- if (empty($value)) {
- $val = 1;
- break;
- } else {
- $val = 2;
- break;
- }
- }
- if ($store->is_platform_transfers == 0 && $val == 2) {
- return [
- 'code' => 1,
- 'msg' => '未开启平台转账'
- ];
- } elseif ($store->is_platform_transfers == 1 && $val == 1) {
- return [
- 'code' => 1,
- 'msg' => '未配置平台微信提现设置'
- ];
- }
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- $res = [];
- $price = ($cash->price - ($cash->price * ($cash->service_charge / 100)));
- $servePrice = ($cash->price * ($cash->service_charge / 100));
- if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
- return [
- 'code' => 1,
- 'msg' => '店铺剩余提现余额不足'
- ];
- }
- $wechat_type = 1;
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- // if (empty($wechat_cash)) {
- // $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- // if ($wechat_cash) {
- // $wechat_type = 0;
- // }
- // }
- foreach ((array)json_decode($wechat_cash, true) as $value) {
- if (empty($value)) {
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- if ($wechat_cash) {
- $wechat_type = 0;
- }
- break;
- }
- }
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- if ((int)$cash->type === 0) {
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $user = User::findOne($cash->user_id);
- $wechat_open_id = $user->wechat_open_id;
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账'
- ];
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- }
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ((int)$cash->type === 1) {
- $order = (object)[
- 'store_id' => $cash->store_id,
- 'order_no' => $cash->order_no,
- 'pay_price' => $price,
- 'name' => $cash->name
- ];
- $result = Alipay::transfer($order, $cash->mobile);
- if (isset($result['code']) && $result['code'] == 1) {
- if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
- $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
- }
- if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
- $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
- }
- if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
- $result['msg'] = '商户余额不足';
- }
- return $result;
- } else {
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- }
- }
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::TYPE_ALIPAY;
- $cash->save();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } elseif ((int)$cash->type === 4) {
- $user = \app\models\User::findOne($cash->user_id);
- $saas_user = \app\models\SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'status' => 1, 'is_delete' => 0])->one();
- $lgApi = new LgApi($cash->store_id);
- //灵工提现
- $post_data = [
- 'store_id' => $cash->store_id,
- 'outTradeNo' => $cash->order_no, //唯一批次号
- 'accNo' => bcmul($price, 100),
- 'amt' => bcmul($price, 100),
- 'name' => $cash->name,
- 'certCard' => $lg_info->cert_card //身份证号
- ];
- $result = $lgApi->FlexiblePay($post_data);
- if (isset($result['status']) && $result['status'] == 999) {
- return $result;
- }
- $cash->status = 6; //灵工待打款
- $cash->save();
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id' => $cash->id, 'store_id' => $store_id, 'type' => 3, 'retry' => 5]), 60);
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- }
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- }
- // if ((int)$cash->type === 3) {
- // AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '股东提现打款');
- // }
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- $res['result_code'] = "SUCCESS";
- // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ];
- }
- }
- /**
- * 提现明细
- * @return \yii\web\Response
- */
- public function actionCashDetail() {
- $page = get_params('page', 1);
- $limit = get_params('limit', 20);
- $status = get_params('status', -1);
- $is_area = get_params('is_area', -1);
- $store_id = get_store_id();
- $user_id = get_user_id();
- $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
- if (!$share_holder) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- if ($is_area) {
- if (intval($share_holder->agent_type) === 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未拥有提现权限'
- ]);
- }
- }
- $query = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id]);
- $cash_type_where = ['cash_type' => 3];
- if ($is_area) {
- $cash_type_where = ['cash_type' => Cash::IS_CASH_TYPE_AREA_AGENT];
- }
- $query->andWhere($cash_type_where);
- if ($status != -1) {
- // 待审核
- if ($status == 0) {
- $query->andWhere(['status' => $status]);
- }
- // 已打款
- if ($status == 1) {
- $query->andWhere(['in', 'status', [2, 4]]);
- }
- // 已拒绝
- if ($status == 2) {
- $query->andWhere(['status' => 3]);
- }
- }
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
- $list = $query->select('*')->limit($pagination->limit)
- ->offset($pagination->offset)->orderBy('created_at desc')->asArray()->all();
- foreach ($list as &$value) {
- $value['price'] = sprintf('%.2f', ($value['price'] - ($value['price'] * ($value['service_charge'] / 100))));
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['audit_time'] = date('Y-m-d H:i:s', $value['updated_at']);
- if ($value['status'] == 2 || $value['status'] == 4) {
- $value['audit_time'] = date('Y-m-d H:i:s', $value['pay_time']);
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- 'page_count' => $pagination->pageCount,
- 'row_count' => $count
- ]
- ]);
- }
- /**
- * @return mixed|string
- * 获取推广海报
- */
- public function actionGetQrcode()
- {
- $form = new ShareQrcodeForm();
- $form->store_id = get_store_id();
- $form->type = 9;
- $form->user = get_saas_user();
- $form->user_id = get_user_id();
- return $this->asJson($form->search());
- }
- public function actionOtherInfo() {
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'agreement' => Option::get('holder_agreement', get_store_id(), 'store', '')['value'] ?: '',
- 'banner' => Option::get('holder_banner', get_store_id(), 'store', '')['value'] ?: [],
- ]
- ]);
- }
- // 获取冻结佣金列表
- public function actionFrostList()
- {
- $status = \get_params('status', -1);
- $user_id = \get_user_id();
- $query = ShareHolderFrostLog::find()->alias('shf')->where([
- 'parent_user_id' => $user_id,
- ]);
- if ($status > -1) {
- $query->andWhere(['status' => $status]);
- }
- $profit_q = clone $query;
- $send_profit_q = clone $query;
- $no_send_profit_q = clone $query;
- $profit = $profit_q->sum('amount') ?? '0.00';
- $send_profit = $send_profit_q->andWhere(['status' => 1])->sum('amount') ?? '0.00';
- $no_send_profit = $no_send_profit_q->andWhere(['status' => 0])->sum('amount') ?? '0.00';
- $query->leftJoin(['o' => Order::tableName()], 'shf.order_id = o.id');
- $query->andWhere(['<>', 'o.trade_status', 1]);
- $query->select('shf.*');
- $data = \pagination_make($query, true, 'shf.created_at desc');
- $data = array_merge($data, [
- 'profit' => $profit,
- 'send_profit' => $send_profit,
- 'no_send_profit' => $no_send_profit,
- ]);
- foreach ($data['list'] as $key => &$value) {
- $value['is_refund'] = Order::is_refund($value['store_id'], $value['order_id']);
- $value['name'] = '未知';
- $value['avatar'] = '';
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $user = User::findOne($value['child_user_id']);
- if ($user && $user->binding) {
- $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
- if ($saasUser) {
- $value['name'] = $saasUser->name;
- $value['avatar'] = $saasUser->avatar;
- }
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ]);
- }
- public function actionGetParentOutList() {
- try {
- // $query = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
- // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
- // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- // ->select('su.name, su.avatar');
- $query = User::find()->alias('u')->where(['u.id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL])
- ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id')
- ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->select('su.name, su.avatar')
- ->groupBy('u.id');
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //判断股东是否达到升级条件
- public function actionAuditStatus() {
- try {
- $store_id = get_store_id();
- $user_id = get_user_id();
- $holder_audit = (int)Option::get('holder_audit', get_store_id(), 'store', 0)['value'] ?: 0;
- //判断是否是股东
- $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]);
- if (!empty($share_holder)) {
- if ((int)$share_holder->status === 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '股东申请正在审核中'
- ]);
- }
- //已经是股东
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'is_holder' => 1
- ],
- 'msg' => '股东已经通过申请'
- ]);
- }
- //默认等级
- $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]);
- if (!$share_holder_level) {
- throw new \Exception('请设置默认等级');
- }
- //是否可申请
- $is_apply_holder = 1;
- //判断是否达成条件
- $data = [];
- $condition = Json::decode($share_holder_level->condition);
- //团队人数
- //TODO 股东人数以及金额
- $man = $condition['man'];
- $data['man'] = [
- 'is_open' => (int)$man['is_open'],
- 'data' => null
- ];
- if ((bool)$man['is_open']) {
- //团队人数
- $count = array_column($man['value'], 'count') ? max(array_column($man['value'], 'count')) : 0;
- $man_data = OldUserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->asArray()->all();
- //团队消费
- $money = array_column($man['value'], 'money') ? max(array_column($man['value'], 'money')) : 0;
- //消费达到$m['money']以上允许升级
- $child_id = array_column($man_data, 'child_id');
- $child_id[] = $user_id;
- $pay_price = Order::find()->where(
- [
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'user_id' => $child_id
- ])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
- if ($count > count($man_data)) {
- $is_apply_holder = 0;
- }
- if ($money > ($pay_price ?? 0)) {
- $is_apply_holder = 0;
- }
- $data['man']['data'] = [
- 'count' => [
- 'predict' => $count, //预计
- 'real' => count($man_data),
- 'status' => (bool)($count <= count($man_data))
- ],
- 'money' => [
- 'predict' => $money,
- 'real' => $pay_price ?? 0,
- 'status' => (bool)($money <= ($pay_price ?? 0))
- ]
- ];
- }
- //团队订单金额
- $team_order = $condition['order'];
- $data['order'] = [
- 'is_open' => (int)$team_order['is_open'],
- 'data' => null
- ];
- if ((bool)$team_order['is_open']) {
- $count = array_column($team_order['value'], 'count') ? max(array_column($team_order['value'], 'count')) : 0;
- $man_data = OldUserTreePath::find()->where([
- 'parent_id' => $user_id
- ])->andWhere([
- '!=',
- 'child_id',
- $user_id
- ])->asArray()->all();
- $man_user_id = User::findOld()->alias('u')->childs($user_id, true)->andWhere(['u.store_id' => $store_id])->select('u.id')->column();
- // if (count($man_data) < 1) {
- // $msg = '团队金额:' . $o['level'] . '级团队人数为空';
- // \Yii::warning(['---------- USER ID: ' . $share['user_id'] . '判断用户是否可以晋升 '. $value->name, $msg]);
- // $is_level = false;
- // break;
- // }
- // $man_user_id = array_column($man_data, 'id');
- // $man_user_id = [];
- // $man_user_id[] = $user_id;
- //
- // foreach($man_data as $val) {
- // $man_user_id[] = $val['child_id'];
- // }
- $order_money = Order::find()->where(
- [
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM
- ])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
- ->andWhere(['in', 'user_id', $man_user_id])->sum('pay_price');
- if ($count > ($order_money ?? 0)) {
- $is_apply_holder = 0;
- }
- $data['order']['data'] = [
- 'money' => [
- 'predict' => $count,
- 'real' => $order_money ?? 0,
- 'status' => (bool)($count <= ($order_money ?? 0))
- ]
- ];
- }
- //自身消费金额
- $self = $condition['self'];
- $data['self'] = [
- 'is_open' => (int)$self['is_open'],
- 'data' => null
- ];
- if ((bool)$self['is_open']) {
- $data['self']['data'] = [
- 'single' => null,
- 'total' => null
- ];
- if ($self['value']['order_number'] == 1) {
- $order_price = Order::find()->where(
- [
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'user_id' => $user_id
- ])
- ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->max('pay_price');
- if ($self['value']['price'] > ($order_price ?? 0)) {
- $is_apply_holder = 0;
- }
- //单笔金额
- $data['self']['data']['single'] = [
- 'predict' => $self['value']['price'],
- 'real' => $order_price ?? 0,
- 'status' => (bool)($self['value']['price'] <= ($order_price ?? 0))
- ];
- } else{
- $order_money = Order::find()->where(
- [
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'user_id' => $user_id
- ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
- if ($self['value']['price'] > ($order_money ?? 0)) {
- $is_apply_holder = 0;
- }
- //单笔金额
- $data['self']['data']['total'] = [
- 'predict' => $self['value']['price'],
- 'real' => $order_money ?? 0,
- 'status' => (bool)($self['value']['price'] <= ($order_money ?? 0))
- ];
- }
- }
- //累计充值金额
- $recharge = $condition['recharge'];
- $data['recharge'] = [
- 'is_open' => (int)$recharge['is_open'],
- 'data' => null
- ];
- if ((bool)$recharge['is_open']) {
- $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price');
- if ($recharge['value'] > ($recharge_money ?? 0)) {
- $is_apply_holder = 0;
- }
- $data['recharge']['data'] = [
- 'money' => [
- 'predict' => $recharge['value'],
- 'real' => $recharge_money ?? 0,
- 'status' => (bool)($recharge['value'] <= ($recharge_money ?? 0))
- ]
- ];
- }
- //是否购买指定产品
- $goods = $condition['goods'];
- $data['goods'] = [
- 'is_open' => (int)$goods['is_open'],
- 'data' => null
- ];
- if ((bool)$goods['is_open']) {
- // 查找该用户是否买过此商品
- $goods_model = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
- ->where(
- [
- 'o.store_id' => $store_id,
- 'o.user_id' => $user_id,
- 'o.is_delete' => 0, 'o.is_pay' => 1,
- 'o.trade_status' => Order::ORDER_FLOW_CONFIRM
- ])
- ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
- ->andWhere(['od.goods_id' => $goods['value']['id']])->one();
- if (!$goods_model) {
- $is_apply_holder = 0;
- }
- $data['goods']['data'] = [
- 'num' => [
- 'predict' => 0,
- 'real' => 0,
- 'status' => (bool)$goods_model
- ]
- ];
- }
- //累计充值金额
- $amount_total = $condition['amount_total'];
- $data['amount_total'] = [
- 'is_open' => (int)$amount_total['is_open'],
- 'data' => null
- ];
- if ((bool)$amount_total['is_open']) {
- $total_price = get_user()->total_price;
- if ($amount_total['value'] > ($total_price ?? 0)) {
- $is_apply_holder = 0;
- }
- $data['amount_total']['data'] = [
- 'money' => [
- 'predict' => $amount_total['value'],
- 'real' => $total_price ?? 0,
- 'status' => (bool)($amount_total['value'] <= ($total_price ?? 0))
- ]
- ];
- }
- if ($holder_audit === 0 && $is_apply_holder === 1 && $user_id) { //如果未开启强制审核并已经达到升级条件
- $is_apply_holder = 2; //直接跳转股东中心
- $user = User::findOne($user_id);
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- $model = new ShareHolder();
- $model->level_id = $share_holder_level->id;
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->name = $saas_user->name;
- $model->address = '';
- $model->mobile = $saas_user->mobile;
- $model->province = 0;
- $model->city = 0;
- $model->district = 0;
- $model->province_name = '';
- $model->city_name = '';
- $model->district_name = '';
- $model->status = 1;
- $model->audit_time = time();
- $result = $model->save();
- if (!$result) {
- throw new \Exception(json_encode($model->errors));
- }
- //添加股东升级记录
- $level_log = new \app\models\ShareHolderLevelLog();
- $level_log->before_level = 0;
- $level_log->after_level = $share_holder_level->id;
- $level_log->user_id = $user_id;
- $level_log->store_id = $store_id;
- $level_log->order_id = 0;
- $level_log->desc = "用户申请自动升级为股东";
- if (!$level_log->save()) {
- throw new \Exception('等级更新失败');
- }
- BonusPool::checkChildHolderUpdateLevel($user_id, $share_holder_level->id);
- // BonusPool::checkLevel($store_id, 0, $model->user_id);
- // BonusPool::ShareHolderLevelJob($store_id, 0, $model->user_id);
- if ((int)$share_holder_level->member_level > (int)$user->level) {
- $user->level = $share_holder_level->member_level;
- }
- $user->is_holder = 1;
- $result = $user->save();
- if (!$result) {
- throw new \Exception(json_encode($user->errors));
- }
- }
- $holder_agreement = Option::get('holder_agreement', get_store_id(), 'store', '')['value'];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => array_merge($data, [
- 'is_holder' => 0,
- 'is_apply_holder' => $is_apply_holder,
- 'holder_agreement' => $holder_agreement
- ])
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- public function actionDefaultLevelGoods() {
- try {
- $store_id = get_store_id();
- //默认等级
- $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]);
- $condition = Json::decode($share_holder_level->condition);
- $goods = $condition['goods'];
- $goods_ = null;
- if ((bool)$goods['is_open']) {
- $goods_ = Goods::find()->where(['id' => $goods['value']['id'], 'status' => 1, 'is_delete' => 0])
- ->select('id, name, cover_pic, price, original_price, virtual_sales')->asArray()->all();
- foreach ($goods_ as &$item) {
- $cacheKey = 'actionDefaultLevelGoods' . $item['id'];
- $cacheVal = cache()->get($cacheKey);
- if($cacheVal){
- $goods_num = $cacheVal;
- }else{
- $goods_num = OrderDetail::find()->alias('od')->where(['od.goods_id' => $item['id'], 'is_pay' => 1])
- ->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
- ->andWhere(['AND',
- ['>=', 'o.created_at', strtotime(date("Y-m"))],
- ['<', 'o.created_at', strtotime(date("Y") . '-' . (date("m") + 1))]
- ])->select('od.num')->sum('od.num');
- if($goods_num > 300){
- cache()->set($cacheKey, $goods_num, 3 * 60);
- }
- }
- $item['sales_num'] = $goods_num ? $goods_num + $item['virtual_sales'] : $item['virtual_sales'];
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'goods' => $goods_
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //区域代理申请
- public function actionAreaAgentApply() {
- try {
- $store_id = get_store_id();
- $user_id = get_user_id();
- $agent_type = post_params('agent_type');
- $province_id = post_params('province_id');
- $city_id = post_params('city_id');
- $district_id = post_params('district_id');
- $town_id = post_params('town_id', 0);
- $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]])
- ->andWhere([">", 'agent_type', 0])->asArray()->one();
- if ($shareHolderModel) {
- throw new \Exception('已经为区域代理,无需申请');
- }
- $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'status' => [0, 1], 'is_delete' => 0])->asArray()->one();
- if ($areaAgentApply) {
- throw new \Exception('请勿重复申请');
- }
- if (!in_array($agent_type, [1, 2, 3, 4])) {
- throw new \Exception('参数错误');
- }
- $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $where = ['agent_type' => $agent_type, 'is_delete' => 0, 'status' => [0, 1], 'store_id' => $store_id];
- switch ($agent_type) {
- case 1:
- $where['province_id'] = $province_id;
- break;
- case 2:
- $where['province_id'] = $province_id;
- $where['city_id'] = $city_id;
- break;
- case 3:
- $where['province_id'] = $province_id;
- $where['city_id'] = $city_id;
- $where['district_id'] = $district_id;
- break;
- case 4:
- $where['province_id'] = $province_id;
- $where['city_id'] = $city_id;
- $where['district_id'] = $district_id;
- $where['town_id'] = $town_id;
- break;
- }
- $area_apply = ShareHolderAreaAgentApply::find()->where($where)->one();
- debug_log(['id' => $area_apply->id], 'area_share.log');
- if ($area_apply && !$is_multi_agent) {
- throw new \Exception('已经存在相同区域下的代理');
- }
- $share_holder = ShareHolder::find()->where($where)->one();
- debug_log(['share_holder_id' => $share_holder->id], 'area_share.log');
- if ($share_holder && !$is_multi_agent) {
- throw new \Exception('已经存在相同区域下的代理');
- }
- $areaAgentApply = new ShareHolderAreaAgentApply();
- $areaAgentApply->user_id = $user_id;
- $areaAgentApply->agent_type = $agent_type;
- $areaAgentApply->province_id = (int)$province_id;
- $areaAgentApply->city_id = (int)$city_id;
- $areaAgentApply->district_id = (int)$district_id;
- $areaAgentApply->town_id = (int)$town_id;
- $areaAgentApply->params = json_encode([
- 'agent_type' => $agent_type,
- 'province_id' => $province_id,
- 'city_id' => $city_id,
- 'town_id' => $town_id,
- 'district_id' => $district_id
- ], JSON_UNESCAPED_UNICODE);
- $areaAgentApply->store_id = $store_id;
- if (!$areaAgentApply->save()) {
- throw new \Exception(json_encode($areaAgentApply->errors, JSON_UNESCAPED_UNICODE));
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '提交成功'
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //获取区域代理申请状态
- public function actionGetAreaAgentApply() {
- $store_id = get_store_id();
- // 轮播广告状态
- $area_agent_banner_status = Option::get('area_agent_banner_status', $store_id, 'bonus_pool_area_agent', 0)['value'];
- // 图片列表
- $area_agent_banner_images = Option::get('area_agent_banner_images', $store_id, 'bonus_pool_area_agent', '')['value'];
- if ($area_agent_banner_images) {
- $area_agent_banner_images = json_decode($area_agent_banner_images, true);
- }
- try {
- $user_id = get_user_id();
- $status = 0;
- $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'is_delete' => 0])->asArray()->one();
- if ($areaAgentApply) {
- $status = $areaAgentApply['status'];
- } else {
- $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]])
- ->andWhere([">", 'agent_type', 0])->asArray()->one();
- if ($shareHolderModel) {
- $status = 1;
- $areaAgentApply['agent_type'] = $shareHolderModel['agent_type'];
- $areaAgentApply['province_id'] = $shareHolderModel['province_id'];
- $areaAgentApply['city_id'] = $shareHolderModel['city_id'];
- $areaAgentApply['district_id'] = $shareHolderModel['district_id'];
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'areaAgentApply' => $areaAgentApply,
- 'agent_type' => $areaAgentApply['agent_type'] ?: 1,
- 'status' => (int)$status,
- 'province_id' => $areaAgentApply['province_id'],
- 'city_id' => $areaAgentApply['city_id'],
- 'district_id' => $areaAgentApply['district_id'],
- 'town_id' => $areaAgentApply['town_id'],
- 'province' => District::findOne($areaAgentApply['province_id'])->name ?: '',
- 'city' => District::findOne($areaAgentApply['city_id'])->name ?: '',
- 'district' => District::findOne($areaAgentApply['district_id'])->name ?: '',
- 'town' => District::findOne($areaAgentApply['town_id'])->name ?: '',
- 'area_agent_banner_status' => (int)$area_agent_banner_status,
- 'area_agent_banner_images' => $area_agent_banner_images ?: []
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $e->getMessage(),
- 'data' => [
- 'agent_type' => 1,
- 'status' => 0,
- 'province_id' => '',
- 'city_id' => '',
- 'district_id' => '',
- 'province' => '',
- 'city' => '',
- 'district' => '',
- 'area_agent_banner_status' => (int)$area_agent_banner_status,
- 'area_agent_banner_images' => $area_agent_banner_images ?: []
- ]
- ]);
- }
- }
- //区域代理中心
- public function actionGetAreaAgentInfo() {
- $saas_user = get_saas_user();
- $data = [
- 'cash_profit' => 0,
- 'total_price' => 0,
- 'cash_price' => 0,
- 'agent_rate' => 0,
- 'agent_type' => 0,
- 'avatar' => $saas_user->avatar,
- 'name' => $saas_user->name,
- 'mobile' => $saas_user->mobile
- ];
- try {
- $user = get_user();
- $store_id = get_store_id();
- $user_id = get_user_id();
- $cash_price = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'cash_type' => Cash::IS_CASH_TYPE_AREA_AGENT])->andWhere(['in', 'status', [2, 4]])->sum('price') ?: '0.00';
- $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]);
- $agent_rate = 0;
- $status = 0;
- $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $multi_agent_type1 = $multi_agent_type2 = $multi_agent_type3 = $multi_agent_type4 = 0;
- if($is_multi_agent){
- $multi_agent_type1 = Option::get('multi_agent_type1', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $multi_agent_type2 = Option::get('multi_agent_type2', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $multi_agent_type3 = Option::get('multi_agent_type3', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $multi_agent_type4 = Option::get('multi_agent_type4', $store_id, 'bonus_pool_area_agent', 0)['value'];
- }
- if ($share_holder) {
- $agent_rate = $share_holder->agent_rate;
- if (intval($is_multi_agent)) {
- switch (intval($share_holder->agent_type)) {
- case 2:
- $agent_rate = $multi_agent_type2;
- break;
- case 3:
- $agent_rate = $multi_agent_type3;
- break;
- case 4:
- $agent_rate = $multi_agent_type4;
- break;
- default:
- $agent_rate = $multi_agent_type1;
- break;
- }
- } else {
- if (intval($share_holder->agent_rate_status) === 0) {
- $agent_rate = 0;
- }
- }
- }
- if (intval($share_holder->agent_type) > 0) {
- $status = 1;
- }
- // 文档要求说只操作区域佣金提现
- // 可提现区域代理佣金 = 区域代理佣金 - 已提现区域代理佣金
- // 获取区域代理佣金
- $area_agent_profit = ShareDetail::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'type' => ShareDetail::TYPE_AREA_PROFIT, 'is_send' => 1])
- ->sum('money') ?: '0.00';
- $data['status'] = $status;
- //可提现分红
- $data['cash_profit'] = $user->price;
- //累计分红
- $data['total_price'] = $area_agent_profit;//$user->total_price;
- //已提现
- $data['cash_price'] = $cash_price;
- //佣金比例
- $data['agent_rate'] = $agent_rate;
- //区域代理类型 1省 2市 3区
- $data['agent_type'] = $share_holder->agent_type;
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $e->getMessage(),
- 'data' => []
- ]);
- }
- }
- public function actionGetFugouInfo()
- {
- $store_id = get_store_id();
- $user_id = \get_user_id();
- $setting = FuGou::getSetting($store_id);
- foreach ($setting['fugou_goods'] as $k => $goods) {
- $g = \app\models\Goods::find()->where(['id' => $goods['id']])->select('id, name, cover_pic, price, goods_num, original_price')->one();
- $setting['fugou_goods'][$k] = [
- 'id' => $g->id,
- 'name' => $g->name,
- 'cover_pic' => $g->cover_pic,
- 'price' => $g->price,
- 'goods_num' => $g->goods_num,
- 'original_price' => $g->original_price,
- 'fugou_price' => $goods['fugou_price'],
- 'fugou_num' => 1,
- 'checked' => 0,
- ];
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'setting' => $setting,
- 'frost_price' => FuGou::getUserFrostPrice($user_id),
- 'cycle_day' => FuGou::cycleByDay($user_id, $store_id, $setting),
- 'cycle_price' => FuGou::cycleByPrice($user_id, $store_id, $setting),
- ],
- ]);
- }
- public function actionFugouCart()
- {
- try {
- $store_id = get_store_id();
- $user_id = \get_user_id();
- $goods_list = \json_decode(\post_params('goods_list'), true);
- $res = [[
- 'mch_id' => 0,
- 'goods_list' => [],
- ]];
- FugouCart::deleteAll(['user_id' => $user_id, 'store_id' => $store_id]);
- foreach ($goods_list as $goods) {
- $g = Goods::findOne($goods['goods_id']);
- if (!$g) {
- throw new \Exception('商品不存在');
- }
- $attr = json_decode($g->attr, true);
- $modelAttr = [];
- foreach ($attr[0]['attr_list'] as $item) {
- if (!empty($item['attr_id'])) {
- $modelAttr[] = intval($item['attr_id']);
- }
- }
- $model = new FugouCart();
- $model->user_id = $user_id;
- $model->store_id = $store_id;
- $model->goods_id = $goods['goods_id'];
- $model->num = $goods['num'];
- $model->price = $goods['price'];
- $model->attr = json_encode($modelAttr);
- if (!$model->save()) {
- throw new \Exception('添加购物车失败');
- }
- $res[0]['goods_list'][] = [
- 'fugou_cart_id' => $model->id,
- ];
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => $res,
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '下单失败,请稍后重试!',
- 'err' => $e->getMessage(),
- 'line' => $e->getLine(),
- ]);
- }
- }
- public function actionFugouFrostLog()
- {
- $user_id = \get_user_id();
- $status = \get_params('status', -1);
- $query = ShareHolderFugouFrostLog::find()->alias('f')
- ->leftJoin(['o' => Order::tableName()], 'f.order_id = o.id')
- ->where(['f.user_id' => $user_id])
- ->select('f.*, o.order_no');
- if ($status > -1) {
- $query->andWhere(['f.status' => $status]);
- }
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- if ($item['updated_at'] > 0) {
- $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ]
- ]);
- }
- }
|