123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php namespace App\Models\WeiBan;
- use App\Facades\Servers\Redis\RedisLock;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 客户同步模型
- *
- */
- class Sync extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'weiban_sync';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 如果操作失败
- if( !$id ) return $id;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 如果操作失败
- if( !$result ) return $result;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $result;
- }
- /**
- * 获取列表
- * @param Bool $force 是否强制更新
- *
- */
- public function getList($force = false)
- {
- // 结果数据
- $list = $force ? [] : cache('admin:weiban:sync:list');
- // 不存在数据
- if ( !$list ) {
- // 从数据库获取数据
- $data = $this->query()->where([['status','=',0]])->get(['id','source','limit','offset','sync_total','total','status','last_time','insert_time','update_time']);
- // 是否有数据
- $data = $data ? $data->toArray() : [];
- // 循环处理数据
- $list = [];
- // 进行更新
- foreach ($data as $value) {
- // 重组数据
- $list[$value['source']][$value['id']] = $value;
- }
- // 存起来
- cache(['admin:weiban:sync:list'=>$list]);
- }
- // 返回结果
- return $list;
- }
- /**
- * 获取对应更新维度的任务
- *
- * @param string $source 更新维度
- *
- */
- public function getOneBySource($source)
- {
- // 获取列表数据
- $list = $this->getList();
- // 如果有对应的更新维度任务,直接返回
- if( isset($list[$source]) ) return array_pop($list[$source]);
- // 获取最近的开始时间
- $lastTime = $this->query()->where([['source','=',$source]])->orderByDesc('last_time')->orderByDesc('id')->value('last_time');
- // 没有时间
- $lastTime = $lastTime ? $lastTime : 0;
- // 创建任务,每分钟定义120条数据,避免超出时间限制
- $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()];
- // 返回结果
- $task['id'] = $this->add($task);
- // 返回值
- return $task;
- }
- /**
- * 上锁
- * @param string $extId 微伴外部ID
- *
- */
- function lockSyncExtidMark($extId){
- // 队列标记
- $syncMark = 'weiban:sync:extid:mark:'.$extId;
- // 多长时间
- return RedisLock::lock($syncMark,$extId,86400);
- }
- /**
- * 解锁
- * @param string $extId 微伴外部ID
- *
- */
- function unLockSyncExtidMark($extId){
- // 队列标记
- $syncMark = 'weiban:sync:extid:mark:'.$extId;
- // 多长时间
- return RedisLock::unlock($syncMark,$extId);
- }
- }
|