SaleHouse.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * 商品管理(SPU)
  7. */
  8. namespace app\popupshop\model;
  9. use think\Model;
  10. use filter\Filter;
  11. class SaleHouse extends Model{
  12. protected $pk = 'id';
  13. protected $table = 'ai_popupshop_sales_house';
  14. protected $autoWriteTimestamp = true;
  15. protected $createTime = false;
  16. /**
  17. * 商品基础库
  18. * @return void
  19. */
  20. public function Category(){
  21. return $this->hasOne('SaleCategory','id','category_id');
  22. }
  23. /**
  24. * 搜索封装
  25. */
  26. public function searchNameAttr($query, $value, $data){
  27. $query->where('name','like','%' . $value . '%');
  28. }
  29. /**
  30. * 读取商品列表
  31. * @param string $status
  32. * @param string $keyword
  33. * @return void
  34. */
  35. public static function list(int $miniapp_id,string $status = '',string $keyword = null,$input = null){
  36. $condition = [];
  37. switch ($status) {
  38. case 'trash': //回收站
  39. $condition['is_del'] = 1;
  40. break;
  41. case 'off_sale': //在售
  42. $condition['is_sale'] = 1;
  43. $condition['is_del'] = 0;
  44. break;
  45. case 'on_sale': //下架
  46. $condition['is_sale'] = 0;
  47. $condition['is_del'] = 0;
  48. break;
  49. default: //在售
  50. $condition['is_del'] = 0;
  51. break;
  52. }
  53. if(!empty($keyword)){
  54. $keyword = Filter::filter_escape($keyword);
  55. $condition[] = ["name","like","%{$keyword}%"];
  56. }
  57. return self::where(['member_miniapp_id' => $miniapp_id])->where($condition)->order('id desc')->paginate(20,false,['query'=>['status' => $status,'keyword' => $keyword,'input'=> $input]]);
  58. }
  59. /**
  60. * 检测单个SPU商品是否上架
  61. * @param integer $spu_id
  62. * @return void
  63. */
  64. public static function isSell(int $spu_id){
  65. return self::where(['id' => $spu_id,'is_sale'=>1])->find();
  66. }
  67. //添加或编辑
  68. public static function edit(int $miniapp_id,array $param){
  69. $data['member_miniapp_id'] = $miniapp_id;
  70. $data['category_id'] = $param['category_id'];
  71. $data['title'] = $param['title'];
  72. $data['name'] = $param['name'];
  73. $data['note'] = $param['note'];
  74. $data['sell_price'] = $param['sell_price'];
  75. $data['cost_price'] = $param['cost_price'];
  76. $data['content'] = $param['content'];
  77. $data['img'] = $param['img'];
  78. $data['imgs'] = json_encode($param['imgs']);
  79. $data['update_time'] = time();
  80. if(isset($param['id'])){
  81. return self::where('id',$param['id'])->update($data);
  82. }else{
  83. $data['is_sale'] = 0;
  84. return self::insertGetId($data);
  85. }
  86. }
  87. //批量操作
  88. public static function ids_action(int $is_sale,string $ids){
  89. switch ($is_sale) {
  90. case 1:
  91. $data['is_sale'] = 1; //上架
  92. break;
  93. case 2:
  94. $data['is_del'] = 0;
  95. break;
  96. default:
  97. $data['is_sale'] = 0; //下架
  98. break;
  99. }
  100. return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
  101. }
  102. /**
  103. * 删除SPU商品
  104. *
  105. * @param [type] $id
  106. * @param [type] $ids
  107. * @return void
  108. */
  109. public static function deletes(int $id,$ids){
  110. if(!empty($id)){
  111. $ids = (int)$id;
  112. }elseif(!empty($ids)){
  113. $ids = ids($ids);
  114. }else{
  115. return false;
  116. }
  117. $rel = self::whereIn('id',$ids)->field('is_del,id,imgs,img,content')->select()->toArray();
  118. if(!empty($rel)){
  119. $del_data = [];
  120. $up_data = [];
  121. foreach ($rel as $value) {
  122. if($value['is_del'] == 1){
  123. $del_data[] = $value['id'];
  124. $imgs[] = json_decode($value['imgs']);
  125. $content[] = $value['content'];
  126. $src[] = $value['img'];
  127. }else{
  128. $up_data[] = $value['id'];
  129. }
  130. }
  131. if(!empty($del_data)){
  132. $img = [];
  133. array_walk_recursive($imgs, function($value) use (&$img) {
  134. array_push($img,$value);
  135. });
  136. $imgc = [];
  137. array_walk_recursive($content, function($value) use (&$imgc) {
  138. array_push($imgc, $value);
  139. });
  140. foreach ($img as $value) {
  141. foreach ($src as $rs) {
  142. if($value != $rs){
  143. $str = PATH_PUBLIC.$value;
  144. if(file_exists($str)){
  145. unlink($str);
  146. }
  147. }
  148. }
  149. }
  150. self::whereIn('id',$del_data)->delete();
  151. }
  152. if(!empty($up_data)){
  153. self::whereIn('id',$up_data)->update(['is_del' => 1]);
  154. }
  155. }
  156. return true;
  157. }
  158. }