123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use think\Db;
- use think\db\Query;
- use think\Model;
- /**
- * 配置模型
- * @method Query admin($adminId)
- */
- class MobileSub extends Model
- {
- protected function scopeAdmin(Query $query,$adminId){
- $query->where('admin_id',$adminId);
- }
- public static function getBy(Mobile $mobile,Admin $admin){
- $one=self::where('mobile_id',$mobile['id'])->where('sub_admin_id',$admin['id'])->find();
- if(!$one){
- $sort=self::where('sub_admin_id',$admin['id'])->max('sub_sort')?:1;
- $one=self::create([
- 'mobile_id'=>$mobile['id'],
- 'sub_sort'=>$sort,
- 'sub_admin_id'=>$admin['id'],
- 'sub_top_time'=>0,
- 'sub_rec_time'=>0,
- ]);
- }
- return $one;
- }
- protected static function init()
- {
- self::beforeUpdate(function (self $mobileSub){
- $change=$mobileSub->getChangedData();
- if(isset($change['sub_sort'])){
- $mobileSub->makeSort();
- }
- if($mobileSub['sub_rec_time']){
- $mobileSub['sub_sort_line']=-10;
- }
- if($mobileSub['sub_top_time']){
- $mobileSub['sub_sort_line']=-20;
- }
- if(!in_array($mobileSub['sub_sort_line'],[-10,-20])){
- $mobileSub['sub_sort_line']=0;
- }
- });
- }
- public function makeSort()
- {
- $sort=$this['sub_sort'];
- $has=self::where('mobile_sub_id','<>',$this['id'])->where('sub_sort',$sort)->find();
- if($has) {
- self::where('sort', '>=', $sort)->setInc('sub_sort');
- self::where('sub_top_time',0)->where('sub_rec_time',0)->update([
- 'sub_sort_line'=>Db::raw('sub_sort')
- ]);
- }
- }
- }
|