AllwinShop.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\allwin\model;
  9. use app\common\facade\Inform;
  10. use think\Model;
  11. class AllwinShop extends Model{
  12. protected $pk = 'id';
  13. protected $autoWriteTimestamp = true;
  14. //店铺
  15. public function store(){
  16. return $this->hasOne('AllwinStore','id','store_id');
  17. }
  18. //栏目名称
  19. public function cate(){
  20. return $this->hasOne('AllwinShopCate','id','category_id');
  21. }
  22. //选择优惠券
  23. public function coupon(){
  24. return $this->hasOne('Coupon','id','coupon_id');
  25. }
  26. //搜索器
  27. public function searchNameAttr($query,$value){
  28. if(!empty($value)){
  29. $query->where('name','like', '%'.$value .'%');
  30. }
  31. }
  32. /**
  33. * @return \think\model\relation\HasOne
  34. * 觅探
  35. */
  36. public function detective(){
  37. return $this->hasOne('app\common\model\SystemUser','id','detective_uid');
  38. }
  39. /**
  40. * 检测单个SPU商品是否上架
  41. * @param integer $spu_id
  42. * @return void
  43. */
  44. public function isSell(int $spu_id){
  45. return self::where(['id' => $spu_id,'is_sale'=>2])->find();
  46. }
  47. //添加或编辑
  48. public static function edit(array $param){
  49. $data['category_id'] = $param['category_id'];
  50. $data['store_id'] = $param['store_id'];
  51. $data['name'] = $param['name'];
  52. $data['title'] = $param['title'];
  53. $data['types'] = $param['types'];
  54. $data['market_price'] = $param['market_price'];
  55. $data['sell_price'] = $param['sell_price'];
  56. $data['cost_price'] = $param['cost_price'];
  57. $data['share_price'] = $param['share_price'];
  58. $data['vip_price'] = $param['vip_price'];
  59. $data['points'] = $param['points'];
  60. $data['warehouse_num'] = $param['warehouse_num'];
  61. $data['warehouse_sellnum'] = $param['warehouse_sellnum'];
  62. $data['coupon_id'] = $param['coupon_id'];
  63. $data['content'] = $param['content'];
  64. $data['img'] = $param['img'];
  65. $data['imgs'] = json_encode($param['imgs']);
  66. $data['types'] = $param['types'];
  67. $data['group_title'] = $param['group_title'];
  68. $data['group_note'] = $param['group_note'];
  69. $data['group_img'] = $param['group_img'];
  70. $data['keyword'] = $param['keyword'];
  71. $data['notice'] = $param['notice'];
  72. $data['detective_price'] = $param['detective_price'];
  73. $data['detective_uid'] = $param['detective_uid'];
  74. $data['end_time'] = empty($param['end_time']) ? '' : strtotime($param['end_time']);
  75. $data['update_time'] = time();
  76. if(isset($param['id'])){
  77. self::where('id',$param['id'])->update($data);
  78. return $param['id'];
  79. }else{
  80. $data['is_sale'] = 0;
  81. $data['member_miniapp_id'] = $param['member_miniapp_id'];
  82. return self::insertGetId($data);
  83. }
  84. }
  85. //添加或编辑
  86. public static function editItem(array $param){
  87. $param['update_time'] = time();
  88. $param['is_sale'] = 0;
  89. if(isset($param['id'])){
  90. $id = (int)$param['id'];
  91. self::where('id',$id)->update($param);
  92. return $id;
  93. }else{
  94. return self::insertGetId($param);
  95. }
  96. }
  97. //批量操作
  98. public static function ids_action(int $is_sale,string $ids){
  99. switch ($is_sale) {
  100. case 1:
  101. $data['is_sale'] = 1; //在售
  102. break;
  103. case 2:
  104. $data['is_del'] = 1; //删除
  105. break;
  106. default:
  107. $data['is_sale'] = 0;
  108. $data['is_del'] = 0;
  109. break;
  110. }
  111. return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
  112. }
  113. /**
  114. * 删除SPU商品
  115. * @param [type] $id
  116. * @param [type] $ids
  117. * @return void
  118. */
  119. public static function ids_delete(int $id,$ids){
  120. if(empty($ids)){
  121. $ids = (int)$id;
  122. }else{
  123. $ids = ids($ids);
  124. }
  125. $rel = self::whereIn('id',$ids)->field('is_del,id')->select()->toArray();
  126. if(!empty($rel)){
  127. $del_data = [];
  128. $up_data = [];
  129. foreach ($rel as $value) {
  130. if($value['is_del'] == 1){
  131. $del_data[] = $value['id'];
  132. }else{
  133. $up_data[] = $value['id'];
  134. }
  135. }
  136. if(!empty($del_data)){
  137. self::whereIn('id',$del_data)->delete();
  138. }
  139. if(!empty($up_data)){
  140. self::whereIn('id',$up_data)->update(['is_del' => 1]);
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * 如果使用了优惠券的默认价格
  147. */
  148. public static function calculatePrice($sell_price,$coupon){
  149. if($coupon->types){
  150. $coupon_user_price = $sell_price - $sell_price * $coupon->discount/10;
  151. }else{
  152. $coupon_user_price = $coupon->price;
  153. }
  154. return $coupon_user_price;
  155. }
  156. }