Customer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. namespace app\customer\model;
  14. use think\model;
  15. use think\facade\Db;
  16. use app\api\model\EditLog;
  17. class Customer extends Model
  18. {
  19. /**
  20. * 获取分页列表
  21. * @param $where
  22. * @param $param
  23. */
  24. public function datalist($param=[],$where=[],$whereOr=[])
  25. {
  26. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  27. $order = 'id desc';
  28. if(!empty($param['order_field']) && !empty($param['order_type'])){
  29. $order = $param['order_field'].' '.$param['order_type'];
  30. }
  31. try {
  32. $list = self::where($where)
  33. ->where(function ($query) use($whereOr) {
  34. if (!empty($whereOr)){
  35. $query->whereOr($whereOr);
  36. }
  37. })
  38. ->orderRaw($order)
  39. ->paginate(['list_rows'=> $rows])
  40. ->each(function ($item, $key){
  41. $item->create_time = to_date($item->create_time);
  42. $item->update_time = to_date($item->update_time);
  43. if($item->belong_uid>0){
  44. $item->belong_name = Db::name('Admin')->where('id',$item->belong_uid)->value('name');
  45. $item->belong_department = Db::name('Department')->where(['id' => $item->belong_did])->value('title');
  46. }
  47. else{
  48. $item->belong_name='-';
  49. $item->belong_department='-';
  50. }
  51. if($item->industry_id>0){
  52. $item->industry = Db::name('Industry')->where(['id' => $item->industry_id])->value('title');
  53. }
  54. else{
  55. $item->industry='-';
  56. }
  57. if($item->grade_id>0){
  58. $item->grade = Db::name('CustomerGrade')->where(['id' => $item->grade_id])->value('title');
  59. }
  60. else{
  61. $item->grade='-';
  62. }
  63. if($item->source_id>0){
  64. $item->source = Db::name('CustomerSource')->where(['id' => $item->source_id])->value('title');
  65. }
  66. else{
  67. $item->source='-';
  68. }
  69. if($item->customer_status>0){
  70. $item->customer_status_name = Db::name('BasicCustomer')->where(['id' => $item->customer_status])->value('title');
  71. }
  72. else{
  73. $item->customer_status_name='-';
  74. }
  75. if($item->intent_status>0){
  76. $item->intent_status_name = Db::name('BasicCustomer')->where(['id' => $item->intent_status])->value('title');
  77. }
  78. else{
  79. $item->intent_status_name='-';
  80. }
  81. if($item->follow_time==0){
  82. $item->follow_time='-';
  83. }
  84. else{
  85. $item->follow_time = date('Y-m-d',$item->follow_time);
  86. }
  87. if($item->next_time==0){
  88. $item->next_time='-';
  89. }
  90. else{
  91. $item->next_time = date('Y-m-d',$item->next_time);
  92. }
  93. $contact = Db::name('CustomerContact')->where(['is_default'=>1,'cid' => $item->id])->find();
  94. if(!empty($contact)){
  95. $item->contact_name = $contact['name'];
  96. $item->contact_mobile = $contact['mobile'];
  97. $item->contact_email = $contact['email'];
  98. }
  99. else{
  100. $item->contact_name = '';
  101. $item->contact_mobile = '';
  102. $item->contact_email = '';
  103. }
  104. });
  105. return $list;
  106. } catch(\Exception $e) {
  107. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  108. }
  109. }
  110. /**
  111. * 添加数据
  112. * @param $param
  113. */
  114. public function add($param)
  115. {
  116. $insertId = 0;
  117. try {
  118. $param['create_time'] = time();
  119. $param['update_time'] = time();
  120. $insertId = self::strict(false)->field(true)->insertGetId($param);
  121. $contact = [
  122. 'name' => $param['c_name'],
  123. 'mobile' => $param['c_mobile'],
  124. 'sex' => $param['c_sex'],
  125. 'qq' => $param['c_qq'],
  126. 'wechat' => $param['c_wechat'],
  127. 'email' => $param['c_email'],
  128. 'cid' => $insertId,
  129. 'is_default' => 1,
  130. 'create_time' => time(),
  131. 'admin_id' => $param['admin_id']
  132. ];
  133. Db::name('CustomerContact')->strict(false)->field(true)->insert($contact);
  134. add_log('add', $insertId, $param);
  135. $log=new EditLog();
  136. $log->add('Customer',$insertId);
  137. } catch(\Exception $e) {
  138. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  139. }
  140. return to_assign(0,'操作成功',['return_id'=>$insertId]);
  141. }
  142. /**
  143. * 编辑信息
  144. * @param $param
  145. */
  146. public function edit($param)
  147. {
  148. try {
  149. $param['update_time'] = time();
  150. $old = self::find($param['id']);
  151. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  152. add_log('edit', $param['id'], $param);
  153. $log=new EditLog();
  154. $log->edit('Customer',$param['id'],$param,$old);
  155. } catch(\Exception $e) {
  156. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  157. }
  158. return to_assign(0,'操作成功',['return_id'=>$param['id']]);
  159. }
  160. /**
  161. * 根据id获取信息
  162. * @param $id
  163. */
  164. public function getById($id)
  165. {
  166. $info = self::find($id);
  167. $info['belong_department'] = Db::name('Department')->where(['id' => $info['belong_did']])->value('title');
  168. $info['belong_name'] = Db::name('Admin')->where(['id' => $info['belong_uid']])->value('name');
  169. if(!empty($info['share_ids'])){
  170. $share_names = Db::name('Admin')->where([['id','in',$info['share_ids']]])->column('name');
  171. $info['share_names'] = implode(',',$share_names);
  172. }
  173. return $info;
  174. }
  175. /**
  176. * 删除信息
  177. * @param $id
  178. * @param $type
  179. * @return array
  180. */
  181. public function delById($id,$type=0)
  182. {
  183. if($type==0){
  184. //逻辑删除
  185. try {
  186. self::where('id', $id)->update(['discard_time'=>time()]);
  187. add_log('delete', $id);
  188. } catch(\Exception $e) {
  189. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  190. }
  191. }
  192. else{
  193. //物理删除
  194. try {
  195. self::destroy($id);
  196. add_log('delete', $id);
  197. } catch(\Exception $e) {
  198. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  199. }
  200. }
  201. return to_assign();
  202. }
  203. }