Item.php 5.7 KB

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