BusinessCardUser.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "qimall_addons_business_card_user".
  11. *
  12. * @property int $id
  13. * @property int $mall_id
  14. * @property int $user_id 成为客户的USERID
  15. * @property int $parent_id 分享者ID
  16. * @property string $tags 标签逗号隔开
  17. * @property int $customer_type 客户类型
  18. * @property int $department_id 部门ID
  19. * @property int $position_id 职位ID
  20. * @property int $role_type 角色类型,1:Boos,2:主管,3:员工
  21. * @property int $is_clue 是否成为线索
  22. * @property string $customer_nickname 客户昵称
  23. * @property int $customer_sex 客户性别, 1:男,2:女
  24. * @property string $customer_mobile 客户手机号
  25. * @property string $customer_email 客户邮箱
  26. * @property string $customer_address 客户地址
  27. * @property string $customer_birthday 客户生日
  28. * @property string $customer_remark 客户备注
  29. * @property int|null $share_num 分享次数
  30. * @property int $status 状态[1启用 0禁用 -1删除]
  31. * @property int $created_at 创建时间
  32. * @property int $updated_at 更新事件
  33. */
  34. class BusinessCardUser extends \common\models\base\BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return 'qimall_addons_business_card_user';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['mall_id', 'user_id', 'parent_id', 'customer_type', 'department_id', 'position_id', 'role_type', 'is_clue', 'customer_sex', 'share_num', 'status', 'created_at', 'updated_at'], 'integer'],
  50. [['tags'], 'string'],
  51. [['customer_nickname'], 'string', 'max' => 48],
  52. [['customer_mobile'], 'string', 'max' => 11],
  53. [['customer_email', 'customer_address'], 'string', 'max' => 120],
  54. [['customer_birthday'], 'string', 'max' => 8],
  55. [['customer_remark'], 'string', 'max' => 255],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => 'Mall ID',
  66. 'user_id' => 'User ID',
  67. 'parent_id' => 'Parent ID',
  68. 'tags' => 'Tags',
  69. 'customer_type' => 'Customer Type',
  70. 'department_id' => 'Department ID',
  71. 'position_id' => 'Position ID',
  72. 'role_type' => 'Role Type',
  73. 'is_clue' => 'Is Clue',
  74. 'customer_nickname' => 'Customer Nickname',
  75. 'customer_sex' => 'Customer Sex',
  76. 'customer_mobile' => 'Customer Mobile',
  77. 'customer_email' => 'Customer Email',
  78. 'customer_address' => 'Customer Address',
  79. 'customer_birthday' => 'Customer Birthday',
  80. 'customer_remark' => 'Customer Remark',
  81. 'share_num' => 'Share Num',
  82. 'status' => 'Status',
  83. 'created_at' => 'Created At',
  84. 'updated_at' => 'Updated At',
  85. ];
  86. }
  87. public function getUser()
  88. {
  89. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');;
  90. }
  91. public function getParent()
  92. {
  93. return $this->hasOne(User::class, ['id' => 'parent_id']);
  94. }
  95. public function getCard()
  96. {
  97. return $this->hasOne(BusinessCard::class, ['user_id' => 'user_id']);
  98. }
  99. /**
  100. * 获取排行榜
  101. * @author hpei
  102. * @param int mall_id
  103. * @param string type user|clue
  104. * @return array [user_id=>num]
  105. */
  106. public static function rank($param) {
  107. $key = $param['type'] == 'user' ? RedisKeyEnum::CARD_USER_STATISTICS : RedisKeyEnum::CARD_CLUE_STATISTICS;
  108. $cacheKey = RedisKeyEnum::suffix($key, 'mall_id_'. $param['mall_id']);
  109. $redis = Yii::$app->redis;
  110. $rank = $redis->zrevrange($cacheKey, 0, 9, 'WITHSCORES');
  111. if (empty($rank)) {
  112. $model = self::find()
  113. ->select(['count(*) count', 'parent_id'])
  114. ->where(['mall_id' => $param['mall_id']])
  115. ->andWhere(['<>','status',StatusEnum::DELETE])
  116. ->orderBy('count desc')
  117. ->groupBy('parent_id')
  118. ->limit(10);
  119. if ($param['type'] == 'clue') $model->andWhere(['=', 'is_clue', 1]);
  120. $list = $model->asArray()->all();
  121. if (!empty($list)) {
  122. $options = [];
  123. foreach ($list as $val) {
  124. array_push($options, $val['count'], 'user_id:' . $val['parent_id']);
  125. }
  126. $redis->zadd($cacheKey, ...$options);
  127. }
  128. } else {
  129. for ($i = 0; $i < count($rank); $i++) {
  130. if ($i %2 == 0) {
  131. $user_id = explode(':', $rank[$i])[1];
  132. $list[$i] = ['user_id' => $user_id, 'count' => 0];
  133. } else {
  134. $list[$i - 1]['count'] = $rank[$i];
  135. }
  136. }
  137. $list = array_values($list);
  138. }
  139. return $list;
  140. }
  141. /**
  142. * 批量修改
  143. *
  144. * @author hpei
  145. * @param $params array
  146. * @param $data array
  147. * @return bool|Exception
  148. */
  149. public static function batchSave($params, $data) {
  150. try {
  151. foreach ($params as $user_id) {
  152. // 获取用户信息
  153. $user = User::find()->where(['id' => $user_id, 'mall_id' => $data['mall_id']])->asArray()->one();
  154. if (empty($user)) throw new Exception('商城暂无此用户!');
  155. // 查询当前用户的名片信息
  156. $model = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  157. // 当前用户已存在,则修改用户信息
  158. if (empty($model)) {
  159. $model = new self();
  160. $model->user_id = $user['id'];
  161. $model->tags = '';
  162. }
  163. if (!$model->load($data, '') || !$model->save()) throw new Exception('名片信息保存失败:'.$model->getErrorMessage());
  164. // 名片修改
  165. $card = BusinessCard::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  166. if (empty($card)) {
  167. $card = new BusinessCard();
  168. $card->user_id = $user['id'];
  169. $card->mobile = $user['mobile'];
  170. $card->nickname = $user['nickname'];
  171. $card->avatar_url = $user['avatar_url'];
  172. }
  173. $card->department_id = $data['department_id'];
  174. $card->position_id = $data['position_id'];
  175. if (!$card->save()) throw new Exception('用户名片保存失败:'.$card->getErrorMessage());
  176. }
  177. return true;
  178. } catch (\Exception $e) {
  179. self::$static_error = $e->getMessage();
  180. return false;
  181. }
  182. }
  183. public static function batchSaves($params, $data) {
  184. $t = Yii::$app->db->beginTransaction();
  185. try {
  186. $userList = User::lists(['where'=>[['id'=>$params['user_id']]],'index'=>'id']);
  187. foreach ($params['user_id'] as $user_id){
  188. if(empty($userList[$user_id])) continue;
  189. $insert_data = [
  190. 'mall_id' => $userList[$user_id]['mall_id'],
  191. 'user_id' => $user_id,
  192. // 'tags' => '',
  193. 'parent_id' => $userList[$user_id]['parent_id'],
  194. 'department_id' => $data['department_id'],
  195. 'position_id' => $data['position_id'],
  196. 'role_type' => $data['role_type'] ?? 3, // 默认为员工
  197. 'is_clue' => !empty($userList[$user_id]['parent_id']) ? 1 : 0
  198. ];
  199. // 查看当前用户是否已经存在
  200. $id = BusinessCardUser::find()->where(['status' => StatusEnum::ENABLED])
  201. ->andWhere(['user_id' => $user_id])
  202. ->andWhere(['mall_id' => $userList[$user_id]['mall_id']])
  203. ->select('id')->scalar();
  204. if (!empty($id)) $insert_data['id'] = $id;
  205. $res = BusinessCardUser::setData($insert_data);
  206. if(empty($res)) throw new Exception(BusinessCardUser::getStaticError());
  207. // card id需要修改职位信息
  208. $card = BusinessCard::getOne([
  209. ['mall_id' => $userList[$user_id]['mall_id']],
  210. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  211. ['user_id' => $user_id]
  212. ]);
  213. if (!empty($card)) {
  214. $card->department_id = $data['department_id'];
  215. $card->position_id = $data['position_id'];
  216. if (!$card->save()) throw new Exception($card->getErrorMessage());
  217. }
  218. }
  219. $t->commit();
  220. return true;
  221. } catch (\Exception $e) {
  222. $t->rollBack();
  223. self::$static_error = $e->getMessage();
  224. return false;
  225. }
  226. }
  227. /**
  228. * 保存数据
  229. *
  230. * @author hpei
  231. * @return bool|Exception
  232. */
  233. public static function setData($params) {
  234. try {
  235. if (empty($params['id'])) {
  236. $model = new self();
  237. } else {
  238. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  239. if (empty($model)) throw new \Exception('数据不存在或者已删除');
  240. }
  241. if (!$model->load($params, '') || !$model->save(false)) throw new \Exception($model->getErrorMessage());
  242. return $model->id;
  243. } catch (\Exception $e) {
  244. self::$static_error = $e->getMessage();
  245. return false;
  246. }
  247. }
  248. /**
  249. * 修改自己客户,部门,小组下的客户数据
  250. * @author hpei
  251. * @return bool|Exception
  252. */
  253. public static function setCustomerData($params) {
  254. if (!$params['user_id'] && !$params['parent_id']) throw new \Exception('客户信息上级信息不能为空');
  255. $userModel = self::findOne(['user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  256. if (empty($userModel)) throw new \Exception('数据不存在或者已删除');
  257. if ($userModel->parent_id != $params['parent_id']) {
  258. //如果父级不是智能卡片会员
  259. $parentModel = self::findOne(['user_id' => $params['parent_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  260. if (empty($parentModel)) throw new \Exception('父级不存在');
  261. if ($parentModel->department_id != $userModel->department_id && $parentModel->position_id != $userModel->position_id) throw new \Exception('不允许修改非本部门会员');
  262. if ($parentModel->role_type >= $userModel->role_type) throw new \Exception('不允许修改职位比自身高的会员');
  263. if ($parentModel->role_type == 1 && $parentModel->department_id != $userModel->department_id) throw new \Exception('主管不允许修改非本职位会员');
  264. }
  265. try {
  266. $transtcion = Yii::$app->db->beginTransaction();
  267. if (!$userModel->load($params, '') || !$userModel->save()) {
  268. self::$static_error = $userModel->getErrorMessage();
  269. $transtcion->rollBack();
  270. return false;
  271. }
  272. if (isset($params['customer_type']) && !empty($params['customer_type'])) {
  273. $where = [['=', 'user_id', $params['user_id']], ['=', 'remark', CardEnums::getCustomerTypeMap($params['customer_type'])], ['=', 'log_type', CardEnums::CUSTOMER_TYPE_PURPOSE]];
  274. $count = BusinessCardFollowLog::count(['where' => $where]);
  275. if ($count > 0) {
  276. self::$static_error = '该类型已经添加';
  277. $transtcion->rollBack();
  278. return false;
  279. }
  280. $data = [
  281. 'mall_id' => $params['mall_id'],
  282. 'user_id' => $params['user_id'],
  283. 'operate_id' => $params['parent_id'],
  284. 'log_type' => CardEnums::CUSTOMER_TYPE_PURPOSE,
  285. 'remark' => CardEnums::getCustomerTypeMap($params['customer_type'])
  286. ];
  287. $res = BusinessCardFollowLog::setData($data);
  288. if (!$res) {
  289. self::$static_error = BusinessCardFollowLog::getStaticError();
  290. $transtcion->rollBack();
  291. return false;
  292. }
  293. }
  294. $transtcion->commit();
  295. return true;
  296. } catch (\Exception $e) {
  297. $transtcion->rollBack();
  298. self::$static_error = $e->getMessage();
  299. return false;
  300. }
  301. }
  302. /**
  303. * 获取转化
  304. * @author hpei
  305. * @param array $param
  306. * @return array
  307. */
  308. public static function conversion($params) {
  309. $model = self::find();
  310. self::paramPack($params, $model);
  311. $list = $model->asArray()->all();
  312. if (empty($list)) return ['count' => 0, 'follow_customer' => 0, 'deal_customer' => 0];
  313. //跟进中=待成交
  314. $data['follow_customer'] = isset($list[CardEnums::CUSTOMER_TYPE_ORDER]) ? $list[CardEnums::CUSTOMER_TYPE_ORDER]['count'] : 0;//待成交
  315. $data['deal_customer'] = isset($list[CardEnums::CUSTOMER_TYPE_MONEY]) ? $list[CardEnums::CUSTOMER_TYPE_MONEY]['count'] : 0;//已成交
  316. $data['cout'] = array_sum(array_column($list, 'count'));
  317. return $data;
  318. }
  319. /**
  320. * 获取部门用户ID
  321. * @author hpei1
  322. * @param array department_ids
  323. * @return array user_ids
  324. */
  325. public static function getDepartmentUid($department_ids) {
  326. return self::find()->where(['department_id' => $department_ids])->select(['user_id'])->column();
  327. }
  328. /**
  329. * 修改父级
  330. * @author hpei
  331. * @param int user_id
  332. * @param int parent_id
  333. * @return void
  334. */
  335. public static function setParent($user_id, $parent_id) {
  336. $card_user = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  337. if (empty($card_user)) return;
  338. $card_user->parent_id = $parent_id;
  339. if (!$card_user->save()) Yii::error('智能名片修改推荐人' . $card_user->getErrorMessage());
  340. }
  341. }