OperationLog.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace addons\qingdongams\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 = 'qingdongams_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 BUSINESS_TYPE = 6;//商机
  20. const QUOTE_TYPE = 7;//报价单
  21. const WORKORDER_TYPE = 8;//工单
  22. const APPROVAL_TYPE = 9;//审批
  23. // 开启自动写入时间戳字段
  24. protected $autoWriteTimestamp = 'int';
  25. // 定义时间戳字段名
  26. protected $createTime = 'createtime';
  27. protected $updateTime = 'updatetime';
  28. protected $deleteTime = 'deletetime';
  29. public function getCreatetimeAttr($value){
  30. return date('Y-m-d H:i',$value);
  31. }
  32. //获取跟进记录列表
  33. public static function getList($relation_type, $relation_id) {
  34. 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();
  35. }
  36. //员工
  37. public function staff() {
  38. return $this->hasOne(Staff::class, 'id', 'operation_id')->removeOption('soft_delete')->field('id,name,img');
  39. }
  40. //员工
  41. public function admin() {
  42. return $this->hasOne(\app\admin\model\Admin::class, 'id', 'operation_id')->field('id,nickname');
  43. }
  44. //创建日志
  45. public static function createLog($relation_type, $relation_id, $content) {
  46. $staff = Staff::info();
  47. if(empty($staff)){
  48. $admin = Session::get('admin');
  49. $data = [
  50. 'content' => $content,
  51. 'operation_type' => 2,
  52. 'operation_id' => $admin['id']??0,
  53. 'relation_type' => $relation_type,
  54. 'relation_id' => $relation_id,
  55. ];
  56. }else{
  57. $data = [
  58. 'content' => $content,
  59. 'operation_type' => 1,
  60. 'operation_id' => $staff->id,
  61. 'relation_type' => $relation_type,
  62. 'relation_id' => $relation_id,
  63. ];
  64. }
  65. $Enent = new self;
  66. $result = $Enent->save($data);
  67. if (false === $result) {
  68. // 验证失败 输出错误信息
  69. throw new Exception($Enent->getError());
  70. }
  71. return true;
  72. }
  73. }