OperationLog.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Exception;
  4. use think\Model;
  5. use think\Session;
  6. use traits\model\SoftDelete;
  7. /**
  8. *操作记录
  9. */
  10. class OperationLog Extends Model {
  11. use SoftDelete;
  12. // 表名,不含前缀
  13. protected $name = 'qingdong_operation_log';
  14. const CUSTOMER_TYPE = 1;//客户
  15. const CONTACTS_TYPE = 2;//联系人
  16. const CONTRACT_TYPE = 3;//合同
  17. const LEADS_TYPE = 4;//线索
  18. const RECEIVABLES_TYPE = 5;//回款记录
  19. const WORKORDER_TYPE = 6;//工单
  20. const APPROVAL_TYPE = 7;//const
  21. const BUSINESS_TYPE = 8;//商机
  22. // 开启自动写入时间戳字段
  23. protected $autoWriteTimestamp = 'int';
  24. // 定义时间戳字段名
  25. protected $createTime = 'createtime';
  26. protected $updateTime = 'updatetime';
  27. protected $deleteTime = 'deletetime';
  28. public function getCreatetimeAttr($value){
  29. return date('Y-m-d H:i',$value);
  30. }
  31. //获取跟进记录列表
  32. public static function getList($relation_type, $relation_id) {
  33. return self::where(['relation_type' => $relation_type, 'relation_id' => $relation_id])->with(['staff','admin'])->field('content,operation_id,createtime,operation_type')->order('id desc')->select();
  34. }
  35. //员工
  36. public function staff() {
  37. return $this->hasOne(Staff::class, 'id', 'operation_id')->removeOption('soft_delete')->field('id,name,img');
  38. }
  39. //员工
  40. public function admin() {
  41. return $this->hasOne(\app\admin\model\Admin::class, 'id', 'operation_id')->field('id,nickname');
  42. }
  43. //创建日志
  44. public static function createLog($relation_type, $relation_id, $content) {
  45. $staff = Staff::info();
  46. if(empty($staff)){
  47. $admin = Session::get('admin');
  48. $data = [
  49. 'content' => $content,
  50. 'operation_type' => 2,
  51. 'operation_id' => $admin['id']??0,
  52. 'relation_type' => $relation_type,
  53. 'relation_id' => $relation_id,
  54. ];
  55. }else{
  56. $data = [
  57. 'content' => $content,
  58. 'operation_type' => 1,
  59. 'operation_id' => $staff->id,
  60. 'relation_type' => $relation_type,
  61. 'relation_id' => $relation_id,
  62. ];
  63. }
  64. $Enent = new self;
  65. $result = $Enent->save($data);
  66. if (false === $result) {
  67. // 验证失败 输出错误信息
  68. throw new Exception($Enent->getError());
  69. }
  70. return true;
  71. }
  72. }