CustomerContact.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. class CustomerContact extends Model
  17. {
  18. /**
  19. * 获取分页列表
  20. * @param $where
  21. * @param $param
  22. */
  23. public function datalist($param,$where,$whereOr=[])
  24. {
  25. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  26. $order = empty($param['order']) ? 'id desc' : $param['order'];
  27. try {
  28. $list = self::where($where)
  29. ->where(function ($query) use($whereOr) {
  30. if (!empty($whereOr)){
  31. $query->whereOr($whereOr);
  32. }
  33. })
  34. ->order($order)
  35. ->paginate(['list_rows'=> $rows])
  36. ->each(function ($item, $key){
  37. $item->admin_name = Db::name('Admin')->where('id',$item->admin_id)->value('name');
  38. $item->customer = Db::name('Customer')->where(['id' => $item->cid])->value('name');
  39. $item->create_time = to_date($item->create_time);
  40. });
  41. return $list;
  42. } catch(\Exception $e) {
  43. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  44. }
  45. }
  46. /**
  47. * 添加数据
  48. * @param $param
  49. */
  50. public function add($param)
  51. {
  52. $insertId = 0;
  53. try {
  54. $param['create_time'] = time();
  55. $insertId = self::strict(false)->field(true)->insertGetId($param);
  56. add_log('add', $insertId, $param);
  57. } catch(\Exception $e) {
  58. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  59. }
  60. return to_assign(0,'操作成功',['return_id'=>$insertId]);
  61. }
  62. /**
  63. * 编辑信息
  64. * @param $param
  65. */
  66. public function edit($param)
  67. {
  68. try {
  69. $param['update_time'] = time();
  70. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  71. add_log('edit', $param['id'], $param);
  72. } catch(\Exception $e) {
  73. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  74. }
  75. return to_assign(0,'操作成功',['return_id'=>$param['id']]);
  76. }
  77. /**
  78. * 根据id获取信息
  79. * @param $id
  80. */
  81. public function getById($id)
  82. {
  83. $info = self::find($id);
  84. if(!empty($info['birthday'])){
  85. $info['birthday'] = date('Y-m-d',$info['birthday']);
  86. }
  87. else{
  88. $info['birthday'] = '';
  89. }
  90. $info['customer'] = Db::name('Customer')->where(['id' => $info['cid']])->value('name');
  91. return $info;
  92. }
  93. /**
  94. * 删除信息
  95. * @param $id
  96. * @param $type
  97. * @return array
  98. */
  99. public function delById($id,$type=0)
  100. {
  101. $detail=self::find($id);
  102. if($detail['is_default'] == 1){
  103. $count = Db::name('Customer')->where(['id' => $detail['cid'],'delete_time'=>0])->count();
  104. if($count>0){
  105. return to_assign(1, '首要联系人,不能删除');
  106. }
  107. }
  108. if($type==0){
  109. //逻辑删除
  110. try {
  111. $param['delete_time'] = time();
  112. self::where('id', $id)->update(['delete_time'=>time()]);
  113. add_log('delete', $id);
  114. } catch(\Exception $e) {
  115. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  116. }
  117. }
  118. else{
  119. //物理删除
  120. try {
  121. self::destroy($id);
  122. add_log('delete', $id);
  123. } catch(\Exception $e) {
  124. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  125. }
  126. }
  127. return to_assign();
  128. }
  129. }