初始化代码
This commit is contained in:
628
app/massage/model/Coupon.php
Normal file
628
app/massage/model/Coupon.php
Normal file
@@ -0,0 +1,628 @@
|
||||
<?php
|
||||
namespace app\massage\model;
|
||||
|
||||
use app\BaseModel;
|
||||
use app\member\model\Level;
|
||||
use think\facade\Db;
|
||||
|
||||
class Coupon extends BaseModel
|
||||
{
|
||||
//定义表名
|
||||
protected $name = 'massage_service_coupon';
|
||||
|
||||
|
||||
protected $append = [
|
||||
|
||||
'service',
|
||||
|
||||
'send_count',
|
||||
|
||||
'member_level',
|
||||
|
||||
'shop_goods',
|
||||
|
||||
'restaurant_goods'
|
||||
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-15 15:22
|
||||
* @功能说明:已派发多少张
|
||||
*/
|
||||
public function getSendCountAttr($value,$data){
|
||||
|
||||
if(!empty($data['id'])){
|
||||
|
||||
$record_model = new CouponRecord();
|
||||
|
||||
$count = $record_model->where(['coupon_id'=>$data['id']])->sum('num');
|
||||
|
||||
return $count;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-11 01:54
|
||||
* @功能说明:
|
||||
*/
|
||||
public function getServiceAttr($value,$data){
|
||||
|
||||
if(!empty($data['id'])){
|
||||
|
||||
$ser_model = new Goods();
|
||||
|
||||
$dis = [
|
||||
|
||||
'a.status' => 1,
|
||||
|
||||
'b.coupon_id' => $data['id'],
|
||||
|
||||
'b.type' => 0,
|
||||
|
||||
'b.scene' => 1
|
||||
];
|
||||
|
||||
$list = $ser_model->alias('a')
|
||||
->join('massage_service_coupon_goods b','b.goods_id = a.id')
|
||||
->where($dis)
|
||||
->field('a.id,a.title,a.price,b.goods_id,a.cover')
|
||||
->group('a.id')
|
||||
->order('a.top desc,a.id desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return $list;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-11 01:54
|
||||
* @功能说明:
|
||||
*/
|
||||
public function getShopGoodsAttr($value,$data){
|
||||
|
||||
if(!empty($data['id'])){
|
||||
|
||||
$ser_model = new \app\shop\model\Goods();
|
||||
|
||||
$dis = [
|
||||
'a.status' => 1,
|
||||
|
||||
'b.coupon_id' => $data['id'],
|
||||
|
||||
'b.type' => 0,
|
||||
|
||||
'b.scene' => 2
|
||||
|
||||
];
|
||||
|
||||
$list = $ser_model->alias('a')
|
||||
->join('massage_service_coupon_goods b','b.goods_id = a.id')
|
||||
->where($dis)
|
||||
->field('a.id,a.name,b.goods_id,a.cover')
|
||||
->group('a.id')
|
||||
->order('a.top desc,a.id desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return $list;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-11 01:54
|
||||
* @功能说明:
|
||||
*/
|
||||
public function getRestaurantGoodsAttr($value,$data){
|
||||
|
||||
if(!empty($data['id'])){
|
||||
|
||||
$ser_model = new \app\restaurant\model\Goods();
|
||||
|
||||
$dis = [
|
||||
'a.status' => 1,
|
||||
|
||||
'b.coupon_id' => $data['id'],
|
||||
|
||||
'b.type' => 0,
|
||||
|
||||
'b.scene' => 3
|
||||
|
||||
];
|
||||
|
||||
$list = $ser_model->alias('a')
|
||||
->join('massage_service_coupon_goods b','b.goods_id = a.id')
|
||||
->where($dis)
|
||||
->field('a.id,a.title,b.goods_id,a.cover')
|
||||
->group('a.id')
|
||||
->order('a.id desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return $list;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-11 01:54
|
||||
* @功能说明:
|
||||
*/
|
||||
public function getMemberLevelAttr($value,$data){
|
||||
|
||||
if(!empty($data['id'])){
|
||||
|
||||
$level_model = new Level();
|
||||
|
||||
$dis = [
|
||||
|
||||
'a.status' => 1,
|
||||
|
||||
'b.coupon_id' => $data['id'],
|
||||
];
|
||||
|
||||
$list = $level_model->alias('a')
|
||||
->join('longbing_card_v2_coupon_connect b','b.member_level = a.id')
|
||||
->where($dis)
|
||||
->field('a.id,a.title')
|
||||
->group('a.id')
|
||||
->order('a.id desc')
|
||||
->column('a.id');
|
||||
|
||||
return array_values($list);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2020-09-29 11:04
|
||||
* @功能说明:添加
|
||||
*/
|
||||
public function dataAdd($data){
|
||||
|
||||
$data['create_time'] = time();
|
||||
|
||||
$data_data = $data;
|
||||
|
||||
if(isset($data['service'])){
|
||||
|
||||
unset($data['service']);
|
||||
}
|
||||
|
||||
if(isset($data['member_level'])){
|
||||
|
||||
unset($data['member_level']);
|
||||
}
|
||||
|
||||
if(isset($data['shop_goods'])){
|
||||
|
||||
unset($data['shop_goods']);
|
||||
}
|
||||
|
||||
if(isset($data['restaurant_goods'])){
|
||||
|
||||
unset($data['restaurant_goods']);
|
||||
}
|
||||
|
||||
|
||||
$res = $this->insert($data);
|
||||
|
||||
$id = $this->getLastInsID();
|
||||
|
||||
$this->updateSome($id,$data_data);
|
||||
|
||||
return $id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2020-09-29 11:05
|
||||
* @功能说明:编辑
|
||||
*/
|
||||
public function dataUpdate($dis,$data){
|
||||
|
||||
$data_data = $data;
|
||||
|
||||
if(isset($data['service'])){
|
||||
|
||||
unset($data['service']);
|
||||
}
|
||||
|
||||
if(isset($data['member_level'])){
|
||||
|
||||
unset($data['member_level']);
|
||||
}
|
||||
|
||||
if(isset($data['shop_goods'])){
|
||||
|
||||
unset($data['shop_goods']);
|
||||
}
|
||||
|
||||
if(isset($data['restaurant_goods'])){
|
||||
|
||||
unset($data['restaurant_goods']);
|
||||
}
|
||||
|
||||
$res = $this->where($dis)->update($data);
|
||||
|
||||
$this->updateSome($dis['id'],$data_data);
|
||||
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $data
|
||||
* @功能说明:
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-11-01 15:40
|
||||
*/
|
||||
public function updateSome($id,$data){
|
||||
|
||||
$server = new \app\shop\server\Coupon();
|
||||
|
||||
$s_model = new CouponService();
|
||||
|
||||
$coupon_member_model = new CouponMember();
|
||||
|
||||
$goods_model = new \app\shop\model\Goods();
|
||||
|
||||
$r_goods_model = new \app\restaurant\model\Goods();
|
||||
|
||||
$coupon_member_model->where(['coupon_id'=>$id])->delete();
|
||||
|
||||
$s_model->where(['coupon_id'=>$id])->delete();
|
||||
//赛车服务
|
||||
$server->addObserver($s_model);
|
||||
//会员
|
||||
$server->addObserver($coupon_member_model);
|
||||
//商城商品
|
||||
$server->addObserver($goods_model);
|
||||
//
|
||||
$server->addObserver($r_goods_model);
|
||||
|
||||
$res = $server->notify($id,$data);
|
||||
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $uniacid
|
||||
* @param $spe
|
||||
* @功能说明:
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-03-23 13:35
|
||||
*/
|
||||
public function updateSomev2($id,$uniacid,$goods,$member_level,$shop_goods){
|
||||
|
||||
$s_model = new CouponService();
|
||||
|
||||
$coupon_member_model = new CouponMember();
|
||||
|
||||
$s_model->where(['coupon_id'=>$id])->delete();
|
||||
|
||||
if(!empty($goods)){
|
||||
|
||||
foreach ($goods as $value){
|
||||
|
||||
$insert['uniacid'] = $uniacid;
|
||||
|
||||
$insert['coupon_id'] = $id;
|
||||
|
||||
$insert['goods_id'] = $value;
|
||||
|
||||
$s_model->dataAdd($insert);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!empty($shop_goods)){
|
||||
|
||||
foreach ($shop_goods as $value){
|
||||
|
||||
$insert['uniacid'] = $uniacid;
|
||||
|
||||
$insert['coupon_id'] = $id;
|
||||
|
||||
$insert['goods_id'] = $value;
|
||||
|
||||
$insert['scene'] = 2;
|
||||
|
||||
$s_model->dataAdd($insert);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$coupon_member_model->where(['coupon_id'=>$id])->delete();
|
||||
|
||||
if(!empty($member_level)){
|
||||
|
||||
foreach ($member_level as &$value){
|
||||
|
||||
$inserts['uniacid'] = $uniacid;
|
||||
|
||||
$inserts['coupon_id'] = $id;
|
||||
|
||||
$inserts['member_level'] = $value;
|
||||
|
||||
$coupon_member_model->dataAdd($inserts);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2020-09-29 11:06
|
||||
* @功能说明:列表
|
||||
*/
|
||||
public function dataList($dis,$page){
|
||||
|
||||
$data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2020-09-29 11:43
|
||||
* @功能说明:
|
||||
*/
|
||||
public function dataInfo($dis,$filed='*'){
|
||||
|
||||
$data = $this->where($dis)->field($filed)->find();
|
||||
|
||||
return !empty($data)?$data->toArray():[];
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-09 23:22
|
||||
* @功能说明:计算优惠券可以优惠多少钱
|
||||
*/
|
||||
|
||||
public function getDicountPrice($order_goods,$coupon_id,$scene=1){
|
||||
|
||||
// $coupon_se_model = new CouponService();
|
||||
//暂时没有商品限制 先注释
|
||||
// $goods_id = $coupon_se_model->where(['coupon_id'=>$coupon_id,'type'=>1,'scene'=>$scene])->column('goods_id');
|
||||
|
||||
$price = 0;
|
||||
|
||||
foreach ($order_goods as $v){
|
||||
|
||||
foreach ($v as $vs){
|
||||
|
||||
// if(in_array($vs['goods_list'],$goods_id)){
|
||||
|
||||
$price += $v['true_price'];
|
||||
//暂时没有商品限制
|
||||
$goods_id[] = $vs['goods_id'];
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$data['discount'] = $price;
|
||||
|
||||
$data['goods_id'] = $goods_id;
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-09 23:37
|
||||
* @功能说明:订单优惠券
|
||||
*/
|
||||
public function orderCouponData($order_goods,$coupon_id,$scene=1){
|
||||
|
||||
if(empty($coupon_id)){
|
||||
|
||||
return $order_goods;
|
||||
}
|
||||
|
||||
$coupon_record_model = new CouponRecord();
|
||||
|
||||
$info = $coupon_record_model->dataInfo(['id'=>$coupon_id]);
|
||||
//是否被使用或者过期
|
||||
if(empty($info)||$info['status']!=1){
|
||||
|
||||
return $order_goods;
|
||||
}
|
||||
|
||||
if($info['start_time']<time()&&$info['end_time']>time()){
|
||||
|
||||
$p_coupon_id = !empty($info['pid'])?$info['pid']:$coupon_id;
|
||||
|
||||
$can_discount_price = $this->getDicountPrice($order_goods['list'],$p_coupon_id,$scene);
|
||||
//是否满足满减条件
|
||||
if($info['full']>$can_discount_price['discount']||$can_discount_price['discount']==0){
|
||||
|
||||
return $order_goods;
|
||||
}
|
||||
|
||||
$total_discount = 0;
|
||||
|
||||
foreach ($order_goods['list'] as $vs){
|
||||
|
||||
foreach ($vs as &$v){
|
||||
//如果该商品可以使用优惠券
|
||||
if(in_array($v['goods_id'],$can_discount_price['goods_id'])){
|
||||
|
||||
$bin = $v['true_price']/$can_discount_price['discount'];
|
||||
//该商品减去的折扣
|
||||
$discount = $bin*$info['discount']<$v['true_price']?$bin*$info['discount']:$v['true_price'];
|
||||
//总计折扣
|
||||
$total_discount+=$discount;
|
||||
|
||||
$v['true_price'] = round($v['true_price']-$discount,2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$total_discount = $info['full']>$info['discount']?$info['discount']:round($total_discount,2);
|
||||
|
||||
$order_goods['total_discount'] = round($total_discount,2);
|
||||
|
||||
$order_goods['coupon_id'] = $coupon_id;
|
||||
|
||||
}
|
||||
|
||||
return $order_goods;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-07-13 11:58
|
||||
* @功能说明:用户可用的优惠券
|
||||
*/
|
||||
public function canUseCoupon($user_id,$type=1,$is_show=1,$no_i=1,$table_id=0){
|
||||
|
||||
$coupon_model = new CouponRecord();
|
||||
|
||||
$coupon_model->where(['user_id'=>$user_id,'status'=>1])->where('end_time','<',time())->update(['status'=>3]);
|
||||
|
||||
$list = $coupon_model->where(['user_id'=>$user_id,'status'=>1])->order('id desc')->select()->toArray();
|
||||
|
||||
if($type==1){
|
||||
|
||||
$car_model = new Car();
|
||||
//获取购物车里面的信息
|
||||
$car_list = $car_model->carPriceAndCount($user_id,1);
|
||||
|
||||
}else{
|
||||
|
||||
if($type==2){
|
||||
|
||||
$car_model = new \app\shop\model\Car();
|
||||
//获取购物车里面的信息
|
||||
$car_list = $car_model->carPriceAndCount($user_id,1,1,$is_show,$no_i);
|
||||
}else{
|
||||
|
||||
$car_model = new \app\restaurant\model\Car();
|
||||
|
||||
$car_list = $car_model->carPriceAndCount($user_id,2,$table_id);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$car_list = $car_list['list'];
|
||||
|
||||
$data = [];
|
||||
|
||||
if(!empty($list)){
|
||||
|
||||
foreach ($list as &$v){
|
||||
|
||||
if($v['start_time']<time()&&$v['end_time']>time()){
|
||||
|
||||
$id = !empty($v['pid'])?$v['pid']:$v['id'];
|
||||
|
||||
$info = $this->getDicountPrice($car_list,$id,$type);
|
||||
|
||||
if($v['full']<=$info['discount']&&$info['discount']>0){
|
||||
|
||||
$data[] = $v['id'];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author chenniang
|
||||
* @DataTime: 2021-10-28 10:50
|
||||
* @功能说明:商城用户可以领取的优惠券
|
||||
*/
|
||||
public function shopCanGetCoupon($uniacid,$user_id,$page=10){
|
||||
|
||||
$record_model = new CouponRecord();
|
||||
|
||||
$id = $record_model->where(['user_id'=>$user_id])->column('coupon_id');
|
||||
|
||||
$dis = [
|
||||
|
||||
'uniacid' => $uniacid,
|
||||
|
||||
'status' => 1,
|
||||
|
||||
'send_type'=> 2
|
||||
|
||||
];
|
||||
|
||||
$where[] = ['time_limit','=',1];
|
||||
|
||||
$where[] = ['end_time','>',time()];
|
||||
|
||||
|
||||
$data = $this->where($dis)->where('id','not in',$id)->where(function ($query) use ($where){
|
||||
$query->whereOr($where);
|
||||
})->order('top desc,id desc')->paginate($page)->toArray();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user