ShareHolderController.php 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\jobs\LgCashJob;
  9. use app\models\AccountLog;
  10. use app\models\Cash;
  11. use app\models\District;
  12. use app\models\Goods;
  13. use app\models\Lg;
  14. use app\models\Md;
  15. use app\models\MdStaff;
  16. use app\models\OldUserTreePath;
  17. use app\models\Option;
  18. use app\models\Order;
  19. use app\models\OrderDetail;
  20. use app\models\ReOrder;
  21. use app\models\SaasUser;
  22. use app\models\ShareDetail;
  23. use app\models\ShareHolder;
  24. use app\models\ShareHolderAreaAgentApply;
  25. use app\models\ShareHolderLevel;
  26. use app\models\ShareHolderOutLog;
  27. use app\models\Store;
  28. use app\models\User;
  29. use app\models\UserTreePath;
  30. use app\modules\client\controllers\BaseController;
  31. use app\modules\client\models\v1\ShareQrcodeForm;
  32. use app\utils\OrderNo;
  33. use app\utils\Share\BonusPool;
  34. use yii\base\BaseObject;
  35. use yii\data\Pagination;
  36. use yii\helpers\Json;
  37. use app\models\OrderRefund;
  38. use app\models\ShareHolderFrostLog;
  39. use app\models\StoreMoneyLog;
  40. use app\models\UserShareMoney;
  41. use app\utils\FuGou;
  42. use app\models\FugouCart;
  43. use app\models\ShareHolderFugouFrostLog;
  44. use app\utils\Alipay\Alipay;
  45. use app\utils\LgApi;
  46. class ShareHolderController extends BaseController
  47. {
  48. /**
  49. * 股东申请
  50. */
  51. public function actionApply() {
  52. $user_id = get_user_id();
  53. $store_id = get_store_id();
  54. $name = post_params('name');
  55. $mobile = post_params('mobile');
  56. $province = post_params('province');
  57. $province_name = post_params('province_name');
  58. $city = post_params('city');
  59. $city_name = post_params('city_name');
  60. $district = post_params('district');
  61. $district_name = post_params('district_name');
  62. $address = post_params('address');
  63. if (empty($name) || empty($mobile) || empty($province) || empty($city) || empty($district) || empty($address)) {
  64. return $this->asJson([
  65. 'code' => 1,
  66. 'msg' => '参数错误'
  67. ]);
  68. }
  69. $province_name = District::findOne($province)->name ?: '';
  70. $city_name = District::findOne($city)->name ?: '';
  71. $district_name = District::findOne($district)->name ?: '';
  72. if (empty($province_name) || empty($city_name) || empty($district_name)) {
  73. return $this->asJson([
  74. 'code' => 1,
  75. 'msg' => '参数错误'
  76. ]);
  77. }
  78. // TODO: 申请股东的条件
  79. $check_res = BonusPool::checkApply($store_id, $user_id);
  80. if ($check_res['code'] > 0) {
  81. return $this->asJson($check_res);
  82. }
  83. $admin = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['in', 'status', [1, 0]])->one();
  84. if ($admin) {
  85. return $this->asJson([
  86. 'code' => 1,
  87. 'msg' => '当前已成为股东或存在待审核记录'
  88. ]);
  89. }
  90. $share_level = ShareHolderLevel::findOne(['is_default' => 1, 'store_id' => get_store_id(), 'is_delete' => 0]);
  91. $share_holder = new ShareHolder();
  92. $share_holder->level_id = $share_level->id;
  93. $share_holder->store_id = get_store_id();
  94. $share_holder->user_id = $user_id;
  95. $share_holder->name = $name;
  96. $share_holder->address = $address;
  97. $share_holder->mobile = $mobile;
  98. $share_holder->province = $province;
  99. $share_holder->city = $city;
  100. $share_holder->district = $district;
  101. $share_holder->province_name = $province_name;
  102. $share_holder->city_name = $city_name;
  103. $share_holder->district_name = $district_name;
  104. if ($share_holder->save()) {
  105. return $this->asJson([
  106. 'code' => 0,
  107. 'msg' => '提交成功'
  108. ]);
  109. }
  110. return $this->asJson([
  111. 'code' => 1,
  112. 'msg' => $share_holder->errors[0]
  113. ]);
  114. }
  115. /**
  116. * 等级
  117. * @return \yii\web\Response
  118. */
  119. public function actionHolderLevel() {
  120. $user_id = get_user_id();
  121. $store_id = get_store_id();
  122. $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]);
  123. if (!$share_holder) {
  124. return $this->asJson([
  125. 'code' => 1,
  126. 'msg' => '不好意思,您不是股东',
  127. ]);
  128. }
  129. $share_holder_level = ShareHolderLevel::findOne($share_holder->level_id);
  130. if (!$share_holder_level) {
  131. return $this->asJson([
  132. 'code' => 1,
  133. 'msg' => '当前数据不存在',
  134. ]);
  135. }
  136. $share_holder_level_arr = ShareHolderLevel::find()->where(['is_delete' => 0, 'store_id' => $store_id])->orderBy('level asc')->asArray()->all();
  137. foreach ($share_holder_level_arr as &$level) {
  138. $tmp = [];
  139. $condition = Json::decode($level['condition']);
  140. //佣金总额
  141. $amount_total = $condition['amount_total'];
  142. if (intval($amount_total['is_open'])) {
  143. $msg = "佣金总额达到";
  144. $title = "佣金总额";
  145. $total_price = get_user()->total_price;
  146. if ($total_price < $amount_total['value']) {
  147. $is_ok = 0;
  148. } else {
  149. $is_ok = 1;
  150. }
  151. $tmp[] = [
  152. 'status' => $is_ok,
  153. 'msg' => $msg,
  154. 'title' => $title,
  155. 'icon' => 'amount_total',
  156. 'require' => $amount_total['value'],
  157. 'value' => $total_price ?: 0
  158. ];
  159. }
  160. $man = $condition['man'];
  161. if ((bool)$man['is_open']) {
  162. $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
  163. foreach ((array)$man['value'] as $m) {
  164. $man_data = OldUserTreePath::find()->where([
  165. 'parent_id' => $user_id
  166. ])->andWhere([
  167. '!=',
  168. 'child_id',
  169. $user_id
  170. ])->andWhere([
  171. 'AND', [
  172. '>', 'child_level', $share_user_parent_tree->parent_level
  173. ], [
  174. '<=', 'child_level', ($share_user_parent_tree->parent_level + (int)$m['level'])
  175. ]
  176. ])->asArray()->all();
  177. $man_data_count = count($man_data);
  178. // $msg = $m['level'] . '级团队总人数达到' . $m['count'];
  179. // $man_data = OldUserTreePath::find()->where([
  180. // 'parent_id' => $user_id
  181. // ])->andWhere([
  182. // '!=',
  183. // 'child_id',
  184. // $user_id
  185. // ])->asArray()->all();
  186. $man_data_count = $man_data_count ?: 0;
  187. $msg = "累计人数";
  188. $title = "团队人数";
  189. if ($man_data_count < $m['count']) {
  190. $is_ok = 0;
  191. } else {
  192. $is_ok = 1;
  193. }
  194. $tmp[] = [
  195. 'status' => $is_ok,
  196. 'msg' => $msg,
  197. 'title' => $title,
  198. 'icon' => 'man',
  199. 'require' => $m['count'],
  200. 'value' => $man_data_count,
  201. ];
  202. if (isset($m['money'])) {
  203. //消费达到$m['money']以上允许升级
  204. $child_id = array_column($man_data, 'child_id');
  205. $pay_price = Order::find()->where(
  206. [
  207. 'is_delete' => 0,
  208. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  209. 'user_id' => $child_id
  210. ])
  211. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  212. $is_ok = 1;
  213. if ($m['money'] > $pay_price) {
  214. $is_ok = 0;
  215. }
  216. $tmp[] = [
  217. 'status' => $is_ok,
  218. 'msg' => $m['level'] . '级内团队人数' . $m['count'] . '且团队消费',
  219. 'title' => '团队人数&消费金额',
  220. 'icon' => 'man',
  221. 'require' => $m['money'],
  222. 'value' => $pay_price ?: 0,
  223. ];
  224. }
  225. }
  226. }
  227. // 团队消费金额
  228. $order = $condition['order'];
  229. if ((bool)$order['is_open']) {
  230. $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
  231. foreach ($order['value'] as $o) {
  232. $man_data = OldUserTreePath::find()->where([
  233. 'parent_id' => $user_id
  234. ])->andWhere([
  235. '!=',
  236. 'child_id',
  237. $user_id
  238. ])->andWhere([
  239. 'AND', [
  240. '>=', 'child_level', $share_user_parent_tree->parent_level
  241. ], [
  242. '<=', 'child_level', ($share_user_parent_tree->parent_level + ($o['level'] ?: 0))
  243. ]
  244. ])->asArray()->all();
  245. // $man_user_id = array_column($man_data, 'id');
  246. // 删除自己会员ID
  247. // $man_user_id = array_diff($man_user_id, [$user_id]);
  248. $man_user_id = array_column($man_data, 'child_id');
  249. $man_user_id[] = $user_id;
  250. $order_money = Order::find()
  251. ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM])
  252. ->andWhere(['in', 'user_id', $man_user_id])
  253. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  254. ->sum('pay_price');
  255. $msg = $o['level'] . '级内团队总订单金额';
  256. // $msg = "总订单累计";
  257. $title = "团队订单";
  258. if ($order_money < $o['count']) {
  259. $is_ok = 0;
  260. } else {
  261. $is_ok = 1;
  262. }
  263. $tmp[] = [
  264. 'status' => $is_ok,
  265. 'msg' => $msg,
  266. 'title' => $title,
  267. 'icon' => 'order',
  268. 'require' => $o['count'],
  269. 'value' => $order_money ?: 0
  270. ];
  271. }
  272. }
  273. // 自身消费金额
  274. $self = $condition['self'];
  275. if ((bool)$self['is_open']) {
  276. // 兼容老版本
  277. if (!isset($self['value']['order_number'])) {
  278. $self_price = $self['value'];
  279. $self['value'] = [];
  280. $self['value']['order_number'] = 0;
  281. $self['value']['price'] = $self_price;
  282. }
  283. if ($self['value']['order_number'] == 1) {
  284. $order = Order::find()
  285. ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
  286. ->andWhere(['>=' , 'pay_price', $self['value']['price']])
  287. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  288. ->one();
  289. // $msg = '自己消费单笔金额达到' . $self['value']['price'] . '元';
  290. $msg = "自己单笔消费金额";
  291. $title = "账户单笔消费";
  292. $max_order = Order::find()
  293. ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
  294. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  295. ->orderBy('pay_price desc')->asArray()->one();
  296. if (!$order) {
  297. $is_ok = 0;
  298. } else {
  299. $is_ok = 1;
  300. }
  301. $tmp[] = [
  302. 'status' => $self['value']['price'] == 0 ? 1 : $is_ok,
  303. 'msg' => $msg,
  304. 'title' => $title,
  305. 'icon' => 'self',
  306. 'require' => $self['value']['price'],
  307. 'value' => $max_order ? $max_order['pay_price'] : 0,
  308. ];
  309. } else {
  310. $order_money = Order::find()
  311. ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id])
  312. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  313. ->sum('pay_price');
  314. // $msg = '自己消费金额达到' . $self['value']['price'] . '元';
  315. $msg = "自己消费金额";
  316. $title = "账户消费";
  317. if ($order_money < $self['value']['price']) {
  318. $is_ok = 0;
  319. } else {
  320. $is_ok = 1;
  321. }
  322. $tmp[] = [
  323. 'status' => $is_ok,
  324. 'msg' => $msg,
  325. 'title' => $title,
  326. 'icon' => 'self',
  327. 'require' => $self['value']['price'],
  328. 'value' => $order_money ?: 0
  329. ];
  330. }
  331. }
  332. // 自身充值金额
  333. $recharge = $condition['recharge'];
  334. if ((bool)$recharge['is_open']) {
  335. $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price');
  336. // $msg = '充值金额达到' . $recharge['value'] . '元';
  337. $msg = "充值金额";
  338. $title = "账户充值";
  339. if ($recharge_money < $recharge['value']) {
  340. $is_ok = 0;
  341. } else {
  342. $is_ok = 1;
  343. }
  344. $tmp[] = [
  345. 'status' => $is_ok,
  346. 'msg' => $msg,
  347. 'title' => $title,
  348. 'icon' => 'recharge',
  349. 'require' => $recharge['value'],
  350. 'value' => $recharge_money ?: 0
  351. ];
  352. }
  353. // 购买指定商品
  354. $goods = $condition['goods'];
  355. if ((bool)$goods['is_open']) {
  356. // 查找该用户是否买过此商品
  357. $goods_model = Order::find()->alias('o')
  358. ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
  359. ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM])
  360. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  361. ->andWhere(['od.goods_id' => $goods['value']['id']])->one();
  362. $goods_order_count = Order::find()->alias('o')
  363. ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
  364. ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM])
  365. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  366. ->andWhere(['od.goods_id' => $goods['value']['id']])->count();
  367. $msg = '购买指定商品';
  368. $title = "购买商品";
  369. // 是否过了售后期
  370. $pay_time = $goods_model->pay_time;
  371. if (!$goods_model) {
  372. $is_ok = 0;
  373. } else {
  374. if (time() < ($pay_time + $goods['day'] * 24 * 3600)) {
  375. $is_ok = 0;
  376. } else {
  377. $is_ok = 1;
  378. }
  379. }
  380. $tmp[] = [
  381. 'goods_id' => $goods['value']['id'],
  382. 'status' => $is_ok,
  383. 'msg' => $msg,
  384. 'title' => $title,
  385. 'icon' => 'goods',
  386. 'require' => 1,
  387. 'value' => $goods_order_count ?: 0
  388. ];
  389. }
  390. // 等级升级
  391. $shareholder = $condition['shareholder'];
  392. if ($shareholder['is_open']) {
  393. $num = User::find()->alias('u')
  394. ->leftJoin(['h' => ShareHolder::tableName()], 'h.user_id = u.id')
  395. ->where([
  396. 'u.old_parent_id' => $user_id,
  397. 'h.status' => 1,
  398. 'h.is_delete' => 0,
  399. ])
  400. ->andWhere(['>=', 'h.level_id', $condition->shareholder->to_level_id])
  401. ->limit($condition->shareholder->num)//感觉不用加这个 下面有数组截取 但是不加怕查询太慢
  402. ->orderBy('h.created_at ASC')
  403. ->select('u.id')
  404. ->count();
  405. $share_level = ShareHolderLevel::findOne(['id' => $shareholder['to_level_id'], 'store_id' => $store_id, 'is_delete' => 0]);
  406. // $msg = '您在本商城发展的股东达到' . $shareholder['num'] . '位等级为'.$share_level->name;
  407. $msg = "升级为" . $share_level->name;
  408. $title = "团队下级账户升级";
  409. $tmp[] = [
  410. 'status' => $num >= $shareholder['num'],
  411. 'msg' => $msg,
  412. 'title' => $title,
  413. 'icon' => 'man',
  414. 'require' => $shareholder['num'],
  415. 'value' => $num
  416. ];
  417. }
  418. $level['text'] = $tmp;
  419. }
  420. return $this->asJson([
  421. 'code' => 0,
  422. 'msg' => 'success',
  423. 'data' => [
  424. 'level_id' => $share_holder->level_id,
  425. 'level_res' => $share_holder_level_arr
  426. ]
  427. ]);
  428. }
  429. /**
  430. * 团队
  431. * @return \yii\web\Response
  432. */
  433. public function actionGroup() {
  434. $user_id = get_user_id();
  435. $store_id = get_store_id();
  436. $type = (int)get_params('type');
  437. $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
  438. \Yii::warning([$user_id, $store_id]);
  439. if ($type === 1) {//团队数据/团队中心—链动
  440. $share_user_parent_tree = UserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
  441. $man_data = UserTreePath::find()->where([
  442. 'parent_id' => $user_id
  443. ])->andWhere([
  444. '!=',
  445. 'child_id',
  446. $user_id
  447. ])->andWhere([
  448. 'AND', [
  449. '>=', 'child_level', $share_user_parent_tree->parent_level
  450. ], [
  451. '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1))
  452. ]
  453. ])->asArray()->all();
  454. $child_user_id = array_column($man_data, 'child_id');
  455. $list = User::find()->where(['id' => $child_user_id])
  456. ->select('id, binding, nickname, avatar_url, store_id, parent_id')
  457. ->asArray()->all();
  458. // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.status' => 1])
  459. // ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  460. // ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
  461. // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')
  462. // ->asArray()->all();
  463. } elseif ($type === 2) {
  464. $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])
  465. ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id')
  466. // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
  467. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  468. ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')
  469. ->groupBy('u.id')->asArray()->all();
  470. foreach ($list as &$item) {
  471. $item['parent_id'] = 0;
  472. }
  473. $user_id = 0;
  474. } else {//股东中心-原推荐关系
  475. $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级
  476. $man_data = OldUserTreePath::find()->where([
  477. 'parent_id' => $user_id
  478. ])->andWhere([
  479. '!=',
  480. 'child_id',
  481. $user_id
  482. ])->andWhere([
  483. 'AND', [
  484. '>=', 'child_level', $share_user_parent_tree->parent_level
  485. ], [
  486. '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1))
  487. ]
  488. ])->asArray()->all();
  489. $child_user_id = array_column($man_data, 'child_id');
  490. $list = User::find()->where(['id' => $child_user_id])->select('id, binding, nickname, avatar_url, store_id, old_parent_id parent_id')
  491. ->asArray()->all();
  492. }
  493. $tree = UserTreePath::formatChildren($list, $user_id);
  494. return $this->asJson([
  495. 'code' => 0,
  496. 'data' => [
  497. 'res' => $tree,
  498. 'count' => UserTreePath::$team_count ?: 0
  499. ]
  500. ]);
  501. }
  502. /**
  503. * 极差分润订单
  504. * @return \yii\web\Response
  505. */
  506. public function actionShareOrder() {
  507. $order_no = get_params('order_no');
  508. $name = get_params('name');
  509. $time = get_params('time');
  510. $page = get_params('page', 1);
  511. $limit = get_params('limit', 10);
  512. $type = get_params('type');
  513. // $query = ShareDetail::find()->alias('sd')->leftJoin(['o' => Order::tableName()], 'o.id=sd.type_id')
  514. // ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
  515. // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  516. // ->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()]);
  517. // if ($name) {
  518. // $query->andWhere(['like', 'su.name', $name]);
  519. // }
  520. // if ($order_no) {
  521. // $query->andWhere(['o.order_no' => $order_no]);
  522. // }
  523. // if ($type) {
  524. // $query->andWhere(['sd.type' => [2, 5, 6, 9]]);
  525. // }
  526. // if ($time) {
  527. // $query->andWhere([
  528. // 'and',
  529. // ['>=', 'o.created_at', strtotime($time . '-01')],
  530. // ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
  531. // ]);
  532. // }
  533. // $total = $query->sum('sd.money');
  534. //获取团队订单金额数据
  535. $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
  536. $team_list = User::find()->childs(get_user_id(), true, $team_num)->andWhere(['store_id' => get_store_id()])->select('id')->column();
  537. $query = Order::find()->alias('o')
  538. ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
  539. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  540. ->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]]);
  541. if ($name) {
  542. $query->andWhere(['like', 'su.name', $name]);
  543. }
  544. if ($order_no) {
  545. $query->andWhere(['o.order_no' => $order_no]);
  546. }
  547. if ($time) {
  548. $query->andWhere([
  549. 'and',
  550. ['>=', 'o.created_at', strtotime($time . '-01')],
  551. ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
  552. ]);
  553. }
  554. $total = $query->sum('o.total_price');
  555. $count = $query->count();
  556. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
  557. $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')
  558. ->limit($pagination->limit)
  559. ->offset($pagination->offset)->asArray()->all();
  560. foreach ($list as &$value) {
  561. $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all();
  562. }
  563. return $this->asJson([
  564. 'code' => 0,
  565. 'msg' => 'success',
  566. 'data' => [
  567. 'total' => $total,
  568. 'row_count' => $count,
  569. 'page_count' => $pagination->pageCount,
  570. 'list' => $list,
  571. ]
  572. ]);
  573. }
  574. /**
  575. * 极差分润订单
  576. * @return \yii\web\Response
  577. */
  578. public function actionShareOrderList()
  579. {
  580. $order_no = input_params('order_no');
  581. $name = input_params('name');
  582. $type = input_params('type');// 区分链动
  583. $is_area = input_params('is_area', 1); //只显示区域分红
  584. $trade_status = input_params('trade_status');
  585. $query = Order::find()->alias('o')->leftJoin(['sd' => ShareDetail::tableName()], 'sd.type_id = o.id')
  586. ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
  587. ->where(['o.store_id' => get_store_id()])
  588. ->andWhere(['<>', 'o.trade_status', 1]);
  589. $query->andWhere(['sd.user_id' => get_user_id()]);
  590. if($trade_status > -1){
  591. $query->andWhere(['o.trade_status' => $trade_status]);
  592. }
  593. if ($name) {
  594. $query->andWhere(['like', 'u.nickname', $name]);
  595. }
  596. $type_where = ['in', 'sd.type', [2, 5, 6, 7, 8, 9]];
  597. if ($type) {
  598. $type_where = ['in', 'sd.type', [2, 5, 6, 9]];
  599. }
  600. if ($is_area) {
  601. $type_where = ['AND', ['sd.type' => 3], ['>', 'sd.money', '0']];
  602. }
  603. $type_where = ['OR', $type_where, ['OR', ['>', 'holder_first_price', 0], ['>', 'holder_second_price', 0], ['>', 'holder_third_price', 0],
  604. ['>', 'old_holder_first_price', 0], ['>', 'old_holder_second_price', 0], ['>', 'old_holder_third_price', 0]]];
  605. $query->andWhere($type_where);
  606. if ($order_no) {
  607. $query->andWhere(['o.order_no' => $order_no]);
  608. }
  609. $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,
  610. 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')
  611. ->orderBy('o.id desc')->groupBy('o.id');
  612. $list = pagination_make($query);
  613. foreach ($list['list'] as &$value) {
  614. $value['order_pay_price'] = 0;
  615. $order = Order::find()->select('pay_price')->where(['order_no' => $value['order_no']])->one();
  616. if ($order) {
  617. $value['order_pay_price'] = $order->pay_price;
  618. }
  619. $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['type_id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all();
  620. if ($type) {
  621. $value['range_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_RANGE_PROFIT, 'type_id' => $value['type_id']])->sum('money');
  622. } else {
  623. $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');
  624. }
  625. $value['agent_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_AREA_PROFIT, 'type_id' => $value['type_id']])->sum('money');
  626. $share_holder_price = bcadd($value['old_holder_first_price'],
  627. bcadd($value['old_holder_second_price'],
  628. bcadd($value['old_holder_third_price'],
  629. bcadd($value['holder_first_price'],
  630. bcadd($value['holder_second_price'], $value['holder_third_price'], 2)
  631. , 2)
  632. , 2)
  633. , 2)
  634. , 2);
  635. $value['share_holder_price'] = '¥' . $share_holder_price;
  636. }
  637. return $this->asJson([
  638. 'q' => $query->createCommand()->getRawSql(),
  639. 'code' => 0,
  640. 'msg' => 'success',
  641. 'data' => [
  642. 'data' => $list['list'],
  643. 'pageNo' => $list['pageNo'],
  644. 'totalCount' => $list['totalCount']
  645. ]
  646. ]);
  647. }
  648. /**
  649. * 股东分销
  650. * @return \yii\web\Response
  651. */
  652. public function actionHolderShareOrder() {
  653. $time = get_params('time');
  654. $page = get_params('page', 1);
  655. $limit = get_params('limit', 10);
  656. $user_id = get_user_id();
  657. $query = Order::find()->where(['store_id' => get_store_id(), 'trade_status' => [
  658. Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM
  659. ]])->andWhere(['OR',
  660. ['AND', ['old_parent_id' => $user_id], ['>', 'old_holder_first_price', '0']],
  661. ['AND', ['old_parent_id_1' => $user_id], ['>', 'old_holder_second_price', '0']],
  662. ['AND', ['old_parent_id_2' => $user_id], ['>', 'old_holder_third_price', '0']],
  663. ['AND', ['parent_id' => $user_id], ['>', 'holder_first_price', '0']],
  664. ['AND', ['parent_id_1' => $user_id], ['>', 'holder_second_price', '0']],
  665. ['AND', ['parent_id_2' => $user_id], ['>', 'holder_third_price', '0']]
  666. ]);
  667. if ($time) {
  668. $query->andWhere([
  669. 'and',
  670. ['>=', 'created_at', strtotime($time . '-01')],
  671. ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
  672. ]);
  673. }
  674. $total_data = (clone $query)->select('old_parent_id, old_holder_first_price, old_parent_id_1, old_holder_second_price,
  675. old_parent_id_2, old_holder_third_price, parent_id, holder_first_price, parent_id_1, holder_second_price, parent_id_2,
  676. holder_third_price')->asArray()->all();
  677. $total = 0;
  678. foreach ($total_data as $total_item) {
  679. if ($total_item['old_parent_id'] == $user_id) {
  680. $total = bcadd($total, $total_item['old_holder_first_price'], 2);
  681. }
  682. if ($total_item['old_parent_id_1'] == $user_id) {
  683. $total = bcadd($total, $total_item['old_holder_second_price'], 2);
  684. }
  685. if ($total_item['old_parent_id_2'] == $user_id) {
  686. $total = bcadd($total, $total_item['old_holder_third_price'], 2);
  687. }
  688. if ($total_item['parent_id'] == $user_id) {
  689. $total = bcadd($total, $total_item['holder_first_price'], 2);
  690. }
  691. if ($total_item['parent_id_1'] == $user_id) {
  692. $total = bcadd($total, $total_item['holder_second_price'], 2);
  693. }
  694. if ($total_item['parent_id_2'] == $user_id) {
  695. $total = bcadd($total, $total_item['holder_third_price'], 2);
  696. }
  697. }
  698. $query->select('id, pay_time, order_no, pay_price, is_pay, trade_status, old_parent_id, old_holder_first_price,
  699. old_parent_id_1, old_holder_second_price, old_parent_id_2, old_holder_third_price, parent_id, holder_first_price,
  700. parent_id_1, holder_second_price, parent_id_2, holder_third_price')->orderBy('created_at desc');
  701. $list = pagination_make($query);
  702. foreach ($list['list'] as &$value) {
  703. $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']])
  704. ->select('goods_name, num, attr, pic, total_price')->asArray()->all();
  705. $value['money'] = 0;
  706. if ($value['old_parent_id'] == $user_id) {
  707. $value['source'] = 1;
  708. $value['money'] = bcadd($value['money'], $value['old_holder_first_price'], 2);
  709. }
  710. if ($value['old_parent_id_1'] == $user_id) {
  711. $value['source'] = 2;
  712. $value['money'] = bcadd($value['money'], $value['old_holder_second_price'], 2);
  713. }
  714. if ($value['old_parent_id_2'] == $user_id) {
  715. $value['source'] = 3;
  716. $value['money'] = bcadd($value['money'], $value['old_holder_third_price'], 2);
  717. }
  718. if ($value['parent_id'] == $user_id) {
  719. $value['source'] = 1;
  720. $value['money'] = bcadd($value['money'], $value['holder_first_price'], 2);
  721. }
  722. if ($value['parent_id_1'] == $user_id) {
  723. $value['source'] = 2;
  724. $value['money'] = bcadd($value['money'], $value['holder_second_price'], 2);
  725. }
  726. if ($value['parent_id_2'] == $user_id) {
  727. $value['source'] = 3;
  728. $value['money'] = bcadd($value['money'], $value['holder_third_price'], 2);
  729. }
  730. $is_send = UserShareMoney::find()->where([
  731. 'type' => 2,
  732. 'user_id' => $user_id,
  733. 'order_id' => $value['id'],
  734. 'source' => $value['source']
  735. ])->asArray()->one();
  736. $is_send_text = '(未发放)';
  737. if ($is_send) {
  738. $is_send_text = '(已发放)';
  739. }
  740. $value['order_no'] .= $is_send_text;
  741. }
  742. $list['total'] = $total;
  743. return $this->asJson([
  744. 'code' => 0,
  745. 'msg' => 'success',
  746. 'data' => $list
  747. ]);
  748. }
  749. // public function actionAuditStatus() {
  750. // $user_id = get_user_id();
  751. // $share_holder = ShareHolder::find()->where(['user_id' => $user_id, 'store_id' => get_store_id(), 'is_delete' => 0])->orderBy('id desc')->limit(1)->one();
  752. // if (!$share_holder) {
  753. // return $this->asJson([
  754. // 'code' => 0,
  755. // 'msg' => '没有审核记录',
  756. // 'data' => ['status' => -1]
  757. // ]);
  758. // }
  759. // return $this->asJson([
  760. // 'code' => 0,
  761. // 'data' => ['status' => $share_holder->status]
  762. // ]);
  763. // }
  764. /**
  765. * 股东中心
  766. * @return \yii\web\Response
  767. */
  768. public function actionIndex() {
  769. $user_id = get_user_id();
  770. $store_id = get_store_id();
  771. $type = get_params('type');
  772. $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
  773. if (!$share_holder && $user_id) {
  774. return $this->asJson([
  775. 'code' => 1,
  776. 'msg' => '未有权限,请先申请成为股东'
  777. ]);
  778. }
  779. $total_price = get_user()->total_price;
  780. $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]);
  781. $share_detail_query = ShareDetail::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'user_id' => $user_id]);
  782. $share_detail_query_1 = ShareDetail::find()->alias('sd')->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id])
  783. // ->leftJoin(OrderRefund::tableName() . ' r', "r.order_id = sd.type_id and r.is_delete = 0")
  784. // ->andWhere([
  785. // 'or',
  786. // 'isnull(r.id)',
  787. // ['r.type' => 2],
  788. // ['in', 'r.status', [2, 3]]
  789. // ]);
  790. ->leftJoin(['o' => Order::tableName()], 'sd.type_id = o.id')->andWhere(['!=', 'o.trade_status', 1]);
  791. if ($type) {
  792. $share_detail_query->andWhere(['type' => [2, 5, 6, 9]]);
  793. $share_detail_query_1->andWhere(['in', 'sd.type', [2, 5, 6, 9]]);
  794. }
  795. $share_detail_query->andWhere(['<=', 'type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]);
  796. $share_detail_query_1->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]);
  797. $order_count_q = clone $share_detail_query;
  798. $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value'];
  799. $team_list = User::find()->alias('u')->childs(get_user_id(), true, $team_num)->andWhere(['u.store_id' => get_store_id()])->select('u.id')->column();
  800. $order_count = Order::find()->alias('o')
  801. ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id')
  802. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  803. ->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();
  804. // $total_price = $order_count_q->sum('money');
  805. // $cash_profit = $order_count_q->andWhere(['is_send' => 1])->sum('money');
  806. $cash_profit = get_user()->price;
  807. if ($user_id) {
  808. $cash_price = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id])->andWhere(['in', 'status', [2, 4]])->sum('price');
  809. $cash_price_apply = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'cash_type' => 3])->andWhere(['in', 'status', [0, 1]])->sum('price');
  810. // $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();
  811. $team_count = User::find()->alias('u')
  812. ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
  813. ->where(['sh.is_delete' => 0, 'sh.status' => 1])
  814. ->andWhere([
  815. 'u.parent_id' => $user_id
  816. ])
  817. ->count();
  818. }
  819. // $cash = get_user()->price;
  820. $agent_data = [];
  821. $district = '';
  822. if ($share_holder) {
  823. if ((int)$share_holder->agent_type === 1) {
  824. $agent_data = [$share_holder->province_id];
  825. } elseif ((int)$share_holder->agent_type === 2) {
  826. $agent_data = [$share_holder->province_id, $share_holder->city_id];
  827. } elseif ((int)$share_holder->agent_type === 3) {
  828. $agent_data = [$share_holder->province_id, $share_holder->city_id, $share_holder->district_id];
  829. }
  830. }
  831. $district_ = District::find()->where(['id' => $agent_data])->select('name')->column();
  832. if (!empty($district_)) {
  833. $district = implode('', $district_);
  834. }
  835. $data = [
  836. 'total_price' => $total_price,
  837. 'is_send' => $share_detail_query->andWhere(['is_send' => 1])->sum('money') ?: 0,
  838. 'no_send' => $share_detail_query_1->andWhere(['is_send' => 0])->sum('money') ?: 0,
  839. 'cash_price' => $cash_price ?? 0,
  840. 'cash_price_apply' => $cash_price_apply ?? '0.00',
  841. 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url,
  842. 'name' => $saas_user ? $saas_user->name : (get_user()->nickname ?: ''),
  843. 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: '',
  844. 'cash_profit' => sprintf("%.2f", $cash_profit),
  845. 'team_count' => $team_count ?? 0,
  846. 'id' => $user_id,
  847. 'order_count' => $order_count,
  848. 'agent_type' => (int)$share_holder->agent_type ?: 0,
  849. 'district' => $district
  850. ];
  851. return $this->asJson([
  852. 'code' => 0,
  853. 'data' => $data
  854. ]);
  855. }
  856. //团队数据
  857. public function actionGetTeamInfo() {
  858. try {
  859. //
  860. $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]);
  861. $user_id = get_user_id();
  862. $store_id = get_store_id();
  863. if ($user_id) {
  864. $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
  865. if (!$share_holder) {
  866. return $this->asJson([
  867. 'code' => 1,
  868. 'msg' => '未有权限,请先申请成为股东'
  869. ]);
  870. }
  871. // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id])
  872. // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all();
  873. $team_count = User::find()->alias('u')
  874. ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id')
  875. ->where(['sh.is_delete' => 0, 'sh.status' => 1])
  876. ->andWhere([
  877. 'u.parent_id' => $user_id
  878. ])
  879. ->count();
  880. $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();
  881. UserTreePath::formatChildren($list, $user_id);
  882. $team_count = UserTreePath::$team_count;
  883. $shareHolderOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.parent_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
  884. ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
  885. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  886. ->select('su.name, su.avatar')
  887. ->asArray()->all();
  888. // $parentOutList = User::find()->alias('u')->where(['u.old_parent_id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL])
  889. // ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id')
  890. // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
  891. // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  892. // ->select('su.name, su.avatar')
  893. // ->groupBy('u.id')
  894. // ->asArray()->all();
  895. $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])
  896. ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id')
  897. // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
  898. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  899. ->select('su.name, su.avatar')
  900. ->groupBy('u.id')
  901. ->asArray()->all();
  902. // $parentOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
  903. // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
  904. // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  905. // ->select('su.name, su.avatar')
  906. // ->asArray()->all();
  907. }
  908. return $this->asJson([
  909. 'code' => 0,
  910. 'msg' => 'success',
  911. 'data' => [
  912. 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url,
  913. 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '',
  914. 'id' => $user_id,
  915. 'team_count' => $team_count ?? '',
  916. 'parent_count' => count($parentOutList ?? []),
  917. 'team' => $shareHolderOutList ?? [],
  918. 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: ''
  919. ]
  920. ]);
  921. } catch (\Exception $e) {
  922. return $this->asJson([
  923. 'code' => 1,
  924. 'msg' => $e->getMessage()
  925. ]);
  926. }
  927. }
  928. /**
  929. * 佣金列表
  930. * @return \yii\web\Response
  931. */
  932. public function actionProfitList() {
  933. $store_id = get_store_id();
  934. $user_id = get_user_id();
  935. $time = get_params('time');
  936. $type = get_params('type');
  937. $status = get_params('status');
  938. $page = get_params('page', 1);
  939. $limit = get_params('limit', 20);
  940. //区域代理
  941. $is_area = get_params('is_area');
  942. $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]);
  943. if (!$share_holder && $user_id) {
  944. return $this->asJson([
  945. 'code' => 1,
  946. 'msg' => '未有权限,请先申请成为股东'
  947. ]);
  948. }
  949. $query = ShareDetail::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['>','money',0]);
  950. if ($type) {
  951. $query->andWhere(['type' => [2, 5, 6, 9]]);
  952. }
  953. if ($is_area) {
  954. $query->andWhere(['type' => 3]);
  955. }
  956. //今日奖励
  957. $t_query = clone $query;
  958. $today_money = $t_query->andWhere(['AND',
  959. ['>=', 'created_at', strtotime(date('Y-m-d'))],
  960. ['<', 'created_at', strtotime(date('Y-m-d') . '+1 day')]
  961. ])->select('money')->sum('money');
  962. //总待结算金额
  963. $no_send_query = clone $query;
  964. $no_send_money = $no_send_query->andWhere(['is_send' => 0])->select('money')->sum('money');
  965. //总已结算金额
  966. $send_query = clone $query;
  967. $send_money = $send_query->andWhere(['is_send' => 1])->select('money')->sum('money');
  968. if (in_array($status, [0, 1]) && !is_null($status)) {
  969. $query->andWhere(['is_send' => $status]);
  970. }
  971. if ($time) {
  972. $query->andWhere([
  973. 'and',
  974. ['>=', 'created_at', strtotime($time . '-01')],
  975. ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))]
  976. ]);
  977. }
  978. $count = $query->count();
  979. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
  980. $list = $query->select('*')->orderBy('created_at desc')
  981. ->limit($pagination->limit)
  982. ->offset($pagination->offset)->asArray()->all();
  983. $range_name = Option::get('range_name', get_store_id(), 'bonus_pool', '')['value'];
  984. $holder_name = Option::get('holder_name', get_store_id(), 'bonus_pool', '')['value'];
  985. $area_name = Option::get('area_name', get_store_id(), 'bonus_pool', '')['value'];
  986. $old_range_name = Option::get('old_range_name', get_store_id(), 'bonus_pool', '')['value'];
  987. $direct_range_name = Option::get('direct_range_name', get_store_id(), 'bonus_pool', '')['value'];
  988. $old_add_holder_name = Option::get('old_add_holder_name', get_store_id(), 'bonus_pool', '')['value'];
  989. $saas_user = get_saas_user();
  990. foreach ($list as &$value) {
  991. $value['is_refund'] = $value['type'] == ShareDetail::TYPE_HOLDER_PROFIT ? 0 : Order::is_refund($value['store_id'], $value['type_id']);
  992. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  993. $value['send_time'] = $value['send_time'] ? date('Y-m-d H:i:s', $value['send_time']) : '';
  994. $value['profit_name'] = '分红';
  995. if ($value['type'] == ShareDetail::TYPE_HOLDER_PROFIT) {
  996. // $value['profit_name'] = $holder_name ?: '股东分红';
  997. $value['profit_name'] = $holder_name ?: '股东分红';
  998. }
  999. if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT) {
  1000. // $value['profit_name'] = $range_name ?: '级差分红';
  1001. $value['profit_name'] = $range_name ?: '级差分红';
  1002. }
  1003. if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD) {
  1004. // $value['profit_name'] = $range_name ?: '级差分红(原关系)';
  1005. $value['profit_name'] = $old_range_name ?: '级差分红(原关系)';
  1006. }
  1007. if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT_OLD) {
  1008. // $value['profit_name'] = $range_name ?: '直属团队收益分成(原关系)';
  1009. $value['profit_name'] = $old_add_holder_name ?: '直属团队收益分成(原关系)';
  1010. }
  1011. if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT) {
  1012. // $value['profit_name'] = $range_name ?: '直属团队收益分成';
  1013. $value['profit_name'] = $direct_range_name ?: '直属团队收益分成';
  1014. }
  1015. if ($value['type'] == ShareDetail::TYPE_AREA_PROFIT) {
  1016. // $value['profit_name'] = $area_name ?: '区域分红';
  1017. $value['profit_name'] = $area_name ?: '区域分红';
  1018. }
  1019. if ($value['type'] == ShareDetail::TYPE_FROST_PROFIT) {
  1020. // $value['profit_name'] = $range_name ?: '冻结金额释放';
  1021. $value['profit_name'] = '冻结金额释放';
  1022. }
  1023. if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL) {
  1024. // $value['profit_name'] = $range_name ?: '冻结金额释放';
  1025. $value['profit_name'] = '商城会员等级分红';
  1026. }
  1027. $value['trade_status'] = 0;
  1028. if ((int)$value['type'] !== 1) {
  1029. $order = Order::findOne($value['type_id']);
  1030. if ($order) {
  1031. $value['trade_status'] = (int)$order->trade_status;
  1032. }
  1033. }
  1034. }
  1035. $send_type_where = [];
  1036. if ($type) {
  1037. $send_type_where = ['sd.type' => [2, 5, 6, 9]];
  1038. }
  1039. if ($is_area) {
  1040. $send_type_where = ['sd.type' => 3];
  1041. }
  1042. $total = ShareDetail::find()->alias('sd')
  1043. ->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id])
  1044. ->leftJoin(['o' => Order::tableName()], 'o.id = sd.type_id')
  1045. ->andWhere(['or', ['!=', 'o.trade_status', 1], ['sd.type_id' => 0]])
  1046. ->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL])
  1047. ->andWhere($send_type_where)
  1048. ->sum('money') ?? '0.00';
  1049. return $this->asJson([
  1050. 'code' => 0,
  1051. 'msg' => 'success',
  1052. 'data' => [
  1053. 'total' => $total,
  1054. 'row_count' => $count,
  1055. 'page_count' => $pagination->pageCount,
  1056. 'list' => $list,
  1057. 'today_money' => $today_money ?? '0.00',
  1058. 'no_send_money' => $no_send_money ?? '0.00',
  1059. 'send_money' => $send_money ?? '0.00',
  1060. 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url ?? '',
  1061. 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '',
  1062. 'id' => $user_id,
  1063. 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?? '',
  1064. ]
  1065. ]);
  1066. }
  1067. /**
  1068. * 获取佣金相关
  1069. */
  1070. public function actionGetPrice()
  1071. {
  1072. $store_id = get_store_id();
  1073. $user_id = get_user_id();
  1074. $is_area = input_params('is_area');
  1075. $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
  1076. if (!$share_holder) {
  1077. return $this->asJson([
  1078. 'code' => 1,
  1079. 'msg' => '未拥有提现权限'
  1080. ]);
  1081. }
  1082. $cash = get_user()->price;
  1083. //区分区域代理
  1084. $groups = 'bonus_pool';
  1085. if ($is_area) {
  1086. $groups = 'bonus_pool_area_agent';
  1087. if (intval($share_holder->agent_type) === 0) {
  1088. return $this->asJson([
  1089. 'code' => 1,
  1090. 'msg' => '未拥有提现权限'
  1091. ]);
  1092. }
  1093. }
  1094. $shareHolderCashProfit = Option::get('shareHolderCashProfit', $this->store_id, $groups, 0)['value'];
  1095. $res = [
  1096. 'cash_profit' => $cash,
  1097. 'shareHolderCashProfit' => $shareHolderCashProfit
  1098. ];
  1099. // 新提现方式
  1100. $res['cash_method_list'] = [];
  1101. $saas_user = get_saas_user();
  1102. $cash_method = [];
  1103. $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one();
  1104. if (!empty($saas_user->withdraw_method)) {
  1105. $cash_method = json_decode($saas_user->withdraw_method, true);
  1106. if ($lg){
  1107. //灵工
  1108. $cash_method = array_merge($cash_method, [[
  1109. 'type' => 'lg'
  1110. ]]);
  1111. }
  1112. if (is_array($cash_method)) {
  1113. foreach($cash_method as $i => $item){
  1114. if ($item['type'] === 'wechat' && Option::get('pay_wechat', $this->store_id, $groups, 0)['value'] == 0) {
  1115. unset($cash_method[$i]);
  1116. }
  1117. if ($item['type'] === 'alipay' && Option::get('pay_alipay', $this->store_id, $groups, 0)['value'] == 0) {
  1118. unset($cash_method[$i]);
  1119. }
  1120. if ($item['type'] === 'bank_card' && Option::get('bank', $this->store_id, $groups, 0)['value'] == 0) {
  1121. unset($cash_method[$i]);
  1122. }
  1123. if ($item['type'] === 'lg' && Option::get('lg', $this->store_id, $groups, 0)['value'] == 0) {
  1124. unset($cash_method[$i]);
  1125. }
  1126. }
  1127. }
  1128. }else{
  1129. if($lg){
  1130. //灵工
  1131. $cash_method[]['type'] = 'lg';
  1132. $res['cash_method_list'] = array_values($cash_method);
  1133. }
  1134. }
  1135. if(Option::get('remaining_sum', $this->store_id, $groups, 0)['value'] == 1){
  1136. $cash_method = array_merge($cash_method, [['type' => 'money']]);
  1137. }
  1138. $res['cash_method_list'] = array_values($cash_method);
  1139. return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]);
  1140. }
  1141. /**
  1142. * 获取提现方式
  1143. */
  1144. public function actionGetCashMethod()
  1145. {
  1146. $saas_user = get_saas_user();
  1147. $cash_method = [];
  1148. $is_area = input_params('is_area');
  1149. $groups = 'bonus_pool';
  1150. //区分区域代理
  1151. if ($is_area) {
  1152. $groups = 'bonus_pool_area_agent';
  1153. }
  1154. $payTypeList = [
  1155. [
  1156. 'name' => '微信',
  1157. 'is_show' => intval(Option::get('pay_wechat', $this->store_id, $groups, 0)['value']),
  1158. 'type' => 'wechat',
  1159. 'is_bind' => false
  1160. ],
  1161. [
  1162. 'name' => '支付宝',
  1163. 'is_show' => intval(Option::get('pay_alipay', $this->store_id, $groups, 0)['value']),
  1164. 'type' => 'alipay',
  1165. 'is_bind' => false
  1166. ],
  1167. [
  1168. 'name' => '银行卡',
  1169. 'is_show' => intval(Option::get('bank', $this->store_id, $groups, 0)['value']),
  1170. 'type' => 'bank_card',
  1171. 'is_bind' => false
  1172. ],
  1173. [
  1174. 'name' => '灵工',
  1175. 'is_show' => intval(Option::get('lg', $this->store_id, $groups, 0)['value']),
  1176. 'type' => 'lg',
  1177. 'is_bind' => false
  1178. ],
  1179. ];
  1180. $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one();
  1181. if (!empty($saas_user->withdraw_method)) {
  1182. $decode = json_decode($saas_user->withdraw_method, true);
  1183. if ($lg){
  1184. //灵工
  1185. $decode = array_merge($decode, [[
  1186. 'type' => 'lg'
  1187. ]]);
  1188. }
  1189. if (is_array($decode)) {
  1190. $cash_method = $decode;
  1191. foreach ($payTypeList as &$pay_item) {
  1192. foreach ($cash_method as $item) {
  1193. if ($item['type'] === $pay_item['type']) {
  1194. $pay_item['is_bind'] = true;
  1195. $pay_item = array_merge($pay_item, $item);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }else{
  1201. if($lg){
  1202. //灵工
  1203. $cash_method[]['type'] = 'lg';
  1204. $res['cash_method_list'] = array_values($cash_method);
  1205. }
  1206. }
  1207. return $this->asJson([
  1208. 'code' => 0,
  1209. 'msg' => 'success',
  1210. 'data' => [
  1211. 'list' => $payTypeList,
  1212. ],
  1213. ]);
  1214. }
  1215. /**
  1216. * 获取提现方式
  1217. */
  1218. public function actionDelCashMethod()
  1219. {
  1220. $saas_user = get_saas_user();
  1221. $type = post_params('type');
  1222. if (empty($type)) {
  1223. return $this->asJson([
  1224. 'code' => 1,
  1225. 'msg' => '参数错误',
  1226. ]);
  1227. }
  1228. if ($type == 'lg'){
  1229. $lg = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
  1230. if ($lg){
  1231. $lg->is_delete = 1;
  1232. $lg->save();
  1233. return $this->asJson([
  1234. 'code' => 0,
  1235. 'msg' => '操作成功',
  1236. ]);
  1237. }
  1238. }
  1239. if (empty($saas_user->withdraw_method)) {
  1240. return $this->asJson([
  1241. 'code' => 0,
  1242. 'msg' => '操作成功',
  1243. ]);
  1244. }
  1245. $cash_method = json_decode($saas_user->withdraw_method, true);
  1246. if (is_array($cash_method)) {
  1247. foreach ($cash_method as $key => $value) {
  1248. if ($value['type'] == $type) {
  1249. unset($cash_method[$key]);
  1250. }
  1251. }
  1252. $saas_user->withdraw_method = json_encode(array_values($cash_method));
  1253. } else {
  1254. $saas_user->withdraw_method = '';
  1255. }
  1256. if ($saas_user->save()) {
  1257. return $this->asJson([
  1258. 'code' => 0,
  1259. 'msg' => '操作成功',
  1260. ]);
  1261. }
  1262. return $this->asJson([
  1263. 'code' => 0,
  1264. 'msg' => '操作失败',
  1265. ]);
  1266. }
  1267. /**
  1268. * 添加提现方式
  1269. */
  1270. public function actionAddCashMethod()
  1271. {
  1272. $type = post_params('type');
  1273. if (empty($type)) {
  1274. return $this->asJson([
  1275. 'code' => 1,
  1276. 'msg' => '参数错误',
  1277. ]);
  1278. }
  1279. $name = post_params('name');
  1280. $account = post_params('account');
  1281. $bank = post_params('bank');
  1282. $cash_img = post_params('cash_img');
  1283. if (empty($name) || empty($account)) {
  1284. return $this->asJson([
  1285. 'code' => 1,
  1286. 'msg' => '账号和姓名不能为空',
  1287. ]);
  1288. }
  1289. if ($type == 'bank_card' && empty($bank)) {
  1290. return $this->asJson([
  1291. 'code' => 1,
  1292. 'msg' => '银行不能为空',
  1293. ]);
  1294. }
  1295. $data = [
  1296. 'type' => $type,
  1297. 'name' => $name,
  1298. 'account' => $account,
  1299. 'cash_img' => $cash_img,
  1300. ];
  1301. if ($type == 'bank_card') {
  1302. $data['bank'] = $bank;
  1303. }
  1304. $saas_user = get_saas_user();
  1305. if (empty($saas_user->withdraw_method)) {
  1306. $saas_user->withdraw_method = json_encode(array_values([$data]));
  1307. } else {
  1308. $decode = json_decode($saas_user->withdraw_method, true);
  1309. if (!$decode) {
  1310. $saas_user->withdraw_method = json_encode(array_values([$data]));
  1311. } else {
  1312. foreach ($decode as $key => $value) {
  1313. if ($value['type'] == $type) {
  1314. unset($decode[$key]);
  1315. break;
  1316. }
  1317. }
  1318. $decode[] = $data;
  1319. $saas_user->withdraw_method = json_encode(array_values($decode));
  1320. }
  1321. }
  1322. if ($saas_user->save()) {
  1323. return $this->asJson([
  1324. 'code' => 0,
  1325. 'msg' => '操作成功',
  1326. ]);
  1327. }
  1328. return $this->asJson([
  1329. 'code' => 1,
  1330. 'msg' => '操作失败',
  1331. ]);
  1332. }
  1333. public function actionCashApply() {
  1334. checkReplay([get_user_id()]);
  1335. $price = post_params('cash');
  1336. $type = post_params('type');
  1337. $is_area = post_params('is_area');
  1338. if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat', 'money','lg'])) {
  1339. return $this->asJson([
  1340. 'code' => 1,
  1341. 'msg' => '参数非法',
  1342. ]);
  1343. }
  1344. $user_id = get_user_id();
  1345. $user = get_user();
  1346. $store_id = get_store_id();
  1347. $groups = 'bonus_pool';
  1348. $cash_type = 3;
  1349. if ($is_area) {
  1350. $groups = 'bonus_pool_area_agent';
  1351. $cash_type = Cash::IS_CASH_TYPE_AREA_AGENT;
  1352. }
  1353. $cash_max_day = Option::get('cash_max_day', $store_id, $groups, 0)['value'];
  1354. $cash_max_single_day = Option::get('cash_max_single_day', $store_id, $groups, 0)['value'];
  1355. $min_money = Option::get('min_money', $store_id, $groups, 0)['value'];
  1356. if($price < $min_money){
  1357. return $this->asJson([
  1358. 'code' => 1,
  1359. 'msg' => '最小提现'. $min_money .'元'
  1360. ]);
  1361. }
  1362. $cash_sum = Cash::find()->where([
  1363. 'store_id' => $store_id,
  1364. 'is_delete' => 0,
  1365. 'status' => [0, 1, 2, 5, 4],
  1366. 'cash_type' => $cash_type,
  1367. 'user_id' => $user_id
  1368. ])->andWhere([
  1369. 'AND',
  1370. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  1371. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  1372. ])->sum('price');
  1373. if ($cash_max_day) {
  1374. $cash_max_day = $cash_max_day - ($cash_sum ?: 0);
  1375. if ($price > $cash_max_day) {
  1376. return $this->asJson([
  1377. 'code' => 1,
  1378. 'msg' => '提现金额不能超过' . $cash_max_day . '元'
  1379. ]);
  1380. }
  1381. }
  1382. if ($cash_max_single_day) {
  1383. $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
  1384. if ($price > $cash_max_single_day_) {
  1385. return $this->asJson([
  1386. 'code' => 1,
  1387. 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元'
  1388. ]);
  1389. }
  1390. }
  1391. //提现手续费金额
  1392. $shareHolderCashProfit = Option::get('shareHolderCashProfit', get_store_id(), $groups, 0)['value'];
  1393. $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
  1394. if (!$share_holder) {
  1395. return $this->asJson([
  1396. 'code' => 1,
  1397. 'msg' => '未拥有提现权限'
  1398. ]);
  1399. }
  1400. if ($is_area) {
  1401. if (intval($share_holder->agent_type) === 0) {
  1402. return $this->asJson([
  1403. 'code' => 1,
  1404. 'msg' => '未拥有提现权限'
  1405. ]);
  1406. }
  1407. }
  1408. //计算提现手续费金额
  1409. // $shareHolderCashPrice = $price * ($shareHolderCashProfit / 100);
  1410. if ($price > $user->price) {
  1411. return $this->asJson([
  1412. 'code' => 1,
  1413. 'msg' => '当前大于可提现金额'
  1414. ]);
  1415. }
  1416. $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $user_id,
  1417. 'store_id' => $store_id, 'cash_type' => $cash_type])->exists();
  1418. if ($exit) {
  1419. return $this->asJson([
  1420. 'code' => 1,
  1421. 'msg' => '尚有未完成的提现申请'
  1422. ]);
  1423. }
  1424. $t = \Yii::$app->db->beginTransaction();
  1425. $cash = new Cash();
  1426. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  1427. $cash->is_delete = 0;
  1428. $cash->status = 0;
  1429. $cash->price = $price;
  1430. $cash->created_at = time();
  1431. $cash->user_id = $user_id;
  1432. $cash->store_id = $store_id;
  1433. $saas_user = get_saas_user();
  1434. if (!empty($saas_user->withdraw_method)) {
  1435. $withdraw_method = Json::decode($saas_user->withdraw_method);
  1436. $withdraw_method = array_column($withdraw_method, null, 'type');
  1437. if ($type == 'wechat') {
  1438. $cash->type = 0;
  1439. $cash->name = $withdraw_method['wechat']['name'];
  1440. $cash->mobile = $withdraw_method['wechat']['account'];
  1441. }
  1442. if ($type == 'alipay') {
  1443. $cash->type = 1;
  1444. $cash->name = $withdraw_method['alipay']['name'];
  1445. $cash->mobile = $withdraw_method['alipay']['account'];
  1446. }
  1447. if ($type == 'bank_card') {
  1448. $cash->type = 2;
  1449. $cash->name = $withdraw_method['bank_card']['name'];
  1450. $cash->mobile = $withdraw_method['bank_card']['account'];
  1451. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  1452. $cash->bank_branch = $withdraw_method['bank_card']['branch'];
  1453. }
  1454. if ($type == 'lg') {
  1455. //灵工提现
  1456. $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
  1457. $cash->type = 4;
  1458. $cash->name = $lg_info->name;
  1459. $cash->mobile = $lg_info->mobile;
  1460. }
  1461. }else{
  1462. if ($type == 'lg') {
  1463. //灵工提现
  1464. $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one();
  1465. $cash->type = 4;
  1466. $cash->name = $lg_info->name;
  1467. $cash->mobile = $lg_info->mobile;
  1468. }
  1469. }
  1470. if ($type == 'money') {
  1471. $cash->type = 3;
  1472. }
  1473. $cash->pay_time = 0;
  1474. $cash->service_charge = $type == 'money' ? 0 : $shareHolderCashProfit;
  1475. $cash->cash_type = $cash_type;
  1476. if ($cash->save()) {
  1477. $user->price -= $cash->price;
  1478. if (!$user->save()) {
  1479. $t->rollBack();
  1480. return $this->asJson([
  1481. 'code' => 1,
  1482. 'msg' => $user->errors
  1483. ]);
  1484. }
  1485. $auto_money = Option::get('auto_money', $store_id, $groups, 0)['value'];
  1486. if ($cash_type == 3 && ($cash->type == Cash::TYPE_RECHARGE || ($price <= $auto_money && $auto_money > 0))) {
  1487. // TODO 在设置金额内,股东 / 2+1佣金提现自动打款
  1488. $this->cashConfirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $store_id);
  1489. }
  1490. $t->commit();
  1491. return $this->asJson([
  1492. 'code' => 0,
  1493. 'msg' => '申请成功'
  1494. ]);
  1495. } else {
  1496. $t->rollBack();
  1497. return $this->asJson([
  1498. 'code' => 1,
  1499. 'msg' => $cash->errors
  1500. ]);
  1501. }
  1502. }
  1503. /**
  1504. * 打款
  1505. * @return \yii\web\Response
  1506. */
  1507. public function cashConfirm($id, $status, $store_id)
  1508. {
  1509. $store = Store::findOne($store_id);
  1510. $cash = Cash::findOne([
  1511. 'id' => $id,
  1512. 'is_delete' => Cash::IS_DELETE_NO,
  1513. 'store_id' => $store_id
  1514. ]);
  1515. if($status == Cash::STATUS_GIVEN){
  1516. $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true) ?: [];
  1517. $val = 0;
  1518. foreach ($store_wechat_cash as $value) {
  1519. if (empty($value)) {
  1520. $val = 1;
  1521. break;
  1522. } else {
  1523. $val = 2;
  1524. break;
  1525. }
  1526. }
  1527. if ($store->is_platform_transfers == 0 && $val == 2) {
  1528. return [
  1529. 'code' => 1,
  1530. 'msg' => '未开启平台转账'
  1531. ];
  1532. } elseif ($store->is_platform_transfers == 1 && $val == 1) {
  1533. return [
  1534. 'code' => 1,
  1535. 'msg' => '未配置平台微信提现设置'
  1536. ];
  1537. }
  1538. }
  1539. if (!$cash->order_no) {
  1540. $order_no = null;
  1541. while (true) {
  1542. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  1543. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  1544. if (!$exist_order_no) {
  1545. break;
  1546. }
  1547. }
  1548. $cash->order_no = $order_no;
  1549. $cash->save();
  1550. }
  1551. $res = [];
  1552. $price = ($cash->price - ($cash->price * ($cash->service_charge / 100)));
  1553. $servePrice = ($cash->price * ($cash->service_charge / 100));
  1554. if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
  1555. return [
  1556. 'code' => 1,
  1557. 'msg' => '店铺剩余提现余额不足'
  1558. ];
  1559. }
  1560. $wechat_type = 1;
  1561. $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
  1562. // if (empty($wechat_cash)) {
  1563. // $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  1564. // if ($wechat_cash) {
  1565. // $wechat_type = 0;
  1566. // }
  1567. // }
  1568. foreach ((array)json_decode($wechat_cash, true) as $value) {
  1569. if (empty($value)) {
  1570. $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  1571. if ($wechat_cash) {
  1572. $wechat_type = 0;
  1573. }
  1574. break;
  1575. }
  1576. }
  1577. if ($status == Cash::STATUS_GIVEN) { //微信自动打款
  1578. if ((int)$cash->type === 0) {
  1579. $cash->status = Cash::STATUS_GIVEN;
  1580. $cash->pay_time = time();
  1581. $cash->pay_type = Cash::PAY_TYPE_WX;
  1582. $user = User::findOne($cash->user_id);
  1583. $wechat_open_id = $user->wechat_open_id;
  1584. $data = [
  1585. 'partner_trade_no' => $cash->order_no,
  1586. 'openid' => $wechat_open_id,
  1587. 'check_name' => 'NO_CHECK',
  1588. 'amount' => $price * 100,
  1589. 'desc' => '转账'
  1590. ];
  1591. // TODO 扣除店铺提现金额
  1592. if ($wechat_type == 0) {
  1593. $before = $store->store_balance;
  1594. $store->store_balance -= $price;
  1595. $store->price -= $price;
  1596. $store->store_withdrawn_cash += $price;
  1597. $after = $store->store_balance;
  1598. if ($store->save()) {
  1599. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  1600. }
  1601. }
  1602. $wechat = \Yii::$app->controller->wechatPay;
  1603. // $res = $wechat->transfer->toBalance($data);
  1604. $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
  1605. //判断是否使用新版本转账 增加转账标识
  1606. $wechat_cash = json_decode($wechat_cash, true);
  1607. if (intval($wechat_cash['is_open']) === 2) {
  1608. if (!$res['code']) {
  1609. $cash->is_platform_transfers = intval($store->is_platform_transfers);
  1610. $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
  1611. $cash->wx_cash_state = $res['data']['state'];
  1612. $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
  1613. $cash->save();
  1614. }
  1615. }
  1616. // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
  1617. } elseif ((int)$cash->type === 1) {
  1618. $order = (object)[
  1619. 'store_id' => $cash->store_id,
  1620. 'order_no' => $cash->order_no,
  1621. 'pay_price' => $price,
  1622. 'name' => $cash->name
  1623. ];
  1624. $result = Alipay::transfer($order, $cash->mobile);
  1625. if (isset($result['code']) && $result['code'] == 1) {
  1626. if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
  1627. $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
  1628. }
  1629. if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
  1630. $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
  1631. }
  1632. if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
  1633. $result['msg'] = '商户余额不足';
  1634. }
  1635. return $result;
  1636. } else {
  1637. // TODO 扣除店铺提现金额
  1638. if ($wechat_type == 0) {
  1639. $before = $store->store_balance;
  1640. $store->store_balance -= $price;
  1641. $store->price -= $price;
  1642. $store->store_withdrawn_cash += $price;
  1643. $after = $store->store_balance;
  1644. if ($store->save()) {
  1645. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  1646. }
  1647. }
  1648. }
  1649. $cash->status = Cash::STATUS_GIVEN;
  1650. $cash->pay_time = time();
  1651. $cash->pay_type = Cash::TYPE_ALIPAY;
  1652. $cash->save();
  1653. return [
  1654. 'code' => 0,
  1655. 'msg' => '成功'
  1656. ];
  1657. } elseif ((int)$cash->type === 4) {
  1658. $user = \app\models\User::findOne($cash->user_id);
  1659. $saas_user = \app\models\SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  1660. $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'status' => 1, 'is_delete' => 0])->one();
  1661. $lgApi = new LgApi($cash->store_id);
  1662. //灵工提现
  1663. $post_data = [
  1664. 'store_id' => $cash->store_id,
  1665. 'outTradeNo' => $cash->order_no, //唯一批次号
  1666. 'accNo' => bcmul($price, 100),
  1667. 'amt' => bcmul($price, 100),
  1668. 'name' => $cash->name,
  1669. 'certCard' => $lg_info->cert_card //身份证号
  1670. ];
  1671. $result = $lgApi->FlexiblePay($post_data);
  1672. if (isset($result['status']) && $result['status'] == 999) {
  1673. return $result;
  1674. }
  1675. $cash->status = 6; //灵工待打款
  1676. $cash->save();
  1677. //灵工提现接口调用后消息队列查询状态
  1678. \queue_push(new LgCashJob(['id' => $cash->id, 'store_id' => $store_id, 'type' => 3, 'retry' => 5]), 60);
  1679. return [
  1680. 'code' => 0,
  1681. 'msg' => '成功'
  1682. ];
  1683. }
  1684. } elseif ($status == Cash::STATUS_HAND) { //手动打款
  1685. // TODO 扣除店铺提现金额
  1686. if ($wechat_type == 0) {
  1687. $before = $store->store_balance;
  1688. $store->store_balance -= $price;
  1689. $store->price -= $price;
  1690. $store->store_withdrawn_cash += $price;
  1691. $after = $store->store_balance;
  1692. if ($store->save()) {
  1693. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  1694. }
  1695. }
  1696. // if ((int)$cash->type === 3) {
  1697. // AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '股东提现打款');
  1698. // }
  1699. $cash->status = Cash::STATUS_GIVEN;
  1700. $cash->pay_time = time();
  1701. $cash->pay_type = Cash::PAY_TYPE_HAND;
  1702. $res['result_code'] = "SUCCESS";
  1703. // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
  1704. }
  1705. if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
  1706. $cash->save();
  1707. return [
  1708. 'code' => 0,
  1709. 'msg' => '成功'
  1710. ];
  1711. } else {
  1712. return [
  1713. 'code' => 1,
  1714. 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
  1715. 'data' => $res
  1716. ];
  1717. }
  1718. }
  1719. /**
  1720. * 提现明细
  1721. * @return \yii\web\Response
  1722. */
  1723. public function actionCashDetail() {
  1724. $page = get_params('page', 1);
  1725. $limit = get_params('limit', 20);
  1726. $status = get_params('status', -1);
  1727. $is_area = get_params('is_area', -1);
  1728. $store_id = get_store_id();
  1729. $user_id = get_user_id();
  1730. $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]);
  1731. if (!$share_holder) {
  1732. return $this->asJson([
  1733. 'code' => 1,
  1734. 'msg' => '未拥有提现权限'
  1735. ]);
  1736. }
  1737. if ($is_area) {
  1738. if (intval($share_holder->agent_type) === 0) {
  1739. return $this->asJson([
  1740. 'code' => 1,
  1741. 'msg' => '未拥有提现权限'
  1742. ]);
  1743. }
  1744. }
  1745. $query = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id]);
  1746. $cash_type_where = ['cash_type' => 3];
  1747. if ($is_area) {
  1748. $cash_type_where = ['cash_type' => Cash::IS_CASH_TYPE_AREA_AGENT];
  1749. }
  1750. $query->andWhere($cash_type_where);
  1751. if ($status != -1) {
  1752. // 待审核
  1753. if ($status == 0) {
  1754. $query->andWhere(['status' => $status]);
  1755. }
  1756. // 已打款
  1757. if ($status == 1) {
  1758. $query->andWhere(['in', 'status', [2, 4]]);
  1759. }
  1760. // 已拒绝
  1761. if ($status == 2) {
  1762. $query->andWhere(['status' => 3]);
  1763. }
  1764. }
  1765. $count = $query->count();
  1766. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]);
  1767. $list = $query->select('*')->limit($pagination->limit)
  1768. ->offset($pagination->offset)->orderBy('created_at desc')->asArray()->all();
  1769. foreach ($list as &$value) {
  1770. $value['price'] = sprintf('%.2f', ($value['price'] - ($value['price'] * ($value['service_charge'] / 100))));
  1771. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  1772. $value['audit_time'] = date('Y-m-d H:i:s', $value['updated_at']);
  1773. if ($value['status'] == 2 || $value['status'] == 4) {
  1774. $value['audit_time'] = date('Y-m-d H:i:s', $value['pay_time']);
  1775. }
  1776. }
  1777. return $this->asJson([
  1778. 'code' => 0,
  1779. 'msg' => 'success',
  1780. 'data' => [
  1781. 'list' => $list,
  1782. 'page_count' => $pagination->pageCount,
  1783. 'row_count' => $count
  1784. ]
  1785. ]);
  1786. }
  1787. /**
  1788. * @return mixed|string
  1789. * 获取推广海报
  1790. */
  1791. public function actionGetQrcode()
  1792. {
  1793. $form = new ShareQrcodeForm();
  1794. $form->store_id = get_store_id();
  1795. $form->type = 9;
  1796. $form->user = get_saas_user();
  1797. $form->user_id = get_user_id();
  1798. return $this->asJson($form->search());
  1799. }
  1800. public function actionOtherInfo() {
  1801. return $this->asJson([
  1802. 'code' => 0,
  1803. 'data' => [
  1804. 'agreement' => Option::get('holder_agreement', get_store_id(), 'store', '')['value'] ?: '',
  1805. 'banner' => Option::get('holder_banner', get_store_id(), 'store', '')['value'] ?: [],
  1806. ]
  1807. ]);
  1808. }
  1809. // 获取冻结佣金列表
  1810. public function actionFrostList()
  1811. {
  1812. $status = \get_params('status', -1);
  1813. $user_id = \get_user_id();
  1814. $query = ShareHolderFrostLog::find()->alias('shf')->where([
  1815. 'parent_user_id' => $user_id,
  1816. ]);
  1817. if ($status > -1) {
  1818. $query->andWhere(['status' => $status]);
  1819. }
  1820. $profit_q = clone $query;
  1821. $send_profit_q = clone $query;
  1822. $no_send_profit_q = clone $query;
  1823. $profit = $profit_q->sum('amount') ?? '0.00';
  1824. $send_profit = $send_profit_q->andWhere(['status' => 1])->sum('amount') ?? '0.00';
  1825. $no_send_profit = $no_send_profit_q->andWhere(['status' => 0])->sum('amount') ?? '0.00';
  1826. $query->leftJoin(['o' => Order::tableName()], 'shf.order_id = o.id');
  1827. $query->andWhere(['<>', 'o.trade_status', 1]);
  1828. $query->select('shf.*');
  1829. $data = \pagination_make($query, true, 'shf.created_at desc');
  1830. $data = array_merge($data, [
  1831. 'profit' => $profit,
  1832. 'send_profit' => $send_profit,
  1833. 'no_send_profit' => $no_send_profit,
  1834. ]);
  1835. foreach ($data['list'] as $key => &$value) {
  1836. $value['is_refund'] = Order::is_refund($value['store_id'], $value['order_id']);
  1837. $value['name'] = '未知';
  1838. $value['avatar'] = '';
  1839. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  1840. $user = User::findOne($value['child_user_id']);
  1841. if ($user && $user->binding) {
  1842. $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
  1843. if ($saasUser) {
  1844. $value['name'] = $saasUser->name;
  1845. $value['avatar'] = $saasUser->avatar;
  1846. }
  1847. }
  1848. }
  1849. return $this->asJson([
  1850. 'code' => 0,
  1851. 'msg' => 'success',
  1852. 'data' => $data,
  1853. ]);
  1854. }
  1855. public function actionGetParentOutList() {
  1856. try {
  1857. // $query = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]])
  1858. // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id')
  1859. // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  1860. // ->select('su.name, su.avatar');
  1861. $query = User::find()->alias('u')->where(['u.id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL])
  1862. ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id')
  1863. ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id')
  1864. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  1865. ->select('su.name, su.avatar')
  1866. ->groupBy('u.id');
  1867. $pagination = pagination_make($query);
  1868. $list = $pagination['list'];
  1869. return $this->asJson([
  1870. 'code' => 0,
  1871. 'msg' => 'success',
  1872. 'data' => [
  1873. 'list' => $list,
  1874. 'pageNo' => $pagination['pageNo'],
  1875. 'totalCount' => $pagination['totalCount']
  1876. ]
  1877. ]);
  1878. } catch (\Exception $e) {
  1879. return $this->asJson([
  1880. 'code' => 1,
  1881. 'msg' => $e->getMessage()
  1882. ]);
  1883. }
  1884. }
  1885. //判断股东是否达到升级条件
  1886. public function actionAuditStatus() {
  1887. try {
  1888. $store_id = get_store_id();
  1889. $user_id = get_user_id();
  1890. $holder_audit = (int)Option::get('holder_audit', get_store_id(), 'store', 0)['value'] ?: 0;
  1891. //判断是否是股东
  1892. $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]);
  1893. if (!empty($share_holder)) {
  1894. if ((int)$share_holder->status === 0) {
  1895. return $this->asJson([
  1896. 'code' => 1,
  1897. 'msg' => '股东申请正在审核中'
  1898. ]);
  1899. }
  1900. //已经是股东
  1901. return $this->asJson([
  1902. 'code' => 0,
  1903. 'data' => [
  1904. 'is_holder' => 1
  1905. ],
  1906. 'msg' => '股东已经通过申请'
  1907. ]);
  1908. }
  1909. //默认等级
  1910. $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]);
  1911. if (!$share_holder_level) {
  1912. throw new \Exception('请设置默认等级');
  1913. }
  1914. //是否可申请
  1915. $is_apply_holder = 1;
  1916. //判断是否达成条件
  1917. $data = [];
  1918. $condition = Json::decode($share_holder_level->condition);
  1919. //团队人数
  1920. //TODO 股东人数以及金额
  1921. $man = $condition['man'];
  1922. $data['man'] = [
  1923. 'is_open' => (int)$man['is_open'],
  1924. 'data' => null
  1925. ];
  1926. if ((bool)$man['is_open']) {
  1927. //团队人数
  1928. $count = array_column($man['value'], 'count') ? max(array_column($man['value'], 'count')) : 0;
  1929. $man_data = OldUserTreePath::find()->where([
  1930. 'parent_id' => $user_id
  1931. ])->andWhere([
  1932. '!=',
  1933. 'child_id',
  1934. $user_id
  1935. ])->asArray()->all();
  1936. //团队消费
  1937. $money = array_column($man['value'], 'money') ? max(array_column($man['value'], 'money')) : 0;
  1938. //消费达到$m['money']以上允许升级
  1939. $child_id = array_column($man_data, 'child_id');
  1940. $child_id[] = $user_id;
  1941. $pay_price = Order::find()->where(
  1942. [
  1943. 'store_id' => $store_id,
  1944. 'is_delete' => 0,
  1945. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  1946. 'user_id' => $child_id
  1947. ])
  1948. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  1949. if ($count > count($man_data)) {
  1950. $is_apply_holder = 0;
  1951. }
  1952. if ($money > ($pay_price ?? 0)) {
  1953. $is_apply_holder = 0;
  1954. }
  1955. $data['man']['data'] = [
  1956. 'count' => [
  1957. 'predict' => $count, //预计
  1958. 'real' => count($man_data),
  1959. 'status' => (bool)($count <= count($man_data))
  1960. ],
  1961. 'money' => [
  1962. 'predict' => $money,
  1963. 'real' => $pay_price ?? 0,
  1964. 'status' => (bool)($money <= ($pay_price ?? 0))
  1965. ]
  1966. ];
  1967. }
  1968. //团队订单金额
  1969. $team_order = $condition['order'];
  1970. $data['order'] = [
  1971. 'is_open' => (int)$team_order['is_open'],
  1972. 'data' => null
  1973. ];
  1974. if ((bool)$team_order['is_open']) {
  1975. $count = array_column($team_order['value'], 'count') ? max(array_column($team_order['value'], 'count')) : 0;
  1976. $man_data = OldUserTreePath::find()->where([
  1977. 'parent_id' => $user_id
  1978. ])->andWhere([
  1979. '!=',
  1980. 'child_id',
  1981. $user_id
  1982. ])->asArray()->all();
  1983. $man_user_id = User::findOld()->alias('u')->childs($user_id, true)->andWhere(['u.store_id' => $store_id])->select('u.id')->column();
  1984. // if (count($man_data) < 1) {
  1985. // $msg = '团队金额:' . $o['level'] . '级团队人数为空';
  1986. // \Yii::warning(['---------- USER ID: ' . $share['user_id'] . '判断用户是否可以晋升 '. $value->name, $msg]);
  1987. // $is_level = false;
  1988. // break;
  1989. // }
  1990. // $man_user_id = array_column($man_data, 'id');
  1991. // $man_user_id = [];
  1992. // $man_user_id[] = $user_id;
  1993. //
  1994. // foreach($man_data as $val) {
  1995. // $man_user_id[] = $val['child_id'];
  1996. // }
  1997. $order_money = Order::find()->where(
  1998. [
  1999. 'store_id' => $store_id,
  2000. 'is_delete' => 0,
  2001. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  2002. ])
  2003. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  2004. ->andWhere(['in', 'user_id', $man_user_id])->sum('pay_price');
  2005. if ($count > ($order_money ?? 0)) {
  2006. $is_apply_holder = 0;
  2007. }
  2008. $data['order']['data'] = [
  2009. 'money' => [
  2010. 'predict' => $count,
  2011. 'real' => $order_money ?? 0,
  2012. 'status' => (bool)($count <= ($order_money ?? 0))
  2013. ]
  2014. ];
  2015. }
  2016. //自身消费金额
  2017. $self = $condition['self'];
  2018. $data['self'] = [
  2019. 'is_open' => (int)$self['is_open'],
  2020. 'data' => null
  2021. ];
  2022. if ((bool)$self['is_open']) {
  2023. $data['self']['data'] = [
  2024. 'single' => null,
  2025. 'total' => null
  2026. ];
  2027. if ($self['value']['order_number'] == 1) {
  2028. $order_price = Order::find()->where(
  2029. [
  2030. 'store_id' => $store_id,
  2031. 'is_delete' => 0,
  2032. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  2033. 'user_id' => $user_id
  2034. ])
  2035. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->max('pay_price');
  2036. if ($self['value']['price'] > ($order_price ?? 0)) {
  2037. $is_apply_holder = 0;
  2038. }
  2039. //单笔金额
  2040. $data['self']['data']['single'] = [
  2041. 'predict' => $self['value']['price'],
  2042. 'real' => $order_price ?? 0,
  2043. 'status' => (bool)($self['value']['price'] <= ($order_price ?? 0))
  2044. ];
  2045. } else{
  2046. $order_money = Order::find()->where(
  2047. [
  2048. 'store_id' => $store_id,
  2049. 'is_delete' => 0,
  2050. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  2051. 'user_id' => $user_id
  2052. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  2053. if ($self['value']['price'] > ($order_money ?? 0)) {
  2054. $is_apply_holder = 0;
  2055. }
  2056. //单笔金额
  2057. $data['self']['data']['total'] = [
  2058. 'predict' => $self['value']['price'],
  2059. 'real' => $order_money ?? 0,
  2060. 'status' => (bool)($self['value']['price'] <= ($order_money ?? 0))
  2061. ];
  2062. }
  2063. }
  2064. //累计充值金额
  2065. $recharge = $condition['recharge'];
  2066. $data['recharge'] = [
  2067. 'is_open' => (int)$recharge['is_open'],
  2068. 'data' => null
  2069. ];
  2070. if ((bool)$recharge['is_open']) {
  2071. $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price');
  2072. if ($recharge['value'] > ($recharge_money ?? 0)) {
  2073. $is_apply_holder = 0;
  2074. }
  2075. $data['recharge']['data'] = [
  2076. 'money' => [
  2077. 'predict' => $recharge['value'],
  2078. 'real' => $recharge_money ?? 0,
  2079. 'status' => (bool)($recharge['value'] <= ($recharge_money ?? 0))
  2080. ]
  2081. ];
  2082. }
  2083. //是否购买指定产品
  2084. $goods = $condition['goods'];
  2085. $data['goods'] = [
  2086. 'is_open' => (int)$goods['is_open'],
  2087. 'data' => null
  2088. ];
  2089. if ((bool)$goods['is_open']) {
  2090. // 查找该用户是否买过此商品
  2091. $goods_model = Order::find()->alias('o')
  2092. ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
  2093. ->where(
  2094. [
  2095. 'o.store_id' => $store_id,
  2096. 'o.user_id' => $user_id,
  2097. 'o.is_delete' => 0, 'o.is_pay' => 1,
  2098. 'o.trade_status' => Order::ORDER_FLOW_CONFIRM
  2099. ])
  2100. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  2101. ->andWhere(['od.goods_id' => $goods['value']['id']])->one();
  2102. if (!$goods_model) {
  2103. $is_apply_holder = 0;
  2104. }
  2105. $data['goods']['data'] = [
  2106. 'num' => [
  2107. 'predict' => 0,
  2108. 'real' => 0,
  2109. 'status' => (bool)$goods_model
  2110. ]
  2111. ];
  2112. }
  2113. //累计充值金额
  2114. $amount_total = $condition['amount_total'];
  2115. $data['amount_total'] = [
  2116. 'is_open' => (int)$amount_total['is_open'],
  2117. 'data' => null
  2118. ];
  2119. if ((bool)$amount_total['is_open']) {
  2120. $total_price = get_user()->total_price;
  2121. if ($amount_total['value'] > ($total_price ?? 0)) {
  2122. $is_apply_holder = 0;
  2123. }
  2124. $data['amount_total']['data'] = [
  2125. 'money' => [
  2126. 'predict' => $amount_total['value'],
  2127. 'real' => $total_price ?? 0,
  2128. 'status' => (bool)($amount_total['value'] <= ($total_price ?? 0))
  2129. ]
  2130. ];
  2131. }
  2132. if ($holder_audit === 0 && $is_apply_holder === 1 && $user_id) { //如果未开启强制审核并已经达到升级条件
  2133. $is_apply_holder = 2; //直接跳转股东中心
  2134. $user = User::findOne($user_id);
  2135. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  2136. $model = new ShareHolder();
  2137. $model->level_id = $share_holder_level->id;
  2138. $model->store_id = $store_id;
  2139. $model->user_id = $user_id;
  2140. $model->name = $saas_user->name;
  2141. $model->address = '';
  2142. $model->mobile = $saas_user->mobile;
  2143. $model->province = 0;
  2144. $model->city = 0;
  2145. $model->district = 0;
  2146. $model->province_name = '';
  2147. $model->city_name = '';
  2148. $model->district_name = '';
  2149. $model->status = 1;
  2150. $model->audit_time = time();
  2151. $result = $model->save();
  2152. if (!$result) {
  2153. throw new \Exception(json_encode($model->errors));
  2154. }
  2155. //添加股东升级记录
  2156. $level_log = new \app\models\ShareHolderLevelLog();
  2157. $level_log->before_level = 0;
  2158. $level_log->after_level = $share_holder_level->id;
  2159. $level_log->user_id = $user_id;
  2160. $level_log->store_id = $store_id;
  2161. $level_log->order_id = 0;
  2162. $level_log->desc = "用户申请自动升级为股东";
  2163. if (!$level_log->save()) {
  2164. throw new \Exception('等级更新失败');
  2165. }
  2166. BonusPool::checkChildHolderUpdateLevel($user_id, $share_holder_level->id);
  2167. // BonusPool::checkLevel($store_id, 0, $model->user_id);
  2168. // BonusPool::ShareHolderLevelJob($store_id, 0, $model->user_id);
  2169. if ((int)$share_holder_level->member_level > (int)$user->level) {
  2170. $user->level = $share_holder_level->member_level;
  2171. }
  2172. $user->is_holder = 1;
  2173. $result = $user->save();
  2174. if (!$result) {
  2175. throw new \Exception(json_encode($user->errors));
  2176. }
  2177. }
  2178. $holder_agreement = Option::get('holder_agreement', get_store_id(), 'store', '')['value'];
  2179. return $this->asJson([
  2180. 'code' => 0,
  2181. 'msg' => 'success',
  2182. 'data' => array_merge($data, [
  2183. 'is_holder' => 0,
  2184. 'is_apply_holder' => $is_apply_holder,
  2185. 'holder_agreement' => $holder_agreement
  2186. ])
  2187. ]);
  2188. } catch (\Exception $e) {
  2189. return $this->asJson([
  2190. 'code' => 1,
  2191. 'msg' => $e->getMessage()
  2192. ]);
  2193. }
  2194. }
  2195. public function actionDefaultLevelGoods() {
  2196. try {
  2197. $store_id = get_store_id();
  2198. //默认等级
  2199. $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]);
  2200. $condition = Json::decode($share_holder_level->condition);
  2201. $goods = $condition['goods'];
  2202. $goods_ = null;
  2203. if ((bool)$goods['is_open']) {
  2204. $goods_ = Goods::find()->where(['id' => $goods['value']['id'], 'status' => 1, 'is_delete' => 0])
  2205. ->select('id, name, cover_pic, price, original_price, virtual_sales')->asArray()->all();
  2206. foreach ($goods_ as &$item) {
  2207. $cacheKey = 'actionDefaultLevelGoods' . $item['id'];
  2208. $cacheVal = cache()->get($cacheKey);
  2209. if($cacheVal){
  2210. $goods_num = $cacheVal;
  2211. }else{
  2212. $goods_num = OrderDetail::find()->alias('od')->where(['od.goods_id' => $item['id'], 'is_pay' => 1])
  2213. ->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
  2214. ->andWhere(['AND',
  2215. ['>=', 'o.created_at', strtotime(date("Y-m"))],
  2216. ['<', 'o.created_at', strtotime(date("Y") . '-' . (date("m") + 1))]
  2217. ])->select('od.num')->sum('od.num');
  2218. if($goods_num > 300){
  2219. cache()->set($cacheKey, $goods_num, 3 * 60);
  2220. }
  2221. }
  2222. $item['sales_num'] = $goods_num ? $goods_num + $item['virtual_sales'] : $item['virtual_sales'];
  2223. }
  2224. }
  2225. return $this->asJson([
  2226. 'code' => 0,
  2227. 'msg' => '获取成功',
  2228. 'data' => [
  2229. 'goods' => $goods_
  2230. ]
  2231. ]);
  2232. } catch (\Exception $e) {
  2233. return $this->asJson([
  2234. 'code' => 1,
  2235. 'msg' => $e->getMessage()
  2236. ]);
  2237. }
  2238. }
  2239. //区域代理申请
  2240. public function actionAreaAgentApply() {
  2241. try {
  2242. $store_id = get_store_id();
  2243. $user_id = get_user_id();
  2244. $agent_type = post_params('agent_type');
  2245. $province_id = post_params('province_id');
  2246. $city_id = post_params('city_id');
  2247. $district_id = post_params('district_id');
  2248. $town_id = post_params('town_id', 0);
  2249. $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]])
  2250. ->andWhere([">", 'agent_type', 0])->asArray()->one();
  2251. if ($shareHolderModel) {
  2252. throw new \Exception('已经为区域代理,无需申请');
  2253. }
  2254. $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'status' => [0, 1], 'is_delete' => 0])->asArray()->one();
  2255. if ($areaAgentApply) {
  2256. throw new \Exception('请勿重复申请');
  2257. }
  2258. if (!in_array($agent_type, [1, 2, 3, 4])) {
  2259. throw new \Exception('参数错误');
  2260. }
  2261. $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2262. $where = ['agent_type' => $agent_type, 'is_delete' => 0, 'status' => [0, 1], 'store_id' => $store_id];
  2263. switch ($agent_type) {
  2264. case 1:
  2265. $where['province_id'] = $province_id;
  2266. break;
  2267. case 2:
  2268. $where['province_id'] = $province_id;
  2269. $where['city_id'] = $city_id;
  2270. break;
  2271. case 3:
  2272. $where['province_id'] = $province_id;
  2273. $where['city_id'] = $city_id;
  2274. $where['district_id'] = $district_id;
  2275. break;
  2276. case 4:
  2277. $where['province_id'] = $province_id;
  2278. $where['city_id'] = $city_id;
  2279. $where['district_id'] = $district_id;
  2280. $where['town_id'] = $town_id;
  2281. break;
  2282. }
  2283. $area_apply = ShareHolderAreaAgentApply::find()->where($where)->one();
  2284. debug_log(['id' => $area_apply->id], 'area_share.log');
  2285. if ($area_apply && !$is_multi_agent) {
  2286. throw new \Exception('已经存在相同区域下的代理');
  2287. }
  2288. $share_holder = ShareHolder::find()->where($where)->one();
  2289. debug_log(['share_holder_id' => $share_holder->id], 'area_share.log');
  2290. if ($share_holder && !$is_multi_agent) {
  2291. throw new \Exception('已经存在相同区域下的代理');
  2292. }
  2293. $areaAgentApply = new ShareHolderAreaAgentApply();
  2294. $areaAgentApply->user_id = $user_id;
  2295. $areaAgentApply->agent_type = $agent_type;
  2296. $areaAgentApply->province_id = (int)$province_id;
  2297. $areaAgentApply->city_id = (int)$city_id;
  2298. $areaAgentApply->district_id = (int)$district_id;
  2299. $areaAgentApply->town_id = (int)$town_id;
  2300. $areaAgentApply->params = json_encode([
  2301. 'agent_type' => $agent_type,
  2302. 'province_id' => $province_id,
  2303. 'city_id' => $city_id,
  2304. 'town_id' => $town_id,
  2305. 'district_id' => $district_id
  2306. ], JSON_UNESCAPED_UNICODE);
  2307. $areaAgentApply->store_id = $store_id;
  2308. if (!$areaAgentApply->save()) {
  2309. throw new \Exception(json_encode($areaAgentApply->errors, JSON_UNESCAPED_UNICODE));
  2310. }
  2311. return $this->asJson([
  2312. 'code' => 0,
  2313. 'msg' => '提交成功'
  2314. ]);
  2315. } catch (\Exception $e) {
  2316. return $this->asJson([
  2317. 'code' => 1,
  2318. 'msg' => $e->getMessage()
  2319. ]);
  2320. }
  2321. }
  2322. //获取区域代理申请状态
  2323. public function actionGetAreaAgentApply() {
  2324. $store_id = get_store_id();
  2325. // 轮播广告状态
  2326. $area_agent_banner_status = Option::get('area_agent_banner_status', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2327. // 图片列表
  2328. $area_agent_banner_images = Option::get('area_agent_banner_images', $store_id, 'bonus_pool_area_agent', '')['value'];
  2329. if ($area_agent_banner_images) {
  2330. $area_agent_banner_images = json_decode($area_agent_banner_images, true);
  2331. }
  2332. try {
  2333. $user_id = get_user_id();
  2334. $status = 0;
  2335. $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'is_delete' => 0])->asArray()->one();
  2336. if ($areaAgentApply) {
  2337. $status = $areaAgentApply['status'];
  2338. } else {
  2339. $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]])
  2340. ->andWhere([">", 'agent_type', 0])->asArray()->one();
  2341. if ($shareHolderModel) {
  2342. $status = 1;
  2343. $areaAgentApply['agent_type'] = $shareHolderModel['agent_type'];
  2344. $areaAgentApply['province_id'] = $shareHolderModel['province_id'];
  2345. $areaAgentApply['city_id'] = $shareHolderModel['city_id'];
  2346. $areaAgentApply['district_id'] = $shareHolderModel['district_id'];
  2347. }
  2348. }
  2349. return $this->asJson([
  2350. 'code' => 0,
  2351. 'msg' => '获取成功',
  2352. 'data' => [
  2353. 'areaAgentApply' => $areaAgentApply,
  2354. 'agent_type' => $areaAgentApply['agent_type'] ?: 1,
  2355. 'status' => (int)$status,
  2356. 'province_id' => $areaAgentApply['province_id'],
  2357. 'city_id' => $areaAgentApply['city_id'],
  2358. 'district_id' => $areaAgentApply['district_id'],
  2359. 'town_id' => $areaAgentApply['town_id'],
  2360. 'province' => District::findOne($areaAgentApply['province_id'])->name ?: '',
  2361. 'city' => District::findOne($areaAgentApply['city_id'])->name ?: '',
  2362. 'district' => District::findOne($areaAgentApply['district_id'])->name ?: '',
  2363. 'town' => District::findOne($areaAgentApply['town_id'])->name ?: '',
  2364. 'area_agent_banner_status' => (int)$area_agent_banner_status,
  2365. 'area_agent_banner_images' => $area_agent_banner_images ?: []
  2366. ]
  2367. ]);
  2368. } catch (\Exception $e) {
  2369. return $this->asJson([
  2370. 'code' => 0,
  2371. 'msg' => $e->getMessage(),
  2372. 'data' => [
  2373. 'agent_type' => 1,
  2374. 'status' => 0,
  2375. 'province_id' => '',
  2376. 'city_id' => '',
  2377. 'district_id' => '',
  2378. 'province' => '',
  2379. 'city' => '',
  2380. 'district' => '',
  2381. 'area_agent_banner_status' => (int)$area_agent_banner_status,
  2382. 'area_agent_banner_images' => $area_agent_banner_images ?: []
  2383. ]
  2384. ]);
  2385. }
  2386. }
  2387. //区域代理中心
  2388. public function actionGetAreaAgentInfo() {
  2389. $saas_user = get_saas_user();
  2390. $data = [
  2391. 'cash_profit' => 0,
  2392. 'total_price' => 0,
  2393. 'cash_price' => 0,
  2394. 'agent_rate' => 0,
  2395. 'agent_type' => 0,
  2396. 'avatar' => $saas_user->avatar,
  2397. 'name' => $saas_user->name,
  2398. 'mobile' => $saas_user->mobile
  2399. ];
  2400. try {
  2401. $user = get_user();
  2402. $store_id = get_store_id();
  2403. $user_id = get_user_id();
  2404. $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';
  2405. $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]);
  2406. $agent_rate = 0;
  2407. $status = 0;
  2408. $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2409. $multi_agent_type1 = $multi_agent_type2 = $multi_agent_type3 = $multi_agent_type4 = 0;
  2410. if($is_multi_agent){
  2411. $multi_agent_type1 = Option::get('multi_agent_type1', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2412. $multi_agent_type2 = Option::get('multi_agent_type2', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2413. $multi_agent_type3 = Option::get('multi_agent_type3', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2414. $multi_agent_type4 = Option::get('multi_agent_type4', $store_id, 'bonus_pool_area_agent', 0)['value'];
  2415. }
  2416. if ($share_holder) {
  2417. $agent_rate = $share_holder->agent_rate;
  2418. if (intval($is_multi_agent)) {
  2419. switch (intval($share_holder->agent_type)) {
  2420. case 2:
  2421. $agent_rate = $multi_agent_type2;
  2422. break;
  2423. case 3:
  2424. $agent_rate = $multi_agent_type3;
  2425. break;
  2426. case 4:
  2427. $agent_rate = $multi_agent_type4;
  2428. break;
  2429. default:
  2430. $agent_rate = $multi_agent_type1;
  2431. break;
  2432. }
  2433. } else {
  2434. if (intval($share_holder->agent_rate_status) === 0) {
  2435. $agent_rate = 0;
  2436. }
  2437. }
  2438. }
  2439. if (intval($share_holder->agent_type) > 0) {
  2440. $status = 1;
  2441. }
  2442. // 文档要求说只操作区域佣金提现
  2443. // 可提现区域代理佣金 = 区域代理佣金 - 已提现区域代理佣金
  2444. // 获取区域代理佣金
  2445. $area_agent_profit = ShareDetail::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'type' => ShareDetail::TYPE_AREA_PROFIT, 'is_send' => 1])
  2446. ->sum('money') ?: '0.00';
  2447. $data['status'] = $status;
  2448. //可提现分红
  2449. $data['cash_profit'] = $user->price;
  2450. //累计分红
  2451. $data['total_price'] = $area_agent_profit;//$user->total_price;
  2452. //已提现
  2453. $data['cash_price'] = $cash_price;
  2454. //佣金比例
  2455. $data['agent_rate'] = $agent_rate;
  2456. //区域代理类型 1省 2市 3区
  2457. $data['agent_type'] = $share_holder->agent_type;
  2458. return $this->asJson([
  2459. 'code' => 0,
  2460. 'msg' => '获取成功',
  2461. 'data' => $data
  2462. ]);
  2463. } catch (\Exception $e) {
  2464. return $this->asJson([
  2465. 'code' => 0,
  2466. 'msg' => $e->getMessage(),
  2467. 'data' => []
  2468. ]);
  2469. }
  2470. }
  2471. public function actionGetFugouInfo()
  2472. {
  2473. $store_id = get_store_id();
  2474. $user_id = \get_user_id();
  2475. $setting = FuGou::getSetting($store_id);
  2476. foreach ($setting['fugou_goods'] as $k => $goods) {
  2477. $g = \app\models\Goods::find()->where(['id' => $goods['id']])->select('id, name, cover_pic, price, goods_num, original_price')->one();
  2478. $setting['fugou_goods'][$k] = [
  2479. 'id' => $g->id,
  2480. 'name' => $g->name,
  2481. 'cover_pic' => $g->cover_pic,
  2482. 'price' => $g->price,
  2483. 'goods_num' => $g->goods_num,
  2484. 'original_price' => $g->original_price,
  2485. 'fugou_price' => $goods['fugou_price'],
  2486. 'fugou_num' => 1,
  2487. 'checked' => 0,
  2488. ];
  2489. }
  2490. return $this->asJson([
  2491. 'code' => 0,
  2492. 'msg' => 'success',
  2493. 'data' => [
  2494. 'setting' => $setting,
  2495. 'frost_price' => FuGou::getUserFrostPrice($user_id),
  2496. 'cycle_day' => FuGou::cycleByDay($user_id, $store_id, $setting),
  2497. 'cycle_price' => FuGou::cycleByPrice($user_id, $store_id, $setting),
  2498. ],
  2499. ]);
  2500. }
  2501. public function actionFugouCart()
  2502. {
  2503. try {
  2504. $store_id = get_store_id();
  2505. $user_id = \get_user_id();
  2506. $goods_list = \json_decode(\post_params('goods_list'), true);
  2507. $res = [[
  2508. 'mch_id' => 0,
  2509. 'goods_list' => [],
  2510. ]];
  2511. FugouCart::deleteAll(['user_id' => $user_id, 'store_id' => $store_id]);
  2512. foreach ($goods_list as $goods) {
  2513. $g = Goods::findOne($goods['goods_id']);
  2514. if (!$g) {
  2515. throw new \Exception('商品不存在');
  2516. }
  2517. $attr = json_decode($g->attr, true);
  2518. $modelAttr = [];
  2519. foreach ($attr[0]['attr_list'] as $item) {
  2520. if (!empty($item['attr_id'])) {
  2521. $modelAttr[] = intval($item['attr_id']);
  2522. }
  2523. }
  2524. $model = new FugouCart();
  2525. $model->user_id = $user_id;
  2526. $model->store_id = $store_id;
  2527. $model->goods_id = $goods['goods_id'];
  2528. $model->num = $goods['num'];
  2529. $model->price = $goods['price'];
  2530. $model->attr = json_encode($modelAttr);
  2531. if (!$model->save()) {
  2532. throw new \Exception('添加购物车失败');
  2533. }
  2534. $res[0]['goods_list'][] = [
  2535. 'fugou_cart_id' => $model->id,
  2536. ];
  2537. }
  2538. return $this->asJson([
  2539. 'code' => 0,
  2540. 'data' => $res,
  2541. ]);
  2542. } catch (\Exception $e) {
  2543. return $this->asJson([
  2544. 'code' => 1,
  2545. 'msg' => '下单失败,请稍后重试!',
  2546. 'err' => $e->getMessage(),
  2547. 'line' => $e->getLine(),
  2548. ]);
  2549. }
  2550. }
  2551. public function actionFugouFrostLog()
  2552. {
  2553. $user_id = \get_user_id();
  2554. $status = \get_params('status', -1);
  2555. $query = ShareHolderFugouFrostLog::find()->alias('f')
  2556. ->leftJoin(['o' => Order::tableName()], 'f.order_id = o.id')
  2557. ->where(['f.user_id' => $user_id])
  2558. ->select('f.*, o.order_no');
  2559. if ($status > -1) {
  2560. $query->andWhere(['f.status' => $status]);
  2561. }
  2562. $pagination = pagination_make($query);
  2563. foreach ($pagination['list'] as &$item) {
  2564. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  2565. if ($item['updated_at'] > 0) {
  2566. $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
  2567. }
  2568. }
  2569. return $this->asJson([
  2570. 'code' => 0,
  2571. 'msg' => 'success',
  2572. 'data' => [
  2573. 'list' => $pagination['list'],
  2574. 'pageNo' => $pagination['pageNo'],
  2575. 'totalCount' => $pagination['totalCount']
  2576. ]
  2577. ]);
  2578. }
  2579. }