AccountLogForm.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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\constants\OptionSetting;
  9. use app\models\AccountLog;
  10. use app\models\Address;
  11. use app\models\common\InviteCode;
  12. use app\models\IntegralAppreciationUser;
  13. use app\models\IntegralAppreciationUserIntegralLog;
  14. use app\models\Level;
  15. use app\models\Option;
  16. use app\models\ReOrder;
  17. use app\models\LevelOrder;
  18. use app\models\SaaSLeaguePriceLog;
  19. use app\models\Store;
  20. use app\models\UserLabel;
  21. use Exception;
  22. use app\models\User;
  23. use app\models\SaasUser;
  24. use app\utils\Notice\NoticeSend;
  25. use yii\base\Model;
  26. use yii\db\ActiveQuery;
  27. use yii\db\Expression;
  28. use yii\db\Query;
  29. use Yii;
  30. use yii\helpers\Json;
  31. class AccountLogForm extends Model
  32. {
  33. public $export;
  34. public $admin;
  35. public $store_id;
  36. public $money;
  37. public $integral;
  38. public $desc;
  39. public $explain;
  40. public $type;
  41. public $recharge_type;
  42. public $user_id;
  43. public $is_we7;
  44. public $is_ind;
  45. public $pic_url;
  46. public $dateStart;
  47. public $dateEnd;
  48. public $name;
  49. public $saas_user_id;
  50. public $log_type;
  51. /**
  52. * @return array the validation rules.
  53. */
  54. public function rules()
  55. {
  56. return [
  57. [['explain', 'name'], 'trim'],
  58. [['explain', 'name', 'dateStart', 'dateEnd'], 'string'],
  59. [['type', 'recharge_type', 'user_id', 'log_type'], 'integer'],
  60. [['money', 'integral'], 'number'],
  61. [['export', 'desc'], 'safe'],
  62. ];
  63. }
  64. /**
  65. * 充值积分或者余额
  66. * @return array
  67. * @throws \yii\base\Exception
  68. */
  69. public function recharge()
  70. {
  71. if (
  72. !in_array($this->type, AccountLog::$valid_type)
  73. || !in_array($this->recharge_type, AccountLog::$type_budget)
  74. ) {
  75. return [
  76. 'code' => 1,
  77. 'msg' => '参数错误'
  78. ];
  79. }
  80. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  81. if (!$user) {
  82. return [
  83. 'code' => 1,
  84. 'msg' => '用户不存在,或已删除',
  85. ];
  86. }
  87. if ($this->validate()) {
  88. // 积分
  89. if ($this->type == AccountLog::TYPE_INTEGRAL) {
  90. if (empty($this->integral)) {
  91. return [
  92. 'code' => 1,
  93. 'msg' => '积分设置不正确'
  94. ];
  95. }
  96. $accountLog = new AccountLog();
  97. $accountLog->store_id = get_store_id();
  98. $accountLog->user_id = $user->id;
  99. // TODO : admin
  100. // if ($this->is_we7) {
  101. // $admin = \Yii::$app->admin->identity;
  102. // } elseif ($this->is_ind) {
  103. // $admin = \Yii::$app->admin->identity;
  104. // } else {
  105. // $admin = \Yii::$app->mchRoleAdmin->identity;
  106. // }
  107. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  108. if ($this->integral > $user->integral) {
  109. return [
  110. 'code' => 1,
  111. 'msg' => '用户当前积分不足',
  112. ];
  113. }
  114. $user->integral -= $this->integral;
  115. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  116. $user->integral += $this->integral;
  117. $user->total_integral += $this->integral;
  118. }
  119. if (!$user->save()) {
  120. foreach ($user->errors as $error) {
  121. return [
  122. 'code' => 1,
  123. 'msg' => $error
  124. ];
  125. }
  126. }
  127. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  128. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
  129. . " 积分扣除:" . $this->integral . " 积分";
  130. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  131. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
  132. . " 积分充值:" . $this->integral . " 积分";
  133. }
  134. $accountLog->amount = $this->integral;
  135. $accountLog->created_at = time();
  136. $accountLog->operator = $this->admin->username ? $this->admin->username : '未知';
  137. $accountLog->store_id = $this->store_id;
  138. $accountLog->operator_id = $this->admin->id;
  139. $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  140. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  141. $accountLog->before = $user->integral + $this->integral;
  142. $accountLog->after = $user->integral;
  143. } else {
  144. $accountLog->before = $user->integral - $this->integral;
  145. $accountLog->after = $user->integral;
  146. }
  147. $accountLog->log_type = $this->recharge_type;
  148. $accountLog->type = AccountLog::TYPE_INTEGRAL;
  149. $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
  150. $accountLog->explain = $this->explain;
  151. if ($accountLog->save()) {
  152. return [
  153. 'code' => 0,
  154. 'msg' => '操作成功',
  155. ];
  156. } else {
  157. return [
  158. 'code' => 1,
  159. 'msg' => '操作失败',
  160. ];
  161. }
  162. }
  163. // 余额
  164. if ($this->type == AccountLog::TYPE_BALANCE) {
  165. if (!$this->validate()) {
  166. return [
  167. 'code' => 1,
  168. 'msg' => '参数有误'
  169. ];
  170. }
  171. $this->money = floatval($this->money);
  172. if ($this->money < 0) {
  173. return [
  174. 'code' => 1,
  175. 'msg' => '输入数值不能小于0'
  176. ];
  177. }
  178. $accountLog = new AccountLog();
  179. $accountLog->store_id = $this->store_id;
  180. $accountLog->user_id = $this->user_id;
  181. $accountLog->pic_url = $this->pic_url;
  182. $accountLog->explain = $this->explain;
  183. switch ($this->recharge_type) {
  184. case AccountLog::LOG_TYPE_INCOME:
  185. $user->money += $this->money;
  186. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:"
  187. . $user->nickname . " 余额充值:" . $this->money . " 元";
  188. $accountLog->log_type = AccountLog::LOG_TYPE_INCOME;
  189. $desc = '充值';
  190. break;
  191. case AccountLog::LOG_TYPE_EXPEND:
  192. if ($user->money < $this->money) {
  193. return [
  194. 'code' => 1,
  195. 'msg' => '扣除数值大于当前用户余额'
  196. ];
  197. }
  198. $user->money -= $this->money;
  199. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
  200. . " 余额扣除:" . $this->money . " 元";
  201. $accountLog->log_type = AccountLog::LOG_TYPE_EXPEND;
  202. $desc = '扣除';
  203. break;
  204. default:
  205. return [
  206. 'code' => 1,
  207. 'msg' => '网络异常,请刷新重试'
  208. ];
  209. }
  210. if($this->desc){
  211. $accountLog->desc = $this->desc;
  212. }
  213. if ($user->save()) {
  214. // NoticeSend::UserChang($this->user_id, $user->binding, $this->money, $desc);
  215. $accountLog->amount = $this->money;
  216. $accountLog->operator = $this->admin->username;
  217. $accountLog->operator_id = $this->admin->id;
  218. $accountLog->created_at = time();
  219. $accountLog->type = AccountLog::TYPE_BALANCE;
  220. $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  221. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  222. $accountLog->before = $user->money + $this->money;
  223. $accountLog->after = $user->money;
  224. } else {
  225. $accountLog->before = $user->money - $this->money;
  226. $accountLog->after = $user->money;
  227. }
  228. $accountLog->log_type = $this->recharge_type;
  229. $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
  230. if (!$accountLog->save()) {
  231. // Yii::error($accountLog->errors);
  232. return [
  233. 'code' => 1,
  234. 'msg' => '记录日志错误'
  235. ];
  236. } else {
  237. return [
  238. 'code' => 0,
  239. 'msg' => '操作成功'
  240. ];
  241. }
  242. } else {
  243. return [
  244. 'code' => 1,
  245. 'msg' => $this->getErrorSummary(false)[0],
  246. ];
  247. }
  248. }
  249. //增值积分
  250. if ($this->type == AccountLog::TYPE_INTEGRAL_POOL) {
  251. if (empty($this->integral)) {
  252. return [
  253. 'code' => 1,
  254. 'msg' => '积分设置不正确'
  255. ];
  256. }
  257. $accountLog = new AccountLog();
  258. $accountLog->store_id = get_store_id();
  259. $accountLog->user_id = $user->id;
  260. $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  261. if (!$integral_appreciation_user) {
  262. $integral_appreciation_user = new IntegralAppreciationUser();
  263. $integral_appreciation_user->user_id = $user->id;
  264. $integral_appreciation_user->store_id = get_store_id();
  265. }
  266. //扣除
  267. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  268. if(!$integral_appreciation_user || $integral_appreciation_user->integral < $this->integral){
  269. return [
  270. 'code' => 1,
  271. 'msg' => '扣除积分不足'
  272. ];
  273. }
  274. $integral_appreciation_user->integral-=$this->integral;
  275. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  276. $integral_appreciation_user->integral += $this->integral;
  277. $integral_appreciation_user->total_integral += $this->integral;
  278. }
  279. if(!$integral_appreciation_user->save()){
  280. return [
  281. 'code' => 1,
  282. 'msg' => '操作失败'
  283. ];
  284. }
  285. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  286. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
  287. . " 增值积分扣除:" . $this->integral . " 积分";
  288. IntegralAppreciationUserIntegralLog::saveIntegralLog(
  289. $integral_appreciation_user->id,
  290. $this->integral,
  291. IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
  292. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_EXPEND,
  293. "后台扣除"
  294. );
  295. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  296. $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
  297. . " 增值积分充值:" . $this->integral . " 积分";
  298. IntegralAppreciationUserIntegralLog::saveIntegralLog(
  299. $integral_appreciation_user->id,
  300. $this->integral,
  301. IntegralAppreciationUserIntegralLog::TYPE_INCOME,
  302. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_INCOME,
  303. "后台充值"
  304. );
  305. }
  306. $accountLog->amount = $this->integral;
  307. $accountLog->created_at = time();
  308. $accountLog->operator = $this->admin->username ? $this->admin->username : '未知';
  309. $accountLog->store_id = $this->store_id;
  310. $accountLog->operator_id = $this->admin->id;
  311. $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  312. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  313. $accountLog->before = $integral_appreciation_user->integral + $this->integral;
  314. $accountLog->after = $integral_appreciation_user->integral;
  315. } else {
  316. $accountLog->before = $integral_appreciation_user->integral - $this->integral;
  317. $accountLog->after = $integral_appreciation_user->integral;
  318. }
  319. $accountLog->log_type = $this->recharge_type;
  320. $accountLog->type = AccountLog::TYPE_INTEGRAL_POOL;
  321. $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
  322. $accountLog->explain = $this->explain;
  323. if ($accountLog->save()) {
  324. return [
  325. 'code' => 0,
  326. 'msg' => '操作成功',
  327. ];
  328. } else {
  329. return [
  330. 'code' => 1,
  331. 'msg' => '操作失败',
  332. ];
  333. }
  334. }
  335. }
  336. return [
  337. 'code' => 1,
  338. 'msg' => $this->getErrorSummary(false)[0],
  339. ];
  340. }
  341. public function batchRecharge()
  342. {
  343. $type = input_params('type', AccountLog::TYPE_BALANCE);
  344. $admin = Yii::$app->jwt->getAdmin();
  345. $store_id = $this->store_id;
  346. $data = [
  347. 'explain' => '',
  348. 'money' => 0,
  349. 'integral' => 0,
  350. 'recharge_type' => AccountLog::LOG_TYPE_INCOME,
  351. 'type' => $type,
  352. 'user_id' => 0,
  353. ];
  354. set_time_limit(0);
  355. $filename = $_FILES['excel']['name'];
  356. $tmpname = $_FILES['excel']['tmp_name'];
  357. $path = \Yii::$app->basePath . '/web/temp/';
  358. if(!is_dir($path)){
  359. mkdir($path);
  360. }
  361. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  362. if (($ext != 'xlsx') && ($ext != 'xls')) {
  363. return [
  364. 'code' => 1,
  365. 'msg' => '请上传excel文件'
  366. ];
  367. }
  368. $file = time() . $this->store_id . '.' . $ext;
  369. $uploadfile = $path . $file;
  370. move_uploaded_file($tmpname, $uploadfile);
  371. $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
  372. ->getRows();
  373. $err = [];
  374. $rows->each(function (array $item) use ($admin, $store_id, $data, &$err) {
  375. try{
  376. $mobile = trim($item['手机号']);
  377. $nickname = trim($item['昵称']);
  378. $change = trim($item['余额增减数额']);
  379. $desc = trim($item['余额变动备注']);
  380. $parent_phone = trim($item['推荐人']);
  381. $level_name = trim($item['会员等级']);
  382. $user = User::findOne(['binding' => $mobile, 'is_delete' => 0, 'store_id' => $store_id]);
  383. $saas_user = SaasUser::findOne(['mobile' => $mobile, 'is_delete' => 0]);
  384. if(!$user){
  385. if (!$saas_user) {
  386. $saas_user = new SaasUser();
  387. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  388. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';;
  389. $saas_user->mobile = $mobile ?: '';
  390. $saas_user->name = $nickname ? $nickname : substr_replace($mobile, '******', 3, 6);
  391. $saas_user->store_id = $store_id;
  392. if (!$saas_user->save()) {
  393. throw new \Exception(array_shift($saas_user->getFirstErrors()));
  394. }
  395. }
  396. $user = new User();
  397. $user->access_token = \Yii::$app->security->generateRandomString();
  398. $user->binding = $saas_user->mobile;
  399. $user->nickname = $saas_user->name;
  400. $user->avatar_url = $saas_user->avatar;
  401. $user->username = \Yii::$app->security->generateRandomString();
  402. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  403. $user->auth_key = \Yii::$app->security->generateRandomString();
  404. $user->store_id = $store_id;
  405. if ($parent_phone) {
  406. $parent = User::findOne(['binding' => $parent_phone, 'is_delete' => 0, 'store_id' => $store_id]);
  407. if ($parent) {
  408. $user->parent_id = $parent->id;
  409. $user->old_parent_id = $parent->id;
  410. }
  411. }
  412. if ($level_name) {
  413. $level = Level::findOne(['store_id' => $store_id, 'name' => $level_name, 'is_delete' => 0]);
  414. if ($level) {
  415. $user->level = $level->level;
  416. }
  417. }
  418. if (!$user->save()) {
  419. throw new \Exception(array_shift($user->getFirstErrors()));
  420. }
  421. } else {
  422. // 已经存在,就更新
  423. if($nickname && $nickname != $saas_user->name){
  424. $saas_user->name = $nickname;
  425. if (!$saas_user->save()) {
  426. throw new \Exception(array_shift($saas_user->getFirstErrors()));
  427. }
  428. }
  429. $isEdit = false;
  430. if($nickname && $nickname != $user->nickname){
  431. $isEdit = true;
  432. $user->nickname = $nickname;
  433. }
  434. if ($parent_phone) {
  435. $parent = User::findOne(['binding' => $parent_phone, 'is_delete' => 0, 'store_id' => $store_id]);
  436. if ($parent && $parent->id != $user->old_parent_id) {
  437. if ($user->parent_id == $user->old_parent_id) {
  438. $user->parent_id = $parent->id;
  439. }
  440. $user->old_parent_id = $parent->id;
  441. $isEdit = true;
  442. }
  443. }
  444. if ($level_name) {
  445. $level = Level::findOne(['store_id' => $store_id, 'name' => $level_name, 'is_delete' => 0]);
  446. if ($level && $level->level != $user->level) {
  447. $user->level = $level->level;
  448. $isEdit = true;
  449. }
  450. }
  451. if ($isEdit) {
  452. if (!$user->save()) {
  453. throw new \Exception(array_shift($user->getFirstErrors()));
  454. }
  455. }
  456. }
  457. if ($change) {
  458. $recharge_type = ($change > 0) ? AccountLog::LOG_TYPE_INCOME : AccountLog::LOG_TYPE_EXPEND;
  459. $data['user_id'] = $user['id'];
  460. $data['desc'] = $desc;
  461. $data['recharge_type'] = $recharge_type;
  462. if($data['type'] == AccountLog::TYPE_BALANCE){
  463. $data['money'] = abs($change);
  464. if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->money < $data['money']){
  465. $data['money'] = $user->money;
  466. }
  467. if($data['money'] == 0){
  468. return;
  469. }
  470. }
  471. if($data['type'] == AccountLog::TYPE_INTEGRAL){
  472. $data['integral'] = abs($change);
  473. if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->integral < $data['integral']){
  474. $data['integral'] = $user->integral;
  475. }
  476. if($data['integral'] == 0){
  477. return;
  478. }
  479. }
  480. $form = new AccountLogForm();
  481. $form->store_id = $store_id;
  482. $form->admin = $admin;
  483. $form->attributes = $data;
  484. $res = $form->recharge();
  485. }
  486. } catch (\Exception $e){
  487. debug_log($e->getMessage());
  488. $res = [
  489. 'code' => 1,
  490. 'msg' => $e->getMessage(),
  491. ];
  492. }
  493. if($res['code'] != 0){
  494. $err[] = array_merge($res, ['mobile' => $mobile]);
  495. }
  496. });
  497. $count = count($err);
  498. @unlink($uploadfile);
  499. return [
  500. 'code' => $count ? 1 : 0,
  501. 'msg' => "操作完成" . ($err ? ",失败{$count}条数据" : ''),
  502. 'err' => $err,
  503. ];
  504. }
  505. /**
  506. * 联盟券记录
  507. */
  508. public function getLeagueList()
  509. {
  510. $query = SaaSLeaguePriceLog::find()->where(['saas_user_id' => $this->saas_user_id, 'role' => SaaSLeaguePriceLog::ROLE_USER])->orderBy('id desc');
  511. if ($this->log_type !== null && in_array($this->log_type, [0, 1])) {
  512. $query->andWhere(['send_or_take_type' => $this->log_type]);
  513. }
  514. $pagination = pagination_make($query);
  515. $saas_model = (new SaasUsersForm())->getUserInfo(['id' => $this->saas_user_id])['data'];
  516. $list = $pagination['list'];
  517. foreach ($list as & $arr) {
  518. $store = Store::findOne($arr['store_id']);
  519. if($store){
  520. $arr['store_name'] = $store->name;
  521. }else{
  522. $arr['store_name'] = '/';
  523. }
  524. $arr['addtime'] = date('Y-m-d H:i:s',$arr['addtime']);
  525. if($arr['type'] == 0){
  526. $arr['type_str'] = '商城转赠';
  527. }else if($arr['type'] == 1){
  528. $arr['type_str'] = '平台修改';
  529. }else if($arr['type'] == 2){
  530. $arr['type_str'] = '消费获赠';
  531. }else if($arr['type'] == 3){
  532. $arr['type_str'] = '订单取消退回';
  533. }else if($arr['type'] == 4){
  534. $arr['type_str'] = '下单抵扣';//支出
  535. }
  536. if($arr['send_or_take_type'] == 0){
  537. $arr['send_or_take_type_str'] = '收入';
  538. }else{
  539. $arr['send_or_take_type_str'] = '支出';
  540. }
  541. }
  542. return [
  543. 'code' => 0,
  544. 'msg' => 'success',
  545. 'data' => [
  546. 'data' => $list,
  547. 'saas_user_id' => $this->saas_user_id,
  548. 'details' => $saas_model,
  549. 'pageNo' => $pagination['pageNo'],
  550. 'totalCount' => $pagination['totalCount'],
  551. ],
  552. ];
  553. }
  554. /**
  555. * 充值记录展示
  556. */
  557. public function getRechargeList()
  558. {
  559. if ($this->type == AccountLog::TYPE_INTEGRAL) {
  560. return $this->getIntegralRechargeList();
  561. }
  562. if ($this->type == 3) {
  563. return $this->getLevelOrderList();
  564. }
  565. if ($this->user_id) {
  566. $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id, 'al.user_id' => $this->user_id])->leftJoin(['u' => User::tableName()], 'u.id=' . $this->user_id);
  567. $details = $this->getUserInfo();
  568. } else {
  569. $query = AccountLog::find()->alias('al')->where([
  570. 'al.type' => $this->type, 'al.store_id' => $this->store_id
  571. ])->leftJoin(['u' => User::tableName()], 'u.id=al.user_id');
  572. }
  573. if ($this->name) {
  574. $query->andWhere(['like', 'u.nickname', $this->name]);
  575. }
  576. if ($this->dateStart) {
  577. $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
  578. }
  579. if ($this->dateEnd) {
  580. $query->andWhere(['<=', 'al.created_at', strtotime($this->dateEnd)]);
  581. }
  582. $query->orderBy('al.id desc')->select('al.*, u.nickname, u.avatar_url');
  583. $pagination = pagination_make($query);
  584. $list = $pagination['list'];
  585. foreach ($list as $key => $value) {
  586. $list[$key]['send_price'] = 0.00;
  587. if (
  588. $value['order_type'] == AccountLog::TYPE_RECHARGE_ORDER
  589. && $value['operator_type'] == AccountLog::TYPE_OPERATOR_NORMAL
  590. ) {
  591. $reOrder = ReOrder::find()->where([
  592. 'id' => $value['order_id'], 'is_pay' => ReOrder::IS_PAY,
  593. 'is_delete' => ReOrder::NOT_DELETE
  594. ])->asArray()->One();
  595. $list[$key]['send_price'] = $reOrder['send_price'];
  596. }
  597. $list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  598. }
  599. $level = $this->getLevelList();
  600. $user = User::findOne($details['id']);
  601. $userLabel = UserLabel::getLabel($user->store_id, $user->user_label_id);
  602. $details['user_label_id'] = empty($user->user_label_id) ? [] : explode(',',trim($user->user_label_id,','));
  603. $details['user_label_name'] = $userLabel->label_name ?: '';
  604. $details['nickname'] = $user->nickname;
  605. $details['avatar_url'] = $user->avatar_url;
  606. $details['address_list'] = [];
  607. //id转换成推荐码
  608. $details['invite_code'] = InviteCode::id2Code($details['id']);
  609. if($user->binding){
  610. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  611. }else{
  612. if($user->alipay_open_id){
  613. $saas_user = SaasUser::findOne(['ali_user_id' => $user->alipay_open_id]);
  614. }
  615. }
  616. if($saas_user){
  617. // $details['nickname'] = $saas_user->name;
  618. // $details['avatar_url'] = $saas_user->avatar;
  619. $address_list = Address::find()->where(['user_id' => $saas_user->id, 'is_delete' => 0])->select('name, mobile, province, city, district, town, village, detail, is_default, id, latitude, longitude, province_id, city_id, district_id, town_id, village_id')->orderBy('addtime desc')->asArray()->all();
  620. foreach ($address_list as &$item) {
  621. $item['is_default'] = (int)$item['is_default'];
  622. }
  623. $details['address_list'] = $address_list;
  624. }
  625. $group_setting = OptionSetting::getOptionSetting();
  626. $group_arr = $group_setting['pay']['list'] ?: [];
  627. foreach ($group_arr as $val) {
  628. if ($val['name'] == 'payment') {
  629. $group_data = Option::findOne(['store_id' => $this->store_id, 'group' => 'user', 'name' => $val['name'].'_'.$user->id]);
  630. if(!$group_data){
  631. foreach($val['default'] as &$item){
  632. $item['value'] = 0;
  633. }
  634. }else{
  635. $group_data_val = Json::decode($group_data->value);
  636. foreach($val['default'] as $k => &$item){
  637. if(isset($group_data_val[$k])){
  638. $item['value'] = $group_data_val[$k]['value'];
  639. continue;
  640. }
  641. $item['value'] = 0;
  642. }
  643. }
  644. $details['payment'] = $val['default'];
  645. $details['$group_data'] = $group_data;
  646. }
  647. }
  648. return [
  649. 'code' => 0,
  650. 'msg' => 'success',
  651. 'data' => [
  652. 'data' => $list,
  653. 'detail' => $details,
  654. 'level' => $level,
  655. 'pageNo' => $pagination['pageNo'],
  656. 'totalCount' => $pagination['totalCount'],
  657. ],
  658. ];
  659. }
  660. private function export($list) {
  661. $rows = [[
  662. '类型',
  663. '数额',
  664. '更改前',
  665. '更改后',
  666. '操作时间',
  667. '操作者',
  668. '说明',
  669. ]];
  670. foreach($list as $item){
  671. $r = [
  672. $item['log_type'] == 2 ? '支出' : '收入',
  673. $item['amount'],
  674. $item['before'],
  675. $item['after'],
  676. date('Y-m-d H:i:s',$item['created_at']),
  677. $item['operator'],
  678. $item['desc'],
  679. ];
  680. $rows[] = $r;
  681. }
  682. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  683. ->addRows($rows)->toBrowser();
  684. }
  685. /**
  686. * 充值记录展示
  687. */
  688. public function getIntegralRechargeList()
  689. {
  690. if ($this->user_id) {
  691. $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id, 'al.user_id' => $this->user_id]);
  692. $details = $this->getUserInfo();
  693. } else {
  694. $query = AccountLog::find()->alias('al')->where(
  695. ['al.type' => $this->type, 'al.store_id' => $this->store_id]
  696. )->leftJoin(['u' => User::tableName()], 'u.id=al.user_id')
  697. ->select('al.*, u.nickname, u.avatar_url');
  698. }
  699. if ($this->name) {
  700. $query->andWhere(['like', 'u.nickname', $this->name]);
  701. }
  702. if ($this->dateStart) {
  703. $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
  704. }
  705. if ($this->dateEnd) {
  706. $query->andWhere(['<=', 'al.created_at', strtotime($this->dateEnd)]);
  707. }
  708. $query->orderBy('al.created_at desc');
  709. $pagination = pagination_make($query);
  710. $list = $pagination['list'];
  711. if($this->export){
  712. return $this->export($list);
  713. }
  714. $level = $this->getLevelList();
  715. $user = User::findOne($details['id']);
  716. $details['nickname'] = $user->nickname;
  717. $details['avatar_url'] = $user->avatar_url;
  718. if($user->binding){
  719. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  720. }else{
  721. if($user->alipay_open_id){
  722. $saas_user = SaasUser::findOne(['ali_user_id' => $user->alipay_open_id]);
  723. }
  724. }
  725. if($saas_user){
  726. $details['nickname'] = $saas_user->name;
  727. $details['avatar_url'] = $saas_user->avatar;
  728. }
  729. return [
  730. 'code' => 0,
  731. 'msg' => 'success',
  732. 'data' => [
  733. 'data' => $list,
  734. 'level' => $level,
  735. 'detail' => $details,
  736. 'pageNo' => $pagination['pageNo'],
  737. 'totalCount' => $pagination['totalCount'],
  738. ],
  739. ];
  740. }
  741. /**
  742. * 获取个人详情
  743. * @return void
  744. */
  745. public function getUserInfo()
  746. {
  747. $data = User::find()->alias('u')->where([
  748. 'u.id' => $this->user_id,
  749. 'u.type' => User::USER_TYPE_NORMAL,
  750. 'u.store_id' => $this->store_id,
  751. 'u.is_delete' => User::USER_NOT_DELETE,
  752. ])->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  753. ->select('u.*,su.avatar sass_avatar,su.name sass_name')->asArray()->one();
  754. $data['user_label_id'] = $data['user_label_id'] == 0 ? [] : explode(',',trim($data['user_label_id'],','));
  755. // 上级id
  756. // $data['nickname'] = !empty($data['sass_name']) ? $data['sass_name'] : $data['nickname'];
  757. // $data['avatar_url'] = !empty($data['sass_avatar']) ? $data['sass_avatar'] : $data['avatar_url'];
  758. $user = User::findOne($data['parent_id']);
  759. // $user = SaasUser::find()->where(['mobile' => User::findOne($data['parent_id'])->binding])
  760. // ->select('name')->asArray()->One();
  761. $data['parent_name'] = !empty($user['nickname']) ? $user['nickname'] : '总店';
  762. $data['old_parent_name'] = '总店';
  763. if ($data['old_parent_id'] > 0) {
  764. // 这里该用用户表的昵称
  765. $oldParent = User::findOne($data['old_parent_id']);
  766. // $oldUser = SaasUser::find()->where(['mobile' => User::findOne($data['old_parent_id'])->binding])
  767. // ->select('name')->asArray()->One();
  768. $data['old_parent_name'] = !empty($oldParent->nickname) ? $oldParent->nickname : '总店';
  769. }
  770. $data['init_parent_name'] = '总店';
  771. if ($data['init_parent_id'] > 0) {
  772. $initUser = User::findOne($data['init_parent_id']);
  773. // $initUser = SaasUser::find()->where(['mobile' => User::findOne($data['init_parent_id'])->binding])
  774. // ->select('name')->asArray()->One();
  775. $data['init_parent_name'] = !empty($initUser['nickname']) ? $initUser['nickname'] : '总店';
  776. }
  777. // 获取会员等级
  778. $level = Level::findOne(['level' => $data['level'], 'store_id' => $this->store_id, 'is_delete' => 0]);
  779. $data['l_name'] = $data['level'] == '-1' ? '普通用户' : $level['name'];
  780. $data['user_level_expires'] = $data['user_level_expires'] > 0 ? date('Y年m月d日 H:i:s', $data['user_level_expires'] ) : '永久有效';
  781. return $data;
  782. }
  783. /**
  784. * 获取会员等级列表
  785. */
  786. public function getLevelList() {
  787. // 会员列表
  788. $level = Level::find()->select('level, name')->where([
  789. 'store_id' => $this->store_id,
  790. 'is_delete' => Level::NOT_DELETE,
  791. 'status' => Level::STATUS_TRUE
  792. ])->orderBy('level ASC')->asArray()->all();
  793. $level = array_merge([['level' => '-1', 'name' => '普通用户']], $level);
  794. return $level;
  795. }
  796. /**
  797. * 用户会员记录列表
  798. * @return array
  799. */
  800. public function getLevelOrderList() {
  801. if ($this->user_id) {
  802. $query = LevelOrder::find()->alias('ro')->where([
  803. 'ro.store_id' => $this->store_id,
  804. 'ro.is_delete' => LevelOrder::NOT_DELETE,
  805. 'ro.is_pay' => LevelOrder::IS_PAY,
  806. 'ro.user_id' => $this->user_id
  807. ])
  808. ->leftJoin(['u' => User::tableName()], 'u.id='.$this->user_id);
  809. $details = $this->getUserInfo();
  810. } else {
  811. return [
  812. 'code' => 1,
  813. 'msg' => '用户参数错误',
  814. ];
  815. }
  816. $currentQuery = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
  817. ->andWhere('level=ro.current_level')->select('name');
  818. $afterQuery = Level::find()->where(['store_id' => $this->store_id,'is_delete' => Level::NOT_DELETE])
  819. ->andWhere('level=ro.after_level')->select('name');
  820. $query->orderBy('ro.pay_time desc');
  821. $query->select(['ro.*', 'u.nickname', 'u.platform', 'after_name' => $afterQuery, 'current_name' => $currentQuery]);
  822. $pagination = pagination_make($query);
  823. // return $pagination;
  824. $list = $pagination['list'];
  825. foreach ($list as $k => $v) {
  826. $list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
  827. if (!$v['current_name']) {
  828. $list[$k]['current_name'] = '普通会员';
  829. }
  830. }
  831. $level = $this->getLevelList();
  832. $user = User::findOne($details['id']);
  833. $details['nickname'] = $user->nickname;
  834. $details['avatar_url'] = $user->avatar_url;
  835. if($user->binding){
  836. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  837. }else{
  838. if($user->alipay_open_id){
  839. $saas_user = SaasUser::findOne(['ali_user_id' => $user->alipay_open_id]);
  840. }
  841. }
  842. if($saas_user){
  843. $details['nickname'] = $saas_user->name;
  844. $details['avatar_url'] = $saas_user->avatar;
  845. }
  846. return [
  847. 'code' => 0,
  848. 'msg' => 'success',
  849. 'data' => [
  850. 'data' => $list,
  851. 'detail' => $details,
  852. 'level' => $level,
  853. 'pageNo' => $pagination['pageNo'],
  854. 'totalCount' => $pagination['totalCount']
  855. ],
  856. ];
  857. }
  858. }