StoreUnion.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商家管理
  7. */
  8. namespace app\allwin\model;
  9. use think\Model;
  10. use util\Util;
  11. use app\allwin\model\AllwinStore;
  12. class StoreUnion extends Model{
  13. protected $pk = 'id';
  14. protected $table = 'ai_allwin_store_union';
  15. //联盟城市
  16. public function store(){
  17. return $this->hasOne('AllwinStore','id','store_id');
  18. }
  19. /**
  20. * ######################
  21. * 联合商家管理
  22. */
  23. public static function unionList(int $store_id){
  24. $rel = self::where(['store_id' => $store_id])->find();
  25. if(empty($rel->store_union)){
  26. return false;
  27. }
  28. $store_union = json_decode($rel->store_union,true);
  29. if(empty($store_union)){
  30. return false;
  31. }
  32. $rel = AllwinStore::where(['id' => $store_union,'is_lock' => 0])->field('id,name,img,address,telphone,score,tips,tags,is_top,state_text')->order('sort desc,id desc')->paginate(10)->toArray();
  33. if(empty($rel['data'])){
  34. return false;
  35. }
  36. $data = $rel['data'];
  37. foreach ($rel['data'] as $key => $value) {
  38. $data[$key]['tags'] = explode(',',$value['tags']);
  39. $data[$key]['img'] = $value['img']."?x-oss-process=style/w100";
  40. }
  41. return $data;
  42. }
  43. /**
  44. * 编辑修改联盟店铺
  45. */
  46. public static function unionEdit(int $store_id,int $union_id){
  47. $union = [];
  48. $union[] = $union_id;
  49. $rel = self::where(['store_id' => $store_id])->find();
  50. $data = [];
  51. if(empty($rel)){
  52. $data['store_id'] = $store_id;
  53. $data['store_union'] = json_encode($union);
  54. return self::insert($data);
  55. }else{
  56. $store_union = json_decode($rel->store_union,true);
  57. $key = array_flip($store_union);
  58. if(array_key_exists($union_id,$key)){
  59. unset($key[$union_id]);
  60. $store_union = array_flip($key);
  61. $store = [];
  62. foreach ($store_union as $value) {
  63. $store[] = $value;
  64. }
  65. sort($store);
  66. $data['store_union'] = json_encode($store);
  67. }else{
  68. $store_union = Util::unique_array(array_merge($store_union,$union));
  69. $data['store_union'] = json_encode($store_union);
  70. }
  71. return self::where(['store_id' => $store_id])->update($data);
  72. }
  73. }
  74. /**
  75. * 删除联合商家
  76. */
  77. public static function unionDelete(array $where){
  78. return self::where($where)->delete();
  79. }
  80. }