Item.php 6.1 KB

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