0
0

UserConveyCate.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use tools\Phptree;
  5. class UserConveyCate extends Base
  6. {
  7. // 更新用户常用运送类型
  8. public function updateCate($userId,$conveyCateId){
  9. $info = Db::name('user_convey_cate')->where('user_id',$userId)->where('convey_cate_id',$conveyCateId)->find();
  10. if($info){
  11. $ret = Db::name('user_convey_cate')->where('id',$info['id'])->setInc('nums',1);
  12. }else{
  13. $ret = Db::name('user_convey_cate')->insert([
  14. 'user_id' => $userId,
  15. 'convey_cate_id' => $conveyCateId,
  16. 'nums' => 1
  17. ]);
  18. }
  19. return $ret?true:false;
  20. }
  21. // 获取用户常用运送类型
  22. public function getUserCates($userId){
  23. $lists = Db::name('user_convey_cate')->where('user_id',$userId)->order('nums desc,id asc')->column('convey_cate_id');
  24. return $lists?$lists:[];
  25. }
  26. public function sortUserCates($lists,$userId){
  27. $cates = $this->getUserCates($userId);
  28. $cates = $cates?array_unique($cates):[];
  29. if(!$cates){
  30. return $lists;
  31. }
  32. $list1 = [];
  33. foreach ($cates as $k=>$v){
  34. foreach ($lists as $k1=>$v1){
  35. if($v1['id'] == $v){
  36. $list1[] = $v1;
  37. break;
  38. }
  39. }
  40. }
  41. foreach ($lists as $k=>$v){
  42. if(!in_array($v['id'],$cates)){
  43. $list1[] = $v;
  44. }
  45. }
  46. return $list1;
  47. }
  48. }