FieldGrantLogic.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /**
  3. * 字段授权逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-01
  7. */
  8. namespace app\admin\logic;
  9. use think\Db;
  10. class FieldGrantLogic
  11. {
  12. /**
  13. * 字段授权列表
  14. *
  15. * @param $param
  16. * @return array|bool|\PDOStatement|string|\think\Model|null
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function index($param)
  22. {
  23. $where = function ($query) use ($param) {
  24. $query->where('module', $param['module']);
  25. $query->where('column', $param['column']);
  26. $query->where('role_id', $param['role_id']);
  27. };
  28. $count = Db::name('admin_field_grant')->where($where)->count();
  29. # 如果该角色下没有字段授权数据则自动添加
  30. if ($count == 0 && Db::name('admin_group')->where('id', $param['role_id'])->find()) {
  31. $this->createCrmFieldGrant($param['role_id']);
  32. }
  33. $data = Db::name('admin_field_grant')->field(['grant_id', 'content'])->where($where)->find();
  34. if (!empty($data['content'])) $data['content'] = unserialize($data['content']);
  35. return !empty($data) ? $data : [];
  36. }
  37. /**
  38. * 更新授权字段
  39. *
  40. * @param $grantId
  41. * @param $content
  42. * @return int|string
  43. * @throws \think\Exception
  44. * @throws \think\exception\PDOException
  45. */
  46. public function update($grantId, $content)
  47. {
  48. return Db::name('admin_field_grant')->where('grant_id', $grantId)->update(['content' => serialize(array_values($content))]);
  49. }
  50. /**
  51. * 添加字段授权信息
  52. *
  53. * @param $roleId
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @throws \think\exception\DbException
  57. */
  58. public function createCrmFieldGrant($roleId)
  59. {
  60. # 添加线索字段授权数据
  61. $this->createLeadsFieldGrant($roleId);
  62. # 添加客户字段授权数据
  63. $this->createCustomerFieldGrant($roleId);
  64. # 添加联系人字段授权数据
  65. $this->createContactsFieldGrant($roleId);
  66. # 添加商机字段授权数据
  67. $this->createBusinessFieldGrant($roleId);
  68. # 添加合同字段授权数据
  69. $this->createContractfieldGrant($roleId);
  70. # 添加回款字段授权数据
  71. $this->createReceivablesFieldGrant($roleId);
  72. # 添加产品字段授权信息
  73. $this->createProductFieldGrant($roleId);
  74. # 添加回访字段授权信息
  75. $this->createVisitFieldGrant($roleId);
  76. }
  77. /**
  78. * 删除授权字段数据
  79. *
  80. * @param $roleId
  81. * @param string $module
  82. * @throws \think\Exception
  83. * @throws \think\exception\PDOException
  84. */
  85. public function deleteCrmFieldGrant($roleId)
  86. {
  87. Db::name('admin_field_grant')->where('module', 'crm')->where('role_id', $roleId)->delete();
  88. }
  89. /**
  90. * 拷贝字段授权数据
  91. *
  92. * @param $copyId
  93. * @param $roleId
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public function copyCrmFieldGrant($copyId, $roleId)
  99. {
  100. $data = [];
  101. $list = Db::name('admin_field_grant')->where('module', 'crm')->where('role_id', $copyId)->select();
  102. foreach ($list AS $key => $value) {
  103. $data[] = [
  104. 'role_id' => $roleId,
  105. 'module' => $value['module'],
  106. 'column' => $value['column'],
  107. 'content' => $value['content'],
  108. 'create_time' => time(),
  109. 'update_time' => time()
  110. ];
  111. }
  112. if (!empty($data)) Db::name('admin_field_grant')->insertAll($data);
  113. }
  114. /**
  115. * 同步更新自定义字段的授权信息
  116. *
  117. * @param $types
  118. * @return bool
  119. * @throws \think\Exception
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @throws \think\exception\DbException
  123. * @throws \think\exception\PDOException
  124. */
  125. public function fieldGrantDiyHandle($types)
  126. {
  127. $typesArray = explode('_', $types);
  128. # 只处理客户管理角色下的字段授权
  129. if ($typesArray[0] != 'crm') return false;
  130. # 查询自定义字段表
  131. $fieldBaseData = [];
  132. $fieldList = Db::name('admin_field')->field(['name', 'field'])->where('types', $types)->select();
  133. foreach ($fieldList AS $key => $value) {
  134. $fieldBaseData[$value['field']] = $value;
  135. }
  136. # 查询字段授权表
  137. $grantList = Db::name('admin_field_grant')->field(['grant_id', 'content'])->where('column', $typesArray[1])->select();
  138. # 处理授权字段的数据更新
  139. foreach ($grantList AS $key => $value) {
  140. $content = unserialize($value['content']);
  141. $fieldData = $fieldBaseData;
  142. foreach ($content AS $k => $v) {
  143. # 只处理自定义字段
  144. if ($v['is_diy'] == 0) continue;
  145. if (empty($fieldData[$v['field']])) {
  146. # 【处理删除:】没有在$fieldData找到,说明自定义字段被删除,则进行同步删除。
  147. unset($content[(int)$k]);
  148. } else {
  149. # 【处理更新:】如果在$fieldData找到,则进行同步更新。
  150. $content[$k]['name'] = $fieldData[$v['field']]['name'];
  151. $content[$k]['field'] = $fieldData[$v['field']]['field'];
  152. # 删除$fieldData的数据,方便统计新增的自定义字段。
  153. unset($fieldData[(string)$v['field']]);
  154. }
  155. }
  156. # 【处理新增】如果$fieldData还有数据,说明是新增的,则进行同步新增。
  157. if (!empty($fieldData)) {
  158. foreach ($fieldData AS $k => $v) {
  159. $content[] = [
  160. 'name' => $v['name'],
  161. 'field' => $v['field'],
  162. 'read' => 1,
  163. 'read_operation' => 1,
  164. 'write' => 1,
  165. 'write_operation' => 1,
  166. 'is_diy' => 1
  167. ];
  168. }
  169. }
  170. # todo 暂时将数据库操作写在循环中!!!
  171. Db::name('admin_field_grant')->where('grant_id', $value['grant_id'])->update(['content' => serialize(array_values($content))]);
  172. }
  173. return true;
  174. }
  175. /**
  176. * 添加线索字段授权数据
  177. *
  178. * @param $roleId
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. */
  183. private function createLeadsFieldGrant($roleId)
  184. {
  185. $content = [];
  186. $leadsList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_leads')->select();
  187. # 处理自定义字段
  188. foreach ($leadsList AS $key => $value) {
  189. $content[] = [
  190. 'name' => $value['name'],
  191. 'field' => $value['field'],
  192. 'read' => 1,
  193. 'read_operation' => in_array($value['field'], ['name', 'next_time']) ? 0 : 1,
  194. 'write' => 1,
  195. 'write_operation' => $value['field'] == 'next_time' ? 0 : 1,
  196. 'is_diy' => 1
  197. ];
  198. }
  199. # 处理固定字段
  200. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  201. $content[] = ['name' => '最后跟进记录', 'field' => 'record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  202. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  203. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  204. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  205. Db::name('admin_field_grant')->insert([
  206. 'role_id' => $roleId,
  207. 'module' => 'crm',
  208. 'column' => 'leads',
  209. 'content' => serialize($content),
  210. 'create_time' => time(),
  211. 'update_time' => time()
  212. ]);
  213. }
  214. /**
  215. * 添加客户字段授权数据
  216. *
  217. * @param $roleId
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. * @throws \think\exception\DbException
  221. */
  222. private function createCustomerFieldGrant($roleId)
  223. {
  224. $content = [];
  225. $customerList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_customer')->select();
  226. # 处理自定义字段
  227. foreach ($customerList AS $key => $value) {
  228. $content[] = [
  229. 'name' => $value['name'],
  230. 'field' => $value['field'],
  231. 'read' => 1,
  232. 'read_operation' => in_array($value['field'], ['name', 'deal_status']) ? 0 : 1,
  233. 'write' => 1,
  234. 'write_operation' => $value['field'] == 'deal_status' ? 0 : 1,
  235. ];
  236. }
  237. # 处理固定字段
  238. $content[] = ['name' => '合同编号', 'field' => 'contract', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  239. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0];
  240. $content[] = ['name' => '最后跟进记录', 'field' => 'record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  241. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  242. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  243. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  244. $content[] = ['name' => '最后跟进时间', 'field' => 'record_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  245. $content[] = ['name' => '负责人获取客户时间', 'field' => 'deal_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  246. $content[] = ['name' => '成交状态', 'field' => 'deal_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0];
  247. $content[] = ['name' => '锁定状态', 'field' => 'is_lock', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  248. $content[] = ['name' => '距进入公海天数', 'field' => 'pool_day', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0];
  249. Db::name('admin_field_grant')->insert([
  250. 'role_id' => $roleId,
  251. 'module' => 'crm',
  252. 'column' => 'customer',
  253. 'content' => serialize($content),
  254. 'create_time' => time(),
  255. 'update_time' => time()
  256. ]);
  257. }
  258. /**
  259. * 添加联系人字段授权数据
  260. *
  261. * @param $roleId
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. * @throws \think\exception\DbException
  265. */
  266. private function createContactsFieldGrant($roleId)
  267. {
  268. $content = [];
  269. $contactsList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contacts')->select();
  270. # 处理自定义字段
  271. foreach ($contactsList AS $key => $value) {
  272. $content[] = [
  273. 'name' => $value['name'],
  274. 'field' => $value['field'],
  275. 'read' => 1,
  276. 'read_operation' => in_array($value['field'], ['name', 'next_time']) ? 0 : 1,
  277. 'write' => 1,
  278. 'write_operation' => $value['field'] == 'next_time' ? 0 : 1,
  279. 'is_diy' => 1
  280. ];
  281. }
  282. # 处理固定字段
  283. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  284. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  285. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  286. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  287. Db::name('admin_field_grant')->insert([
  288. 'role_id' => $roleId,
  289. 'module' => 'crm',
  290. 'column' => 'contacts',
  291. 'content' => serialize($content),
  292. 'create_time' => time(),
  293. 'update_time' => time()
  294. ]);
  295. }
  296. /**
  297. * 添加商机字段授权数据
  298. *
  299. * @param $roleId
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. * @throws \think\exception\DbException
  303. */
  304. private function createBusinessFieldGrant($roleId)
  305. {
  306. $content = [];
  307. $BusinessList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_business')->select();
  308. # 处理自定义字段
  309. foreach ($BusinessList AS $key => $value) {
  310. $content[] = [
  311. 'name' => $value['name'],
  312. 'field' => $value['field'],
  313. 'read' => 1,
  314. 'read_operation' => in_array($value['field'], ['customer_id', 'type_id', 'status_id']) == '' ? 0 : 1,
  315. 'write' => 1,
  316. 'write_operation' => in_array($value['field'], ['customer_id', 'type_id', 'status_id']) ? 0 : 1,
  317. 'is_diy' => 1
  318. ];
  319. }
  320. # 处理固定字段
  321. $content[] = ['name' => '整单折扣', 'field' => 'discount_rate', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0];
  322. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  323. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  324. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  325. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  326. Db::name('admin_field_grant')->insert([
  327. 'role_id' => $roleId,
  328. 'module' => 'crm',
  329. 'column' => 'business',
  330. 'content' => serialize($content),
  331. 'create_time' => time(),
  332. 'update_time' => time()
  333. ]);
  334. }
  335. /**
  336. * 添加合同字段授权数据
  337. *
  338. * @param $roleId
  339. * @throws \think\db\exception\DataNotFoundException
  340. * @throws \think\db\exception\ModelNotFoundException
  341. * @throws \think\exception\DbException
  342. */
  343. private function createContractfieldGrant($roleId)
  344. {
  345. $content = [];
  346. $contractList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contract')->select();
  347. # 处理自定义字段
  348. foreach ($contractList AS $key => $value) {
  349. $content[] = [
  350. 'name' => $value['name'],
  351. 'field' => $value['field'],
  352. 'read' => 1,
  353. 'read_operation' => $value['field'] == 'num' ? 0 : 1,
  354. 'write' => 1,
  355. 'write_operation' => 1,
  356. 'is_diy' => 1
  357. ];
  358. }
  359. # 处理固定字段
  360. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  361. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  362. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  363. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  364. $content[] = ['name' => '最后跟进记录', 'field' => 'record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  365. $content[] = ['name' => '最后跟进记录', 'field' => 'record_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  366. $content[] = ['name' => '已收款金额', 'field' => 'received', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  367. $content[] = ['name' => '未收款金额', 'field' => 'uncollected', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  368. $content[] = ['name' => '审核状态', 'field' => 'check_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  369. Db::name('admin_field_grant')->insert([
  370. 'role_id' => $roleId,
  371. 'module' => 'crm',
  372. 'column' => 'contract',
  373. 'content' => serialize($content),
  374. 'create_time' => time(),
  375. 'update_time' => time()
  376. ]);
  377. }
  378. /**
  379. * 添加回款字段授权数据
  380. *
  381. * @param $roleId
  382. * @throws \think\db\exception\DataNotFoundException
  383. * @throws \think\db\exception\ModelNotFoundException
  384. * @throws \think\exception\DbException
  385. */
  386. private function createReceivablesFieldGrant($roleId)
  387. {
  388. $content = [];
  389. $receivablesList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_receivables')->select();
  390. # 处理自定义字段
  391. foreach ($receivablesList AS $key => $value) {
  392. $content[] = [
  393. 'name' => $value['name'],
  394. 'field' => $value['field'],
  395. 'read' => 1,
  396. 'read_operation' => 1,
  397. 'write' => 1,
  398. 'write_operation' => 1,
  399. 'is_diy' => 1
  400. ];
  401. }
  402. # 处理固定字段
  403. $content[] = ['name' => '合同金额', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  404. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  405. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  406. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  407. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  408. $content[] = ['name' => '审核状态', 'field' => 'check_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  409. Db::name('admin_field_grant')->insert([
  410. 'role_id' => $roleId,
  411. 'module' => 'crm',
  412. 'column' => 'receivables',
  413. 'content' => serialize($content),
  414. 'create_time' => time(),
  415. 'update_time' => time()
  416. ]);
  417. }
  418. /**
  419. * 添加产品字段授权信息
  420. *
  421. * @param $roleId
  422. * @throws \think\db\exception\DataNotFoundException
  423. * @throws \think\db\exception\ModelNotFoundException
  424. * @throws \think\exception\DbException
  425. */
  426. private function createProductFieldGrant($roleId)
  427. {
  428. $content = [];
  429. $productList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_product')->select();
  430. # 处理自定义字段
  431. foreach ($productList AS $key => $value) {
  432. $readOperation = 1;
  433. $writeOperation = 1;
  434. if (in_array($value['field'], ['name', 'category_id', 'unit', 'price', 'status'])) $readOperation = 0;
  435. if (in_array($value['field'], ['status'])) $writeOperation = 0;
  436. $content[] = [
  437. 'name' => $value['name'],
  438. 'field' => $value['field'],
  439. 'read' => 1,
  440. 'read_operation' => $readOperation,
  441. 'write' => 1,
  442. 'write_operation' => $writeOperation,
  443. 'is_diy' => 1
  444. ];
  445. }
  446. # 处理固定字段
  447. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  448. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  449. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  450. $content[] = ['name' => '更新时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  451. Db::name('admin_field_grant')->insert([
  452. 'role_id' => $roleId,
  453. 'module' => 'crm',
  454. 'column' => 'product',
  455. 'content' => serialize($content),
  456. 'create_time' => time(),
  457. 'update_time' => time()
  458. ]);
  459. }
  460. /**
  461. * 添加回访字段授权信息
  462. *
  463. * @param $roleId
  464. * @throws \think\db\exception\DataNotFoundException
  465. * @throws \think\db\exception\ModelNotFoundException
  466. * @throws \think\exception\DbException
  467. */
  468. private function createVisitFieldGrant($roleId)
  469. {
  470. $content = [];
  471. $visitList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_visit')->select();
  472. # 处理自定义字段
  473. foreach ($visitList AS $key => $value) {
  474. $content[] = [
  475. 'name' => $value['name'],
  476. 'field' => $value['field'],
  477. 'read' => 1,
  478. 'read_operation' => 1,
  479. 'write' => 1,
  480. 'write_operation' => 1,
  481. 'is_diy' => 1
  482. ];
  483. }
  484. # 处理固定字段
  485. // $content[] = ['name' => '回访时间', 'field' => 'visit_time', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0];
  486. $content[] = ['name' => '负责人', 'field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  487. $content[] = ['name' => '客户', 'field' => 'customer_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  488. $content[] = ['name' => '联系人', 'field' => 'contract_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0];
  489. $content[] = ['name' => '创建时间', 'field' => 'create_time', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  490. $content[] = ['name' => '编辑时间', 'field' => 'update_time', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  491. $content[] = ['name' => '创建人', 'field' => 'create_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  492. $content[] = ['name' => '合同', 'field' => 'contacts_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0];
  493. Db::name('admin_field_grant')->insert([
  494. 'role_id' => $roleId,
  495. 'module' => 'crm',
  496. 'column' => 'visit',
  497. 'content' => serialize($content),
  498. 'create_time' => time(),
  499. 'update_time' => time()
  500. ]);
  501. }
  502. }