123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- /**
- * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
- * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
- * @author pillar<ltmn@qq.com>
- * 商品管理(SPU)
- */
- namespace app\ais\model;
- use app\ais\model\AisCoupon;
- use app\ais\model\AisCouponUser;
- use app\ais\model\AisVipUser;
- use think\Model;
- class AisShop extends Model{
-
- protected $pk = 'id';
- protected $autoWriteTimestamp = true;
- protected $json = ['imgs'];
- //店铺
- public function store(){
- return $this->hasOne('AisStore','id','store_id');
- }
- //栏目名称
- public function cate(){
- return $this->hasOne('AisShopCate','id','category_id');
- }
- //选择优惠券
- public function coupon(){
- return $this->hasOne('AisCoupon','id','coupon_id');
- }
- //搜索器
- public function searchNameAttr($query,$value){
- if(!empty($value)){
- $query->where('name','like', '%'.$value .'%');
- }
- }
- //Tags字符串转数组
- public function getImgsAttr($value){
- $imga = [];
- foreach ($value as $key => $img) {
- $imga[$key] = $img;
- }
- return $imga;
- }
- //图片转换
- public function getImgAttr($value){
- return $value;
- }
- //上下架状态返回
- public function getSaleAttr($value,$data){
- $status = [0 =>'下架',1 =>'在售'];
- return $status[$data['is_sale']];
- }
- //状态返回
- public function getTypesnameAttr($value,$data){
- $status = [0 =>'默认属性',1 =>'首页推荐',2 =>'首页专题',3 =>'精选专栏'];
- return $status[$data['types']];
- }
- //添加或编辑
- public static function edit(array $param){
- $data['category_id'] = $param['category_id'];
- $data['store_id'] = $param['store_id'];
- $data['citycode'] = $param['citycode'];
- $data['name'] = $param['name'];
- $data['title'] = $param['title'];
- $data['types'] = $param['types'];
- $data['market_price'] = $param['market_price'];
- $data['sell_price'] = $param['sell_price'];
- $data['cost_price'] = $param['cost_price'];
- $data['vip_price'] = $param['vip_price'];
- $data['share_price'] = $param['share_price'];
- $data['share_vip_price'] = $param['share_vip_price'];
- $data['points_price'] = $param['points_price'];
- $data['warehouse_num'] = $param['warehouse_num'];
- $data['warehouse_sellnum'] = $param['warehouse_sellnum'];
- $data['coupon_id'] = $param['coupon_id'];
- $data['content'] = $param['content'];
- $data['img'] = $param['img'];
- $data['imgs'] = $param['imgs'];
- $data['types'] = $param['types'];
- $data['group_title'] = $param['group_title'];
- $data['group_note'] = $param['group_note'];
- $data['group_img'] = $param['group_img'];
- $data['keyword'] = $param['keyword'];
- $data['notice'] = $param['notice'];
- $data['end_time'] = empty($param['end_time']) ? '' : strtotime($param['end_time']);
- $data['update_time'] = time();
- if(empty($param['id'])){
- $data['is_sale'] = 0;
- $data['member_miniapp_id'] = $param['member_miniapp_id'];
- return self::insertGetId($data);
- }else{
- self::where('id',$param['id'])->update($data);
- return $param['id'];
- }
- }
-
- //批量操作
- public static function ids_action(int $is_sale,string $ids){
- switch ($is_sale) {
- case 1:
- $data['is_sale'] = 1; //在售
- break;
- case 2:
- $data['is_del'] = 1; //删除
- break;
- default:
- $data['is_sale'] = 0;
- $data['is_del'] = 0;
- break;
- }
- return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
- }
- /**
- * 删除SPU商品
- * @param [type] $id
- * @param [type] $ids
- * @return void
- */
- public static function ids_delete(int $id,$ids){
- if(empty($ids)){
- $ids = (int)$id;
- }else{
- $ids = ids($ids);
- }
- $rel = self::whereIn('id',$ids)->field('is_del,id')->select()->toArray();
- if(!empty($rel)){
- $del_data = [];
- $up_data = [];
- foreach ($rel as $value) {
- if($value['is_del'] == 1){
- $del_data[] = $value['id'];
- }else{
- $up_data[] = $value['id'];
- }
- }
- if(!empty($del_data)){
- self::whereIn('id',$del_data)->delete();
- }
- if(!empty($up_data)){
- self::whereIn('id',$up_data)->update(['is_del' => 1]);
- }
- }
- return true;
- }
- /**
- * 如果使用了优惠券的默认价格
- * [0 =>'代金券',1 =>'折扣券',2 =>'兑换券',3 =>'储值券'];
- */
- public static function couponPrice($sell_price,$coupon){
- if(empty($coupon)){
- return 0;
- }
- switch ($coupon->types) {
- case 1:
- return money($sell_price - $sell_price * $coupon->discount/10);
- break;
- case 2:
- return money($coupon->price);
- break;
- case 3:
- return 0;
- break;
- default:
- return money($coupon->price);
- break;
- }
- }
- /**
- * 用户下单价格计算
- */
- public static function userPayPrice($item,$uid){
- $item->coupon_price = 0;
- $item->coupon_user_price = 0;
- if($item->coupon_id){
- $item->coupon_price = self::couponPrice($item->sell_price,$item->coupon);
- $coupon = AisCouponUser::where([['uid','=',$uid],['is_end','=',0],['coupon_id','=',$item->coupon_id]])->order('id desc')->find();
- if($coupon){
- $item->coupon_user_price = self::couponPrice($item->sell_price,$item->coupon);
- }
- }
- //判断积分
- $item->is_vip = 0;
- $item->user_points = 0; //有多少积分
- $item->user_point = 0; //多少积分
- $item->user_point_price = 0; //抵多少钱
- $item->user_vip_price = 0; //会员实际节省多少钱
- $vip_user = AisVipUser::where(['uid' => $uid,'is_lock' => 0])->find();
- if($vip_user){
- $item->user_vip_price = $item->vip_price;
- $item->user_points = $vip_user->point;
- $item->is_vip = 1;
- //判断积分可以
- if(!empty($vip_user->point)){
- if($vip_user->point >= $item->points_price*100){
- $item->user_point = $item->points_price*100;
- $item->user_point_price = money($item->points_price);
- }else{
- $item->user_point = $vip_user->point;
- $item->user_point_price = money($vip_user->point/100);
- }
- }
- }
- $item->amount = $item->sell_price - $item->user_vip_price - $item->user_point_price - $item->coupon_user_price;
- $item->thrifty = money($item->market_price - $item->amount) ?: 0;
- return $item;
- }
- }
|