CardController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace addons\BusinessCard\backend\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\models\BusinessCard;
  5. use addons\BusinessCard\common\models\BusinessCardPosition;
  6. use addons\BusinessCard\common\models\BusinessCardTag;
  7. use addons\BusinessCard\common\models\BusinessCardUser;
  8. use addons\BusinessCard\common\models\BusinessCardUserTag;
  9. use common\models\common\PlatformSetting;
  10. use common\models\user\User;
  11. use common\models\goods\Goods;
  12. use common\enums\StatusEnum;
  13. use Exception;
  14. use Yii;
  15. class CardController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. /**
  19. * 名片列表
  20. *
  21. * @author hpei
  22. * @param string keyword
  23. * @return array|mixed
  24. */
  25. public function actionIndex() {
  26. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  27. $get = Yii::$app->request->get();
  28. $where = [['=', 'mall_id', $this->mall_id], ['<>', 'status' , StatusEnum::DELETE]];
  29. if (isset($get['keyword']) && !empty($get['keyword'])) {
  30. $where[] = ['or', ['=', 'user_id', $get['keyword']], ['=', 'mobile', $get['keyword']], ['like', 'nickname', $get['keyword']]];
  31. }
  32. $params = [
  33. 'limit' => 10,
  34. 'where' => $where,
  35. 'select' => ['id', 'user_id', 'nickname', 'mobile', 'avatar_url','share_num', 'position_id', 'like_num', 'browse_num'],
  36. 'order' => 'created_at desc',
  37. 'with' => ['user', 'cardUser']
  38. ];
  39. $list = BusinessCard::listPage($params);
  40. $Platform_config = PlatformSetting::getConfByGroup( 'system', true);
  41. $list['web_url'] = $Platform_config['h5_url']['value'];
  42. $list['mall_sign'] = $this->mall['mall_sign'];
  43. if (!empty($list['list'])) {
  44. foreach ($list['list'] as &$item) {
  45. $item['qrcode'] = '';
  46. $item['h5_url'] = "{$list['web_url']}/plugins2/BusinessCard/index?sign={$list['mall_sign']}&card_id={$item['id']}";
  47. }
  48. }
  49. $list['sum'] = Yii::$app->db->createCommand(BusinessCard::find()
  50. ->where(['mall_id' => $this->mall_id])
  51. ->andWhere(['status' => StatusEnum::ENABLED])
  52. ->select(["COALESCE(SUM(share_num), 0) as share",
  53. "COALESCE(SUM(browse_num), 0) as view",
  54. "COALESCE(SUM(like_num), 0) as like"])
  55. ->createCommand()->getRawSql())->queryAll()[0] ?? [0, 0, 0];
  56. $list['roles'] = CardEnums::getRoleTypeMap();
  57. return $this->success('操作成功', $list);
  58. }
  59. return $this->render('index');
  60. }
  61. /**
  62. * 客户管理
  63. *
  64. * @author hpei
  65. * @param string keyword
  66. * @return array|mixed
  67. */
  68. public function actionCardCustomer() {
  69. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  70. $where = [['=', 'mall_id', $this->mall_id], ['<>','status', StatusEnum::DELETE]];
  71. $get = Yii::$app->request->get();
  72. if (isset($get['keyword']) && !empty($get['keyword'])) {
  73. $userWhere = [
  74. ['=', 'mall_id', $this->mall_id],
  75. ['<>','status', StatusEnum::DELETE],
  76. ['=', 'mobile', $get['keyword']],
  77. ['or', ['=', 'id', $get['keyword']], ['=', 'mobile', $get['keyword']], ['like', 'nickname', $get['keyword']]]
  78. ];
  79. }
  80. if (isset($get['nickname']) && !empty($get['nickname'])) {
  81. $userWhere = [
  82. ['=', 'mall_id', $this->mall_id],
  83. ['<>','status', StatusEnum::DELETE],
  84. ['like', 'nickname', $get['nickname']]
  85. ];
  86. }
  87. if (isset($userWhere)) {
  88. $user_select = 'id,mobile,nickname,avatar_url';
  89. $userParams = ['where' => $userWhere, 'select' => $user_select];
  90. $userList = User::lists($userParams);
  91. if (!empty($userList)) $where[] = ['in', 'user_id', array_column($userList, 'id')];
  92. }
  93. $with = ['parent' => function($query){
  94. $query->select('id,mobile,nickname,avatar_url');
  95. },'user' => function($query){
  96. $query->select('id,mobile,nickname,avatar_url');
  97. }
  98. ];
  99. $params = ['limit' => 10, 'where' => $where, 'select' => ['user_id', 'parent_id', 'tags'], 'with' =>$with,'order' => 'id desc'];
  100. $list = BusinessCardUser::listPage($params);
  101. return $this->success('操作成功', $list);
  102. }
  103. return $this->render('card-customer');
  104. }
  105. /**
  106. * 查找会员
  107. * @Author: lun
  108. * @DateTime: 2023/3/10 0010 9:26
  109. * @Copyright: copyright (c) 2021 广东七件事集团
  110. * @return mixed|string|void
  111. */
  112. public function actionSelectUser() {
  113. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  114. $get = Yii::$app->request->get();
  115. $where[] = ['mall_id' => $this->mall_id];
  116. $where[] = ['mobile' => $get['keyword']];
  117. $where[] = ['status' => StatusEnum::ENABLED];
  118. $user_select = 'id,mobile,nickname,avatar_url';
  119. $userParams = ['where' => $where, 'select' => $user_select];
  120. $list = User::listPage($userParams);
  121. return $this->success('操作成功', $list);
  122. }
  123. }
  124. /**
  125. * 线索列表
  126. *
  127. * @author hpei
  128. * @param int customer_type
  129. * @param string keyword
  130. * @return array|mixed
  131. */
  132. public function actionClueList() {
  133. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  134. $get = Yii::$app->request->get();
  135. $where = [['=', 'mall_id', $this->mall_id], ['=', 'is_clue', 1], ['<>','status', StatusEnum::DELETE]];
  136. if (isset($get['keyword']) && !empty($get['keyword'])) {
  137. $userWhere = [
  138. ['=', 'mall_id', $this->mall_id],
  139. ['<>','status', StatusEnum::DELETE],
  140. ['or', ['=', 'id', $get['keyword']], ['in', 'mobile', $get['keyword']], ['like', 'nickname', $get['keyword']]]
  141. ];
  142. $userParams = ['where' => $userWhere];
  143. $userList = User::lists($userParams);
  144. if (!empty($userList)) $where[] = ['in', 'user_id', array_column($userList, 'id')];
  145. }
  146. if (isset($get['customer_type']) && !empty($get['customer_type']) &&$get['customer_type']>0) $where[] = ['=', 'customer_type', $get['customer_type']];
  147. $with = ['parent' => function($query){
  148. $query->select('id,mobile,nickname,avatar_url');
  149. },'user' => function($query){
  150. $query->select('id,mobile,nickname,avatar_url');
  151. }
  152. ];
  153. $params = ['limit' => 10, 'where' => $where, 'select' => ['user_id', 'parent_id', 'customer_type', 'created_at'],'with' =>$with,'order' => 'id desc'];
  154. $list = BusinessCardUser::listPage($params);
  155. $customerType = CardEnums::getCustomerTypeMap();
  156. $list['customerType'] = $customerType;
  157. if (!empty($list['list'])) {
  158. foreach ($list['list'] as &$val) {
  159. if (empty($val['buying_point'])) {
  160. $val['goods_name'] = '';
  161. } else {
  162. //找出browse_num最大值对应的goods_id
  163. $buyingPoint = array_column($val['buying_point'], null, 'browse_num');
  164. $maxBrowse = max(array_keys($buyingPoint));
  165. $goods_id = $buyingPoint[$maxBrowse]['item_id'];
  166. $goodsInfo = Goods::getOne([['id' => $goods_id]], true, [], false, 'goods_name');
  167. $val['goods_name'] = empty($goodsInfo) ? '' : $goodsInfo['goods_name'];
  168. }
  169. unset($val['buying_point']);
  170. }
  171. }
  172. return $this->success('操作成功', $list);
  173. }
  174. return $this->render('clue-list');
  175. }
  176. /**
  177. * 查看卡片
  178. *
  179. * @param int card_id
  180. * @author hpei
  181. * @return array|mixed
  182. */
  183. public function actionCardInfo() {
  184. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  185. $id = Yii::$app->request->get('id', 0);
  186. $info = BusinessCard::getOne([['=', 'id', $id], ['<>','status', StatusEnum::DELETE]], true);
  187. if (empty($info)) return $this->error('卡片不存在或者已删除');
  188. $department_id = $info['department_id'];
  189. $position_id = $info['position_id'];
  190. $params = [
  191. 'where' => [['in', 'id', [$department_id, $position_id]], ['<>', 'status', StatusEnum::DELETE], ['=', 'mall_id', $this->mall_id]],
  192. 'select' => ['name', 'id'],
  193. 'index' => 'id'
  194. ];
  195. $position = BusinessCardPosition::lists($params);
  196. $info['department_name'] = isset($position[$department_id]) ? $position[$department_id]['name'] : '';
  197. $info['position_name'] = isset($position[$position_id]) ? $position[$position_id]['name'] : '';
  198. $info['tags'] = $info['tags'] ? explode(',', $info['tags']) : '';
  199. $info['url'] = $info['url'] ? explode(',', $info['url']) : '';
  200. $info['images'] = explode(',',$info['images']);
  201. $zodiacMap = CardEnums::getZodiac();
  202. if (!empty($info['zodiac'])) {
  203. $info['zodiac'] = $zodiacMap[$info['zodiac']];
  204. }
  205. $info['supply_resources'] = []; //提供的人脉
  206. $info['need_resources'] = [];//需要的人脉
  207. $info['education'] = [];//学历
  208. $info['industry'] = [];//从事的行业
  209. $info['identity_tags'] = [];//身份标签
  210. //获取商城标签数据
  211. $tag_info = BusinessCardUserTag::lists([
  212. 'where' => [
  213. ['mall_id' => $this->mall_id],
  214. ['user_id' => $info['user_id']],
  215. ['cid' => $info['id']],
  216. ['status' => StatusEnum::ENABLED],
  217. ],
  218. 'with' => ['tag'],
  219. ]);
  220. $tag = BusinessCardTag::lists([
  221. 'where' => [
  222. ['mall_id' => $this->mall_id],
  223. ['status' => StatusEnum::ENABLED]
  224. ],
  225. 'index' => 'id',
  226. ]);
  227. if (!empty($info['education_id'])) {
  228. $info['education'][] = $tag[$info['education_id']];
  229. }
  230. if (!empty($tag_info)) {
  231. foreach ($tag_info as $key => $value) {
  232. switch ($value['tag']['type']) {
  233. case 1:
  234. $info['supply_resources'][] = $value['tag'];
  235. break;
  236. case 2:
  237. $info['need_resources'][] = $value['tag'];
  238. break;
  239. case 3:
  240. $info['education'][] = $value['tag'];
  241. break;
  242. case 4:
  243. $info['industry'][] = $value['tag'];
  244. break;
  245. case 5:
  246. $info['identity_tags'][] = $value['tag'];
  247. break;
  248. }
  249. }
  250. }
  251. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  252. $info['sign'] = !empty($info['sign']) ? $info['sign'] : $setting['sign'];
  253. return $this->success('操作成功', $info);
  254. }
  255. $id = Yii::$app->request->get('id', '');
  256. if (!$id) throw new Exception('卡片不存在或者已删除');
  257. return $this->render('card-info', ['id' => $id]);
  258. }
  259. /**
  260. * 删除卡片
  261. *
  262. * @author hpei
  263. * @return string
  264. */
  265. public function actionDelete() {
  266. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  267. $id = Yii::$app->request->post('id', 0);
  268. $t = Yii::$app->db->beginTransaction();
  269. try {
  270. $card = BusinessCard::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id]);
  271. if (empty($card)) throw new Exception('名片不存在');
  272. $card->status = StatusEnum::DELETE;
  273. if (!$card->save()) throw new Exception($card->getErrorMessage());
  274. // 所属部门需要删除
  275. $user = BusinessCardUser::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $card->user_id]);
  276. if (!empty($user)) {
  277. $user->status = StatusEnum::DELETE;
  278. if (!$user->save()) throw new Exception($user->getErrorMessage());
  279. }
  280. $t->commit();
  281. } catch (\Exception $e) {
  282. $t->rollBack();
  283. return $this->error($e->getMessage());
  284. }
  285. }
  286. return $this->success('操作成功');
  287. }
  288. /**
  289. * 获取全部职位
  290. *
  291. * @author hpei
  292. * @return array
  293. */
  294. public function actionGetPosition() {
  295. $position = BusinessCardPosition::getPosition($this->mall_id, ['name', 'id'], 'id');
  296. return $this->success('操作成功', $position);
  297. }
  298. public function actionTest(){
  299. $res = Statistics::goodsStatistics();
  300. }
  301. }