StoreAuditController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace addons\Store\backend\controllers;
  3. use addons\BusinessBonus\common\events\user\UpgradeUserEvent;
  4. use addons\Store\common\enums\StoreEnum;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreCate;
  7. use addons\Store\common\models\StoreClerk;
  8. use addons\Store\common\models\StoreCommunity;
  9. use addons\Store\common\models\StoreGoodsCate;
  10. use addons\Store\common\models\StoreSettings;
  11. use common\enums\AddonsEnum;
  12. use common\enums\StatusEnum;
  13. use common\models\common\Region;
  14. use common\models\mall\MallAddons;
  15. use common\models\user\Town;
  16. use common\models\user\User;
  17. use addons\Store\common\logic\StoreCreateSync;
  18. use common\models\backend\Admin;
  19. class StoreAuditController extends BaseController
  20. {
  21. public function actionIndex()
  22. {
  23. if (!\Yii::$app->request->isAjax) {
  24. $upload_license_image = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter.upload_license_image');
  25. if(empty($upload_license_image)) $upload_license_image= 0;
  26. return $this->render('/store-audit/index',['upload_license_image'=>$upload_license_image]);
  27. }
  28. if (\Yii::$app->request->isGet) {
  29. $page = \Yii::$app->request->get('page', 1);
  30. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  31. $tabname = \Yii::$app->request->get('tabname', 'waiting-audit');
  32. if (!in_array($tabname, ['waiting-audit', 'reject', 'record'])) {
  33. return $this->error('参数错误');
  34. }
  35. if ('waiting-audit' == $tabname) {
  36. $check_status = StoreEnum::CHECK_WAITING;
  37. } else if ('reject' == $tabname){
  38. $check_status = StoreEnum::CHECK_FAILED;
  39. } else {
  40. $check_status = StoreEnum::CHECK_ADOPT;
  41. }
  42. $store_name = \Yii::$app->request->get('store_name');
  43. $cate_id = \Yii::$app->request->get('cate_id');
  44. $storeowner = \Yii::$app->request->get('storeowner');
  45. $nickname = \Yii::$app->request->get('nickname');
  46. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => $check_status];
  47. if (!empty($cate_id)) {
  48. $wheres[] = ['c_id' => $cate_id];
  49. }
  50. if (!empty($store_name)) {
  51. $wheres[] = ['like', 'store_name', $store_name];
  52. }
  53. if (!empty($storeowner)) {
  54. $wheres[] = ['like', 'shop_owner', $storeowner];
  55. }
  56. if (!empty($nickname)) {
  57. $users = User::find()
  58. ->select('id,nickname,mobile,avatar_url')
  59. ->where(['like', 'nickname', $nickname])
  60. ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  61. ->asArray()
  62. ->indexBy('id')
  63. ->all();
  64. $user_ids = array_column($users, 'id');
  65. $wheres[] = ['user_id' => $user_ids];
  66. }
  67. $params = [
  68. 'select' => 'id,mall_id,user_id,shop_owner,shop_mobile,store_name,c_id,province_id,city_id,district_id,address,logo_img,check_status,created_at,license_img,street_id,community_id,is_examine',
  69. 'where' => $wheres,
  70. 'order' => 'created_at desc',
  71. 'page' => $page,
  72. 'limit' => $limit,
  73. ];
  74. $store_list = Store::listPage($params);
  75. if (false === $store_list) {
  76. return $this->error(Store::getStaticError());
  77. }
  78. if (!empty($store_list['list'])) {
  79. if (empty($users)) {
  80. $user_ids = array_unique(array_column($store_list['list'], 'user_id'));
  81. $users = User::find()
  82. ->select('id,nickname,mobile,avatar_url')
  83. ->where(['id' => $user_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  84. ->asArray()
  85. ->indexBy('id')
  86. ->all();
  87. }
  88. $cate_ids = array_unique(array_column($store_list['list'], 'c_id'));
  89. $categorys = StoreCate::find()
  90. ->select('id,name')
  91. ->where(['id' => $cate_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  92. ->asArray()
  93. ->indexBy('id')
  94. ->all();
  95. $region_ids = [];
  96. $province_ids = array_unique(array_column($store_list['list'], 'province_id'));
  97. $city_ids = array_unique(array_column($store_list['list'], 'city_id'));
  98. $district_ids = array_unique(array_column($store_list['list'], 'district_id'));
  99. $street_ids = array_unique(array_column($store_list['list'], 'street_id'));
  100. $community_ids = array_unique(array_column($store_list['list'], 'community_id'));
  101. $store_ids = array_unique(array_column($store_list['list'], 'id'));
  102. $region_ids = array_merge($province_ids, $city_ids, $district_ids);
  103. $regions = Region::find()
  104. ->select('id,name')
  105. ->where(['id' => $region_ids])
  106. ->asArray()
  107. ->indexBy('id')
  108. ->all();
  109. $streets = town::find()
  110. ->select('id,name')
  111. ->where(['id'=>$street_ids])
  112. ->asArray()
  113. ->indexBy('id')
  114. ->all();
  115. $communitys = StoreCommunity::find()
  116. ->select('id,community_name')
  117. ->where(['mall_id'=>$this->mall_id,'id'=>$community_ids])
  118. ->asArray()
  119. ->indexBy('id')
  120. ->all();
  121. $examineList = array_column($store_list['list'], 'is_examine');
  122. $validExamineIds = array_unique(array_filter($examineList, function($id) {return $id !== null;}));
  123. $admins = [];
  124. if (!empty($validExamineIds)) {
  125. $admins = Admin::find()->where(['id' => $validExamineIds])->indexBy('id') ->asArray()->all();
  126. }
  127. $adminList = [];
  128. foreach ($examineList as $examineId) {
  129. $adminList[] = $examineId !== null ? ($admins[$examineId] ?? null) : null;
  130. }
  131. $store_setting = StoreSettings::find()
  132. ->where([
  133. 'store_id' => $store_ids,
  134. 'status' => StatusEnum::ENABLED
  135. ])
  136. ->indexBy('store_id')
  137. ->all();
  138. foreach ($store_list['list'] as $k => &$list) {
  139. $user = $users[$list['user_id']] ?? [];
  140. $category = $categorys[$list['c_id']] ?? [];
  141. $province = $regions[$list['province_id']] ?? [];
  142. $city = $regions[$list['city_id']] ?? [];
  143. $district = $regions[$list['district_id']] ?? [];
  144. $streetInfo = $streets[$list['street_id']] ?? [];
  145. if($list['community_id'] == '-1'){
  146. $CommunityInfo = '其它社区';
  147. }else{
  148. if(isset($communitys[$list['community_id']])){
  149. $CommunityInfo = $communitys[$list['community_id']]['community_name'];
  150. }else{
  151. $CommunityInfo = '';
  152. }
  153. }
  154. $list['nickname'] = $user['nickname'] ?? '';
  155. $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  156. $list['category'] = $category['name'] ?? '';
  157. //判断详细地址里面是否有省市区,如果有的话,那么就只拼接社区
  158. preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $list['address'], $matches);
  159. if(count($matches) > 1){
  160. $list['store_address'] = $list['address'];
  161. }else{
  162. $list['store_address'] = ($province['name'] ?? '') . ($city['name'] ?? '') . ($district['name'] ?? ''). ($streetInfo['name'] ?? ''). ($CommunityInfo ?? '') . $list['address'];
  163. }
  164. if ($list['check_status'] == StoreEnum::CHECK_WAITING) {
  165. $list['check_status'] = 1;
  166. }
  167. if (isset($list['is_examine']) && $list['is_examine'] !== null) {
  168. if($list['is_examine'] == -1){
  169. $list['is_examine'] = '后台添加';
  170. } else if ($list['is_examine'] == 0){
  171. $list['is_examine'] = '直接通过';
  172. } else{
  173. $list['is_examine'] = 'id:'.$adminList[$k]['id'].','.$adminList[$k]['username'];
  174. }
  175. }
  176. $list['store_service_ratio'] = 0;
  177. if (isset($store_setting[$list['id']]) && $store_setting[$list['id']]['store_service_fee']) {
  178. $list['store_service_ratio'] = $store_setting[$list['id']]['store_service_ratio'];
  179. }
  180. }
  181. }
  182. $cate_list = StoreCate::find()
  183. ->select('id,name')
  184. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  185. ->asArray()
  186. ->all();
  187. $store_list['cate_list'] = $cate_list;
  188. return $this->success('success', $store_list);
  189. }
  190. }
  191. /**
  192. * @name:
  193. * @msg: 门店审核
  194. * @param {*}
  195. * @return {*}
  196. */
  197. public function actionAudit()
  198. {
  199. if (!\Yii::$app->request->isPost) {
  200. return $this->error('请求方式错误');
  201. }
  202. $post = \Yii::$app->request->post();
  203. if (!in_array($post['action'], ['adopt', 'reject'])) {
  204. return $this->error('参数错误');
  205. }
  206. $rules = [
  207. [['id', 'action'], 'required'],
  208. ['id', 'integer'],
  209. [['action', 'check_remark'], 'string'],
  210. ['check_remark', 'safe'],
  211. ['check_remark', 'string', 'max' => 255],
  212. ];
  213. $res = \Yii::$app->services->validate->validate($post, $rules);
  214. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  215. if ('adopt' == $post['action']) {
  216. $check_status = StoreEnum::CHECK_ADOPT;
  217. $is_freeze = StoreEnum::FREEZE_NO;
  218. $is_admin = 1;
  219. } else {
  220. $check_status = StoreEnum::CHECK_FAILED;
  221. }
  222. $params = [
  223. 'mall_id' => $this->mall_id,
  224. 'id' => $post['id'],
  225. 'check_status' => $check_status,
  226. 'check_remark' => $post['check_remark'] ?? '',
  227. ];
  228. if(isset($is_freeze)){
  229. $params['is_freeze'] = $is_freeze;
  230. }
  231. if(isset($is_admin)){
  232. $params['is_examine'] = $this->admin->id;
  233. }
  234. $enter = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  235. if($enter['is_template'] && $enter['is_front_template']) {
  236. $params['is_sync'] = 1;
  237. }
  238. $store = Store::setData($params);
  239. if (false === $store) {
  240. return $this->error(Store::getStaticError());
  241. }
  242. if ('adopt' == $post['action']) { // 订单为审核通过 则删除前端保留缓存
  243. //删除草稿缓存
  244. $res = \Yii::$app->cache->get('draft_store_apply');
  245. if(isset($res[$this->mall_id][$store['user_id']]))
  246. {
  247. unset($res[$this->mall_id][$store['user_id']]);
  248. \Yii::$app->cache->set('draft_store_apply', $res);
  249. }
  250. }
  251. $cate = StoreCate::findOne(['id'=>$store['c_id']]);
  252. if(!empty($cate['is_collect_service_fee'])&&!empty($store['check_status'])&&$store['check_status']==1){
  253. $exist_setting = StoreSettings::find()
  254. ->where([
  255. 'store_id' => $store['id'],
  256. ])->one();
  257. $cashier_info = [
  258. 'store_service_fee' => $cate['is_collect_service_fee'],
  259. 'store_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'],
  260. 'cachier_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'],
  261. 'store_settle_type' => 1,
  262. 'store_settle_scene' =>1,
  263. 'store_id' => $store['id'],
  264. ];
  265. StoreSettings::setData($this->mall_id,$cashier_info);
  266. }
  267. if($check_status==StoreEnum::CHECK_ADOPT){
  268. $add_cate = [
  269. 'mall_id'=>$this->mall_id,
  270. 'store_id'=>$post['id'],
  271. 'parent_id'=>0,
  272. 'name'=>'全部',
  273. 'pic'=>'',
  274. 'big_pic'=>'',
  275. 'advert_pic'=>'',
  276. 'advert_url'=>'',
  277. 'advert_open_type'=>'',
  278. 'advert_params'=>'',
  279. 'is_show'=>1,
  280. ];
  281. $res = StoreGoodsCate::setData($add_cate);
  282. if(!empty($store)){
  283. $params = [
  284. 'mall_id'=>$store['mall_id'],
  285. 'user_id'=>$store['user_id'],
  286. 'store_id'=>$store['id'],
  287. 'remark'=>'门店审核通过添加',
  288. ];
  289. $res = StoreClerk::setData($params);
  290. }
  291. //处理复制数据,判断是否开启模板门店、前端入驻同步模板门店
  292. if ($enter['is_template'] && $enter['is_front_template']) {
  293. \Yii::$app->services->rabbitMq->delay(10, [
  294. 'mall_id' => $this->mall_id,
  295. 'self_store_id'=> $store['id'],
  296. ], StoreCreateSync::class, 'synStoreData');
  297. }
  298. //触发招商分红用户条件升级
  299. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
  300. $store_info = Store::find()
  301. ->where(['id' => $post['id']])
  302. ->one();
  303. $event = new UpgradeUserEvent($this->mall_id);
  304. $data = ['mall_id' => $this->mall_id, 'user_id' => $store_info['user_id']];
  305. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  306. }
  307. }
  308. $key = StoreEnum::STORE_WAITING_AUDIT_STORE_NUMBERS . ':' . $this->mall_id;
  309. \Yii::$app->redis->decrby($key, 1);//待审核门店
  310. return $this->success('操作成功');
  311. }
  312. }