MobileSub.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\Admin;
  4. use think\Db;
  5. use think\db\Query;
  6. use think\Model;
  7. /**
  8. * 配置模型
  9. * @method Query admin($adminId)
  10. */
  11. class MobileSub extends Model
  12. {
  13. protected function scopeAdmin(Query $query,$adminId){
  14. $query->where('admin_id',$adminId);
  15. }
  16. public static function getBy(Mobile $mobile,Admin $admin){
  17. $one=self::where('mobile_id',$mobile['id'])->where('sub_admin_id',$admin['id'])->find();
  18. if(!$one){
  19. $sort=self::where('sub_admin_id',$admin['id'])->max('sub_sort')?:1;
  20. $one=self::create([
  21. 'mobile_id'=>$mobile['id'],
  22. 'sub_sort'=>$sort,
  23. 'sub_admin_id'=>$admin['id'],
  24. 'sub_top_time'=>0,
  25. 'sub_rec_time'=>0,
  26. ]);
  27. }
  28. return $one;
  29. }
  30. protected static function init()
  31. {
  32. self::beforeUpdate(function (self $mobileSub){
  33. $change=$mobileSub->getChangedData();
  34. if(isset($change['sub_sort'])){
  35. $mobileSub->makeSort();
  36. }
  37. if($mobileSub['sub_rec_time']){
  38. $mobileSub['sub_sort_line']=-10;
  39. }
  40. if($mobileSub['sub_top_time']){
  41. $mobileSub['sub_sort_line']=-20;
  42. }
  43. if(!in_array($mobileSub['sub_sort_line'],[-10,-20])){
  44. $mobileSub['sub_sort_line']=0;
  45. }
  46. });
  47. }
  48. public function makeSort()
  49. {
  50. $sort=$this['sub_sort'];
  51. $has=self::where('mobile_sub_id','<>',$this['id'])->where('sub_sort',$sort)->find();
  52. if($has) {
  53. self::where('sort', '>=', $sort)->setInc('sub_sort');
  54. self::where('sub_top_time',0)->where('sub_rec_time',0)->update([
  55. 'sub_sort_line'=>Db::raw('sub_sort')
  56. ]);
  57. }
  58. }
  59. }