FieldGrantLogic.php 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. private $except = [
  13. 'leads' => [
  14. 'name', 'email', 'source', 'mobile', 'telephone', 'detail_address', 'industry', 'level', 'next_time',
  15. 'remark', 'owner_user_id', 'last_record', 'create_user_id', 'create_time', 'update_time', 'last_time'
  16. ],
  17. 'customer' => [
  18. 'name', 'source', 'mobile', 'telephone', 'website', 'industry', 'level', 'next_time', 'remark', 'email',
  19. 'owner_user_id', 'last_record', 'create_user_id', 'create_time', 'update_time', 'last_time', 'obtain_time',
  20. 'deal_status', 'is_lock', 'pool_day'
  21. ],
  22. 'contacts' => [
  23. 'name', 'customer_id', 'mobile', 'telephone', 'email', 'post', 'decision', 'detail_address', 'next_time',
  24. 'remark', 'sex', 'owner_user_id', 'create_user_id', 'create_time', 'update_time', 'last_time', 'last_record'
  25. ],
  26. 'business' => [
  27. 'name', 'customer_id', 'money', 'deal_date', 'remark', 'status_id', 'type_id', 'owner_user_id',
  28. 'create_user_id', 'create_time', 'update_time', 'last_time', 'last_record'
  29. ],
  30. 'contract' => [
  31. 'name', 'num', 'customer_id', 'business_id', 'money', 'order_date', 'start_time', 'end_time', 'contacts_id',
  32. 'order_user_id', 'remark', 'owner_user_id', 'create_user_id', 'create_time', 'update_time', 'last_time',
  33. 'last_record', 'done_money', 'un_money', 'check_status'
  34. ],
  35. 'receivables' => [
  36. 'number', 'customer_id', 'contract_id', 'plan_id', 'return_time', 'money', 'return_type', 'remark',
  37. 'owner_user_id', 'create_user_id', 'create_time', 'update_time', 'check_status'
  38. ],
  39. 'product' => [
  40. 'name', 'category_id', 'unit', 'num', 'price', 'description', 'status', 'owner_user_id', 'create_user_id',
  41. 'create_time', 'update_time'
  42. ],
  43. 'visit' => [
  44. 'number', 'visit_time', 'owner_user_id', 'shape', 'customer_id', 'contacts_id', 'contract_id', 'satisfaction',
  45. 'feedback', 'create_user_id', 'create_time', 'update_time'
  46. ],
  47. 'invoice' => [
  48. 'invoice_apple_number', 'customer_id', 'contract_id', 'contract_money', 'invoice_date', 'invoice_money', 'invoice_type', 'remark'
  49. ],
  50. ];
  51. /**
  52. * 字段授权列表
  53. *
  54. * @param $param
  55. * @return array|bool|\PDOStatement|string|\think\Model|null
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function index($param)
  61. {
  62. $where = function ($query) use ($param) {
  63. $query->where('module', $param['module']);
  64. $query->where('column', $param['column']);
  65. $query->where('role_id', $param['role_id']);
  66. };
  67. $count = Db::name('admin_field_grant')->where($where)->count();
  68. # 如果该角色下没有字段授权数据则自动添加
  69. if ($count == 0 && Db::name('admin_group')->where('id', $param['role_id'])->find()) {
  70. $this->createCrmFieldGrant($param['role_id']);
  71. }
  72. $data = Db::name('admin_field_grant')->field(['grant_id', 'content'])->where($where)->find();
  73. if (!empty($data['content'])) $data['content'] = unserialize($data['content']);
  74. return !empty($data) ? $data : [];
  75. }
  76. /**
  77. * 添加字段授权信息
  78. *
  79. * @param $roleId
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function createCrmFieldGrant($roleId)
  85. {
  86. # 防止重复,先删除一下
  87. $this->deleteCrmFieldGrant($roleId);
  88. # 添加线索字段授权数据
  89. $this->createLeadsFieldGrant($roleId);
  90. # 添加客户字段授权数据
  91. $this->createCustomerFieldGrant($roleId);
  92. # 添加联系人字段授权数据
  93. $this->createContactsFieldGrant($roleId);
  94. # 添加商机字段授权数据
  95. $this->createBusinessFieldGrant($roleId);
  96. # 添加合同字段授权数据
  97. $this->createContractFieldGrant($roleId);
  98. # 添加回款字段授权数据
  99. $this->createReceivablesFieldGrant($roleId);
  100. # 添加产品字段授权信息
  101. $this->createProductFieldGrant($roleId);
  102. # 添加回访字段授权信息
  103. $this->createVisitFieldGrant($roleId);
  104. # 添加发票字段授权信息
  105. $this->createInvoiceFieldGrant($roleId);
  106. }
  107. /**
  108. * 更新授权字段
  109. *
  110. * @param $grantId
  111. * @param $content
  112. * @return int|string
  113. * @throws \think\Exception
  114. * @throws \think\exception\PDOException
  115. */
  116. public function update($grantId, $content)
  117. {
  118. return Db::name('admin_field_grant')->where('grant_id', $grantId)->update(['content' => serialize(array_values($content))]);
  119. }
  120. /**
  121. * 删除授权字段数据
  122. *
  123. * @param $roleId
  124. * @param string $module
  125. * @throws \think\Exception
  126. * @throws \think\exception\PDOException
  127. */
  128. public function deleteCrmFieldGrant($roleId)
  129. {
  130. Db::name('admin_field_grant')->where('module', 'crm')->where('role_id', $roleId)->delete();
  131. }
  132. /**
  133. * 拷贝字段授权数据
  134. *
  135. * @param $copyId
  136. * @param $roleId
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @throws \think\exception\DbException
  140. */
  141. public function copyCrmFieldGrant($copyId, $roleId)
  142. {
  143. $data = [];
  144. $list = Db::name('admin_field_grant')->where('module', 'crm')->where('role_id', $copyId)->select();
  145. foreach ($list AS $key => $value) {
  146. $data[] = [
  147. 'role_id' => $roleId,
  148. 'module' => $value['module'],
  149. 'column' => $value['column'],
  150. 'content' => $value['content'],
  151. 'create_time' => time(),
  152. 'update_time' => time()
  153. ];
  154. }
  155. if (!empty($data)) Db::name('admin_field_grant')->insertAll($data);
  156. }
  157. /**
  158. * 同步更新自定义字段的授权信息
  159. *
  160. * @param $types
  161. * @return bool
  162. * @throws \think\Exception
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. * @throws \think\exception\PDOException
  167. */
  168. public function fieldGrantDiyHandle($types)
  169. {
  170. $typesArray = explode('_', $types);
  171. # 只处理客户管理角色下的字段授权
  172. if ($typesArray[0] != 'crm') return false;
  173. # 查询自定义字段表
  174. $fieldBaseData = [];
  175. $fieldList = Db::name('admin_field')->field(['name', 'field'])->where('types', $types)->select();
  176. foreach ($fieldList AS $key => $value) {
  177. # 排除掉固定字段
  178. if (in_array($value['field'], $this->except[$typesArray[1]])) continue;
  179. $fieldBaseData[$value['field']] = $value;
  180. }
  181. # 查询字段授权表
  182. $grantList = Db::name('admin_field_grant')->field(['grant_id', 'content'])->where('column', $typesArray[1])->select();
  183. # 处理授权字段的数据更新
  184. foreach ($grantList AS $key => $value) {
  185. $content = unserialize($value['content']);
  186. $fieldData = $fieldBaseData;
  187. foreach ($content AS $k => $v) {
  188. # 只处理自定义字段
  189. if ($v['is_diy'] == 0) continue;
  190. if (empty($fieldData[$v['field']])) {
  191. # 【处理删除:】没有在$fieldData找到,说明自定义字段被删除,则进行同步删除。
  192. unset($content[(int)$k]);
  193. } else {
  194. # 【处理更新:】如果在$fieldData找到,则进行同步更新。
  195. $content[$k]['name'] = $fieldData[$v['field']]['name'];
  196. $content[$k]['field'] = $fieldData[$v['field']]['field'];
  197. # 删除$fieldData的数据,方便统计新增的自定义字段。
  198. unset($fieldData[(string)$v['field']]);
  199. }
  200. }
  201. # 【处理新增】如果$fieldData还有数据,说明是新增的,则进行同步新增。
  202. if (!empty($fieldData)) {
  203. foreach ($fieldData AS $k => $v) {
  204. $content[] = [
  205. 'name' => $v['name'],
  206. 'field' => $v['field'],
  207. 'read' => 1,
  208. 'read_operation' => 1,
  209. 'write' => 1,
  210. 'write_operation' => 1,
  211. 'is_diy' => 1
  212. ];
  213. }
  214. }
  215. Db::name('admin_field_grant')->where('grant_id', $value['grant_id'])->update(['content' => serialize(array_values($content))]);
  216. }
  217. return true;
  218. }
  219. /**
  220. * 处理线索字段授权数据
  221. *
  222. * @param int $roleId 角色ID
  223. * @author fanqi
  224. * @date 2021-03-22
  225. */
  226. private function createLeadsFieldGrant($roleId)
  227. {
  228. # 固定字段
  229. $content = [
  230. ['field' => 'name', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '线索名称'],
  231. ['field' => 'email', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '邮箱'],
  232. ['field' => 'source', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '线索来源'],
  233. ['field' => 'mobile', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '手机'],
  234. ['field' => 'telephone', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '电话'],
  235. ['field' => 'detail_address', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '地址'],
  236. ['field' => 'industry', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户行业'],
  237. ['field' => 'level', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户级别'],
  238. ['field' => 'next_time', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '下次联系时间'],
  239. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  240. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  241. ['field' => 'last_record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进记录'],
  242. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  243. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  244. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  245. ['field' => 'last_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进时间'],
  246. ];
  247. $leadsList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_leads')->select();
  248. # 自定义字段
  249. foreach ($leadsList AS $key => $value) {
  250. if (in_array($value['field'], $this->except['leads'])) continue;
  251. $content[] = [
  252. 'name' => $value['name'],
  253. 'field' => $value['field'],
  254. 'read' => 1,
  255. 'read_operation' => 1,
  256. 'write' => 1,
  257. 'write_operation' => 1,
  258. 'is_diy' => 1
  259. ];
  260. }
  261. Db::name('admin_field_grant')->insert([
  262. 'role_id' => $roleId,
  263. 'module' => 'crm',
  264. 'column' => 'leads',
  265. 'content' => serialize($content),
  266. 'create_time' => time(),
  267. 'update_time' => time()
  268. ]);
  269. }
  270. /**
  271. * 处理客户字段授权数据
  272. *
  273. * @param int $roleId 角色ID
  274. * @author fanqi
  275. * @date 2021-03-22
  276. */
  277. private function createCustomerFieldGrant($roleId)
  278. {
  279. # 固定字段
  280. $content = [
  281. ['field' => 'name', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户名称'],
  282. ['field' => 'source', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户来源'],
  283. ['field' => 'mobile', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '手机'],
  284. ['field' => 'telephone', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '电话'],
  285. ['field' => 'website', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '网址'],
  286. ['field' => 'industry', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户行业'],
  287. ['field' => 'level', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户级别'],
  288. ['field' => 'next_time', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '下次联系时间'],
  289. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  290. ['field' => 'email', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '邮箱'],
  291. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  292. ['field' => 'last_record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进记录'],
  293. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  294. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  295. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  296. ['field' => 'last_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进时间'],
  297. ['field' => 'obtain_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人获取客户时间'],
  298. ['field' => 'deal_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '成交状态'],
  299. ['field' => 'is_lock', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '锁定状态'],
  300. ['field' => 'pool_day', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '距进入公海天数'],
  301. ];
  302. $customerList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_customer')->select();
  303. # 自定义字段
  304. foreach ($customerList AS $key => $value) {
  305. if (in_array($value['field'], $this->except['customer'])) continue;
  306. $content[] = [
  307. 'name' => $value['name'],
  308. 'field' => $value['field'],
  309. 'read' => 1,
  310. 'read_operation' => 1,
  311. 'write' => 1,
  312. 'write_operation' => 1,
  313. 'is_diy' => 1
  314. ];
  315. }
  316. Db::name('admin_field_grant')->insert([
  317. 'role_id' => $roleId,
  318. 'module' => 'crm',
  319. 'column' => 'customer',
  320. 'content' => serialize($content),
  321. 'create_time' => time(),
  322. 'update_time' => time()
  323. ]);
  324. }
  325. /**
  326. * 处理联系人字段授权数据
  327. *
  328. * @param int $roleId 角色ID
  329. * @author fanqi
  330. * @date 2021-03-22
  331. */
  332. private function createContactsFieldGrant($roleId)
  333. {
  334. # 固定字段
  335. $content = [
  336. ['field' => 'name', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '姓名'],
  337. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '客户名称'],
  338. ['field' => 'mobile', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '手机'],
  339. ['field' => 'telephone', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '电话'],
  340. ['field' => 'email', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '邮箱'],
  341. ['field' => 'post', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '职务'],
  342. ['field' => 'decision', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '是否关键决策人'],
  343. ['field' => 'detail_address', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '地址'],
  344. ['field' => 'next_time', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '下次联系时间'],
  345. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  346. ['field' => 'sex', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '性别'],
  347. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  348. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  349. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  350. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  351. ['field' => 'last_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进时间'],
  352. ];
  353. $contactsList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contacts')->select();
  354. # 自定义字段
  355. foreach ($contactsList AS $key => $value) {
  356. if (in_array($value['field'], $this->except['contacts'])) continue;
  357. $content[] = [
  358. 'name' => $value['name'],
  359. 'field' => $value['field'],
  360. 'read' => 1,
  361. 'read_operation' => 1,
  362. 'write' => 1,
  363. 'write_operation' => 1,
  364. 'is_diy' => 1
  365. ];
  366. }
  367. Db::name('admin_field_grant')->insert([
  368. 'role_id' => $roleId,
  369. 'module' => 'crm',
  370. 'column' => 'contacts',
  371. 'content' => serialize($content),
  372. 'create_time' => time(),
  373. 'update_time' => time()
  374. ]);
  375. }
  376. /**
  377. * 处理商机字段授权数据
  378. *
  379. * @param int $roleId 角色ID
  380. * @author fanqi
  381. * @date 2021-03-22
  382. */
  383. private function createBusinessFieldGrant($roleId)
  384. {
  385. # 固定字段
  386. $content = [
  387. ['field' => 'name', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '商机名称'],
  388. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '客户名称'],
  389. ['field' => 'money', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '商机金额'],
  390. ['field' => 'deal_date', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '预计成交日期'],
  391. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  392. ['field' => 'status_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '商机阶段'],
  393. ['field' => 'type_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '商机状态组'],
  394. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  395. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  396. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  397. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  398. ['field' => 'last_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进时间'],
  399. ];
  400. $BusinessList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_business')->select();
  401. # 自定义字段
  402. foreach ($BusinessList AS $key => $value) {
  403. if (in_array($value['field'], $this->except['business'])) continue;
  404. $content[] = [
  405. 'name' => $value['name'],
  406. 'field' => $value['field'],
  407. 'read' => 1,
  408. 'read_operation' => 1,
  409. 'write' => 1,
  410. 'write_operation' => 1,
  411. 'is_diy' => 1
  412. ];
  413. }
  414. Db::name('admin_field_grant')->insert([
  415. 'role_id' => $roleId,
  416. 'module' => 'crm',
  417. 'column' => 'business',
  418. 'content' => serialize($content),
  419. 'create_time' => time(),
  420. 'update_time' => time()
  421. ]);
  422. }
  423. /**
  424. * 处理合同字段授权数据
  425. *
  426. * @param int $roleId 角色ID
  427. * @author fanqi
  428. * @date 2021-03-22
  429. */
  430. private function createContractFieldGrant($roleId)
  431. {
  432. # 固定字段
  433. $content = [
  434. ['field' => 'name', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同名称'],
  435. ['field' => 'num', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同编号'],
  436. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户名称'],
  437. ['field' => 'business_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '商机名称'],
  438. ['field' => 'money', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同金额'],
  439. ['field' => 'order_date', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '下单时间'],
  440. ['field' => 'start_time', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同开始时间'],
  441. ['field' => 'end_time', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同结束时间'],
  442. ['field' => 'contacts_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户签约人'],
  443. ['field' => 'order_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '公司签约人'],
  444. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  445. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  446. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  447. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  448. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  449. ['field' => 'last_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进时间'],
  450. ['field' => 'last_record', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '最后跟进记录'],
  451. ['field' => 'done_money', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '已收款金额'],
  452. ['field' => 'un_money', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '未收款金额'],
  453. ['field' => 'check_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '审核状态'],
  454. ];
  455. $contractList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contract')->select();
  456. # 自定义字段
  457. foreach ($contractList AS $key => $value) {
  458. if (in_array($value['field'], $this->except['contract'])) continue;
  459. $content[] = [
  460. 'name' => $value['name'],
  461. 'field' => $value['field'],
  462. 'read' => 1,
  463. 'read_operation' => 1,
  464. 'write' => 1,
  465. 'write_operation' => 1,
  466. 'is_diy' => 1
  467. ];
  468. }
  469. Db::name('admin_field_grant')->insert([
  470. 'role_id' => $roleId,
  471. 'module' => 'crm',
  472. 'column' => 'contract',
  473. 'content' => serialize($content),
  474. 'create_time' => time(),
  475. 'update_time' => time()
  476. ]);
  477. }
  478. /**
  479. * 处理回款字段授权数据
  480. *
  481. * @param int $roleId 角色ID
  482. * @author fanqi
  483. * @date 2021-03-22
  484. */
  485. private function createReceivablesFieldGrant($roleId)
  486. {
  487. # 固定字段
  488. $content = [
  489. ['field' => 'number', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回款编号'],
  490. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户名称'],
  491. ['field' => 'contract_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '合同编号'],
  492. ['field' => 'plan_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '期数'],
  493. ['field' => 'return_time', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回款日期'],
  494. ['field' => 'money', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回款金额'],
  495. ['field' => 'return_type', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回款方式'],
  496. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  497. ['field' => 'contract_money', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '合同金额'],
  498. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  499. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  500. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  501. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  502. ['field' => 'check_status', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '审核状态'],
  503. ];
  504. $receivablesList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_receivables')->select();
  505. # 自定义字段
  506. foreach ($receivablesList AS $key => $value) {
  507. if (in_array($value['field'], $this->except['receivables'])) continue;
  508. $content[] = [
  509. 'name' => $value['name'],
  510. 'field' => $value['field'],
  511. 'read' => 1,
  512. 'read_operation' => 1,
  513. 'write' => 1,
  514. 'write_operation' => 1,
  515. 'is_diy' => 1
  516. ];
  517. }
  518. Db::name('admin_field_grant')->insert([
  519. 'role_id' => $roleId,
  520. 'module' => 'crm',
  521. 'column' => 'receivables',
  522. 'content' => serialize($content),
  523. 'create_time' => time(),
  524. 'update_time' => time()
  525. ]);
  526. }
  527. /**
  528. * 处理产品字段授权信息
  529. *
  530. * @param int $roleId 角色ID
  531. * @author fanqi
  532. * @date 2021-03-22
  533. */
  534. private function createProductFieldGrant($roleId)
  535. {
  536. # 固定字段
  537. $content = [
  538. ['field' => 'name', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '产品名称'],
  539. ['field' => 'category_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '产品类型'],
  540. ['field' => 'unit', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '产品单位'],
  541. ['field' => 'num', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '产品编码'],
  542. ['field' => 'price', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '价格'],
  543. ['field' => 'description', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '产品描述'],
  544. ['field' => 'status', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '是否上下架'],
  545. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '负责人'],
  546. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  547. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  548. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  549. ];
  550. $productList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_product')->select();
  551. # 自定义字段
  552. foreach ($productList AS $key => $value) {
  553. if (in_array($value['field'], $this->except['product'])) continue;
  554. $content[] = [
  555. 'name' => $value['name'],
  556. 'field' => $value['field'],
  557. 'read' => 1,
  558. 'read_operation' => 1,
  559. 'write' => 1,
  560. 'write_operation' => 1,
  561. 'is_diy' => 1
  562. ];
  563. }
  564. Db::name('admin_field_grant')->insert([
  565. 'role_id' => $roleId,
  566. 'module' => 'crm',
  567. 'column' => 'product',
  568. 'content' => serialize($content),
  569. 'create_time' => time(),
  570. 'update_time' => time()
  571. ]);
  572. }
  573. /**
  574. * 处理回访字段授权信息
  575. *
  576. * @param int $roleId
  577. * @author fanqi
  578. * @date 2021-03-22
  579. */
  580. private function createVisitFieldGrant($roleId)
  581. {
  582. $content = [
  583. ['field' => 'number', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回访编号'],
  584. ['field' => 'visit_time', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回访时间'],
  585. ['field' => 'owner_user_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '回访人'],
  586. ['field' => 'shape', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '回访形式'],
  587. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '客户名称'],
  588. ['field' => 'contacts_id', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '联系人'],
  589. ['field' => 'contract_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '合同编号'],
  590. ['field' => 'satisfaction', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户满意度'],
  591. ['field' => 'feedback', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '客户反馈'],
  592. ['field' => 'create_user_id', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建人'],
  593. ['field' => 'create_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '创建时间'],
  594. ['field' => 'update_time', 'read' => 1, 'read_operation' => 1, 'write' => 0, 'write_operation' => 0, 'is_diy' => 0, 'name' => '更新时间'],
  595. ];
  596. $visitList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_visit')->select();
  597. # 处理自定义字段
  598. foreach ($visitList AS $key => $value) {
  599. if (in_array($value['field'], $this->except['visit'])) continue;
  600. $content[] = [
  601. 'name' => $value['name'],
  602. 'field' => $value['field'],
  603. 'read' => 1,
  604. 'read_operation' => 1,
  605. 'write' => 1,
  606. 'write_operation' => 1,
  607. 'is_diy' => 1
  608. ];
  609. }
  610. Db::name('admin_field_grant')->insert([
  611. 'role_id' => $roleId,
  612. 'module' => 'crm',
  613. 'column' => 'visit',
  614. 'content' => serialize($content),
  615. 'create_time' => time(),
  616. 'update_time' => time()
  617. ]);
  618. }
  619. /**
  620. * 处理回访字段授权信息
  621. *
  622. * @param $roleId
  623. * @author fanqi
  624. * @since 2021-06-25
  625. */
  626. private function createInvoiceFieldGrant($roleId)
  627. {
  628. $content = [
  629. ['field' => 'invoice_apple_number', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '发票申请编号'],
  630. ['field' => 'customer_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '客户名称'],
  631. ['field' => 'contract_id', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '合同编号'],
  632. ['field' => 'contract_money', 'read' => 1, 'read_operation' => 0, 'write' => 1, 'write_operation' => 0, 'is_diy' => 0, 'name' => '合同金额'],
  633. ['field' => 'invoice_money', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '开票金额'],
  634. ['field' => 'invoice_date', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '开票日期'],
  635. ['field' => 'invoice_type', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '开票类型'],
  636. ['field' => 'remark', 'read' => 1, 'read_operation' => 1, 'write' => 1, 'write_operation' => 1, 'is_diy' => 0, 'name' => '备注'],
  637. ];
  638. $invoiceList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_invoice')->select();
  639. # 处理自定义字段
  640. foreach ($invoiceList AS $key => $value) {
  641. if (in_array($value['field'], $this->except['invoice'])) continue;
  642. $content[] = [
  643. 'name' => $value['name'],
  644. 'field' => $value['field'],
  645. 'read' => 1,
  646. 'read_operation' => 1,
  647. 'write' => 1,
  648. 'write_operation' => 1,
  649. 'is_diy' => 1
  650. ];
  651. }
  652. Db::name('admin_field_grant')->insert([
  653. 'role_id' => $roleId,
  654. 'module' => 'crm',
  655. 'column' => 'invoice',
  656. 'content' => serialize($content),
  657. 'create_time' => time(),
  658. 'update_time' => time()
  659. ]);
  660. }
  661. }