Files
Smart-Farm/app/shop/model/IntegralList.php
2025-12-22 14:32:54 +08:00

656 lines
14 KiB
PHP

<?php
namespace app\shop\model;
use app\BaseModel;
use app\farm\model\ShopGoods;
use think\facade\Db;
class IntegralList extends BaseModel
{
//定义表名
protected $name = 'lbfarm_v2_integral_shop';
protected $append = [
'show_price',
'all_stock',
'all_have_stock',
'goods_info',
'store_info'
];
/**
* @param $value
* @param $data
* @功能说明:获取门店信息
* @author chenniang
* @DataTime: 2022-07-13 18:38
*/
public function getStoreInfoAttr($value,$data){
if(!empty($data['id'])){
$store_model = new IntegralStore();
$dis = [
'a.integral_id' => $data['id'],
'b.status' => 2,
'b.type' => 2
];
$list = $store_model->alias('a')
->join('lbfarm_farmer b','a.store_id = b.id')
->where($dis)
->field('a.*,b.title,b.is_admin,b.cover')
->select()
->toArray();
return array_values($list);
}
}
/**
* @author chenniang
* @DataTime: 2021-10-29 18:52
* @功能说明:参与活动的商品规格信息
*/
public function getGoodsInfoAttr($value,$data){
if(!empty($data['id'])){
$i_model = new IntegralGoods();
$list = $i_model->alias('a')
->join('lbfarm_v2_shop_spe_price b','a.spe_id = b.id')
->where(['a.atv_id' => $data['id']])
->field(['b.stock as goods_stock','b.price as goods_price','a.*'])
->select()
->toArray();
return $list;
}
}
/**
* @author chenniang
* @DataTime: 2021-10-29 18:46
* @功能说明:获取第一个规格的价格
*/
public function getShowPriceAttr($value,$data){
if(!empty($data['id'])){
$i_model = new IntegralGoods();
$info = $i_model->dataInfo(['atv_id'=>$data['id']]);
$text = '';
if(!empty($info['integral'])){
$text .= $info['integral'].'积分';
}
if(!empty($info['price'])){
$text .= '+'.$info['price'].'元';
}
return $text;
}
}
/**
* @author chenniang
* @DataTime: 2021-10-29 18:50
* @功能说明:总的库存
*/
public function getAllStockAttr($value,$data){
if(!empty($data['id'])){
$i_model = new IntegralGoods();
$num = $i_model->where(['atv_id'=>$data['id']])->sum('stock');
return $num;
}
}
/**
* @author chenniang
* @DataTime: 2021-10-29 18:50
* @功能说明:总的库存
*/
public function getAllHaveStockAttr($value,$data){
if(!empty($data['id'])){
$i_model = new IntegralGoods();
$num = $i_model->where(['atv_id'=>$data['id']])->sum('have_stock');
return $num;
}
}
/**
* @author chenniang
* @DataTime: 2020-09-29 11:04
* @功能说明:添加
*/
public function dataAdd($data){
$data['create_time'] = time();
$goods_info = $data['goods_info'];
unset($data['goods_info']);
// $store_info = $data['store_info'];
//
// unset($data['store_info']);
Db::startTrans();
$res = $this->insert($data);
$id = $this->getLastInsID();
$res = $this->updateSome($id,$data,$goods_info);
if(!empty($res['code'])){
Db::rollback();
return $res;
}
Db::commit();
return $res;
}
/**
* @author chenniang
* @DataTime: 2021-10-29 18:21
* @功能说明:
*/
public function updateSome($id,$data,$goods_info){
$i_model = new IntegralGoods();
$atv = $this->dataInfo(['id'=>$id]);
$arr = [];
if(!empty($goods_info)){
foreach ($goods_info as &$value){
$i_dis = [
'atv_id' => $id,
'goods_id'=> $data['goods_id'],
'spe_id' => $value['spe_id'],
];
$have_stock = $i_model->where($i_dis)->value('have_stock');
$i_model->where($i_dis)->delete();
$where = [];
$where[] = ['a.goods_id','=',$data['goods_id']];
$where[] = ['b.spe_id','=',$value['spe_id']];
$where[] = ['a.status','>',-1];
$where[] = ['a.id','<>',$id];
$info = $i_model->alias('b')
->join('lbfarm_v2_integral_shop a','a.id = b.atv_id')
->where($where)
->field('a.*,b.*')
->select()
->toArray();
if(!empty($info)){
foreach ($info as $vs){
$res = is_time_cross($vs['start_time'],$vs['end_time'],$atv['start_time'],$atv['end_time']);
if($res==false){
return ['code'=>500,'msg'=>'规格'.$vs['spe_name'].'该时间段已有积分活动,请先删除,再添加'];
}
}
}
$insert = [
'uniacid' => $data['uniacid'],
'goods_id'=> $data['goods_id'],
'spe_id' => $value['spe_id'],
'stock' => $value['stock'],
'price' => $value['price'],
'integral'=> $value['integral'],
'have_stock'=> !empty($have_stock)?$have_stock:0,
'atv_id' => $id,
];
$i_model->dataAdd($insert);
$arr[] = $i_model->getLastInsID();
}
}
$i_model->where('id','not in',$arr)->where('atv_id','=',$id)->delete();
// $store_model = new IntegralStore();
//
// $store_model->where(['integral_id'=>$id])->delete();
//
// foreach ($store_info as $k=>$v){
//
// $store_insert[$k] = [
//
// 'uniacid' => $data['uniacid'],
//
// 'integral_id' => $id,
//
// 'store_id' => $v
//
// ];
//
// }
//
// $store_model->saveAll($store_insert);
return true;
}
/**
* @author chenniang
* @DataTime: 2020-09-29 11:05
* @功能说明:编辑
*/
public function dataUpdate($dis,$data){
if(!empty($data['goods_info'])){
$goods_info = $data['goods_info'];
unset($data['goods_info']);
}
// if(!empty($data['store_info'])){
//
// $store_info = $data['store_info'];
//
// unset($data['store_info']);
// }
Db::startTrans();
$res = $this->where($dis)->update($data);
if(!empty($goods_info)){
$res = $this->updateSome($dis['id'],$data,$goods_info);
if(!empty($res['code'])){
Db::rollback();
return $res;
}
}
Db::commit();
return $res;
}
/**
* @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){
$data = $this->where($dis)->find();
return !empty($data)?$data->toArray():[];
}
/**
* @author chenniang
* @DataTime: 2021-10-29 13:34
* @功能说明:积分列表可以通过商品名字查询
*/
public function dataGoodsList($dis,$page=10){
$data = $this->alias('a')
->join('lbfarm_shop_goods b','a.goods_id = b.id','left')
->where($dis)
->field('a.*,b.goods_name,cover')
->group('a.id')
->order('a.id desc')
->paginate($page)
->toArray();
return $data;
}
/**
* @author chenniang
* @DataTime: 2021-10-29 13:54
* @功能说明:初始化活动时间
*/
public function initAtv(){
$this->where('start_time','>',time())->update(['atv_status'=>1]);
$this->where('start_time','<',time())->update(['atv_status'=>2]);
$this->where('end_time','<',time())->update(['atv_status'=>3]);
return true;
}
/**
* @author chenniang
* @DataTime: 2021-11-01 10:18
* @功能说明:正在进行中的活动
*/
public function atvIng($goods_id,$spe_id=0){
$dis[] = ['a.atv_status','=',2];
$dis[] = ['a.goods_id','=',$goods_id];
$dis[] = ['a.status','=',1];
if(!empty($spe_id)){
$dis[] = ['b.spe_id','=',$spe_id];
}
// if(!empty($store_id)){
//
// $dis[] = ['c.store_id','=',$store_id];
//
// }
$data = $this->alias('a')
->join('lbfarm_v2_integral_shop_goods b','a.id = b.atv_id')
// ->join('lbfarm_v2_integral_store c','a.id = c.integral_id')
->where($dis)
->field('a.*,b.*')
->find();
return !empty($data)?$data->toArray():[];
}
/**
* @author chenniang
* @DataTime: 2021-11-01 10:18
* @功能说明:正在进行中的活动
*/
public function buyLimit($goods_id,$spe_id=0){
$dis[] = ['a.atv_status','=',2];
$dis[] = ['a.goods_id','=',$goods_id];
$dis[] = ['a.status','=',1];
if(!empty($spe_id)){
$dis[] = ['b.spe_id','=',$spe_id];
}
// if(!empty($store_id)){
//
// $dis[] = ['c.store_id','=',$store_id];
//
// }
$data = $this->alias('a')
->join('lbfarm_v2_integral_shop_goods b','a.id = b.atv_id')
// ->join('lbfarm_v2_integral_store c','a.id = c.integral_id')
->where($dis)
->field('a.*')
->find();
return !empty($data)?$data->toArray():[];
}
/**
* @author chenniang
* @DataTime: 2021-11-04 14:41
* @功能说明:获取是否
*/
public function getBuyLimit($list){
$this->initAtv();
$data['buy_limit'] = 1;
$data['discount_add'] = 1;
foreach ($list as $value){
$res = $this->buyLimit($value['goods_id'],$value['spe_id']);
if(!empty($res)){
if($res['buy_limit']==0){
$data['buy_limit'] = 0;
}
if($res['discount_add']==0){
$data['discount_add'] = 0;
}
}
}
return $data;
}
/**
* @author chenniang
* @DataTime: 2021-11-11 16:50
* @功能说明:获取活动次数
*/
public function getAtvNum1($id,$user_id){
$order_model = new Order();
$dis = [
'b.integral_id' => $id,
'a.user_id' => $user_id
];
$num = $order_model->alias('a')
->join('longbing_card_v2_shop_order_goods b','a.id = b.order_id')
->where('a.pay_type','>=',1)
->where($dis)
->sum('goods_num');
return $num;
}
/**
* @param $dis
* @return float
* 获取历史商品件数
*/
public function getAtvNum($id,$user_id){
$dis = [
'a.integral_id' => $id,
'b.user_id' => $user_id
];
$order_model = new \app\farm\model\ShopOrderGoods();
//取消订单的不算 chen
$num = $order_model->alias('a')
->join('lbfarm_shop_order b','a.order_id = b.id')
->where($dis)
->where('b.pay_type','>=',1)
->group('a.id')
->sum('a.goods_num');
$order_id = $order_model->alias('a')
->join('lbfarm_shop_order b','a.order_id = b.id')
->where($dis)
->where('b.pay_type','>=',1)
->column('b.id');
$order_refund_goods = new \app\farm\model\ShopRefundGoods();
//申请退款成功的
$refund_num = $order_refund_goods->where('order_id','in',$order_id)->where(['status'=>2])->sum('goods_num');
return $num - $refund_num;
}
/**
* @author chenniang
* @DataTime: 2022-07-22 11:01
* @功能说明:积分商品列表
*/
public function integralGoodsList($dis,$page=10){
$goods_model = new ShopGoods();
$i_goods_model = new IntegralGoods();
$spe_model = new GoodsSpePrice();
$data = $goods_model->alias('a')
->join('lbfarm_v2_integral_shop_goods b','b.goods_id = a.id')
->join('lbfarm_v2_integral_shop c','b.atv_id = c.id')
->where($dis)
->field('a.goods_name,a.cover,a.id as goods_id,c.id as atv_id,c.type')
->group('a.id')
->order('a.id desc')
->paginate($page)
->toArray();
if(!empty($data['data'])){
foreach ($data['data'] as &$v){
$spe = $i_goods_model->where(['atv_id'=>$v['atv_id']])->order('price')->find()->toArray();
$v['integral'] = $spe['integral'];
$v['price'] = $spe['price'];
$v['init_price'] = $spe_model->where(['id'=>$spe['spe_id']])->sum('price');
$v['all_have_stock'] = $i_goods_model->where(['atv_id'=>$v['atv_id']])->sum('have_stock');
}
}
return $data;
}
}