Setting.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 客户模块设置
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace addons\qingdongams\controller;
  8. use addons\qingdongams\model\Customer;
  9. use addons\qingdongams\model\OperationLog;
  10. use addons\qingdongams\model\Staff;
  11. use addons\qingdongams\model\Message;
  12. use think\Db;
  13. use think\Exception;
  14. /**
  15. * 客户模块设置
  16. */
  17. class Setting extends StaffApi
  18. {
  19. /**
  20. * 获取团队成员
  21. */
  22. public function team()
  23. {
  24. $id = input('id');
  25. $customer = Customer::get($id);
  26. if (empty($customer)) {
  27. $this->error('客户不存在');
  28. }
  29. $owner_staff = Staff::where(['id' => $customer->owner_staff_id])->find();
  30. $rw_staffs = Staff::where(['id' => ['in', explode(',', $customer->rw_staff_id)],'status'=>1])->select();
  31. $ro_staffs = Staff::where(['id' => ['in', explode(',', $customer->ro_staff_id)],'status'=>1])->select();
  32. $staffs = [];
  33. if ($owner_staff) {
  34. $staffs[] = [
  35. 'id' => $owner_staff->id,
  36. 'name' => $owner_staff->name,
  37. 'img' => $owner_staff->img,
  38. 'post' => $owner_staff->post,
  39. 'mobile' => $owner_staff->mobile,
  40. 'roles' => 1,
  41. 'is_edit' => 1,
  42. ];
  43. }
  44. foreach ($rw_staffs as $v) {
  45. $staffs[] = [
  46. 'id' => $v->id,
  47. 'name' => $v->name,
  48. 'img' => $v->img,
  49. 'post' => $v->post,
  50. 'mobile' => $v->mobile,
  51. 'roles' => 2,
  52. 'is_edit' => 1,
  53. ];
  54. }
  55. foreach ($ro_staffs as $v) {
  56. $staffs[] = [
  57. 'id' => $v->id,
  58. 'name' => $v->name,
  59. 'img' => $v->img,
  60. 'post' => $v->post,
  61. 'mobile' => $v->mobile,
  62. 'roles' => 2,
  63. 'is_edit' => 0,
  64. ];
  65. }
  66. $this->success('请求成功', $staffs);
  67. }
  68. /**
  69. * 修改团队成员
  70. */
  71. public function editShowStaff()
  72. {
  73. $id = input('id');
  74. $staff = input('staff/a');
  75. $model = Customer::get($id);
  76. if (empty($model)) {
  77. $this->error('客户不存在');
  78. }
  79. $ro_staff_id = [];
  80. $rw_staff_id = [];
  81. foreach ($staff as $v) {
  82. if ($v['is_edit'] == 1) {
  83. $rw_staff_id[] = $v['id'];
  84. } else {
  85. $ro_staff_id[] = $v['id'];
  86. }
  87. }
  88. $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
  89. $rw_staff_id=implode(',', $rw_staff_id);
  90. $ro_staff_id=implode(',', $ro_staff_id);
  91. $result = $model->save(['rw_staff_id' => ",{$rw_staff_id},",
  92. 'ro_staff_id' => ",{$ro_staff_id},"]);
  93. if ($result === false) {
  94. $this->error('修改失败');
  95. }
  96. $this->success('修改成功');
  97. }
  98. //批量新增联合跟进人
  99. public function batchAddShowStaff()
  100. {
  101. $ids = input('ids');
  102. $staff_id = input('staff_id');
  103. $is_edit = input('is_edit', 0);
  104. if(empty($ids)){
  105. $this->error('参数不能为空');
  106. }
  107. $ids=explode(',',$ids);
  108. $customers = Customer::where(['id'=>['in',$ids]])->select();
  109. if (empty($customers)) {
  110. $this->error('客户不存在');
  111. }
  112. $staff_id = explode(',', $staff_id);
  113. $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
  114. $sname=implode(',',$sname);
  115. Db::startTrans();
  116. try {
  117. foreach ($customers as $customer){
  118. foreach ($staff_id as $sid) {
  119. $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
  120. $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
  121. if ($is_edit == 1) {
  122. if(in_array($sid,$rw_staff_id)){
  123. continue;
  124. }
  125. $rw_staff_id[]=$sid;
  126. } else {
  127. if(in_array($sid,$ro_staff_id)){
  128. continue;
  129. }
  130. $ro_staff_id[]=$sid;
  131. }
  132. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
  133. '批量新增联合跟进人:'.$sname);
  134. Message::addMessage(Message::CUSTOMER_TYPE, $customer['id'], $sid, $this->auth->id, $this->auth->name . '邀请您联合跟进客户《' . $customer['name'] . '》');
  135. }
  136. $model=new Customer();
  137. $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
  138. $rw_staff_id=implode(',', $rw_staff_id);
  139. $ro_staff_id=implode(',', $ro_staff_id);
  140. $model->save(['rw_staff_id' => ",{$rw_staff_id},",
  141. 'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
  142. }
  143. Db::commit();
  144. }catch (Exception $e){
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. $this->success('添加成功');
  149. }
  150. //批量删除联合跟进人
  151. public function batchDelShowStaff()
  152. {
  153. $ids = input('ids');
  154. $staff_id = input('staff_id');
  155. $is_edit = input('is_edit', 0);
  156. if(empty($ids)){
  157. $this->error('参数不能为空');
  158. }
  159. $ids=explode(',',$ids);
  160. $customers = Customer::where(['id'=>['in',$ids]])->select();
  161. if (empty($customers)) {
  162. $this->error('客户不存在');
  163. }
  164. $staff_id = explode(',', $staff_id);
  165. $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
  166. $sname=implode(',',$sname);
  167. Db::startTrans();
  168. try {
  169. foreach ($customers as $customer){
  170. foreach ($staff_id as $sid) {
  171. $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
  172. $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
  173. if ($is_edit == 1) {
  174. if(in_array($sid,$rw_staff_id)){
  175. continue;
  176. }
  177. $rw_staff_id = array_diff($rw_staff_id, [$sid]);
  178. } else {
  179. if(!in_array($sid,$ro_staff_id)){
  180. continue;
  181. }
  182. $ro_staff_id = array_diff($ro_staff_id, [$sid]);
  183. }
  184. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
  185. '批量新增联合跟进人:'.$sname);
  186. }
  187. $model=new Customer();
  188. $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
  189. $rw_staff_id=implode(',', $rw_staff_id);
  190. $ro_staff_id=implode(',', $ro_staff_id);
  191. $model->save(['rw_staff_id' => ",{$rw_staff_id},",
  192. 'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
  193. }
  194. Db::commit();
  195. }catch (Exception $e){
  196. Db::rollback();
  197. $this->error($e->getMessage());
  198. }
  199. $this->success('删除成功');
  200. }
  201. }