ActivityOrderRebateSelfForm.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use app\models\ActivityOrderRebateSelf;
  12. use app\models\ActivityOrderRebateSelfLevel;
  13. use app\models\ActivityOrderRebateSelfGoods;
  14. use app\models\ActivityOrderRebateSelfOrder;
  15. use app\models\ActivityOrderRebateSelfLog;
  16. use app\models\ActivityOrderRebateSelfUser;
  17. use app\models\AccountLog;
  18. use app\models\UserShareMoney;
  19. use app\models\Store;
  20. use app\models\User;
  21. use yii\base\Model;
  22. use app\models\Option;
  23. use app\constants\OptionSetting;
  24. use app\models\SaasUser;
  25. use app\models\Order;
  26. use app\models\Attr;
  27. use app\models\AttrGroup;
  28. use app\models\OrderDetail;
  29. use app\models\IntegralAppreciationPool;
  30. class ActivityOrderRebateSelfForm extends Model
  31. {
  32. public $store_id;
  33. public $saas_id;
  34. public $user_id;
  35. public $id;
  36. public $act_id;
  37. public $ids;
  38. public $name;
  39. public $start_time;
  40. public $end_time;
  41. public $status;
  42. public $goods_ids;
  43. public $goods_name;
  44. public $send_type;
  45. public $send_time;
  46. public $send_num;
  47. public $send_days;
  48. // 新增字段
  49. public $send_types;
  50. public $send_type_percentages;
  51. public $release_rate;
  52. public $send_rebate_type;
  53. public $fixed_rebate_profit;
  54. public $level;
  55. public $money;
  56. public $child_num;
  57. public $child_level;
  58. public $add_rat;
  59. public $add_rebate;
  60. public $phone;
  61. public $order_no;
  62. public $is_send;
  63. public $admin;
  64. public function rules()
  65. {
  66. return [
  67. [['status', 'id', 'send_rebate_type'], 'integer'],
  68. [['start_time', 'end_time', 'ids', 'name'], 'string'],
  69. [[
  70. 'goods_name',
  71. 'store_id',
  72. 'goods_ids',
  73. 'act_id',
  74. 'send_type',
  75. 'send_time',
  76. 'send_num',
  77. 'send_days',
  78. 'saas_id',
  79. 'user_id'
  80. ], 'safe'],
  81. [[
  82. 'level',
  83. 'money',
  84. 'child_num',
  85. 'child_level',
  86. 'add_rat',
  87. 'add_rebate'
  88. ], 'safe'],
  89. [['order_no'], 'safe'],
  90. [['is_send'], 'safe'],
  91. [['phone'], 'safe'],
  92. [['fixed_rebate_profit'], 'number'],
  93. // 新增规则
  94. [['send_types', 'send_type_percentages', 'release_rate'], 'safe']
  95. ];
  96. }
  97. public function init()
  98. {
  99. parent::init();
  100. if (empty($this->store_id)) {
  101. $this->store_id = get_store_id();
  102. }
  103. }
  104. /**
  105. * 导入会员增加积分和佣金
  106. * @return array
  107. */
  108. public function importUserRecharge()
  109. {
  110. $admin = \Yii::$app->jwt->getAdmin();
  111. $store_id = $this->store_id;
  112. // 获取活动类型参数
  113. $activity_type = post_params('send_type', 'integral'); // 默认为积分活动
  114. $activity_id = post_params('activity_id', 0); // 获取选择的活动ID
  115. set_time_limit(0);
  116. $filename = $_FILES['excel']['name'];
  117. $tmpname = $_FILES['excel']['tmp_name'];
  118. $path = \Yii::$app->basePath . '/web/temp/';
  119. if(!is_dir($path)){
  120. mkdir($path);
  121. }
  122. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  123. if (($ext != 'xlsx') && ($ext != 'xls')) {
  124. return [
  125. 'code' => 1,
  126. 'msg' => '请上传excel文件'
  127. ];
  128. }
  129. $file = time() . $this->store_id . '.' . $ext;
  130. $uploadfile = $path . $file;
  131. move_uploaded_file($tmpname, $uploadfile);
  132. $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
  133. ->getRows();
  134. $err = [];
  135. $success = [];
  136. // 如果指定了活动ID,则获取该活动
  137. $activity = null;
  138. if ($activity_id) {
  139. $activity = ActivityOrderRebateSelf::findOne($activity_id);
  140. if (!$activity) {
  141. return [
  142. 'code' => 1,
  143. 'msg' => '指定的活动不存在'
  144. ];
  145. }
  146. // 获取活动关联的产品
  147. $product = Goods::findOne($activity->goods_ids);
  148. if (!$product) {
  149. // 如果找不到产品,创建一个新产品
  150. $product = $this->createProduct($store_id, $activity->name . '导入产品');
  151. }
  152. } else {
  153. return [
  154. 'code' => 1,
  155. 'msg' => '指定的活动不存在'
  156. ];
  157. // 如果没有指定活动ID,则创建默认产品和活动
  158. // $product = $this->createProduct($store_id, '积分余额导入产品');
  159. //
  160. // // 根据活动类型创建不同的活动
  161. // switch ($activity_type) {
  162. // case 'integral':
  163. // $activity = $this->createActivity($store_id, '积分导入活动', $product->id, [2], [2=>100]);
  164. // break;
  165. // case 'balance':
  166. // $activity = $this->createActivity($store_id, '余额导入活动', $product->id, [3], [3=>100]);
  167. // break;
  168. // case 'commission':
  169. // $activity = $this->createActivity($store_id, '佣金导入活动', $product->id, [1], [1=>100]);
  170. // break;
  171. // default:
  172. // $activity = $this->createActivity($store_id, '积分余额导入活动', $product->id, [1,2,3], [1=>33,2=>33,3=>34]);
  173. // }
  174. }
  175. // 统计每种类型的成功和失败数量
  176. $stats = [
  177. 'balance' => ['success' => 0, 'fail' => 0],
  178. 'integral' => ['success' => 0, 'fail' => 0],
  179. 'commission' => ['success' => 0, 'fail' => 0]
  180. ];
  181. $rows->each(function (array $item) use ($admin, $store_id, &$err, &$success, &$stats, $activity, $activity_type, $product) {
  182. try {
  183. $mobile = trim($item['手机号']);
  184. $nickname = trim($item['昵称']);
  185. $change = isset($item['余额']) ? trim($item['余额']) : 0;
  186. $integral = isset($item['积分']) ? trim($item['积分']) : 0;
  187. $commission = isset($item['佣金']) ? trim($item['佣金']) : 0;
  188. $desc = trim($item['余额变动备注']) ? trim($item['余额变动备注']) : '';
  189. if ($activity->send_type ==1 && $commission == 0) {
  190. throw new \Exception('导入失败:请用佣金模板导入' );
  191. }
  192. if ($activity->send_type ==2 && $integral == 0) {
  193. throw new \Exception('导入失败:请用积分模板导入' );
  194. }
  195. if ($activity->send_type ==3 && $change == 0) {
  196. throw new \Exception('导入失败:请用余额模板导入' );
  197. }
  198. // 查找或创建用户
  199. $user = $this->findOrCreateUser($mobile, $nickname, $store_id);
  200. // $product = $activity = [];
  201. // if (empty($product)) {
  202. // $product = $this->createProduct($store_id, '积分余额导入产品');
  203. // $activity = $this->createActivity($store_id, '积分余额导入活动', $product->id, [1,2,3], [1=>33,2=>33,3=>34]);
  204. // }
  205. // 根据活动类型处理不同的数据
  206. switch ($activity_type) {
  207. case 'integral':
  208. if ($integral != 0) {
  209. try {
  210. $this->createOrder($user, $product, $activity, abs($integral), $store_id, '积分');
  211. $stats['integral']['success']++;
  212. } catch (\Exception $e) {
  213. $stats['integral']['fail']++;
  214. $err[] = [
  215. 'code' => 1,
  216. 'msg' => '处理积分失败: ' . $e->getMessage(),
  217. 'mobile' => $mobile
  218. ];
  219. }
  220. }
  221. break;
  222. case 'balance':
  223. if ($change != 0) {
  224. try {
  225. $this->createOrder($user, $product, $activity, abs($change), $store_id, '余额');
  226. $stats['balance']['success']++;
  227. } catch (\Exception $e) {
  228. $stats['balance']['fail']++;
  229. $err[] = [
  230. 'code' => 1,
  231. 'msg' => '处理余额失败: ' . $e->getMessage(),
  232. 'mobile' => $mobile
  233. ];
  234. }
  235. }
  236. break;
  237. case 'commission':
  238. if ($commission != 0) {
  239. try {
  240. $this->createOrder($user, $product, $activity, abs($commission), $store_id, '佣金');
  241. $stats['commission']['success']++;
  242. } catch (\Exception $e) {
  243. $stats['commission']['fail']++;
  244. $err[] = [
  245. 'code' => 1,
  246. 'msg' => '处理佣金失败: ' . $e->getMessage(),
  247. 'mobile' => $mobile
  248. ];
  249. }
  250. }
  251. break;
  252. default:
  253. // 处理所有类型
  254. // 处理余额增减
  255. if ($change != 0) {
  256. try {
  257. $this->createOrder($user, $product, $activity, abs($change), $store_id, '余额');
  258. $stats['balance']['success']++;
  259. } catch (\Exception $e) {
  260. $stats['balance']['fail']++;
  261. $err[] = [
  262. 'code' => 1,
  263. 'msg' => '处理余额失败: ' . $e->getMessage(),
  264. 'mobile' => $mobile
  265. ];
  266. }
  267. }
  268. // 处理积分增减
  269. if ($integral != 0) {
  270. try {
  271. $this->createOrder($user, $product, $activity, abs($integral), $store_id, '积分');
  272. $stats['integral']['success']++;
  273. } catch (\Exception $e) {
  274. $stats['integral']['fail']++;
  275. $err[] = [
  276. 'code' => 1,
  277. 'msg' => '处理积分失败: ' . $e->getMessage(),
  278. 'mobile' => $mobile
  279. ];
  280. }
  281. }
  282. // 处理佣金增减
  283. if ($commission != 0) {
  284. try {
  285. $this->createOrder($user, $product, $activity, abs($commission), $store_id, '佣金');
  286. $stats['commission']['success']++;
  287. } catch (\Exception $e) {
  288. $stats['commission']['fail']++;
  289. $err[] = [
  290. 'code' => 1,
  291. 'msg' => '处理佣金失败: ' . $e->getMessage(),
  292. 'mobile' => $mobile
  293. ];
  294. }
  295. }
  296. }
  297. $success[] = [
  298. 'mobile' => $mobile,
  299. 'nickname' => $nickname,
  300. 'change' => $change,
  301. 'integral' => $integral,
  302. 'commission' => $commission
  303. ];
  304. } catch (\Exception $e){
  305. debug_log($e->getMessage());
  306. $err[] = [
  307. 'code' => 1,
  308. 'msg' => $e->getMessage(),
  309. 'mobile' => $mobile
  310. ];
  311. }
  312. });
  313. $count = count($err);
  314. $successCount = count($success);
  315. @unlink($uploadfile);
  316. // 生成详细的统计信息
  317. $statMsg = sprintf(
  318. "余额导入成功%d条,失败%d条;积分导入成功%d条,失败%d条;佣金导入成功%d条,失败%d条",
  319. $stats['balance']['success'], $stats['balance']['fail'],
  320. $stats['integral']['success'], $stats['integral']['fail'],
  321. $stats['commission']['success'], $stats['commission']['fail']
  322. );
  323. if ($activity->send_type == 1) {
  324. $statMsg = sprintf(
  325. "佣金导入成功%d条,失败%d条",
  326. $stats['commission']['success'], $stats['commission']['fail']
  327. );
  328. }
  329. if ($activity->send_type == 2) {
  330. $statMsg = sprintf(
  331. "积分导入成功%d条,失败%d条",
  332. $stats['integral']['success'], $stats['integral']['fail']
  333. );
  334. }
  335. if ($activity->send_type == 3) {
  336. $statMsg = sprintf(
  337. "余额导入成功%d条,失败%d条",
  338. $stats['balance']['success'], $stats['balance']['fail']
  339. );
  340. }
  341. return [
  342. 'code' => $count ? 1 : 0,
  343. 'msg' => "操作完成,导入条数据" . ($err ? ",失败{$count}条数据" : '') . "\n{$statMsg}",
  344. 'err' => $err,
  345. 'success' => $success,
  346. ];
  347. }
  348. /**
  349. * 创建活动产品
  350. * @param int $store_id 店铺ID
  351. * @param string $name 产品名称
  352. * @return Goods 产品对象
  353. */
  354. public function createProduct($store_id, $name)
  355. {
  356. $product = new Goods();
  357. $product->store_id = $store_id;
  358. $product->name = $name;
  359. $product->detail = $name . '详情';
  360. $product->price = 1; // 产品金额1元
  361. $product->status = 0;
  362. $product->created_at = time();
  363. $product->updated_at = time();
  364. if (!$product->save()) {
  365. throw new \Exception('创建活动产品失败:' . json_encode($product->errors));
  366. }
  367. return $product;
  368. }
  369. /**
  370. * 创建活动
  371. * @param int $store_id 店铺ID
  372. * @param string $name 活动名称
  373. * @param int $product_id 产品ID
  374. * @param array $send_types 返利类型
  375. * @param array $send_type_percentages 返利比例
  376. * @return ActivityOrderRebateSelf 活动对象
  377. */
  378. public function createActivity($store_id, $name, $product_id, $send_types, $send_type_percentages, $release_rate = 10)
  379. {
  380. $activity = new ActivityOrderRebateSelf();
  381. $activity->store_id = $store_id;
  382. $activity->name = $name . '-' . date('YmdHis');
  383. $activity->start_time = time();
  384. $activity->end_time = time() + 86400 * 30; // 30天后结束
  385. $activity->goods_ids = $product_id; // 产品
  386. $activity->send_days = 1; // 周期
  387. $activity->send_num = 100; // 返利比例
  388. $activity->send_types = json_encode($send_types); // 返利类型
  389. $activity->send_type_percentages = json_encode($send_type_percentages); // 返利比例
  390. $activity->status = 1; // 活动状态:启用
  391. $activity->release_rate = $release_rate;
  392. $activity->created_at = time();
  393. $activity->updated_at = time();
  394. if (!$activity->save()) {
  395. throw new \Exception('创建活动失败:' . json_encode($activity->errors));
  396. }
  397. $gids = [$product_id];
  398. if(!empty($gids)){
  399. $result = ActivityOrderRebateSelfGoods::saveList($gids, $activity->id);
  400. if ($result['code'] !== 0) {
  401. throw new \Exception($result['msg']);
  402. }
  403. }
  404. return $activity;
  405. }
  406. /**
  407. * 查找或创建用户
  408. * @param string $mobile 手机号
  409. * @param string $nickname 昵称
  410. * @param int $store_id 店铺ID
  411. * @return User 用户对象
  412. */
  413. private function findOrCreateUser($mobile, $nickname, $store_id)
  414. {
  415. $user = User::findOne(['binding' => $mobile, 'is_delete' => 0, 'store_id' => $store_id]);
  416. $saas_user = SaasUser::findOne(['mobile' => $mobile, 'is_delete' => 0]);
  417. if(!$user){
  418. if (!$saas_user) {
  419. $saas_user = new SaasUser();
  420. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  421. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  422. $saas_user->mobile = $mobile ?: '';
  423. $saas_user->name = $nickname ? $nickname : substr_replace($mobile, '******', 3, 6);
  424. $saas_user->store_id = $store_id;
  425. if (!$saas_user->save()) {
  426. throw new \Exception(array_shift($saas_user->getFirstErrors()));
  427. }
  428. }
  429. $user = new User();
  430. $user->access_token = \Yii::$app->security->generateRandomString();
  431. $user->binding = $saas_user->mobile;
  432. $user->nickname = $saas_user->name;
  433. $user->avatar_url = $saas_user->avatar;
  434. $user->username = \Yii::$app->security->generateRandomString();
  435. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  436. $user->auth_key = \Yii::$app->security->generateRandomString();
  437. $user->store_id = $store_id;
  438. if (!$user->save()) {
  439. throw new \Exception(array_shift($user->getFirstErrors()));
  440. }
  441. }
  442. if($nickname && $nickname != $saas_user->name){
  443. $saas_user->name = $nickname;
  444. if (!$saas_user->save()) {
  445. throw new \Exception(array_shift($saas_user->getFirstErrors()));
  446. }
  447. }
  448. if($nickname && $nickname != $user->nickname){
  449. $user->nickname = $nickname;
  450. if (!$user->save()) {
  451. throw new \Exception(array_shift($user->getFirstErrors()));
  452. }
  453. }
  454. return $user;
  455. }
  456. /**
  457. * 处理余额增减
  458. * @param User $user 用户对象
  459. * @param float $change 变动金额
  460. * @param string $desc 变动描述
  461. * @param object $admin 管理员对象
  462. * @param int $store_id 店铺ID
  463. */
  464. private function processBalanceChange($user, $change, $desc, $admin, $store_id)
  465. {
  466. $recharge_type = ($change > 0) ? AccountLog::LOG_TYPE_INCOME : AccountLog::LOG_TYPE_EXPEND;
  467. $data = [
  468. 'explain' => '',
  469. 'money' => abs($change),
  470. 'integral' => 0,
  471. 'recharge_type' => $recharge_type,
  472. 'type' => AccountLog::TYPE_BALANCE,
  473. 'user_id' => $user->id,
  474. 'desc' => $desc
  475. ];
  476. if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->money < $data['money']){
  477. $data['money'] = $user->money;
  478. }
  479. if($data['money'] > 0){
  480. $form = new AccountLogForm();
  481. $form->store_id = $store_id;
  482. $form->admin = $admin;
  483. $form->attributes = $data;
  484. $res = $form->recharge();
  485. if($res['code'] != 0){
  486. throw new \Exception($res['msg']);
  487. }
  488. }
  489. }
  490. /**
  491. * 处理积分增减
  492. * @param User $user 用户对象
  493. * @param float $integral 变动积分
  494. * @param string $desc 变动描述
  495. * @param object $admin 管理员对象
  496. * @param int $store_id 店铺ID
  497. */
  498. private function processIntegralChange($user, $integral, $desc, $admin, $store_id)
  499. {
  500. $recharge_type = ($integral > 0) ? AccountLog::LOG_TYPE_INCOME : AccountLog::LOG_TYPE_EXPEND;
  501. $data = [
  502. 'explain' => '',
  503. 'money' => 0,
  504. 'integral' => abs($integral),
  505. 'recharge_type' => $recharge_type,
  506. 'type' => AccountLog::TYPE_INTEGRAL,
  507. 'user_id' => $user->id,
  508. 'desc' => $desc
  509. ];
  510. if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->integral < $data['integral']){
  511. $data['integral'] = $user->integral;
  512. }
  513. if($data['integral'] > 0){
  514. $form = new AccountLogForm();
  515. $form->store_id = $store_id;
  516. $form->admin = $admin;
  517. $form->attributes = $data;
  518. $res = $form->recharge();
  519. if($res['code'] != 0){
  520. throw new \Exception($res['msg']);
  521. }
  522. }
  523. }
  524. /**
  525. * 处理佣金增减
  526. * @param User $user 用户对象
  527. * @param float $commission 变动佣金
  528. * @param string $desc 变动描述
  529. * @param int $store_id 店铺ID
  530. */
  531. private function processCommissionChange($user, $commission, $desc, $store_id)
  532. {
  533. // 使用 UserShareMoney 模型处理佣金变动
  534. // 参数说明:金额, 用户ID, 订单ID(0表示无订单), 类型(0表示佣金), 来源(10表示后台修改), 店铺ID, 订单类型(0表示商城订单), 描述
  535. $result = UserShareMoney::set(
  536. abs($commission),
  537. $user->id,
  538. 0,
  539. 0,
  540. 10,
  541. $store_id,
  542. 0,
  543. $desc ?: '后台导入佣金' . ($commission > 0 ? '增加' : '减少')
  544. );
  545. if (!$result) {
  546. throw new \Exception('佣金' . ($commission > 0 ? '增加' : '减少') . '失败');
  547. }
  548. }
  549. /**
  550. * 创建订单和返利记录
  551. * @param User $user 用户对象
  552. * @param Goods $product 产品对象
  553. * @param ActivityOrderRebateSelf $activity 活动对象
  554. * @param float $quantity 数量
  555. * @param int $store_id 店铺ID
  556. * @param string $type_name 类型名称(用于日志)
  557. * @return Order 订单对象
  558. */
  559. public function createOrder($user, $product, $activity, $quantity, $store_id, $type_name)
  560. {
  561. // 创建订单
  562. $order = new Order();
  563. $order->user_id = $user->id;
  564. $order->store_id = $store_id;
  565. $order->order_no = 'AO' . date('YmdHis') . mt_rand(1000, 9999);
  566. // $order->total_price = $product->price * $quantity;
  567. $order->total_price = 1 * $quantity;
  568. $order->first_price = 0.00;
  569. $order->second_price = 0.00;
  570. $order->third_price = 0.00;
  571. $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0]);
  572. $order->name = $user->nickname;
  573. $order->mobile = $user->binding;
  574. $order->is_pay = 1; // 已支付
  575. $order->is_sale = 1; // 标记为已售后,以便触发返利
  576. $order->created_at = time();
  577. $order->updated_at = time();
  578. if (!$order->save()) {
  579. throw new \Exception('创建订单失败:' . json_encode($order->errors));
  580. }
  581. // 创建订单详情
  582. $orderDetail = new OrderDetail();
  583. $orderDetail->order_id = $order->id;
  584. $orderDetail->goods_id = $product->id;
  585. $orderDetail->num = $quantity;
  586. $orderDetail->attr = json_encode(["规格" => "默认规格"]); // 修改为JSON格式的规格信息
  587. $orderDetail->pic = $product->cover_pic ?: 'default.jpg'; // 使用产品的封面图片,如果没有则使用默认图片
  588. // $orderDetail->total_price = $product->price * $quantity;
  589. $orderDetail->total_price = 1 * $quantity;
  590. $orderDetail->goods_info = json_encode([
  591. 'id' => $product->id,
  592. 'name' => $product->name,
  593. 'price' => $product->price,
  594. 'store_id' => $store_id
  595. ]);
  596. if (!$orderDetail->save()) {
  597. throw new \Exception('创建订单详情失败:' . json_encode($orderDetail->errors, JSON_UNESCAPED_UNICODE));
  598. }
  599. // 调用afterOrderDetailSave函数处理返利
  600. $result = self::afterOrderDetailSave([
  601. 'orderDetail' => $orderDetail,
  602. 'goods' => json_encode([
  603. 'id' => $product->id,
  604. 'name' => $product->name,
  605. 'price' => $product->price,
  606. 'store_id' => $store_id,
  607. 'user_id' => $user->id,
  608. ]),
  609. 'user' => $user,
  610. 'order' => $order,
  611. 'activity' => $activity,
  612. 'goods_id' => $product->id
  613. ]);
  614. if (isset($result['code']) && $result['code'] != 0) {
  615. throw new \Exception('处理返利失败:' . ($result['msg'] ?? '未知错误'));
  616. }
  617. }
  618. /**
  619. * 配置项
  620. */
  621. public function conf()
  622. {
  623. $conf = Option::get(OptionSetting::ACTIVITY_ORDER_REBATE_SELF, $this->store_id, 'store')['value'];
  624. if ($conf) {
  625. $conf = json_decode($conf, true);
  626. if (isset($conf['conf'])) {
  627. if (!isset($conf['conf']['type'])) {
  628. $conf['conf']['type'] = 0;
  629. }
  630. }
  631. } else {
  632. $conf = ['conf' => [
  633. 'is_open' => 0,
  634. 'type' => 0
  635. ]];
  636. }
  637. return [
  638. 'code' => 0,
  639. 'msg' => 'ok',
  640. 'data' => $conf,
  641. ];
  642. }
  643. public static function is_open($store_id = 0)
  644. {
  645. $is_open = (new self(['store_id' => $store_id ?: get_store_id()]))->conf()['data']['conf']['is_open'];
  646. return $is_open;
  647. }
  648. public function confSave($conf)
  649. {
  650. if (!is_array($conf)) {
  651. $conf = json_decode($conf, true);
  652. }
  653. if (!isset($conf['is_open'])) {
  654. $conf['is_open'] = 0;
  655. }
  656. $data = ['conf' => $conf];
  657. Option::set(OptionSetting::ACTIVITY_ORDER_REBATE_SELF, json_encode($data), $this->store_id, 'store');
  658. return [
  659. 'code' => 0,
  660. 'msg' => '保存成功'
  661. ];
  662. }
  663. /**
  664. * 配置项结束
  665. */
  666. /**
  667. * 活动逻辑
  668. */
  669. //订单活动数据写入
  670. public static function afterOrderDetailSave($od)
  671. {
  672. try {
  673. $goods = json_decode($od->goods_info, true);
  674. debug_log(['goods_store_id_01' => $goods->store_id, 'goods_store_id_02' => $goods['store_id']], 'ActivityOrderRebateSelfForm.log');
  675. if (!self::is_open($goods['store_id'])) {
  676. return [
  677. 'code' => 0,
  678. 'msg' => '功能未开启!'
  679. ];
  680. }
  681. $order = $od->order;
  682. if (!$order->user_id) {
  683. return;
  684. }
  685. $activity = ActivityOrderRebateSelf::activityAt($order->store_id, $od->goods_id);
  686. if (!$activity) {
  687. return;
  688. }
  689. $add_rat = 0;
  690. $add_rebate = 0;
  691. $aUser = ActivityOrderRebateSelfUser::findOne(['user_id' => $order->user_id]);
  692. if ($aUser && $aUser->level) {
  693. $aUserLevel = ActivityOrderRebateSelfLevel::findOne(['store_id' => $order->store_id, 'level' => $aUser->level, 'is_delete' => 0, 'status' => 1]);
  694. if ($aUserLevel) {
  695. $add_rat = $aUserLevel['add_rat']; //释放加速比例
  696. $add_rebate = $aUserLevel['add_rebate']; //返利赠送比例
  697. }
  698. }
  699. $AF = $activity->send_num;
  700. $LF = $add_rebate;
  701. $P = $od->total_price;
  702. $type = 0;
  703. $conf = Option::get(OptionSetting::ACTIVITY_ORDER_REBATE_SELF, $goods['store_id'], 'store')['value'];
  704. if ($conf) {
  705. $conf = json_decode($conf, true);
  706. $type = $conf['conf']['type'];
  707. }
  708. if (intval($type)) {
  709. $P = 0;
  710. // $goods_info = json_decode($order_detail['goods_info'], true);
  711. $goods_attr = json_decode($goods['attr'], true);
  712. $order_goods_attr = json_decode($od->attr, true);
  713. $order_goods_attr_id = array_column($order_goods_attr, 'attr_id');
  714. sort($order_goods_attr_id);
  715. $goods_price = 0;
  716. foreach ($goods_attr as $item) {
  717. $goods_attr_list = $item['attr_list'];
  718. $goods_attr_id = array_column($goods_attr_list, 'attr_id');
  719. sort($goods_attr_id);
  720. if (!array_diff($order_goods_attr_id, $goods_attr_id)) {
  721. $goods_price = $item['price'];
  722. }
  723. }
  724. if ($goods_price > 0) {
  725. $P = $goods_price * $od->num;
  726. }
  727. }
  728. $price_total = floor_num($P * $AF / 100 * (100 + $LF) / 100);
  729. if ($price_total <= 0) {
  730. return;
  731. }
  732. $ao = new ActivityOrderRebateSelfOrder();
  733. $ao->store_id = $order->store_id;
  734. $ao->user_id = $order->user_id;
  735. $ao->act_id = $activity->id;
  736. $ao->order_id = $order->id;
  737. $ao->od_id = $od->id;
  738. $ao->goods_id = $od->goods_id;
  739. $ao->send_type = $activity->send_type;
  740. $ao->send_time = $activity->send_time;
  741. $ao->send_days = $activity->send_days;
  742. $ao->release_rate = $activity->release_rate;
  743. $ao->send_num = $activity->send_num;
  744. $ao->price_total = $price_total;
  745. $ao->price_send = 0;
  746. $ao->price_wait = $price_total;
  747. $ao->add_rat = $add_rat;
  748. $ao->add_rebate = $add_rebate;
  749. $ao->fixed_rebate_profit = $activity->fixed_rebate_profit;
  750. $ao->send_rebate_type = $activity->send_rebate_type;
  751. if (!$ao->save()) {
  752. throw new \Exception(array_shift($ao->getFirstErrors()));
  753. }
  754. if ($order->is_sale) {
  755. self::afterOrderSales($order);
  756. }
  757. return [
  758. 'code' => 0,
  759. 'msg' => '操作成功!'
  760. ];
  761. } catch (\Exception $e) {
  762. \Yii::error($e);
  763. return [
  764. 'code' => 1,
  765. 'msg' => $e->getMessage()
  766. ];
  767. }
  768. }
  769. //处理订单售后(升级、返利)
  770. public static function afterOrderSales($order)
  771. {
  772. try {
  773. if (!self::is_open($order->store_id)) {
  774. return [
  775. 'code' => 0,
  776. 'msg' => '功能未开启!'
  777. ];
  778. }
  779. $levelUp = self::levelUp($order->user_id);
  780. if ($levelUp['code'] != 0) {
  781. debug_log([__METHOD__, $order->user_id, $levelUp['msg']], __CLASS__ . '.log');
  782. return;
  783. }
  784. $time1 = strtotime('today midnight'); // 今天凌晨时间戳
  785. $aos = ActivityOrderRebateSelfOrder::findAll(['order_id' => $order->id, 'is_sale' => 0, 'is_delete' => 0]);
  786. foreach ($aos as $ao) {
  787. $ao->is_sale = 1;
  788. if (!$ao->save()) {
  789. throw new \Exception(array_shift($ao->getFirstErrors()));
  790. }
  791. //
  792. $od = \app\models\OrderDetail::findOne($ao->od_id);
  793. $activity = ActivityOrderRebateSelf::findOne($ao->act_id);
  794. // 获取返利类型和比例
  795. $send_types = $activity->send_types ? json_decode($activity->send_types, true) : [$activity->send_type];
  796. $send_type_percentages = $activity->send_type_percentages ? json_decode($activity->send_type_percentages, true) : [$activity->send_type => 100];
  797. $AC = $ao->send_days; //返利周期
  798. $send_rebate_type = intval($ao->send_rebate_type); //全返类型:0=时间,1=固定比例
  799. $fixed_rebate_profit = $ao->fixed_rebate_profit; //固定比例
  800. $LA = $ao->add_rat; //释放加速比例
  801. $price_total = $ao->price_total; //总金额
  802. // $release_rate = isset($ao->release_rate) ? floatval($ao->release_rate) : 0; // 获取释放速率
  803. $price_sends = [];
  804. //if ($release_rate > 0) {
  805. // // 按释放速率计算
  806. // $remaining = $price_total;
  807. // $day = 1;
  808. // $AC = 90; //最多90天
  809. //
  810. // while ($remaining > 0.01 && $day <= $AC) {
  811. // // 按释放速率计算当天释放金额
  812. // $price_day = floor_num($remaining * $release_rate / 100);
  813. //
  814. // // 确保最小释放金额为0.01
  815. // if ($price_day < 0.01) {
  816. // $price_day = 0.01;
  817. // }
  818. //
  819. // // 最后一天释放所有剩余金额
  820. // if ($day == $AC) {
  821. // $price_day = $remaining;
  822. // }
  823. //
  824. // $price_sends[] = $price_day;
  825. // $remaining -= $price_day;
  826. // $day++;
  827. // }
  828. //} else {
  829. if ($send_rebate_type) {
  830. //固定比例类型
  831. if ($fixed_rebate_profit > 0) {
  832. $p1 = $price_total;
  833. $day = 1;
  834. $AC = 90; //最多90天
  835. while ($p1 >= 0.01 && $day <= $AC) {
  836. // $p1 = $price_total - array_sum($price_sends); //计算剩余还需要分多钱
  837. //等同于 $price_once = ($p1 * $fixed_rebate_profit) / 100 ;
  838. $price_once = bcdiv(bcmul($p1, $fixed_rebate_profit, 2), 100, 2); //计算按照原进度的分红比例每次需要分多钱
  839. //等同于$price_once += ($price_once * $LA) / 100;
  840. $price_once = bcadd($price_once, bcdiv(bcmul($price_once, $LA, 2), 100, 2), 2); //将每次的进度的钱再加速获取
  841. // $price_sends[] = $price_once >= 0.01 ? $price_once : $p1; //获取结果
  842. if ($p1 < 0.01) {
  843. $p1 = 0.01;
  844. }
  845. if ($day == $AC) {
  846. $price_once = $p1;
  847. }
  848. $price_sends[] = $p1; //获取结果
  849. $p1 -= $price_once; //计算剩余还需要分多钱
  850. $day++;
  851. };
  852. }
  853. } else {
  854. //固定时间类型
  855. $price_day_rate = (100 / $AC + $LA) / 100;
  856. $price_day = floor_num($price_total * $price_day_rate);
  857. if ($price_day < 0.01) {
  858. $price_day = 0;
  859. }
  860. $price_sends = [];
  861. for ($i = 1; $i <= $AC; $i++) {
  862. $p1 = $price_total - array_sum($price_sends);
  863. if ($p1 < 0.01) {
  864. break;
  865. }
  866. $price_sends[] = $i == $AC ? $p1 : $price_day;
  867. }
  868. }
  869. // }
  870. foreach ($price_sends as $i => $price_send) {
  871. if ($price_send <= 0.00) {
  872. continue;
  873. }
  874. // 按照返利类型比例分配返利金额
  875. foreach($send_types as $type) {
  876. $percentage = isset($send_type_percentages[$type]) ? $send_type_percentages[$type] : 0;
  877. if ($percentage <= 0) continue;
  878. // 计算当前类型的返利金额
  879. $type_price_send = floor_num($price_send * $percentage / 100);
  880. if ($type_price_send <= 0) continue;
  881. $al = new ActivityOrderRebateSelfLog();
  882. $al->self_id = $ao->id;
  883. $al->store_id = $order->store_id;
  884. $al->user_id = $order->user_id;
  885. $al->act_id = $ao->act_id;
  886. $al->order_id = $order->id;
  887. $al->od_id = $od->id;
  888. $al->goods_id = $ao->goods_id;
  889. //$al->send_type = $ao->send_type;
  890. //$al->price_send = $price_send;
  891. $al->send_type = $type; // 使用当前循环的返利类型
  892. $al->price_send = $type_price_send; // 按比例分配的返利金额
  893. $al->day = $i + 1;
  894. $al->is_sale = 1;
  895. if ($ao->send_time == 1) {
  896. $al->pre_send_time = $time1 + $al['day'] * 86400 * 365;
  897. } else if ($ao->send_time == 2) {
  898. $al->pre_send_time = $time1 + $al['day'] * 86400 * 30;
  899. } else if ($ao->send_time == 4) {
  900. $al->pre_send_time = time() + $al['day'] * 60;
  901. } else {
  902. $al->pre_send_time = $time1 + $al['day'] * 86400;
  903. }
  904. if (!$al->save()) {
  905. throw new \Exception(array_shift($al->getFirstErrors()));
  906. }
  907. }
  908. }
  909. }
  910. return [
  911. 'code' => 0,
  912. 'msg' => '操作成功!'
  913. ];
  914. } catch (\Exception $e) {
  915. \Yii::error($e);
  916. return [
  917. 'code' => 1,
  918. 'msg' => $e->getMessage()
  919. ];
  920. }
  921. }
  922. //返利
  923. public static function rebate($time = 0, $queue = 0)
  924. {
  925. try {
  926. if (!$time) {
  927. $time = time();
  928. }
  929. if ($queue) {
  930. $queue = queue_push(new \app\jobs\orderEvent\ActivityOrderRebateSelfRebateJob([
  931. 'time' => $time,
  932. ]), 0, 1);
  933. return [
  934. 'code' => 0,
  935. 'msg' => '操作成功!' . $queue
  936. ];
  937. }
  938. $count = 100;
  939. $als = ActivityOrderRebateSelfLog::find()->where(['is_sale' => 1, 'is_send' => 0])->andWhere(['<=', 'pre_send_time', $time])->limit($count)->all();
  940. foreach ($als as $al) {
  941. $al->is_send = 1;
  942. $al->send_time = time();
  943. if (!$al->save()) {
  944. throw new \Exception(array_shift($al->getFirstErrors()));
  945. }
  946. $ao = ActivityOrderRebateSelfOrder::findOne(['is_sale' => 1, 'order_id' => $al->order_id, 'goods_id' => $al->goods_id, 'is_delete' => 0, 'id' => $al->self_id]);
  947. $ao->price_send += $al->price_send;
  948. $ao->price_wait -= $al->price_send;
  949. if (!$ao->save()) {
  950. throw new \Exception(array_shift($ao->getFirstErrors()));
  951. }
  952. $order = Order::findOne($al->order_id);
  953. $user = User::findOne(['id' => $al->user_id]);
  954. if ($al->send_type == 2) {
  955. $save = AccountLog::saveLog(
  956. $al->user_id,
  957. $al->price_send,
  958. AccountLog::TYPE_INTEGRAL,
  959. AccountLog::LOG_TYPE_INCOME,
  960. AccountLog::TYPE_PLATFORM_ORDER,
  961. $order->id,
  962. "活动返利赠送积分, 订单号:{$order->order_no}"
  963. );
  964. } else if ($al->send_type == 3) {
  965. $save = AccountLog::saveLog(
  966. $al->user_id,
  967. $al->price_send,
  968. AccountLog::TYPE_BALANCE,
  969. AccountLog::LOG_TYPE_INCOME,
  970. AccountLog::TYPE_PLATFORM_ORDER,
  971. $order->id,
  972. "活动返利赠送余额, 订单号:{$order->order_no}"
  973. );
  974. } else if ($al->send_type == 4) {
  975. $pool = IntegralAppreciationPool::findOne(['store_id' => $al->store_id]);
  976. if ($pool->integral_price > 0) {
  977. $save = AccountLog::saveLog(
  978. $al->user_id,
  979. floor_num($al->price_send/$pool->integral_price),
  980. AccountLog::TYPE_INTEGRAL,
  981. AccountLog::LOG_TYPE_INCOME,
  982. AccountLog::TYPE_PLATFORM_ORDER,
  983. $order->id,
  984. "活动返利赠送增值积分, 订单号:{$order->order_no}"
  985. );
  986. }
  987. } else {
  988. $user->total_price += doubleval($al->price_send);
  989. $user->price += doubleval($al->price_send);
  990. $user->save();
  991. $order->share_price += doubleval($al->price_send);
  992. UserShareMoney::set($al->price_send, $al->user_id, $order->id, 0, 4, $order->store_id, 0);
  993. }
  994. }
  995. if ($count == count($als)) {
  996. $queue = queue_push(new \app\jobs\orderEvent\ActivityOrderRebateSelfRebateJob([
  997. 'time' => $time,
  998. ]), 0, 1);
  999. }
  1000. return [
  1001. 'code' => 0,
  1002. 'msg' => '操作成功!'
  1003. ];
  1004. } catch (\Exception $e) {
  1005. \Yii::error($e);
  1006. return [
  1007. 'code' => 1,
  1008. 'msg' => $e->getMessage()
  1009. ];
  1010. }
  1011. }
  1012. //升级(自己和上级)
  1013. public static function levelUp($user_id)
  1014. {
  1015. try {
  1016. $user = User::findOne($user_id);
  1017. $store_id = $user->store_id;
  1018. $aUser = ActivityOrderRebateSelfUser::findOne(['user_id' => $user_id]);
  1019. $aUserLevel = 0;
  1020. if ($aUser) {
  1021. $aUserLevel = $aUser->level;
  1022. } else {
  1023. $aUser = new ActivityOrderRebateSelfUser();
  1024. $aUser->store_id = $store_id;
  1025. $aUser->user_id = $user_id;
  1026. $aUser->level = 0;
  1027. }
  1028. $aLevels = ActivityOrderRebateSelfLevel::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1])->orderBy('level DESC')->all();
  1029. if (!in_array($aUserLevel, array_column($aLevels, 'level'))) {
  1030. $aUserLevel = 0;
  1031. }
  1032. $order_money = Order::find()->where([
  1033. 'store_id' => $store_id,
  1034. 'is_delete' => 0,
  1035. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  1036. 'user_id' => $user_id
  1037. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  1038. $childsLevel = ActivityOrderRebateSelfUser::find()->alias('au')->leftJoin(['u' => User::tableName()], 'au.user_id = u.id')->where([
  1039. 'u.old_parent_id' => $user_id,
  1040. ])->groupBy('au.level')->select('au.level, count(1) acount')->asArray()->all();
  1041. foreach ($aLevels as $aLevel) {
  1042. $moneyOk = $order_money > $aLevel['money'];
  1043. $childOk = 0;
  1044. if (!$aLevel['child_level'] || !$aLevel['child_num']) {
  1045. $childOk = 1;
  1046. } else {
  1047. foreach ($childsLevel as $item) {
  1048. $child_levelOk = $item['level'] >= $aLevel['child_level'];
  1049. $child_numOk = $item['acount'] >= $aLevel['child_num'];
  1050. if ($child_levelOk && $child_numOk) {
  1051. $childOk = 1;
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. if ($moneyOk && $childOk) {
  1057. $aUserLevel = $aLevel['level'];
  1058. break;
  1059. }
  1060. }
  1061. if ($aUser->level != $aUserLevel) {
  1062. $aUser->level = $aUserLevel;
  1063. if (!$aUser->save()) {
  1064. throw new \Exception(array_shift($aUser->getFirstErrors()));
  1065. }
  1066. self::levelUp($user->old_parent_id);
  1067. }
  1068. return [
  1069. 'code' => 0,
  1070. 'msg' => '操作成功!'
  1071. ];
  1072. } catch (\Exception $e) {
  1073. \Yii::error($e);
  1074. return [
  1075. 'code' => 1,
  1076. 'msg' => $e->getMessage()
  1077. ];
  1078. }
  1079. }
  1080. /**
  1081. * 活动逻辑结束
  1082. */
  1083. /**
  1084. * 活动信息
  1085. */
  1086. public function search()
  1087. {
  1088. try {
  1089. $query = ActivityOrderRebateSelf::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  1090. if ((int)$this->status === 1) { //未开始
  1091. $query->andWhere(['>', 'start_time', time()]);
  1092. }
  1093. if ((int)$this->status === 2) { //进行中
  1094. $query->andWhere(['AND', ['<', 'start_time', time()], ['>', 'end_time', time()]]);
  1095. }
  1096. if ((int)$this->status === 3) { //已结束
  1097. $query->andWhere(['<', 'end_time', time()]);
  1098. }
  1099. if (isset($this->send_type) && $this->send_type > 0) {
  1100. $query->andWhere(['send_type' => $this->send_type]);
  1101. }
  1102. if (!empty($this->name)) { //名称
  1103. $query->andWhere(['LIKE', 'name', $this->name]);
  1104. }
  1105. if (!empty($this->start_time)) {
  1106. $query->andWhere(['>', 'end_time', strtotime($this->start_time)]);
  1107. }
  1108. if (!empty($this->end_time)) {
  1109. $query->andWhere(['<', 'start_time', strtotime($this->end_time)]);
  1110. }
  1111. $query->orderBy('id DESC');
  1112. $pagination = pagination_make($query);
  1113. foreach ($pagination['list'] as &$item) {
  1114. $item['publish'] = $item['status'];
  1115. //获取活动状态
  1116. if ($item['start_time'] > time()) {
  1117. $item['status'] = 1;
  1118. }
  1119. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  1120. $item['status'] = 2;
  1121. }
  1122. if ($item['end_time'] < time()) {
  1123. $item['status'] = 3;
  1124. }
  1125. //格式化时间
  1126. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  1127. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  1128. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  1129. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  1130. $userCount = ActivityOrderRebateSelfOrder::find()->where(['act_id' => $item['id']])->groupBy(['user_id'])->count();
  1131. $item['userCount'] = $userCount;
  1132. $orderCount = ActivityOrderRebateSelfOrder::find()->where(['act_id' => $item['id']])->count();
  1133. $item['orderCount'] = $orderCount;
  1134. $orderSum = ActivityOrderRebateSelfOrder::find()->where(['act_id' => $item['id']])->sum('price_total');
  1135. $item['orderSum'] = $orderSum;
  1136. }
  1137. return [
  1138. 'code' => 0,
  1139. 'msg' => 'success',
  1140. 'data' => [
  1141. 'data' => $pagination['list'],
  1142. 'pageNo' => $pagination['pageNo'],
  1143. 'totalCount' => $pagination['totalCount'],
  1144. 'q' => $query->createCommand()->getRawSql(),
  1145. ]
  1146. ];
  1147. } catch (\Exception $e) {
  1148. return [
  1149. 'code' => 1,
  1150. 'msg' => $e->getMessage()
  1151. ];
  1152. }
  1153. }
  1154. public function listSelect()
  1155. {
  1156. $query = ActivityOrderRebateSelf::find()->where(['is_delete' => 0]);
  1157. $this->store_id && $query->andWhere(['store_id' => $this->store_id]);
  1158. // 根据活动类型筛选活动
  1159. // $activity_type = get_params('activity_type');
  1160. if ($this->send_type) {
  1161. switch ($this->send_type) {
  1162. case 'integral':
  1163. // 筛选积分类型的活动(send_type=2 或 send_types 包含 2)
  1164. $query->andWhere(['or',
  1165. ['send_type' => 2],
  1166. ['like', 'send_types', '"2222"']
  1167. ]);
  1168. break;
  1169. case 'balance':
  1170. // 筛选余额类型的活动(send_type=3 或 send_types 包含 3)
  1171. $query->andWhere(['or',
  1172. ['send_type' => 3],
  1173. ['like', 'send_types', '"3333"']
  1174. ]);
  1175. break;
  1176. case 'commission':
  1177. // 筛选佣金类型的活动(send_type=1 或 send_types 包含 1)
  1178. $query->andWhere(['or',
  1179. ['send_type' => 1],
  1180. ['like', 'send_types', '"1111"']
  1181. ]);
  1182. break;
  1183. }
  1184. }
  1185. if($this->name){
  1186. $query->andWhere(['like', 'name', $this->name]);
  1187. }
  1188. if (!empty($this->start_time)) {
  1189. $query->andWhere(['>', 'end_time', strtotime($this->start_time)]);
  1190. }
  1191. if (!empty($this->end_time)) {
  1192. $query->andWhere(['<', 'start_time', strtotime($this->end_time)]);
  1193. }
  1194. $query->select('id, name');
  1195. $query->orderBy('id desc');
  1196. $res = $query->asArray()->all();
  1197. return [
  1198. 'code' => 0,
  1199. 'msg' => 'success',
  1200. 'data' => $res,
  1201. ];
  1202. }
  1203. public static function sortGoods($goods_ids, $goods)
  1204. {
  1205. $res = [];
  1206. foreach ($goods_ids as $id) {
  1207. foreach ($goods as $gitem) {
  1208. if ($gitem['id'] == $id) {
  1209. $res[] = $gitem;
  1210. }
  1211. }
  1212. }
  1213. return $res;
  1214. }
  1215. public function getInfo()
  1216. {
  1217. try {
  1218. $activity = ActivityOrderRebateSelf::find()->where(['id' => $this->id])->asArray()->one();
  1219. if ($activity) {
  1220. $gids = ActivityOrderRebateSelfGoods::find()->select('goods_id')->where(['act_id' => $activity['id']]);
  1221. $activity_goods = Goods::find()->where(['id' => $gids])->andWhere(['is_delete' => 0])->asArray()->all();
  1222. $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']);
  1223. $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']);
  1224. $activity_goods = self::sortGoods(explode(',', $activity['goods_ids']), $activity_goods);
  1225. $activity['send_rebate_type'] = intval($activity['send_rebate_type']);
  1226. //$activity['send_type'] = intval($activity['send_type']);
  1227. $activity['send_types'] = json_decode($activity['send_types'], true);
  1228. $activity['send_type_percentages'] = json_decode($activity['send_type_percentages'], true);
  1229. }
  1230. return [
  1231. 'code' => 0,
  1232. 'msg' => '获取成功',
  1233. 'data' => [
  1234. 'activity_goods' => $activity_goods ?? [],
  1235. 'activity' => $activity ?: [],
  1236. ]
  1237. ];
  1238. } catch (\Exception $e) {
  1239. return [
  1240. 'code' => 1,
  1241. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  1242. ];
  1243. }
  1244. }
  1245. public function save()
  1246. {
  1247. $t = \Yii::$app->db->beginTransaction();
  1248. try {
  1249. if (!$this->name || !$this->start_time || !$this->end_time) {
  1250. throw new \Exception("请将参数填充完整");
  1251. }
  1252. $activity = ActivityOrderRebateSelf::findOne($this->id);
  1253. if (empty($activity)) {
  1254. $activity = new ActivityOrderRebateSelf();
  1255. $activity->store_id = $this->store_id;
  1256. if (isset($this->send_rebate_type) && !in_array($this->send_rebate_type, ActivityOrderRebateSelf::SEND_REBATE_TYPE_ARR)) {
  1257. throw new \Exception("请选择返利方式");
  1258. }
  1259. $activity->send_rebate_type = $this->send_rebate_type ?: 0;
  1260. }
  1261. $activity->name = $this->name;
  1262. $activity->start_time = strtotime($this->start_time);
  1263. $activity->end_time = strtotime($this->end_time);
  1264. $activity->goods_ids = $this->goods_ids ?: '';
  1265. $activity->send_type = $this->send_type ?: 0;
  1266. $activity->send_time = $this->send_time;
  1267. $activity->send_num = $this->send_num ?: 0;
  1268. $activity->send_days = $this->send_days ?: 1;
  1269. $activity->fixed_rebate_profit = $this->fixed_rebate_profit ?: 0;
  1270. $activity->send_types = $this->send_types ? $this->send_types : null;
  1271. $activity->send_type_percentages = $this->send_type_percentages ? $this->send_type_percentages : null;
  1272. $activity->release_rate = $this->release_rate;
  1273. if (!$activity->save()) {
  1274. throw new \Exception(implode(';', array_values($activity->firstErrors)));
  1275. }
  1276. $gids = explode(',', $activity->goods_ids);
  1277. if (!empty($gids)) {
  1278. $result = ActivityOrderRebateSelfGoods::saveList($gids, $activity->id);
  1279. if ($result['code'] !== 0) {
  1280. throw new \Exception($result['msg']);
  1281. }
  1282. }
  1283. $t->commit();
  1284. return [
  1285. 'code' => 0,
  1286. 'msg' => '操作成功!'
  1287. ];
  1288. } catch (\Exception $e) {
  1289. $t->rollBack();
  1290. return [
  1291. 'code' => 1,
  1292. 'msg' => $e->getMessage()
  1293. ];
  1294. }
  1295. }
  1296. public function status()
  1297. {
  1298. try {
  1299. if ($this->ids) {
  1300. $ids = explode(',', $this->ids);
  1301. foreach ($ids as $id) {
  1302. $model = ActivityOrderRebateSelf::findOne(['id' => $id, 'store_id' => $this->store_id]);
  1303. $model->status = (int)$this->status;
  1304. if (!$model->save()) {
  1305. throw new \Exception(array_shift($model->getFirstErrors()));
  1306. }
  1307. }
  1308. }
  1309. return [
  1310. 'code' => 0,
  1311. 'msg' => '操作成功!'
  1312. ];
  1313. } catch (\Exception $e) {
  1314. return [
  1315. 'code' => 1,
  1316. 'msg' => $e->getMessage()
  1317. ];
  1318. }
  1319. }
  1320. public function del()
  1321. {
  1322. try {
  1323. if ($this->ids) {
  1324. $ids = explode(',', $this->ids);
  1325. ActivityOrderRebateSelf::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  1326. }
  1327. return [
  1328. 'code' => 0,
  1329. 'msg' => '操作成功!'
  1330. ];
  1331. } catch (\Exception $e) {
  1332. return [
  1333. 'code' => 1,
  1334. 'msg' => $e->getMessage()
  1335. ];
  1336. }
  1337. }
  1338. /**
  1339. * 活动信息结束
  1340. */
  1341. /**
  1342. * 等级信息
  1343. */
  1344. public function levelSearch()
  1345. {
  1346. if (!$this->validate()) {
  1347. return [
  1348. 'code' => 1,
  1349. 'msg' => '参数错误'
  1350. ];
  1351. }
  1352. $query = ActivityOrderRebateSelfLevel::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
  1353. if (isset($this->status) && $this->status >= 0) {
  1354. $query->andWhere(['status' => $this->status]);
  1355. }
  1356. if ($this->name) {
  1357. $query->andWhere(['like', 'name', $this->name]);
  1358. }
  1359. if (!empty($this->start_time)) {
  1360. $query->andWhere(['>', 'created_at', strtotime($this->start_time)]);
  1361. }
  1362. if (!empty($this->end_time)) {
  1363. $query->andWhere(['<', 'created_at', strtotime($this->end_time)]);
  1364. }
  1365. $query->orderBy(['level' => SORT_ASC]);
  1366. $pagination = pagination_make($query);
  1367. $aul = ActivityOrderRebateSelfUser::find()->where(['store_id' => $this->store_id])->select('level, count(1) count')->groupBy('level')->asArray()->all();
  1368. foreach ($pagination['list'] as &$value) {
  1369. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  1370. $value['user_count'] = 0;
  1371. foreach ($aul as $l) {
  1372. if ($l['level'] == $value['level']) {
  1373. $value['user_count'] = $l['count'];
  1374. }
  1375. }
  1376. }
  1377. return [
  1378. 'code' => 0,
  1379. 'msg' => 'success',
  1380. 'data' => $pagination,
  1381. 'q' => $query->createCommand()->getRawSql(),
  1382. ];
  1383. }
  1384. public function levelListSelect()
  1385. {
  1386. $list = ActivityOrderRebateSelfLevel::find()->where(['store_id' => $this->store_id, 'status' => 1, 'is_delete' => 0])
  1387. ->select('id, level, name')
  1388. ->orderBy('level')
  1389. ->asArray()->all();
  1390. return [
  1391. 'code' => 0,
  1392. 'msg' => 'success',
  1393. 'data' => $list,
  1394. ];
  1395. }
  1396. public function levelSave()
  1397. {
  1398. $level = ActivityOrderRebateSelfLevel::findOne($this->id);
  1399. if (empty($level)) {
  1400. $level = new ActivityOrderRebateSelfLevel();
  1401. $level->store_id = $this->store_id;
  1402. }
  1403. $level->store_id = $this->store_id;
  1404. $level->level = $this->level;
  1405. $level->name = $this->name;
  1406. $level->money = $this->money;
  1407. $level->child_num = (int)$this->child_num;
  1408. $level->child_level = (int)$this->child_level;
  1409. $level->add_rat = $this->add_rat;
  1410. $level->add_rebate = $this->add_rebate;
  1411. if (!$level->save()) {
  1412. return [
  1413. 'code' => 1,
  1414. 'msg' => '操作失败,' . array_shift($level->getFirstErrors())
  1415. ];
  1416. }
  1417. return [
  1418. 'code' => 0,
  1419. 'msg' => '成功'
  1420. ];
  1421. }
  1422. public function levelStatus()
  1423. {
  1424. try {
  1425. if ($this->ids) {
  1426. $ids = explode(',', $this->ids);
  1427. foreach ($ids as $id) {
  1428. $model = ActivityOrderRebateSelfLevel::findOne(['id' => $id, 'store_id' => $this->store_id]);
  1429. $model->status = (int)$this->status;
  1430. if (!$model->save()) {
  1431. throw new \Exception(array_shift($model->getFirstErrors()));
  1432. }
  1433. }
  1434. }
  1435. return [
  1436. 'code' => 0,
  1437. 'msg' => '操作成功!'
  1438. ];
  1439. } catch (\Exception $e) {
  1440. return [
  1441. 'code' => 1,
  1442. 'msg' => $e->getMessage()
  1443. ];
  1444. }
  1445. }
  1446. public function levelDel()
  1447. {
  1448. try {
  1449. if ($this->ids) {
  1450. $ids = explode(',', $this->ids);
  1451. foreach ($ids as $id) {
  1452. $model = ActivityOrderRebateSelfLevel::findOne(['id' => $id, 'store_id' => $this->store_id]);
  1453. $model->is_delete = 1;
  1454. if (!$model->save()) {
  1455. throw new \Exception(array_shift($model->getFirstErrors()));
  1456. }
  1457. }
  1458. }
  1459. return [
  1460. 'code' => 0,
  1461. 'msg' => '操作成功!'
  1462. ];
  1463. } catch (\Exception $e) {
  1464. return [
  1465. 'code' => 1,
  1466. 'msg' => $e->getMessage()
  1467. ];
  1468. }
  1469. }
  1470. /**
  1471. * 等级信息结束
  1472. */
  1473. /**
  1474. * 用户信息
  1475. */
  1476. public function userSearch()
  1477. {
  1478. if (!$this->validate()) {
  1479. return [
  1480. 'code' => 1,
  1481. 'msg' => '参数错误'
  1482. ];
  1483. }
  1484. $store_id = $this->store_id;
  1485. $query = ActivityOrderRebateSelfUser::find()->alias('au')
  1486. ->leftJoin(['al' => ActivityOrderRebateSelfLevel::tableName()], 'au.level = al.level')
  1487. ->leftJoin(['u' => User::tableName()], 'u.id = au.user_id')
  1488. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile AND su.mobile != ""')
  1489. ->where(['au.store_id' => $store_id]);
  1490. if (isset($this->level) && $this->level > 0) {
  1491. $query->andWhere(['au.level' => $this->level]);
  1492. }
  1493. if ($this->name) {
  1494. $query->andWhere(['like', 'su.name', $this->name]);
  1495. }
  1496. if ($this->phone) {
  1497. $query->andWhere(['like', 'su.mobile', $this->phone]);
  1498. }
  1499. $query->orderBy('au.id desc');
  1500. $query->select('au.*, al.name level_name, su.mobile, su.name, su.avatar');
  1501. $pagination = pagination_make($query);
  1502. foreach ($pagination['list'] as &$value) {
  1503. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  1504. $order_money = Order::find()->where([
  1505. 'store_id' => $store_id,
  1506. 'is_delete' => 0,
  1507. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  1508. 'user_id' => $value['user_id'],
  1509. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  1510. $value['user_order_money'] = $order_money;
  1511. $childsLevel = User::find()->alias('u')->leftJoin(['au' => ActivityOrderRebateSelfUser::tableName()], 'au.user_id = u.id')->where([
  1512. 'u.old_parent_id' => $value['user_id'],
  1513. ])->groupBy('au.level')->select(['IF(au.level>0,au.level,0) level', 'count(1) count'])->asArray()->all();
  1514. $value['childsLevel'] = $childsLevel;
  1515. }
  1516. return [
  1517. 'code' => 0,
  1518. 'msg' => 'success',
  1519. 'data' => $pagination,
  1520. 'q' => $query->createCommand()->getRawSql(),
  1521. ];
  1522. }
  1523. /**
  1524. * 用户信息结束
  1525. */
  1526. /**
  1527. * 订单信息
  1528. */
  1529. public function orderList($getTotal = 1)
  1530. {
  1531. $query = ActivityOrderRebateSelfOrder::find()->alias('ao')
  1532. ->leftJoin(['a' => ActivityOrderRebateSelf::tableName()], 'ao.act_id = a.id')
  1533. ->leftJoin(['o' => Order::tableName()], 'ao.order_id = o.id')
  1534. ->leftJoin(['u' => User::tableName()], 'ao.user_id = u.id')
  1535. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding');
  1536. // 新增的删除状态
  1537. $query->andWhere(['ao.is_delete' => ActivityOrderRebateSelfOrder::NOT_DELETE]);
  1538. $query->andWhere(['!=', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  1539. $query->andWhere(['ao.store_id' => $this->store_id]);
  1540. if ($this->user_id) {
  1541. $query->andWhere(['ao.user_id' => $this->user_id]);
  1542. $level = ActivityOrderRebateSelfUser::find()->alias('u')
  1543. ->leftJoin(['l' => ActivityOrderRebateSelfLevel::tableName()], 'u.level = l.level AND u.store_id = l.store_id AND l.is_delete = 0 AND l.status = 1')
  1544. ->where(['u.user_id' => $this->user_id])
  1545. ->select('l.*')->limit(1)->asArray()->one();
  1546. }
  1547. if ($this->act_id) {
  1548. $query->andWhere(['ao.act_id' => $this->act_id]);
  1549. }
  1550. if ($this->send_type > 0) {
  1551. $query->andWhere(['ao.send_type' => $this->send_type]);
  1552. }
  1553. if (!empty($this->name)) {
  1554. $query->andWhere(['LIKE', 'su.name', $this->name]);
  1555. }
  1556. if (!empty($this->order_no)) {
  1557. $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
  1558. }
  1559. if (!empty($this->start_time)) {
  1560. $query->andWhere(['>', 'o.created_at', strtotime($this->start_time)]);
  1561. }
  1562. if (!empty($this->end_time)) {
  1563. $query->andWhere(['<', 'o.created_at', strtotime($this->end_time)]);
  1564. }
  1565. $total = null;
  1566. if ($getTotal) {
  1567. $select = ['IFNULL(SUM(ao.price_total),0) price_total', 'IFNULL(SUM(ao.price_wait),0) price_wait', 'IFNULL(SUM(ao.price_send),0) price_send', 'count(DISTINCT order_id) num'];
  1568. $queryPrice = clone $query;
  1569. $total['price'] = $queryPrice->andWhere(['ao.send_type' => 1])->select($select)->asArray()->one();
  1570. $queryJifen = clone $query;
  1571. $total['jifen'] = $queryJifen->andWhere(['ao.send_type' => 2])->select($select)->asArray()->one();
  1572. $queryBalance = clone $query;
  1573. $total['balance'] = $queryBalance->andWhere(['ao.send_type' => 3])->select($select)->asArray()->one();
  1574. }
  1575. $query->select('ao.*, su.avatar, su.name, su.mobile, o.trade_status, o.order_type, o.is_pay, o.total_price, o.pay_price, o.order_no, o.created_at order_created_at')->orderBy('ao.id desc');
  1576. $res = pagination_make($query);
  1577. foreach ($res['list'] as &$value) {
  1578. $value['order_created_at'] = $value['order_created_at'] ? date('Y-m-d H:i:s', $value['order_created_at']) : '-';
  1579. }
  1580. return [
  1581. 'code' => 0,
  1582. 'msg' => 'success',
  1583. 'data' => $res,
  1584. 'total' => $total,
  1585. 'level' => $level,
  1586. 'q' => $query->createCommand()->getRawSql(),
  1587. ];
  1588. }
  1589. /**
  1590. * 删除返利订单
  1591. */
  1592. public function orderDelete() {
  1593. if(count($this->ids) <= 0){
  1594. return ['code' => 1, 'msg' => '请选择要删除的数据'];
  1595. }
  1596. $count = ActivityOrderRebateSelfOrder::updateAll(['is_delete' => ActivityOrderRebateSelfOrder::IS_DELETE], ['id' => $this->ids]);
  1597. $ol = ActivityOrderRebateSelfOrder::findAll(['id' => $this->ids]);
  1598. foreach($ol as $item){
  1599. if(!$item->is_delete){
  1600. continue;
  1601. }
  1602. ActivityOrderRebateSelfLog::deleteAll(['order_id' => $item['order_id'], 'act_id' => $item['act_id']]);
  1603. }
  1604. if($count == count($this->ids)){
  1605. return ['code' => 0, 'msg' => '删除成功'];
  1606. }else{
  1607. return ['code' => 0, 'msg' => '部分删除成功'];
  1608. }
  1609. }
  1610. /**
  1611. * 订单信息结束
  1612. */
  1613. /**
  1614. * 返利信息
  1615. */
  1616. public function logList($getTotal = 1)
  1617. {
  1618. $query = ActivityOrderRebateSelfLog::find()->alias('al')
  1619. ->leftJoin(['a' => ActivityOrderRebateSelf::tableName()], 'al.act_id = a.id')
  1620. ->leftJoin(['o' => Order::tableName()], 'al.order_id = o.id')
  1621. ->leftJoin(['u' => User::tableName()], 'al.user_id = u.id')
  1622. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding');
  1623. $query->andWhere(['!=', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  1624. $query->andWhere(['al.store_id' => $this->store_id]);
  1625. if ($this->user_id) {
  1626. $query->andWhere(['al.user_id' => $this->user_id]);
  1627. }
  1628. if ($this->act_id) {
  1629. $query->andWhere(['al.act_id' => $this->act_id]);
  1630. }
  1631. if ($this->send_type > 0) {
  1632. $query->andWhere(['al.send_type' => $this->send_type]);
  1633. }
  1634. if (isset($this->is_send) && $this->is_send >= 0) {
  1635. $query->andWhere(['al.is_send' => $this->is_send]);
  1636. }
  1637. if (!empty($this->name)) {
  1638. $query->andWhere(['LIKE', 'su.name', $this->name]);
  1639. }
  1640. if (!empty($this->order_no)) {
  1641. $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
  1642. }
  1643. if (!empty($this->start_time)) {
  1644. $query->andWhere(['>', 'o.created_at', strtotime($this->start_time)]);
  1645. }
  1646. if (!empty($this->end_time)) {
  1647. $query->andWhere(['<', 'o.created_at', strtotime($this->end_time)]);
  1648. }
  1649. $total = null;
  1650. if ($getTotal) {
  1651. $queryCountUser = clone $query;
  1652. $totalCountUser = intval($queryCountUser->groupBy(['al.user_id'])->count());
  1653. $select = ['IFNULL(SUM(al.price_send),0) price_send', 'count(1) num'];
  1654. $queryPrice = clone $query;
  1655. $total['price'] = $queryPrice->andWhere(['al.send_type' => 1])->select($select)->asArray()->one();
  1656. $queryJifen = clone $query;
  1657. $total['jifen'] = $queryJifen->andWhere(['al.send_type' => 2])->select($select)->asArray()->one();
  1658. $queryBalance = clone $query;
  1659. $total['balance'] = $queryBalance->andWhere(['al.send_type' => 3])->select($select)->asArray()->one();
  1660. }
  1661. $query->select('al.*, su.avatar, su.name, su.mobile, o.trade_status, o.order_type, o.is_pay, o.total_price, o.pay_price, o.order_no, o.created_at order_created_at')
  1662. ->orderBy('al.pre_send_time DESC, al.id asc');
  1663. $res = pagination_make($query);
  1664. foreach ($res['list'] as &$value) {
  1665. $value['order_created_at'] = $value['order_created_at'] ? date('Y-m-d H:i:s', $value['order_created_at']) : '-';
  1666. $value['pre_send_time'] = $value['pre_send_time'] ? date('Y-m-d H:i:s', $value['pre_send_time']) : '-';
  1667. $value['send_time'] = $value['send_time'] ? date('Y-m-d H:i:s', $value['send_time']) : '-';
  1668. }
  1669. return [
  1670. 'code' => 0,
  1671. 'msg' => 'success',
  1672. 'data' => $res,
  1673. 'total' => $total,
  1674. 'totalCountUser' => $totalCountUser ?? 0,
  1675. 'q' => $query->createCommand()->getRawSql(),
  1676. ];
  1677. }
  1678. /**
  1679. * 返利信息结束
  1680. */
  1681. }