LevelController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace addons\CloudStockTwo\backend\controllers;
  3. use addons\Agent\common\enums\AgentEnum;
  4. use addons\Agent\common\models\AgentLevel;
  5. use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
  6. use addons\CloudStockTwo\common\models\CloudStockTwoAgentQuota;
  7. use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
  8. use addons\CloudStockTwo\common\models\CloudStockTwoSms;
  9. use common\enums\AddonsEnum;
  10. use common\enums\StatusEnum;
  11. use common\models\goods\Goods;
  12. use common\models\user\User;
  13. use Yii;
  14. use common\controllers\AddonsController;
  15. /**
  16. * 默认控制器
  17. *
  18. * Class DefaultController
  19. * @package addons\CloudStockTwo\backend\controllers
  20. */
  21. class LevelController extends BaseController
  22. {
  23. public $enableCsrfValidation = false;
  24. public function actionIndex()
  25. {
  26. if (\Yii::$app->request->isAjax) {
  27. $where[] = ['=', 'mall_id', $this->mall_id];
  28. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  29. $params = [
  30. 'where' => $where, 'order' => 'level desc'
  31. ];
  32. $page_data = CloudStockTwoLevel::listPage($params, false);
  33. if (!empty($page_data['list'])) {
  34. foreach ($page_data['list'] as &$item) {
  35. $item['give_quota'] = json_decode($item['give_quota'], true);
  36. $item['give_quota_count'] = count($item['give_quota']) > 0 ? 1 : 0;
  37. }
  38. }
  39. return $this->success('操作成功', $page_data);
  40. }
  41. return $this->render('index', [
  42. ]);
  43. }
  44. public function actionEdit()
  45. {
  46. try {
  47. $request = Yii::$app->request;
  48. if ($request->isAjax) {
  49. if ($request->isGet) {
  50. $id = Yii::$app->request->get('id', '');
  51. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic');
  52. $data = CloudStockTwoLevel::getOne([['id' => $id], ['mall_id' => $this->mall_id], ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]], true);
  53. $configs['setting'] = $setting;
  54. if (!empty($data)) {
  55. $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['status'=>StatusEnum::ENABLED]],'select'=>'level,name','index'=>'level']);
  56. $data['checked_condition_values'] = json_decode($data['checked_condition_values'], true);
  57. $data['checked_condition_keys'] = json_decode($data['checked_condition_keys'], true);
  58. $data['agent_setting_info'] = json_decode($data['agent_setting_info'],true) ?? [];
  59. $data['commission'] = json_decode($data['commission'], true);
  60. if(!$data['commission']){
  61. $data['commission'] = [
  62. 'is_percent_team'=>0,
  63. 'is_percent_equal'=>0,
  64. 'agent_team_prize'=>0,
  65. 'agent_equal_prize'=>0,
  66. 'agent_over_prize'=>0,
  67. ];
  68. }
  69. $data['score'] = json_decode($data['score'], true);
  70. if(!$data['score']){
  71. $data['score'] = [
  72. 'is_percent_team'=>0,
  73. 'is_percent_equal'=>0,
  74. 'agent_team_prize'=>0,
  75. 'agent_equal_prize'=>0,
  76. 'agent_over_prize'=>0,
  77. ];
  78. }
  79. $data['give_quota'] = json_decode($data['give_quota'], true);
  80. if(!empty($data['give_quota'] )&&is_array($data['give_quota'] )){
  81. foreach ($data['give_quota'] as $key=>&$item){
  82. if(isset($level_list[$item['level']])&&$data['level']>$item['level']){
  83. $item['name'] = $level_list[$item['level']]['name'];
  84. $item['is_use'] = 1;
  85. }else{
  86. $item['is_use'] = 0;
  87. }
  88. }
  89. }
  90. if(!empty($data['checked_condition_values'])){
  91. if(isset($data['checked_condition_values'][0]['value'])){
  92. $goods_ids = array_column($data['checked_condition_values'][0]['value'],'goods_id');
  93. $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name,price,stock','index'=>'id']);
  94. foreach ($data['checked_condition_values'][0]['value'] as&$item){
  95. if(isset($goods_list[$item['goods_id']])){
  96. $item['cover_pic'] = $goods_list[$item['goods_id']]['cover_pic'];
  97. $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
  98. $item['price'] = $goods_list[$item['goods_id']]['price'];
  99. $item['stock'] = $goods_list[$item['goods_id']]['stock'];
  100. }
  101. }
  102. }
  103. if(isset($data['checked_condition_values'][1]['value'])){
  104. $level_arr = array_column($data['checked_condition_values'][1]['value'],'level');
  105. $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
  106. foreach ($data['checked_condition_values'][1]['value'] as &$val){
  107. if(isset($level_list[$val['level']])){
  108. $val['name'] = $level_list[$val['level']]['name'];
  109. }
  110. }
  111. }
  112. if(isset($data['checked_condition_values'][2]['value'])){
  113. $level_arr = array_column($data['checked_condition_values'][2]['value'],'level');
  114. $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
  115. foreach ($data['checked_condition_values'][2]['value'] as &$val){
  116. if(isset($level_list[$val['level']])){
  117. $val['name'] = $level_list[$val['level']]['name'];
  118. }
  119. }
  120. }
  121. if(isset($data['checked_condition_values'][4]['value'])){
  122. $level_arr = array_column($data['checked_condition_values'][4]['value'],'level');
  123. $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
  124. foreach ($data['checked_condition_values'][4]['value'] as &$val){
  125. if(isset($level_list[$val['level']])){
  126. $val['name'] = $level_list[$val['level']]['name'];
  127. }
  128. }
  129. }
  130. }
  131. $data['fill_min'] =$data['fill_min']??0;
  132. $data['is_open_fill_min'] =$data['is_open_fill_min']??0;
  133. }
  134. return $this->success('操作成功', $data);
  135. }
  136. if ($request->isPost) {
  137. $params = Yii::$app->request->post();
  138. // $res = Yii::$app->services->validate->validate($params, [
  139. // [['level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'buy_goods_type', 'goods_type', 'status'], 'integer'],
  140. // [['name'], 'string', 'max' => 45],
  141. // [['explain'], 'string', 'max' => 5000],
  142. // [['id', 'checked_condition_values', 'checked_condition_keys', 'score', 'commission', 'goods_ids', 'goods_list'], 'safe']
  143. // ]);
  144. // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  145. $params['mall_id'] = $this->mall_id;
  146. $res = CloudStockTwoLevel::setData($params);
  147. if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
  148. return $this->success('操作成功');
  149. }
  150. }
  151. } catch (\Exception $e) {
  152. return $this->error($e->getMessage(), []);
  153. }
  154. return $this->render('edit');
  155. }
  156. public function actionUpdateStatus()
  157. {
  158. $params = Yii::$app->request->post();
  159. if (!in_array($params['status'], [StatusEnum::ENABLED, StatusEnum::DISABLED])) {
  160. return $this->error('参数不对');
  161. }
  162. $params['mall_id'] = $this->mall_id;
  163. $res = CloudStockTwoLevel::setData($params);
  164. if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
  165. return $this->success('操作成功');
  166. }
  167. public function actionDel()
  168. {
  169. $params = Yii::$app->request->post();
  170. $params['mall_id'] = $this->mall_id;
  171. $params['status'] = StatusEnum::DELETE;
  172. $res = CloudStockTwoLevel::setData($params);
  173. if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
  174. return $this->success('操作成功');
  175. }
  176. //不用验证权限
  177. public function actionGetCanChooseLevelList()
  178. {
  179. $params = Yii::$app->request->get();
  180. $level = isset($params['level']) ? $params['level'] : null;
  181. $can_choose_list = CloudStockTwoLevel::find()->select('level')->where([
  182. 'mall_id' => $this->mall_id,
  183. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  184. ])->column();
  185. $new_list = [];
  186. for ($i = 1; $i <= 10; $i++) {
  187. $can_choose = 1;
  188. //已经存在的等级为false
  189. if (in_array($i, $can_choose_list)) $can_choose = 0;
  190. //有指定等级,比指定等级小的等级为false
  191. if ($level && $i < $level) $can_choose = 0;
  192. //有指定等级,指定等级为true
  193. if ($level && $i == $level) $can_choose = 1;
  194. $new_list[] = [
  195. 'name' => "等级" . $i,
  196. 'level' => $i,
  197. 'is_can_choose' => $can_choose,
  198. ];
  199. }
  200. return $this->success('操作成功', $new_list);
  201. }
  202. //不用验证权限
  203. public function actionGetLessLevelList()
  204. {
  205. $params = Yii::$app->request->get();
  206. $level = isset($params['level']) ? $params['level'] : 10;
  207. $use_list = CloudStockTwoLevel::find()->select('level')->where([
  208. 'mall_id' => $this->mall_id,
  209. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  210. ])->column();
  211. $new_list = [];
  212. $list = CloudStockTwoLevel::lists(
  213. [
  214. 'where' => [
  215. ['mall_id' => $this->mall_id,],
  216. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  217. ],
  218. 'select' => 'name,level',
  219. 'index' => 'level'
  220. ]
  221. );
  222. for ($i = 1; $i <= 10; $i++) {
  223. if (in_array($i, $use_list) && $i <= $level) {
  224. $is_use = 1;
  225. } else {
  226. $is_use = 0;
  227. }
  228. $new_list[] = [
  229. 'name' => $list[$i]['name'] ?? '等级不存在',
  230. 'level' => $i,
  231. 'is_use' => $is_use,
  232. ];
  233. }
  234. return $this->success('操作成功', $new_list);
  235. }
  236. public function actionAgentQuota()
  237. {
  238. if (\Yii::$app->request->isAjax) {
  239. $get = Yii::$app->request->get();
  240. $where[] = ['=', 'mall_id', $this->mall_id];
  241. $where[] = ['=', 'status', StatusEnum::ENABLED];
  242. if (!empty($get['keyword'])) {
  243. $user_list = User::find()->where(['or',
  244. ['id' => $get['keyword']],
  245. ['like', 'mobile', $get['keyword']],
  246. ['like', 'nickname', $get['keyword']]])->select('id')->asArray()->all();
  247. $user_id_arr = array_column($user_list, 'id');
  248. $where[] = ['user_id' => $user_id_arr];
  249. }
  250. if (!empty($get['level'])) $where[] = ['level' => $get['level']];
  251. $mall_id = $this->mall_id;
  252. $params = [
  253. 'where' => $where,
  254. 'with' => ['stockLevel'=>function($query)use($mall_id){
  255. $query->where(['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'mall_id'=>$mall_id]);
  256. }, 'user']
  257. ];
  258. $page_data = CloudStockTwoAgent::listPage($params, false);
  259. if (!empty($page_data['list'])) {
  260. foreach ($page_data['list'] as &$item) {
  261. $item['agent_quota_list'] = CloudStockTwoAgentQuota::lists([
  262. 'where' => [
  263. ['user_id' => $item['user_id']],
  264. ['status'=>StatusEnum::ENABLED]
  265. ],
  266. 'with' => [
  267. 'stockLevel'=>function($query)use($mall_id){
  268. $query->where(['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'mall_id'=>$mall_id]);
  269. }
  270. ]
  271. ], true);
  272. }
  273. }
  274. return $this->success('操作成功', $page_data);
  275. }
  276. return $this->render('agent-quota');
  277. }
  278. public function actionGetLevelList()
  279. {
  280. $data = CloudStockTwoLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]]], true);
  281. return $this->success('操作成功', $data);
  282. }
  283. }