DefaultController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. <?php
  2. namespace addons\Achievement\backend\controllers;
  3. use addons\Achievement\common\enums\AchievementEnum;
  4. use addons\Achievement\common\events\user\AchievementUserDelEvent;
  5. use addons\Achievement\common\listeners\order\OrderPayRewardListener;
  6. use addons\Achievement\common\logic\AchievementLogic;
  7. use addons\Achievement\common\models\AchievementAwardLog;
  8. use addons\Achievement\common\models\AchievementGrantLog;
  9. use addons\Achievement\common\models\AchievementOrderLog;
  10. use addons\Achievement\common\models\AchievementUser;
  11. use addons\Achievement\common\models\AchievementGrantConfirmLog;
  12. use addons\Achievement\common\models\AchievementAwardLogUpdateLog;
  13. use addons\Achievement\common\logic\AchievementOrderLogic;
  14. use addons\Store\common\models\StoreGoods;
  15. use common\enums\AddonsEnum;
  16. use common\enums\RabbitMqEnum;
  17. use common\enums\StatusEnum;
  18. use common\enums\RedisKeyEnum;
  19. use common\helpers\LockHelper;
  20. use common\models\common\UpgradeCondition;
  21. use common\models\mall\MallAddons;
  22. use common\models\user\User;
  23. use common\enums\DownloadEnum;
  24. use common\helpers\csv\CsvExportHelper;
  25. use Yii;
  26. /**
  27. * 默认控制器
  28. *
  29. * Class DefaultController
  30. * @package addons\Achievement\backend\controllers
  31. */
  32. class DefaultController extends BaseController
  33. {
  34. public $enableCsrfValidation = false;
  35. /**
  36. * 首页
  37. *
  38. * @return string
  39. */
  40. public function actionIndex()
  41. {
  42. if (\Yii::$app->request->isAjax) {
  43. if (\Yii::$app->request->isGet) {
  44. $get = \Yii::$app->request->get();
  45. $where[] = ['=', 'mall_id', $this->mall_id];
  46. // $where[] = ['=', 'is_wait', AchievementEnum::IS_WAIT_YES];
  47. $where[] = ['=', 'status', StatusEnum::ENABLED];
  48. if (!empty($get['date_start']) && !empty($get['date_end'])) {
  49. $where[] = ['between', 'settlement_time', strtotime($get['date_start']), strtotime($get['date_end'])];
  50. }
  51. $maxMoney = $get['max_money'] > $get['min_money'] ? $get['max_money'] : $get['min_money'];
  52. $minMoney = $get['max_money'] > $get['min_money'] ? $get['min_money'] : $get['max_money'];
  53. if (!empty($get['max_money']) && !empty($get['min_money'])) {
  54. $where[] = ['between', 'award_money', $minMoney, $maxMoney];
  55. } elseif (!empty($this->max_money)) {
  56. $where[] = ['<=', 'award_money', $get['max_money']];
  57. } elseif (!empty($this->min_money)) {
  58. $where[] = ['>=', 'award_money', $get['min_money']];
  59. }
  60. $params['order'] = 'id desc';
  61. $params['where'] = $where;
  62. $params['limit'] = 10;
  63. $list = AchievementGrantLog::listPage($params, false);
  64. if(!empty($list['list'])){
  65. foreach ($list['list'] as &$item){
  66. $item['award_proportion'] = json_decode($item['award_proportion'],true);
  67. $item['award_money'] = $item['award_money'] + $item['equal_money'];
  68. }
  69. }
  70. $sum = AchievementGrantLog::getOne($where, $is_array = true, $with = [], $order = false, $select = 'sum(award_money) as money,sum(user_num) as award_user', $index = '');
  71. $returnData['sum'] = [
  72. 'award_money' => $sum['money'],
  73. 'award_user' => $sum['award_user'],
  74. 'grant_num' => $list['count']
  75. ];
  76. $returnData["list"] = $list;
  77. return $this->success('success', $returnData);
  78. }
  79. }
  80. return $this->render('index', []);
  81. }
  82. public function actionList()
  83. {
  84. if (\Yii::$app->request->isAjax) {
  85. if (\Yii::$app->request->isGet) {
  86. $get = \Yii::$app->request->get();
  87. $where[] = ['=', 'mall_id', $this->mall_id];
  88. $where[] = ['=', 'status', StatusEnum::ENABLED];
  89. if (!empty($get['date_start']) && !empty($get['date_end'])) {
  90. $where[] = ['between', 'created_at', strtotime($get['date_start']), strtotime($get['date_end'])];
  91. }
  92. if (!empty($get['grant_id'])) {
  93. $where[] = ['=', 'grant_id', $get['grant_id']];
  94. }
  95. $is_search = 0;
  96. $user_id_arr = [];
  97. if (!empty($get['keyword'])) {
  98. $is_search = 1;
  99. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  100. 'or',
  101. ['=', 'id', $get['keyword']],
  102. ['like', 'nickname', $get['keyword']],
  103. ['like', 'mobile', $get['keyword']]
  104. ]], 'select' => 'id']);
  105. $user_ids = array_column($user_list, 'id');
  106. if (!empty($user_id_arr)) {
  107. $user_id_arr = array_intersect($user_id_arr, $user_ids);
  108. } else {
  109. $user_id_arr = $user_ids;
  110. }
  111. }
  112. if ($is_search) $where[] = ['in', 'user_id', $user_id_arr];
  113. // if (!empty($get['keyword'])) {
  114. // $where[] = ['=', 'user_id', $get['keyword']];
  115. // }
  116. $params['order'] = 'id desc';
  117. $params['where'] = $where;
  118. $params['with'] = ['grantLog' => function ($query) {
  119. // $query->where(['is_wait' => AchievementEnum::IS_WAIT_YES]);
  120. }, 'user', 'updateLog'];
  121. $params['limit'] = 10;
  122. $list = AchievementAwardLog::listPage($params, false);
  123. foreach($list['list'] as &$item){
  124. $item['award_money'] = $item['award_money'] + $item['equal_money'];
  125. $item['award_sum'] = $item['award_sum'] + $item['equal_money'];
  126. $item['edit_award_money'] = $item['award_money'];
  127. if ($item['updateLog']) {
  128. $item['edit_award_money'] = $item['updateLog']['before_award_money'];
  129. }
  130. }
  131. return $this->success('操作成功', $list);
  132. }
  133. if (Yii::$app->request->post('flag') === 'EXPORT') {
  134. $post = Yii::$app->request->post();
  135. $post['mall_id'] = $this->mall_id;
  136. $filename = '业绩奖励明细-' . date('YmdHis') . '_' . rand(10000, 99999);
  137. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ACHIEVEMENT_AWARD_LOG, AchievementAwardLog::class, 'exportHandle', $post);
  138. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  139. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  140. }
  141. }
  142. return $this->render('list', []);
  143. }
  144. public function actionUpdateGrantAward()
  145. {
  146. try {
  147. if (\Yii::$app->request->isAjax) {
  148. if (\Yii::$app->request->isPost) {
  149. $params = \Yii::$app->request->post();
  150. $res = Yii::$app->services->validate->validate($params, [
  151. [['id', 'award_money'], 'required'],
  152. [['id'], 'integer'],
  153. [['award_money'], 'number'],
  154. ]);
  155. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  156. $params['mall_id'] = $this->mall_id;
  157. if ($params['award_money'] < 0) {
  158. return $this->error('奖励金额要大于0');
  159. }
  160. $award = AchievementAwardLog::find()
  161. ->where([
  162. 'is_end' => 0,
  163. 'is_settlement' => 0,
  164. 'status' => StatusEnum::ENABLED,
  165. 'id' => $params['id'],
  166. 'mall_id' => $this->mall_id,
  167. ])
  168. ->with(['updateLog'])
  169. ->one();
  170. if (!$award) {
  171. return $this->error('该条奖励数据不可修改');
  172. }
  173. if ($award['updateLog']) {
  174. return $this->error('该条奖励数据已修改过奖励金额');
  175. }
  176. $log_info = [
  177. 'mall_id' => $this->mall_id,
  178. 'grant_id' => $award->grant_id,
  179. 'award_id' => $award->id,
  180. 'before_award_money' => $award->award_money,
  181. 'after_award_money' => $params['award_money'],
  182. ];
  183. $t = \Yii::$app->db->beginTransaction();
  184. //更新奖励明细
  185. $award->award_money = $params['award_money'];
  186. if (!$award->save()) throw new \Exception($award->getErrorMessage());
  187. //更新奖励明细更改日志
  188. AchievementAwardLogUpdateLog::setData($log_info);
  189. //更新周期统计
  190. $grant = AchievementGrantLog::findOne(['mall_id' => $this->mall_id, 'id' => $award->grant_id, 'status' => 1, 'is_settlement' => 0]);
  191. AchievementOrderLogic::updateGrant($grant);
  192. $t->commit();
  193. return $this->success('操作成功');
  194. }
  195. }
  196. } catch (\Exception $exception) {
  197. $t->rollBack();
  198. return $this->error($exception->getMessage());
  199. }
  200. }
  201. public function actionOrder()
  202. {
  203. if (\Yii::$app->request->isAjax) {
  204. if (\Yii::$app->request->isGet) {
  205. $get = \Yii::$app->request->get();
  206. $where[] = ['=', 'mall_id', $this->mall_id];
  207. $where[] = ['=', 'status', StatusEnum::ENABLED];
  208. if (!empty($get['user_id'])) {
  209. $where[] = ['=', 'user_id', $get['user_id']];
  210. }
  211. if (!empty($get['goods_id'])) {
  212. $where[] = ['=', 'goods_id', $get['goods_id']];
  213. }
  214. if (!empty($get['order_no'])) {
  215. $where[] = ['=', 'order_no', $get['order_no']];
  216. }
  217. if (!empty($get['grant_id'])) {
  218. $where[] = ['=', 'grant_id', $get['grant_id']];
  219. }
  220. $params['order'] = 'id desc';
  221. $params['where'] = $where;
  222. $params['with'] = ['goods' => function ($query) {
  223. $query->select('id, goods_name')->where(['status' => StatusEnum::ENABLED]);
  224. }, 'user' => function ($query) {
  225. $query->select('id, nickname')->where(['status' => StatusEnum::ENABLED]);
  226. }];
  227. $params['limit'] = 10;
  228. $list = AchievementOrderLog::listPage($params, false);
  229. //dd($list);
  230. $install_store = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
  231. $store_goods_ids = $store_goods_list = [];
  232. foreach ($list['list'] as $k) {
  233. if(strpos($k['order_no'],'st') === 0){
  234. $store_goods_ids[] = $k['goods_id'];
  235. }
  236. }
  237. if($install_store && $store_goods_ids){
  238. $store_goods_list = StoreGoods::find()->select('id,goods_name')->where(['id' => $store_goods_ids])->asArray()->all();
  239. $store_goods_list = array_column($store_goods_list,'goods_name','id');
  240. }
  241. foreach ($list['list'] as &$k) {
  242. if(strpos($k['order_no'],'st') === 0 ){
  243. //门店订单号
  244. unset($k['goods']);
  245. if($install_store && $store_goods_list){
  246. $k['goods'] = ['goods_name' => $store_goods_list[$k['goods_id']].'【门店商品】'];
  247. }else{
  248. $k['goods'] = ['goods_name' => '【门店商品】'];
  249. }
  250. }
  251. $k['is_cloud_text'] = $k['is_cloud'] == 1 ? "云库存补货" : '前端会员购买';
  252. }
  253. return $this->success('操作成功', $list);
  254. }
  255. }
  256. return $this->render('order', []);
  257. }
  258. public function actionSetting()
  259. {
  260. $list = [];
  261. // 获取权重升级的条件
  262. $upgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, AchievementEnum::ADDONS_NAME, 'upgrade_condition.achievement_upgrade_condition', true);
  263. if (!empty($upgradeCondition)) {
  264. $ids = json_decode($upgradeCondition, true);
  265. if(!empty($ids)) {
  266. $list = UpgradeCondition::getData($ids);
  267. }
  268. }
  269. $upgrade_condition_data = UpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
  270. $group = \Yii::$app->request->get('group');
  271. $group = $group ?: 'basic';
  272. //此处因为历史脏数据 先不读取缓存数据
  273. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, $group, true);
  274. if (!$configs) {
  275. $params = [
  276. 'upgrade_condition' => [
  277. 'achievement_upgrade_condition' => ["8", "10", "11", "12", "40"],
  278. ],
  279. 'form' => [
  280. 'status' => 0,
  281. 'condition_type' => 0,
  282. 'goods_type' => 0,
  283. 'buy_goods_type' => 0,
  284. 'upgrade_type_goods' => 0,
  285. 'upgrade_type_condition' => 0,
  286. 'statistical_methods' => 0,
  287. 'settlement_cycle' => 0,
  288. 'delay_time' => 0,
  289. 'is_notice' => 0,
  290. 'enable_rules' => 0,
  291. 'settlement_mode' => 0,
  292. 'is_auto_upgrade' => 0,
  293. 'checked_condition_keys' => [],
  294. 'checked_condition_values' => [
  295. [
  296. 'key' => '10',
  297. 'value' => [
  298. 'direct_user_num' => '',
  299. 'direct_user_buy_goods_id' => '',
  300. 'direct_user_buy_goods_id_num' => '',
  301. ]
  302. ],
  303. [
  304. 'key' => '11',
  305. 'value' => [
  306. 'personal_achievement' => '',
  307. 'subordinates_achievement' => '',
  308. ]
  309. ],
  310. [
  311. 'key' => '12',
  312. 'value' => [
  313. 'indirect_user_num' => '',
  314. 'indirect_user_order_price' => '',
  315. ]
  316. ],
  317. [
  318. 'key' => '40',
  319. 'value' => [
  320. 'direct_user_consume' => '',
  321. 'direct_user_consume_num' => '',
  322. 'team_user_consume' => '',
  323. 'team_user_consume_num' => ''
  324. ]
  325. ],
  326. [
  327. 'key' => '95',
  328. 'value' => [
  329. 'checked_level' => [],
  330. ]
  331. ]
  332. ],
  333. 'award_proportion' => [
  334. 0 => [
  335. 'money' => '0.00',
  336. 'proportion' => '0.00',
  337. ]
  338. ],
  339. 'settlement_time' => '',
  340. 'goods_ids' => '',
  341. 'goods_list' => '',
  342. 'rule_rights' => null,
  343. 'is_equal' => 0,
  344. 'equal_type' => 0,
  345. 'equal_percent' => 0,
  346. 'equal_price_first' => 0,
  347. 'equal_price_second'=> 0,
  348. 'team_achievement' => 0,
  349. ]
  350. ];
  351. \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, ['basic' => $params['form'], 'upgrade_condition' => $params['upgrade_condition']]);
  352. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, $group);
  353. }
  354. if (\Yii::$app->request->isAjax) {
  355. if (\Yii::$app->request->isGet) {
  356. if (!empty($configs['award_proportion'])) {
  357. $configs['award_proportion'] = json_decode($configs['award_proportion'], true);
  358. } else {
  359. $configs['award_proportion'] = [[
  360. 'money' => 0,
  361. 'proportion' => 0,
  362. ]];
  363. }
  364. if (!empty($configs['checked_condition_keys'])) {
  365. $configs['checked_condition_keys'] = json_decode($configs['checked_condition_keys'], true);
  366. }
  367. if (!empty($configs['checked_condition_values'])) {
  368. $configs['checked_condition_values'] = json_decode($configs['checked_condition_values'], true);
  369. $ids = UpgradeCondition::$achievement_condition_ids;
  370. $default_list = UpgradeCondition::getData($ids);
  371. $checked_condition_values = array_column($configs['checked_condition_values'], null, 'key');
  372. foreach ($default_list as $item) {
  373. if (!isset($checked_condition_values[$item['id']]) && $item['id'] != 8) {
  374. $value = [];
  375. foreach ($item['setting'] as $val) {
  376. $value[$val['name']] = '';
  377. }
  378. $configs['checked_condition_values'][] = [
  379. 'key' => $item['id'],
  380. 'value' => $value,
  381. ];
  382. }
  383. }
  384. }
  385. $keyArr = array_column(json_decode($upgrade_condition_data['key_arr']),'key');
  386. $configs['checked_condition_values'] = isset($configs['checked_condition_values']) && is_array($configs['checked_condition_values']) ? $configs['checked_condition_values'] : [];
  387. foreach($configs['checked_condition_values'] as $k => $v){
  388. if(!in_array($v['key'],$keyArr)){
  389. unset($configs['checked_condition_values'][$k]);
  390. }
  391. }
  392. usort($configs['checked_condition_values'], function($a, $b) {
  393. return intval($a['key']) - intval($b['key']);
  394. });
  395. $targetKey = "95";
  396. $index = null;
  397. foreach ($configs['checked_condition_values'] as $i => $item) {
  398. if (isset($item['key']) && strval($item['key']) === $targetKey) {
  399. $index = $i;
  400. break;
  401. }
  402. }
  403. if ($index !== null) {
  404. if(!isset($configs['checked_condition_values'][$index]['value'])){
  405. $configs['checked_condition_values'][$index]['value'] = ['checked_level' => []];
  406. }
  407. }
  408. $configs['checked_condition_values'] = array_values($configs['checked_condition_values']);
  409. if (!empty($configs['goods_list'])) {
  410. $configs['goods_list'] = json_decode($configs['goods_list'], true);
  411. }
  412. if (!empty($configs['goods_ids'])) {
  413. $configs['goods_ids'] = json_decode($configs['goods_ids'], true);
  414. }
  415. if (!isset($configs['is_show_at_center'])) $configs['is_show_at_center'] = 1;
  416. $configs['install_store'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE) ? 1:0;
  417. $configs['install_mch'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_MCH) ? 1:0;
  418. if (!isset($configs['first_order'])) $configs['first_order'] = 1;
  419. return $this->success('操作成功', $configs);
  420. }
  421. if (\Yii::$app->request->isPost) {
  422. $post = \Yii::$app->request->post();
  423. $rules = [
  424. [['status', 'condition_type', 'goods_type', 'buy_goods_type', 'upgrade_type_goods', 'upgrade_type_condition', 'statistical_methods', 'settlement_cycle', 'delay_time', 'is_notice', 'is_notice', 'enable_rules', 'settlement_mode', 'is_auto_upgrade', 'is_show_at_center', 'is_equal', 'equal_percent', 'equal_type', 'is_leader_first_month_no_award', 'is_award_need_confirm'], 'integer'],
  425. [['checked_condition_keys', 'checked_condition_values', 'award_proportion', 'settlement_time', 'goods_ids', 'goods_list'], 'trim'],
  426. [['rule_rights'], 'string'],
  427. [['status','first_order'], 'in', 'range' => [0, 1]],
  428. [['condition_type'], 'in', 'range' => [0, 1, 2]],
  429. [['goods_type'], 'in', 'range' => [0, 1, 2]],
  430. [['store_order_award','mch_order_award'], 'integer'],
  431. [['equal_price_first','equal_price_second', 'team_achievement'], 'number'],
  432. ];
  433. $res = \Yii::$app->services->validate->validate($post['form'], $rules);
  434. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  435. if (isset($configs['status'])) {
  436. $status = $configs['status'];
  437. } else {
  438. $status = 0;
  439. }
  440. \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, ['basic' => $post['form']]);
  441. $logic = new AchievementLogic();
  442. if ($post['form']['status'] == StatusEnum::ENABLED) {
  443. $result = AchievementLogic::addGrantLog($this->mall_id, 1);
  444. if (!$result) return $this->error('操作失败');
  445. }
  446. \Yii::$app->custom->logs('post_form_status:' . $post['form']['status']);
  447. if ($post['form']['status'] == StatusEnum::DISABLED) {
  448. \Yii::$app->custom->logs('setLogStatus');
  449. $result = $logic->setLogStatus($this->mall_id);
  450. if (!$result) return $this->error('操作失败');
  451. }
  452. return $this->success('操作成功');
  453. }
  454. }
  455. return $this->render('setting', $upgrade_condition_data);
  456. }
  457. /**
  458. * @desc:设置升级条件
  459. * @Author: hua
  460. * @Date: 2021/10/23
  461. * @Time: 16:27
  462. * @Copyright: copyright (c) 2021 广东七件事集团
  463. * @return mixed|string|void
  464. */
  465. public function actionConditionSetting()
  466. {
  467. if (\Yii::$app->request->isAjax) {
  468. try {
  469. $request = \Yii::$app->request;
  470. if ($request->isAjax) {
  471. if ($request->isGet) {
  472. $upgrade_condition = UpgradeCondition::getData(UpgradeCondition::$achievement_condition_ids);
  473. $data['upgrade_condition'] = $upgrade_condition;
  474. $upgrade_condition = \Yii::$app->services->setting->addons->get($this->mall_id, AchievementEnum::ADDONS_NAME, 'upgrade_condition.achievement_upgrade_condition');
  475. $data['checked_condition_keys'] = $upgrade_condition ? json_decode($upgrade_condition, true) : [];
  476. return $this->success('操作成功', $data);
  477. } elseif ($request->isPost) {
  478. $params = \Yii::$app->request->post();
  479. $data['upgrade_condition'] = ['achievement_upgrade_condition' => $params['checked_condition_keys'] ?? []];
  480. \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, $data);
  481. return $this->success('操作成功');
  482. }
  483. }
  484. } catch (\Exception $e) {
  485. return $this->error($e->getMessage(), []);
  486. }
  487. } else {
  488. return $this->render('condition-setting');
  489. }
  490. }
  491. public function actionUserList()
  492. {
  493. if (\Yii::$app->request->isAjax) {
  494. if (\Yii::$app->request->isGet) {
  495. $get = \Yii::$app->request->get();
  496. $get['mall_id'] = $this->mall_id;
  497. $params = [];
  498. $user_id_arr = [];
  499. $is_search = 0;
  500. if (!empty($get['user_id'])) {
  501. $is_search = 1;
  502. $user_id_arr[] = $get['user_id'];
  503. }
  504. if (!empty($get['keyword'])) {
  505. $is_search = 1;
  506. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  507. 'or',
  508. ['like', 'username', $get['keyword']],
  509. ['like', 'nickname', $get['keyword']],
  510. ['like', 'mobile', $get['keyword']]
  511. ]], 'select' => 'id']);
  512. $user_ids = array_column($user_list, 'id');
  513. if (!empty($user_id_arr)) {
  514. $user_id_arr = array_intersect($user_id_arr, $user_ids);
  515. } else {
  516. $user_id_arr = $user_ids;
  517. }
  518. }
  519. if ($is_search) $params['where'][] = ['in', 'user_id', $user_id_arr];
  520. $params['where'][] = ['=', 'mall_id', $this->mall_id];
  521. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  522. $params['limit'] = 10;
  523. $params['order'] = 'id DESC';
  524. $pageData = AchievementUser::listPage($params, false);
  525. if ($pageData === false) return $this->error(AchievementUser::getStaticError());
  526. if (!empty($pageData['list'])) {
  527. $user_id_arr = array_column($pageData['list'], 'user_id');
  528. $user_list = $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'select' => 'id,nickname,avatar_url,mobile,parent_id']);
  529. $user_arr = $parent_arr = [];
  530. $parent_id_arr = [];
  531. foreach ($user_list as $item) {
  532. $parent_id_arr[] = $item['parent_id'];
  533. $user_arr[$item['id']] = $item;
  534. }
  535. $user_parent_list = $user_list = User::lists(['where' => [['id' => $parent_id_arr]], 'select' => 'id,nickname,avatar_url,mobile']);
  536. foreach ($user_parent_list as $item) {
  537. $parent_arr[$item['id']] = $item;
  538. }
  539. foreach ($pageData['list'] as &$v) {
  540. $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
  541. if (isset($user_arr[$v['user_id']])) {
  542. $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
  543. $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
  544. $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
  545. $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
  546. }
  547. }
  548. }
  549. return $this->success('操作成功', $pageData);
  550. }
  551. }
  552. return $this->render('user-list');
  553. }
  554. public function actionEdit()
  555. {
  556. try {
  557. $request = Yii::$app->request;
  558. if ($request->isAjax) {
  559. if ($request->isPost) {
  560. $params = Yii::$app->request->post();
  561. $res = Yii::$app->services->validate->validate($params, [
  562. [['user_id'], 'required'],
  563. ]);
  564. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  565. $params['mall_id'] = $this->mall_id;
  566. $res = AchievementUser::getOne([['mall_id' => $this->mall_id], ['user_id' => $params['user_id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]);
  567. if ($res) return $this->error('该会员已是团长');
  568. $params['is_manual'] = 1;
  569. $res = AchievementUser::setData($params);
  570. if ($res === false) return $this->error(AchievementUser::getStaticError());
  571. $data = [
  572. 'mall_id' => $this->mall_id,
  573. 'user_id' => $params['user_id'],
  574. ];
  575. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $data, AchievementLogic::class, 'fillParentTeam');
  576. return $this->success('操作成功');
  577. }
  578. }
  579. } catch (\Exception $e) {
  580. return $this->error($e->getMessage(), []);
  581. }
  582. return $this->render('edit');
  583. }
  584. //不用验证权限
  585. public function actionSearchUser()
  586. {
  587. if (\Yii::$app->request->isAjax) {
  588. $keyword = \Yii::$app->request->get('keyword', '');
  589. $list['list'] = User::searchUser(['keyword' => $keyword, 'mall_id' => $this->mall_id]);
  590. return $this->success('操作成功', $list);
  591. }
  592. return $this->error();
  593. }
  594. public function actionDelete()
  595. {
  596. if (\Yii::$app->request->isAjax) {
  597. if (\Yii::$app->request->isPost) {
  598. $params = \Yii::$app->request->post();
  599. $res = Yii::$app->services->validate->validate($params, [
  600. [['id'], 'required'],
  601. [['id'], 'integer']
  602. ]);
  603. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  604. $params['mall_id'] = $this->mall_id;
  605. $params['status'] = StatusEnum::DELETE;
  606. $res = AchievementUser::setData($params);
  607. if ($res === false) return $this->error(AchievementUser::getStaticError());
  608. $data = [];
  609. $event = new AchievementUserDelEvent($this->mall_id);
  610. $data['id'] = $params['id'];
  611. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  612. return $this->success('操作成功');
  613. }
  614. }
  615. return $this->error();
  616. }
  617. /**
  618. * 获取订单列表
  619. * @DateTime: 2023/04/12 15:53
  620. * @Copyright: copyright (c) 2021 xuan-e
  621. * @return mixed|void
  622. */
  623. public function actionOrderList()
  624. {
  625. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  626. $params = Yii::$app->request->get();
  627. $params['where'][] = ['mall_id' => $this->mall_id];
  628. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  629. $params['where'][] = ['grant_id' => $params['grant_id']];
  630. $params['where'][] = ['is_alone_bonus' => [StatusEnum::ENABLED]];
  631. $params['order'] = 'id desc';
  632. $list = AchievementOrderLog::listPage($params);
  633. return $this->success('操作成功', $list);
  634. }
  635. }
  636. public function actionTest()
  637. {
  638. $data = [
  639. 'id' => 65749,
  640. ];
  641. $res = (new OrderPayRewardListener())->handleSync($data);
  642. print_r($res);exit;
  643. }
  644. public function actionToGrantAchievement()
  645. {
  646. try {
  647. if (\Yii::$app->request->isAjax) {
  648. if (\Yii::$app->request->isPost) {
  649. $lock_key = RedisKeyEnum::suffix('lock_grant_achievement');
  650. $identification = uniqid();
  651. if (!LockHelper::lock($lock_key, $identification, 60, 10)) throw new \Exception('结算业绩处理阶段,请勿重复提交');
  652. $params = \Yii::$app->request->post();
  653. $res = Yii::$app->services->validate->validate($params, [
  654. [['id'], 'required'],
  655. [['id'], 'integer']
  656. ]);
  657. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  658. $params['mall_id'] = $this->mall_id;
  659. $now = time();
  660. $grant = AchievementGrantLog::find()
  661. ->where([
  662. 'is_wait' => 1,
  663. 'is_settlement' => 0,
  664. 'status' => StatusEnum::ENABLED,
  665. 'id' => $params['id'],
  666. 'mall_id' => $this->mall_id,
  667. ])
  668. ->andWhere(['<=', 'settlement_time', $now])
  669. ->asArray()
  670. ->one();
  671. if (!$grant) {
  672. return $this->error('周期数据不存在');
  673. }
  674. //结算业绩
  675. // AchievementLogic::grantAchievement($grant['id']);
  676. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $grant, AchievementLogic::class, 'grantAchievement');
  677. //添加审核记录
  678. AchievementGrantConfirmLog::setData([
  679. 'mall_id' => $this->mall_id,
  680. 'grant_id' => $grant['id'],
  681. 'admin_id' => $this->admin['id']
  682. ]);
  683. LockHelper::uLock($lock_key, $identification);
  684. return $this->success('操作成功');
  685. }
  686. }
  687. } catch (\Exception $exception) {
  688. return $this->error($exception->getMessage());
  689. }
  690. }
  691. }