Sync.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php namespace App\Models\WeiBan;
  2. use App\Facades\Servers\Redis\RedisLock;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. /**
  6. * 客户同步模型
  7. *
  8. */
  9. class Sync extends Model
  10. {
  11. use HasFactory;
  12. // 与模型关联的表名
  13. protected $table = 'weiban_sync';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. $data['insert_time'] = time();
  27. $data['update_time'] = time();
  28. // 写入数据表
  29. $id = $this->query()->insertGetId($data);
  30. // 如果操作失败
  31. if( !$id ) return $id;
  32. // 更新缓存
  33. $this->getList(true);
  34. // 返回结果
  35. return $id;
  36. }
  37. /**
  38. * 添加数据
  39. *
  40. */
  41. public function edit($id,$data)
  42. {
  43. // 更新时间
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $result = $this->query()->where(['id'=>$id])->update($data);
  47. // 如果操作失败
  48. if( !$result ) return $result;
  49. // 更新缓存
  50. $this->getList(true);
  51. // 返回结果
  52. return $result;
  53. }
  54. /**
  55. * 获取列表
  56. * @param Bool $force 是否强制更新
  57. *
  58. */
  59. public function getList($force = false)
  60. {
  61. // 结果数据
  62. $list = $force ? [] : cache('admin:weiban:sync:list');
  63. // 不存在数据
  64. if ( !$list ) {
  65. // 从数据库获取数据
  66. $data = $this->query()->where([['status','=',0]])->get(['id','source','limit','offset','sync_total','total','status','last_time','insert_time','update_time']);
  67. // 是否有数据
  68. $data = $data ? $data->toArray() : [];
  69. // 循环处理数据
  70. $list = [];
  71. // 进行更新
  72. foreach ($data as $value) {
  73. // 重组数据
  74. $list[$value['source']][$value['id']] = $value;
  75. }
  76. // 存起来
  77. cache(['admin:weiban:sync:list'=>$list]);
  78. }
  79. // 返回结果
  80. return $list;
  81. }
  82. /**
  83. * 获取对应更新维度的任务
  84. *
  85. * @param string $source 更新维度
  86. *
  87. */
  88. public function getOneBySource($source)
  89. {
  90. // 获取列表数据
  91. $list = $this->getList();
  92. // 如果有对应的更新维度任务,直接返回
  93. if( isset($list[$source]) ) return array_pop($list[$source]);
  94. // 获取最近的开始时间
  95. $lastTime = $this->query()->where([['source','=',$source]])->orderByDesc('last_time')->orderByDesc('id')->value('last_time');
  96. // 没有时间
  97. $lastTime = $lastTime ? $lastTime : 0;
  98. // 创建任务,每分钟定义120条数据,避免超出时间限制
  99. $task = ['limit'=>config('weiban_sync_limit',100),'offset'=>0,'source'=>$source,'sync_total'=>0,'total'=>0,'last_time'=>$lastTime,'insert_time'=>time(),'update_time'=>time()];
  100. // 返回结果
  101. $task['id'] = $this->add($task);
  102. // 返回值
  103. return $task;
  104. }
  105. /**
  106. * 上锁
  107. * @param string $extId 微伴外部ID
  108. *
  109. */
  110. function lockSyncExtidMark($extId){
  111. // 队列标记
  112. $syncMark = 'weiban:sync:extid:mark:'.$extId;
  113. // 多长时间
  114. return RedisLock::lock($syncMark,$extId,86400);
  115. }
  116. /**
  117. * 解锁
  118. * @param string $extId 微伴外部ID
  119. *
  120. */
  121. function unLockSyncExtidMark($extId){
  122. // 队列标记
  123. $syncMark = 'weiban:sync:extid:mark:'.$extId;
  124. // 多长时间
  125. return RedisLock::unlock($syncMark,$extId);
  126. }
  127. }