State.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php namespace App\Models\Work;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 企业自定义的state参数记录表
  6. *
  7. */
  8. class State extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'work_state';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 如果操作失败
  30. if( !$id ) return $id;
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id,$data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where(['id'=>$id])->update($data);
  44. // 如果操作失败
  45. if( !$result ) return $result;
  46. // 返回结果
  47. return $result;
  48. }
  49. /**
  50. * 获取列表
  51. * @param Bool $force 是否强制更新
  52. *
  53. */
  54. public function getList()
  55. {
  56. // 结果数据
  57. $list = $this->query()->get(['id','remark','config_id','is_follow','qr_code','tags','user','user_type','custom_uid']);
  58. // 返回结果
  59. return $list;
  60. }
  61. /**
  62. * 单个
  63. *
  64. * @param int ID
  65. * @param string 指定字段
  66. *
  67. */
  68. public function getOne($id,$field='')
  69. {
  70. // 获取列表数据
  71. $one = $this->query()->where([['id','=',$id]])->first(['id','remark','config_id','is_follow','qr_code','tags','user','user_type','custom_uid']);
  72. // 获取数据
  73. $one = $one ? $one->toArray() : [];
  74. // 返回值
  75. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  76. }
  77. /**
  78. * 推送客服专用
  79. *
  80. */
  81. public function getFollowQrcode($force = false){
  82. // 获取结果
  83. $qrcode = $force ? null : cache('admin:work:state:follow:qrcode');
  84. // 判断结果
  85. if( is_null($qrcode) ){
  86. // 获取列表数据
  87. $qrcode = $this->query()->where([['is_follow','=',1]])->value('qr_code');
  88. // 存起来
  89. cache(['admin:work:state:follow:qrcode'=>$qrcode],now()->addHours(2));
  90. }
  91. // 返回值
  92. return $qrcode;
  93. }
  94. }