PoolController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. namespace addons\DoubleCopy\backend\controllers;
  3. use addons\DoubleCopy\common\enums\DoubleCopyEnum;
  4. use addons\DoubleCopy\common\models\DoubleCopyPoolLevel;
  5. use addons\DoubleCopy\common\models\DoubleCopyPoolLog;
  6. use addons\DoubleCopy\common\models\DoubleCopyPoolSend;
  7. use addons\DoubleCopy\common\models\DoubleCopyPoolSource;
  8. use addons\DoubleCopy\common\models\DoubleCopyPoolUser;
  9. use common\enums\StatusEnum;
  10. use common\models\goods\Goods;
  11. use common\models\user\User;
  12. use Yii;
  13. use yii\helpers\Json;
  14. /**
  15. * Class PoolController
  16. * @package addons\DoubleCopy\backend\controllers
  17. */
  18. class PoolController extends BaseController
  19. {
  20. public $enableCsrfValidation = false;
  21. /**
  22. * @return string|\yii\web\Response
  23. */
  24. public function actionIndex()
  25. {
  26. return $this->render('index');
  27. }
  28. /**
  29. * @return mixed|void
  30. * @throws \Exception
  31. */
  32. public function actionLevel()
  33. {
  34. $retuest = Yii::$app->request;
  35. if ($retuest->isAjax && $retuest->isGet) {
  36. // 检查提交的参数
  37. $get = $retuest->get();
  38. $res = Yii::$app->services->validate->validate($get,[
  39. [['page','limit'], 'integer'],
  40. [['keyword'], 'string']
  41. ]);
  42. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  43. $where = [];
  44. $where[] = ['=', 'mall_id', $this->mall_id];
  45. $where[] = ['<>', 'status', StatusEnum::DELETE];
  46. $order = 'level asc';
  47. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
  48. $list = DoubleCopyPoolLevel::listPage($params, false, true);
  49. if (empty($list)) return $this->error('获取失败');
  50. //基础设置:当前奖励类型
  51. $config = Yii::$app->services->setting->addons->get($this->mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic');
  52. $reward_currency = $config['reward_currency'] ?? 'commission';
  53. foreach ($list['list'] as &$item) {
  54. $item['reward_currency'] = $reward_currency == 'score' ? '积分' : '佣金';
  55. if($reward_currency == 'score'){
  56. $item['current_money'] = $item['current_score'];
  57. $item['total_money'] = $item['total_score'];
  58. }
  59. // 统计当前等级人数
  60. $item['bonus_num'] = DoubleCopyPoolUser::find()->where(['mall_id' => $this->mall_id, 'level' => $item['level'], 'status' => StatusEnum::ENABLED])->count();
  61. }
  62. return $this->success('获取成功', $list);
  63. }
  64. return $this->render($this->action->id);
  65. }
  66. /**
  67. * 删除处理
  68. * @Author: lun
  69. * @DateTime: 2022/9/16 0016 10:31
  70. * @Copyright: copyright (c) 2021 广东七件事集团
  71. * @return mixed|void
  72. */
  73. public function actionHandle()
  74. {
  75. $request = Yii::$app->request;
  76. if ($request->isAjax) {
  77. if ($request->isGet) {
  78. $params = Yii::$app->request->get();
  79. // 检查提交的参数
  80. $res = Yii::$app->services->validate->validate($params,[
  81. [['id'], 'required'],
  82. [['id','status'], 'integer'],
  83. ]);
  84. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  85. if (!isset($params['status'])) unset($params['status']);
  86. $params['mall_id'] = $this->mall_id;
  87. $res = DoubleCopyPoolLevel::setData($params);
  88. if (!$res) return $this->error('修改失败:msg=' . DoubleCopyPoolLevel::getStaticError());
  89. return $this->success('修改成功');
  90. }
  91. }
  92. }
  93. /**
  94. * 等级编辑
  95. * @Author: lun
  96. * @DateTime: 2021/10/21 0021 11:29
  97. * @Copyright: copyright (c) 2021 广东七件事集团
  98. * @return mixed|string|void
  99. */
  100. public function actionLevelEdit()
  101. {
  102. try {
  103. $request = Yii::$app->request;
  104. if ($request->isAjax) {
  105. // 查看等级详情
  106. if ($request->isGet) {
  107. $id = Yii::$app->request->get('id', '');
  108. $data = [];
  109. if(!empty($id)){
  110. // 获取等级信息
  111. $field = "id,name,level,achievement,single_market,goods_setting,status,is_enable_extra_pool,extra_pool_level";
  112. $model = DoubleCopyPoolLevel::getOne([['id' => $id, 'mall_id' => $this->mall_id]], true,[],false,$field);
  113. // 解析被选中的商品
  114. $goods_setting = Json::decode($model['goods_setting'],true) ?? [];
  115. if ($goods_setting) {
  116. $goods_ids = array_column($goods_setting, 'goods_id');
  117. $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name as name,price','index'=>'id']);
  118. foreach ($goods_setting as &$item) {
  119. if (empty($item['is_percent'])) $item['is_percent'] = 0;
  120. $item['goods'] = $goods_list[$item['goods_id']] ?? [];
  121. }
  122. }
  123. $model['goods_setting'] = $goods_setting;
  124. if (!empty($model['extra_pool_level'])) {
  125. $model['extra_pool_level'] = Json::decode($model['extra_pool_level']);
  126. } else {
  127. $model['extra_pool_level'] = [];
  128. }
  129. $data['detail'] = $model;
  130. }
  131. $data['weights'] = DoubleCopyPoolLevel::getLevelWeights($this->mall_id);
  132. return $this->success('操作成功', $data);
  133. }
  134. // 提交等级内容
  135. if ($request->isPost) {
  136. $params = Yii::$app->request->post('form');
  137. $res = Yii::$app->services->validate->validate($params, [
  138. [['level', 'name', 'achievement','goods_setting'], 'required'],
  139. [['id','level', 'status', 'is_enable_extra_pool'], 'integer'],
  140. [['achievement','single_market'], 'double'],
  141. [['name'], 'string'],
  142. [['goods_setting', 'extra_pool_level'], 'safe']
  143. ]);
  144. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  145. $params['mall_id'] = $this->mall_id;
  146. foreach ($params['goods_setting'] as $key => $item) {
  147. unset($params['goods_setting'][$key]['goods']);
  148. }
  149. $params['goods_setting'] = Json::encode($params['goods_setting']);
  150. $res = DoubleCopyPoolLevel::setData($params);
  151. if ($res === false) throw new \Exception(DoubleCopyPoolLevel::getStaticCodeError()['error']);
  152. return $this->success('操作成功');
  153. }
  154. }
  155. } catch (\Exception $e) {
  156. return $this->error($e->getMessage(), []);
  157. }
  158. return $this->render('level-edit');
  159. }
  160. /**
  161. * 分红用户
  162. * @Author: lun
  163. * @DateTime: 2022/9/16 0016 16:45
  164. * @Copyright: copyright (c) 2021 广东七件事集团
  165. * @return mixed|string|void
  166. */
  167. public function actionUser()
  168. {
  169. $retuest = Yii::$app->request;
  170. if ($retuest->isAjax && $retuest->isGet) {
  171. // 检查提交的参数
  172. $get = $retuest->get();
  173. $res = Yii::$app->services->validate->validate($get,[
  174. [['page','limit','user_id','mobile','level'], 'integer'],
  175. [['start_at','end_at'], 'safe'],
  176. ]);
  177. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  178. // 获取等级
  179. $level_map = DoubleCopyPoolLevel::getMap($this->mall_id);
  180. $where = $list = [];
  181. $where[] = ['=', 'mall_id', $this->mall_id];
  182. !empty($get['user_id']) && $where[] = ['=', 'user_id', $get['user_id']];
  183. !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
  184. if (!empty($get['mobile'])) {
  185. $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
  186. if (empty($user_id)) $this->success('获取成功',compact('list','level_map'));
  187. $where[] = ['=', 'user_id', $user_id];
  188. }
  189. if (!empty($get['start_at']) && !empty($get['end_at'])) {
  190. $where[] = ['between', 'created_at', strtotime($get['start_at']), strtotime($get['end_at'])];
  191. }
  192. $where[] = ['=', 'status', StatusEnum::ENABLED];
  193. $with = ['user'];
  194. $order = 'id desc,created_at desc';
  195. $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit']);
  196. $list = DoubleCopyPoolUser::listPage($params, false, true);
  197. if (empty($list)) return $this->error('获取失败');
  198. return $this->success('获取成功', compact('list','level_map'));
  199. }
  200. return $this->render($this->action->id);
  201. }
  202. /**
  203. * 用户分红记录
  204. * @Author: lun
  205. * @DateTime: 2022/9/16 0016 17:45
  206. * @Copyright: copyright (c) 2021 广东七件事集团
  207. * @return mixed|void
  208. */
  209. public function actionLog()
  210. {
  211. $retuest = Yii::$app->request;
  212. if ($retuest->isAjax && $retuest->isGet) {
  213. // 检查提交的参数
  214. $get = $retuest->get();
  215. $res = Yii::$app->services->validate->validate($get,[
  216. [['page','limit','user_id'], 'integer'],
  217. ]);
  218. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  219. $where = $list = [];
  220. $where[] = ['=', 'mall_id', $this->mall_id];
  221. !empty($get['user_id']) && $where[] = ['=', 'user_id', $get['user_id']];
  222. $where[] = ['=', 'status', StatusEnum::ENABLED];
  223. $order = 'created_at desc';
  224. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
  225. $list = DoubleCopyPoolLog::listPage($params, false, true);
  226. if (empty($list)) return $this->error('获取失败');
  227. return $this->success('获取成功', $list);
  228. }
  229. }
  230. /**
  231. * 分红发放记录
  232. * @Author: lun
  233. * @DateTime: 2022/9/16 0016 17:47
  234. * @Copyright: copyright (c) 2021 广东七件事集团
  235. * @return mixed|void
  236. */
  237. public function actionSend()
  238. {
  239. $retuest = Yii::$app->request;
  240. if ($retuest->isAjax && $retuest->isGet) {
  241. // 检查提交的参数
  242. $get = $retuest->get();
  243. $res = Yii::$app->services->validate->validate($get,[
  244. [['page','limit','level'], 'integer'],
  245. ]);
  246. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  247. $where = $list = [];
  248. $where[] = ['=', 'mall_id', $this->mall_id];
  249. !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
  250. $where[] = ['=', 'status', StatusEnum::ENABLED];
  251. $order = 'created_at desc';
  252. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
  253. $list = DoubleCopyPoolSend::listPage($params, false, true);
  254. if (empty($list)) return $this->error('获取失败');
  255. return $this->success('获取成功', $list);
  256. }
  257. }
  258. /**
  259. * 删除分红池用户跟分红记录
  260. * @Author: lun
  261. * @DateTime: 2023/7/19 0019 9:29
  262. * @Copyright: copyright (c) 2021 广东七件事集团
  263. * @return mixed|void
  264. */
  265. public function actionDelUser()
  266. {
  267. $retuest = Yii::$app->request;
  268. if ($retuest->isAjax && $retuest->isPost) {
  269. // 检查提交的参数
  270. $post = $retuest->post();
  271. $res = Yii::$app->services->validate->validate($get,[
  272. [['user_id'], 'integer'],
  273. ]);
  274. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  275. $res = DoubleCopyPoolUser::deleteUser($this->mall_id, $post['user_id']);
  276. if ($res === false) return $this->error('用户删除失败');
  277. return $this->success('已成功删除该用户,并删除已删除该用户的分红记录');
  278. }
  279. }
  280. public function actionEditUser()
  281. {
  282. if (\Yii::$app->request->isAjax) {
  283. if (\Yii::$app->request->isPost) {
  284. $postData = \Yii::$app->request->post();
  285. $res = Yii::$app->services->validate->validate($postData, [
  286. [['user_id', 'level'], 'required'],
  287. [['user_id', 'level','id'], 'integer'],
  288. ]);
  289. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  290. if($postData['id']){
  291. $wheres = [
  292. 'user_id' => $postData['user_id'],
  293. 'mall_id' => $this->mall_id,
  294. 'status' => StatusEnum::ENABLED,
  295. 'id' => $postData['id']
  296. ];
  297. $model = DoubleCopyPoolUser::findOne($wheres);
  298. if (empty($model)) return $this->error('修改会员不存在');
  299. if($model->level == $postData['level']) return $this->error('修改会员等级不能与原等级相同');
  300. if($model->level > $postData['level']) return $this->error('会员等级不能比原等级低');
  301. $postData['upgrade_level_at'] = time();
  302. }else{
  303. $wheres = [
  304. 'user_id' => $postData['user_id'],
  305. 'mall_id' => $this->mall_id,
  306. 'status' => StatusEnum::ENABLED,
  307. ];
  308. $model = DoubleCopyPoolUser::findOne($wheres);
  309. if (!empty($model)) return $this->error('该会员已存在,不能重复添加');
  310. }
  311. $postData['mall_id'] = $this->mall_id;
  312. $postData['status'] = StatusEnum::ENABLED;
  313. $postData['upgrade_status'] = DoubleCopyEnum::UPGRADE_STATUS_MANUAL;
  314. $res = DoubleCopyPoolUser::create($postData);
  315. if ($res === false) return $this->error(DoubleCopyPoolUser::getStaticError());
  316. return $this->success('操作成功');
  317. }
  318. }
  319. return $this->error();
  320. }
  321. public function actionEnableList()
  322. {
  323. if (\Yii::$app->request->isAjax) {
  324. if (\Yii::$app->request->isGet) {
  325. $level = \Yii::$app->request->get('level');
  326. $where = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  327. if (!empty($level)) $where[] = ['level' => $level];
  328. $args['where'] = $where;
  329. $args['select'] = 'id,level,name';
  330. $args['order'] = 'level asc';
  331. $list['list'] = DoubleCopyPoolLevel::lists($args, true);
  332. return $this->success('操作成功', $list);
  333. }
  334. }
  335. return $this->error();
  336. }
  337. /**
  338. * 分红池充值金额
  339. * @return mixed|null
  340. */
  341. public function actionRecharge()
  342. {
  343. $trans = Yii::$app->db->beginTransaction();
  344. try{
  345. $request = Yii::$app->request;
  346. $post = $request->post();
  347. $res = Yii::$app->services->validate->validate($post,[
  348. [['level_id'], 'integer'],
  349. [['level_id','num'], 'required'],
  350. [['desc'], 'string'],
  351. ]);
  352. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  353. $config = Yii::$app->services->setting->addons->get($this->mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic');
  354. $reward_currency = $config['reward_currency'] ?? 'commission';
  355. $level = DoubleCopyPoolLevel::find()->where(['id'=>$post['level_id'],'mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED])->asArray()->one();
  356. if(!$level) throw new \Exception('等级不存在');
  357. if($reward_currency == 'commission'){
  358. DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $level['level'], [
  359. 'current_money' => $post['num'],
  360. 'total_money' => $post['num'],
  361. ]);
  362. }else{
  363. DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $level['level'], [
  364. 'current_score' => $post['num'],
  365. 'total_score' => $post['num'],
  366. ]);
  367. }
  368. $res = DoubleCopyPoolSource::setData([
  369. 'mall_id' => $this->mall_id,
  370. 'level' => $level['level'],
  371. 'order_detail_id' => 0,
  372. 'money' => $post['num'],
  373. 'reward_currency' => $reward_currency,
  374. 'remark' => $post['desc'] ?? '',
  375. ]);
  376. if(!$res) throw new \Exception(DoubleCopyPoolSource::getStaticError());
  377. $trans->commit();
  378. return $this->success('操作成功');
  379. //ALTER TABLE `qimall_addons_double_copy_pool_source`
  380. //ADD COLUMN `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '备注' AFTER `reward_currency`;
  381. }catch (\Exception $e){
  382. $trans->rollBack();
  383. return $this->error($e->getMessage());
  384. }
  385. }
  386. public function actionLevelList()
  387. {
  388. $request = Yii::$app->request;
  389. $data = $request->get();
  390. $res = Yii::$app->services->validate->validate($data,[
  391. [['level'], 'integer'],
  392. [['level'], 'required'],
  393. ]);
  394. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  395. $list = DoubleCopyPoolLevel::lists([
  396. 'where' => [
  397. ['<', 'level', $data['level']],
  398. ['mall_id' => $this->mall_id],
  399. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  400. ],
  401. 'select'=> 'id, level, name',
  402. ]);
  403. return $this->success('获取成功', $list);
  404. }
  405. }