UserLabelServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user\label;
  13. use app\jobs\user\UserLabelJob;
  14. use app\services\BaseServices;
  15. use app\dao\user\label\UserLabelDao;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\FormBuilder as Form;
  18. use crmeb\services\wechat\Work;
  19. use FormBuilder\Factory\Iview;
  20. use think\exception\ValidateException;
  21. use think\facade\Route as Url;
  22. /**
  23. * 用户标签
  24. * Class UserLabelServices
  25. * @package app\services\user\label
  26. * @mixin UserLabelDao
  27. */
  28. class UserLabelServices extends BaseServices
  29. {
  30. /**
  31. * UserLabelServices constructor.
  32. * @param UserLabelDao $dao
  33. */
  34. public function __construct(UserLabelDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取某一本标签
  40. * @param $id
  41. * @return array|\think\Model|null
  42. */
  43. public function getLable($id)
  44. {
  45. return $this->dao->get($id);
  46. }
  47. /**
  48. * 获取所有用户标签
  49. * @param array $where
  50. * @param array|string[] $field
  51. * @return array
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getLabelList(array $where = [], array $field = ['*'])
  57. {
  58. return $this->dao->getList(0, 0, $where, $field);
  59. }
  60. /**
  61. * 获取列表
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getList(array $where)
  68. {
  69. [$page, $limit] = $this->getPageValue();
  70. $list = $this->dao->getList($page, $limit, $where);
  71. $count = $this->dao->count($where);
  72. return compact('list', 'count');
  73. }
  74. /**
  75. * 添加修改标签表单
  76. * @param int $id
  77. * @param int $type
  78. * @param int $relation_id
  79. * @param int $label_cate
  80. * @return mixed
  81. */
  82. public function add(int $id, int $type = 0, int $relation_id = 0, int $label_cate = 0)
  83. {
  84. $label = $this->getLable($id);
  85. $field = array();
  86. /** @var UserLabelCateServices $service */
  87. $service = app()->make(UserLabelCateServices::class);
  88. $options = [];
  89. foreach ($service->getLabelCateAll($type, $relation_id) as $item) {
  90. $options[] = ['value' => $item['id'], 'label' => $item['name']];;
  91. }
  92. if (!$label) {
  93. $title = '添加标签';
  94. $field[] = Form::select('label_cate', '标签分类', $label_cate)->setOptions($options)->filterable(true)->appendValidate(Iview::validateInt()->message('请选择标签分类')->required());
  95. $field[] = Form::input('label_name', '标签名称', '')->maxlength(20)->required('请填写标签名称');
  96. } else {
  97. $title = '修改标签';
  98. $field[] = Form::select('label_cate', '分类', (int)$label->getData('label_cate'))->setOptions($options)->filterable(true)->appendValidate(Iview::validateInt()->message('请选择标签分类')->required());
  99. $field[] = Form::hidden('id', $label->getData('id'));
  100. $field[] = Form::input('label_name', '标签名称', $label->getData('label_name'))->maxlength(20)->required('请填写标签名称');
  101. }
  102. return create_form($title, $field, Url::buildUrl('/user/user_label/save'), 'POST');
  103. }
  104. /**
  105. * 保存标签表单数据
  106. * @param int $id
  107. * @param array $data
  108. * @return mixed
  109. */
  110. public function save(int $id, array $data, int $type = 0, int $relation_id = 0)
  111. {
  112. if (!$data['label_cate']) {
  113. throw new ValidateException('请选择标签分类');
  114. }
  115. $data['type'] = $type;
  116. $data['relation_id'] = $relation_id;
  117. $levelName = $this->dao->getOne(['label_name' => $data['label_name'], 'type' => $type, 'relation_id' => $relation_id]);
  118. if ($id) {
  119. if (!$this->getLable($id)) {
  120. throw new AdminException('数据不存在');
  121. }
  122. if ($levelName && $id != $levelName['id']) {
  123. throw new AdminException('该标签已经存在');
  124. }
  125. if ($this->dao->update($id, $data)) {
  126. return true;
  127. } else {
  128. throw new AdminException('修改失败或者您没有修改什么!');
  129. }
  130. } else {
  131. unset($data['id']);
  132. if ($levelName) {
  133. throw new AdminException('该标签已经存在');
  134. }
  135. if ($this->dao->save($data)) {
  136. return true;
  137. } else {
  138. throw new AdminException('添加失败!');
  139. }
  140. }
  141. }
  142. /**
  143. * 删除
  144. * @param $id
  145. * @throws \Exception
  146. */
  147. public function delLabel(int $id)
  148. {
  149. if ($this->getLable($id)) {
  150. if (!$this->dao->delete($id)) {
  151. throw new AdminException('删除失败,请稍候再试!');
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * 同步标签
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function authWorkClientLabel()
  163. {
  164. /** @var UserLabelCateServices $cateService */
  165. $cateService = app()->make(UserLabelCateServices::class);
  166. $data = $cateService->getLabelList(['group' => 0, 'owner_id' => 0, 'type' => 0]);
  167. if ($data['list']) {
  168. foreach ($data['list'] as $item) {
  169. UserLabelJob::dispatchDo('authLabel', [$item['id'], $item['name']]);
  170. }
  171. }
  172. UserLabelJob::dispatchSece(count($data['list']) + 1, 'authWorkLabel');
  173. return true;
  174. }
  175. /**
  176. * 同步平台标签到企业微信客户
  177. * @param int $cateId
  178. * @param string $groupName
  179. * @return bool
  180. */
  181. public function addCorpClientLabel(int $cateId, string $groupName)
  182. {
  183. try {
  184. $list = $this->dao->getList(0, 0, ['not_tag_id' => 1, 'type' => 0, 'label_cate' => $cateId], ['label_name as name', 'id']);
  185. if (!$list) {
  186. return true;
  187. }
  188. $data = [];
  189. foreach ($list as $item) {
  190. $data[] = ['name' => $item['name']];
  191. }
  192. $res = Work::addCorpTag($groupName, $data);
  193. /** @var UserLabelCateServices $categoryService */
  194. $categoryService = app()->make(UserLabelCateServices::class);
  195. $categoryService->update($cateId, ['other' => $res['tag_group']['group_id']]);
  196. foreach ($res['tag_group']['tag'] ?? [] as $item) {
  197. foreach ($list as $value) {
  198. if ($item['name'] == $value['name']) {
  199. $this->dao->update($value['id'], ['tag_id' => $item['id']]);
  200. }
  201. }
  202. }
  203. return true;
  204. } catch (\Throwable $e) {
  205. return false;
  206. }
  207. }
  208. /**
  209. * 客户标签同步到平台
  210. * @param array $tagIds
  211. * @param array $group
  212. * @return bool
  213. */
  214. public function authWorkLabel(array $tagIds = [], array $group = [])
  215. {
  216. $res = Work::getCorpTags($tagIds, $group);
  217. $tagGroup = $res['tag_group'] ?? [];
  218. $cateData = [];
  219. $labelData = [];
  220. $groupIds = [];
  221. /** @var UserLabelCateServices $cateService */
  222. $cateService = app()->make(UserLabelCateServices::class);
  223. $this->transaction(function () use ($tagGroup, $cateData, $cateService, $labelData, $groupIds) {
  224. foreach ($tagGroup as $item) {
  225. if ($id = $cateService->value(['other' => $item['group_id']], 'id')) {
  226. $cateService->update(['id' => $id], ['name' => $item['group_name'], 'other' => $item['group_id'], 'sort' => $item['order']]);
  227. } else {
  228. $cateData[] = [
  229. 'name' => $item['group_name'],
  230. 'sort' => $item['order'],
  231. 'add_time' => $item['create_time'],
  232. 'other' => $item['group_id'],
  233. 'group' => 0
  234. ];
  235. }
  236. $groupIds[] = $item['group_id'];
  237. foreach ($item['tag'] as $tag) {
  238. if ($labelId = $this->dao->value(['tag_id' => $tag['id']], 'id')) {
  239. $this->dao->update($labelId, ['tag_id' => $tag['id']]);
  240. } else {
  241. $labelData[$item['group_id']][] = [
  242. 'label_name' => $tag['name'],
  243. 'type' => 1,
  244. 'tag_id' => $tag['id'],
  245. ];
  246. }
  247. }
  248. }
  249. if ($cateData) {
  250. $cateService->saveAll($cateData);
  251. }
  252. $cateIds = $cateService->getColumn([
  253. ['other', 'in', $groupIds],
  254. ['type', '=', 1],
  255. ['owner_id', '=', 0],
  256. ['group', '=', 0],
  257. ], 'id', 'other');
  258. if ($labelData) {
  259. $saveData = [];
  260. foreach ($labelData as $groupId => $labels) {
  261. $cateId = $cateIds[$groupId];
  262. foreach ($labels as $label) {
  263. $label['label_cate'] = $cateId;
  264. $saveData[] = $label;
  265. }
  266. }
  267. $this->dao->saveAll($saveData);
  268. }
  269. });
  270. $cateService->deleteCateCache();
  271. return true;
  272. }
  273. /**
  274. * 获取同步企业微信的标签数据
  275. * @return array
  276. */
  277. public function getWorkLabel()
  278. {
  279. /** @var UserLabelCateServices $cateService */
  280. $cateService = app()->make(UserLabelCateServices::class);
  281. $list = $cateService->getLabelTree(['type' => 0, 'owner_id' => 0, 'group' => 0, 'other' => true], ['name', 'id', 'other as value'], [
  282. 'label' => function ($query) {
  283. $query->where('tag_id', '<>', '')->where('type', 0)->where('relation_id', 0)->field(['id', 'label_cate', 'tag_id as value', 'label_name as label']);
  284. }
  285. ]);
  286. foreach ($list as &$item) {
  287. $label = $item['label'];
  288. $item['children'] = $label;
  289. unset($item['label']);
  290. $item['label'] = $item['name'];
  291. }
  292. return $list;
  293. }
  294. /**
  295. * 企业微信创建客户标签事件
  296. * @param string $corpId
  297. * @param string $strId
  298. * @param string $type
  299. * @return bool
  300. */
  301. public function createUserLabel(string $corpId, string $strId, string $type)
  302. {
  303. return $this->authWorkLabel($type === 'tag' ? [$strId] : [], $type === 'tag_group' ? [$strId] : []);
  304. }
  305. /**
  306. * 企业微信更新客户标签事件
  307. * @param string $corpId
  308. * @param string $strId
  309. * @param string $type
  310. * @return bool
  311. */
  312. public function updateUserLabel(string $corpId, string $strId, string $type)
  313. {
  314. return $this->authWorkLabel($type === 'tag' ? [$strId] : [], $type === 'tag_group' ? [$strId] : []);
  315. }
  316. /**
  317. * 删除标签
  318. * @param string $corpId
  319. * @param string $strId
  320. * @param string $type
  321. */
  322. public function deleteUserLabel(string $corpId, string $strId, string $type)
  323. {
  324. if ('tag' === $type) {
  325. $this->dao->delete(['tag_id' => $strId]);
  326. } else if ('tag_group' === $type) {
  327. /** @var UserLabelCateServices $cateService */
  328. $cateService = app()->make(UserLabelCateServices::class);
  329. $cateInfo = $cateService->get(['type' => 0, 'owner_id' => 0, 'group' => 0, 'other' => $strId]);
  330. if ($cateInfo) {
  331. $this->dao->delete(['label_cate' => $cateInfo->id, 'type' => 1]);
  332. $cateInfo->delete();
  333. }
  334. }
  335. }
  336. }