Setting.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: CRM相关设置
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use think\Exception;
  12. class Setting extends Common
  13. {
  14. /**
  15. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  16. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  17. */
  18. /**
  19. * 团队成员
  20. * @author Michael_xu
  21. * @param types 类型
  22. * @param types_id 类型ID数组
  23. * @param type 权限 1只读2读写
  24. * @param user_id [array] 协作人
  25. * @param is_del 1 移除操作
  26. * @return
  27. */
  28. public function createTeamData($param)
  29. {
  30. if (!is_array($param['user_id'])) {
  31. $param['user_id'] = [intval($param['user_id'])];
  32. }
  33. if (!is_array($param['types_id'])) {
  34. $param['types_id'] = [intval($param['types_id'])];
  35. }
  36. $res = teamUserId($param,$param['types'], $param['types_id'], $param['type'], $param['user_id'], $param['is_del'], $param['owner_user_id']);
  37. if ($res == '1') {
  38. //同时关联其他模块(仅限客户模块)
  39. if (is_array($param['module']) && $param['types'] == 'crm_customer') {
  40. foreach ($param['module'] as $v) {
  41. $where = [];
  42. $where['customer_id'] = array('in',$param['types_id']);
  43. // $where['owner_user_id'] = $param['owner_user_id'];
  44. $moduleList = db($v)->where($where)->select();
  45. switch ($v) {
  46. case 'crm_contacts' : $module_id = 'contacts_id'; break;
  47. case 'crm_business' : $module_id = 'business_id'; break;
  48. case 'crm_contract' : $module_id = 'contract_id'; break;
  49. case 'crm_leads' : $module_id = 'leads_id'; break;
  50. case 'crm_receivables' : $module_id = 'receivables_id'; break;
  51. }
  52. foreach ($moduleList as $val) {
  53. teamUserId($param,$v, $val[$module_id], $param['type'], $param['user_id'], $param['is_del'], $param['owner_user_id'], 0);
  54. }
  55. }
  56. }
  57. return true;
  58. } else {
  59. return $res;
  60. }
  61. }
  62. /**
  63. * 设置回访提醒
  64. *
  65. * @param $status
  66. * @param $day
  67. * @return bool
  68. * @throws \think\Exception
  69. * @throws \think\exception\PDOException
  70. */
  71. public function setVisitDay($status, $day,$userId)
  72. {
  73. $status = intval($status);
  74. $day = intval($day);
  75. # 是否开启回访提醒
  76. if (Db::name('crm_config')->where('name', 'visit_config')->value('id')) {
  77. Db::name('crm_config')->where('name', 'visit_config')->update(['value' => $status]);
  78. } else {
  79. Db::name('crm_config')->insert([
  80. 'name' => 'visit_config',
  81. 'value' => $status,
  82. 'description' => '是否开启回访提醒:1开启;0不开启'
  83. ]);
  84. }
  85. # 客户回访提醒天数
  86. if (!empty($day)) {
  87. if (Db::name('crm_config')->where('name', 'visit_day')->value('id')) {
  88. Db::name('crm_config')->where('name', 'visit_day')->update(['value' => $day]);
  89. } else {
  90. Db::name('crm_config')->insert([
  91. 'name' => 'visit_day',
  92. 'value' => $day,
  93. 'description' => '客户回访提醒天数'
  94. ]);
  95. }
  96. }
  97. # 系统操作日志
  98. SystemActionLog($userId, 'crm_config','customer', 1, 'update','客户回访提醒' , '', '','设置了客户回访提醒');
  99. return true;
  100. }
  101. /**
  102. * 获取回访提醒
  103. *
  104. * @return array
  105. */
  106. public function getVisitDay()
  107. {
  108. $status = Db::name('crm_config')->where('name', 'visit_config')->value('value');
  109. $day = Db::name('crm_config')->where('name', 'visit_day')->value('value');
  110. return ['status' => !empty($status) ? 1 : 0, 'day' => !empty($day) ? intval($day) : 0];
  111. }
  112. /**
  113. * 设置自动编号
  114. *
  115. * @param $param
  116. * @return bool
  117. */
  118. public function setNumber($param,$userId)
  119. {
  120. $apiCommon = new ApiCommon();
  121. Db::startTrans();
  122. try {
  123. foreach ($param AS $key => $value) {
  124. # 前端传来的status值为1代表启用,后端保存的status值为0代表启用,这里执行以下取反操作;
  125. $status = $value['status'] == 1 ? 0 : 1;
  126. $sort = 0;
  127. # 删除未提交过来的数据,先查出某一类型的全部ID数据
  128. $sequenceIds = Db::name('crm_number_sequence')->where('number_type', $value['number_type'])->column('number_sequence_id');
  129. # 记录提交的ID,用于删除,没有提交过来的就是要删除的
  130. $updateIds = [];
  131. foreach ($value['setting'] AS $k => $v) {
  132. $v['status'] = $status;
  133. if (!empty($v['sort'])) $sort = $v['sort'];
  134. # 编辑
  135. if (!empty($v['number_sequence_id'])) {
  136. $updateIds[] = $v['number_sequence_id'];
  137. Db::name('crm_number_sequence')->update($v);
  138. }
  139. # 新增
  140. if (empty($v['number_sequence_id'])) {
  141. $increaseNumber = !empty($v['increase_number']) ? $v['increase_number'] : 1;
  142. $reset = !empty($v['reset']) ? $v['reset'] : 0;
  143. $insertData =[
  144. 'sort' => $sort + 1,
  145. 'type' => $v['type'],
  146. 'value' => $v['value'],
  147. 'increase_number' => $v['type'] == 3 ? $increaseNumber : null,
  148. 'reset' => $v['type'] == 3 ? $reset : null,
  149. 'create_time' => time(),
  150. 'create_user_id' => $apiCommon->userInfo['id'],
  151. 'status' => $v['status'],
  152. 'number_type' => $value['number_type']
  153. ];
  154. Db::name('crm_number_sequence')->insert($insertData);
  155. }
  156. }
  157. # 删除
  158. $sequenceIds = array_diff($sequenceIds, $updateIds);
  159. if (!empty($sequenceIds)) Db::name('crm_number_sequence')->whereIn('number_sequence_id', $sequenceIds)->delete();
  160. }
  161. Db::commit();
  162. # 系统操作日志
  163. SystemActionLog($userId, 'crm_number_sequence','customer', 1, 'update','编号规则设置' , '', '','设置了编号规则');
  164. return true;
  165. } catch (Exception $e) {
  166. Db::rollback();
  167. return false;
  168. }
  169. }
  170. /**
  171. * 导航列表
  172. * @param $param
  173. *
  174. * @author alvin guogaobo
  175. * @version 1.0 版本号
  176. * @since 2021/6/22 0022 11:39
  177. */
  178. public function appMenuConfig($param)
  179. {
  180. $data = db('app_navigation')->where('create_user_id', $param)->value('data');
  181. $data = unserialize($data);
  182. return $data?:[];
  183. }
  184. /**
  185. * 修改导航信息
  186. * @param $param
  187. * @param $userId
  188. *
  189. * @author alvin guogaobo
  190. * @version 1.0 版本号
  191. * @since 2021/6/22 0022 14:49
  192. */
  193. public function setMenuConfig($param,$userId){
  194. $data = db('app_navigation')->where('create_user_id', $userId)->value('data');
  195. $res=[];
  196. $res['data']=serialize($param);
  197. if($data){
  198. $list=db('app_navigation')->where('create_user_id', $userId)->update($res);
  199. }else{
  200. $res['create_user_id']=$userId;
  201. $list=db('app_navigation')->insertGetId($res);
  202. }
  203. return $list;
  204. }
  205. }