AisShop.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\ais\model;
  9. use app\ais\model\AisCoupon;
  10. use app\ais\model\AisCouponUser;
  11. use app\ais\model\AisVipUser;
  12. use think\Model;
  13. class AisShop extends Model{
  14. protected $pk = 'id';
  15. protected $autoWriteTimestamp = true;
  16. protected $json = ['imgs'];
  17. //店铺
  18. public function store(){
  19. return $this->hasOne('AisStore','id','store_id');
  20. }
  21. //栏目名称
  22. public function cate(){
  23. return $this->hasOne('AisShopCate','id','category_id');
  24. }
  25. //选择优惠券
  26. public function coupon(){
  27. return $this->hasOne('AisCoupon','id','coupon_id');
  28. }
  29. //搜索器
  30. public function searchNameAttr($query,$value){
  31. if(!empty($value)){
  32. $query->where('name','like', '%'.$value .'%');
  33. }
  34. }
  35. //Tags字符串转数组
  36. public function getImgsAttr($value){
  37. $imga = [];
  38. foreach ($value as $key => $img) {
  39. $imga[$key] = $img;
  40. }
  41. return $imga;
  42. }
  43. //图片转换
  44. public function getImgAttr($value){
  45. return $value;
  46. }
  47. //上下架状态返回
  48. public function getSaleAttr($value,$data){
  49. $status = [0 =>'下架',1 =>'在售'];
  50. return $status[$data['is_sale']];
  51. }
  52. //状态返回
  53. public function getTypesnameAttr($value,$data){
  54. $status = [0 =>'默认属性',1 =>'首页推荐',2 =>'首页专题',3 =>'精选专栏'];
  55. return $status[$data['types']];
  56. }
  57. //添加或编辑
  58. public static function edit(array $param){
  59. $data['category_id'] = $param['category_id'];
  60. $data['store_id'] = $param['store_id'];
  61. $data['citycode'] = $param['citycode'];
  62. $data['name'] = $param['name'];
  63. $data['title'] = $param['title'];
  64. $data['types'] = $param['types'];
  65. $data['market_price'] = $param['market_price'];
  66. $data['sell_price'] = $param['sell_price'];
  67. $data['cost_price'] = $param['cost_price'];
  68. $data['vip_price'] = $param['vip_price'];
  69. $data['share_price'] = $param['share_price'];
  70. $data['share_vip_price'] = $param['share_vip_price'];
  71. $data['points_price'] = $param['points_price'];
  72. $data['warehouse_num'] = $param['warehouse_num'];
  73. $data['warehouse_sellnum'] = $param['warehouse_sellnum'];
  74. $data['coupon_id'] = $param['coupon_id'];
  75. $data['content'] = $param['content'];
  76. $data['img'] = $param['img'];
  77. $data['imgs'] = $param['imgs'];
  78. $data['types'] = $param['types'];
  79. $data['group_title'] = $param['group_title'];
  80. $data['group_note'] = $param['group_note'];
  81. $data['group_img'] = $param['group_img'];
  82. $data['keyword'] = $param['keyword'];
  83. $data['notice'] = $param['notice'];
  84. $data['end_time'] = empty($param['end_time']) ? '' : strtotime($param['end_time']);
  85. $data['update_time'] = time();
  86. if(empty($param['id'])){
  87. $data['is_sale'] = 0;
  88. $data['member_miniapp_id'] = $param['member_miniapp_id'];
  89. return self::insertGetId($data);
  90. }else{
  91. self::where('id',$param['id'])->update($data);
  92. return $param['id'];
  93. }
  94. }
  95. //批量操作
  96. public static function ids_action(int $is_sale,string $ids){
  97. switch ($is_sale) {
  98. case 1:
  99. $data['is_sale'] = 1; //在售
  100. break;
  101. case 2:
  102. $data['is_del'] = 1; //删除
  103. break;
  104. default:
  105. $data['is_sale'] = 0;
  106. $data['is_del'] = 0;
  107. break;
  108. }
  109. return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
  110. }
  111. /**
  112. * 删除SPU商品
  113. * @param [type] $id
  114. * @param [type] $ids
  115. * @return void
  116. */
  117. public static function ids_delete(int $id,$ids){
  118. if(empty($ids)){
  119. $ids = (int)$id;
  120. }else{
  121. $ids = ids($ids);
  122. }
  123. $rel = self::whereIn('id',$ids)->field('is_del,id')->select()->toArray();
  124. if(!empty($rel)){
  125. $del_data = [];
  126. $up_data = [];
  127. foreach ($rel as $value) {
  128. if($value['is_del'] == 1){
  129. $del_data[] = $value['id'];
  130. }else{
  131. $up_data[] = $value['id'];
  132. }
  133. }
  134. if(!empty($del_data)){
  135. self::whereIn('id',$del_data)->delete();
  136. }
  137. if(!empty($up_data)){
  138. self::whereIn('id',$up_data)->update(['is_del' => 1]);
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * 如果使用了优惠券的默认价格
  145. * [0 =>'代金券',1 =>'折扣券',2 =>'兑换券',3 =>'储值券'];
  146. */
  147. public static function couponPrice($sell_price,$coupon){
  148. if(empty($coupon)){
  149. return 0;
  150. }
  151. switch ($coupon->types) {
  152. case 1:
  153. return money($sell_price - $sell_price * $coupon->discount/10);
  154. break;
  155. case 2:
  156. return money($coupon->price);
  157. break;
  158. case 3:
  159. return 0;
  160. break;
  161. default:
  162. return money($coupon->price);
  163. break;
  164. }
  165. }
  166. /**
  167. * 用户下单价格计算
  168. */
  169. public static function userPayPrice($item,$uid){
  170. $item->coupon_price = 0;
  171. $item->coupon_user_price = 0;
  172. if($item->coupon_id){
  173. $item->coupon_price = self::couponPrice($item->sell_price,$item->coupon);
  174. $coupon = AisCouponUser::where([['uid','=',$uid],['is_end','=',0],['coupon_id','=',$item->coupon_id]])->order('id desc')->find();
  175. if($coupon){
  176. $item->coupon_user_price = self::couponPrice($item->sell_price,$item->coupon);
  177. }
  178. }
  179. //判断积分
  180. $item->is_vip = 0;
  181. $item->user_points = 0; //有多少积分
  182. $item->user_point = 0; //多少积分
  183. $item->user_point_price = 0; //抵多少钱
  184. $item->user_vip_price = 0; //会员实际节省多少钱
  185. $vip_user = AisVipUser::where(['uid' => $uid,'is_lock' => 0])->find();
  186. if($vip_user){
  187. $item->user_vip_price = $item->vip_price;
  188. $item->user_points = $vip_user->point;
  189. $item->is_vip = 1;
  190. //判断积分可以
  191. if(!empty($vip_user->point)){
  192. if($vip_user->point >= $item->points_price*100){
  193. $item->user_point = $item->points_price*100;
  194. $item->user_point_price = money($item->points_price);
  195. }else{
  196. $item->user_point = $vip_user->point;
  197. $item->user_point_price = money($vip_user->point/100);
  198. }
  199. }
  200. }
  201. $item->amount = $item->sell_price - $item->user_vip_price - $item->user_point_price - $item->coupon_user_price;
  202. $item->thrifty = money($item->market_price - $item->amount) ?: 0;
  203. return $item;
  204. }
  205. }