354 lines
7.5 KiB
PHP
354 lines
7.5 KiB
PHP
<?php
|
|
namespace app\massage\model;
|
|
|
|
use app\BaseModel;
|
|
use think\facade\Db;
|
|
|
|
class Commission extends BaseModel
|
|
{
|
|
//定义表名
|
|
protected $name = 'massage_service_order_commission';
|
|
|
|
|
|
|
|
protected $append = [
|
|
|
|
'order_goods'
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getOrderGoodsAttr($value,$data){
|
|
|
|
if(!empty($data['id'])){
|
|
|
|
$order_goods_model = new CommissionGoods();
|
|
|
|
$list = $order_goods_model->goodsList(['a.commission_id'=>$data['id']]);
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2021-08-25 23:24
|
|
* @功能说明:记录
|
|
*/
|
|
public function recordList($dis,$page=10){
|
|
|
|
$data = $this->alias('a')
|
|
->join('massage_service_user_list b','a.user_id = b.id')
|
|
->join('massage_service_user_list c','a.top_id = c.id')
|
|
->join('massage_service_order_list d','a.order_id = d.id')
|
|
->where($dis)
|
|
->field('a.*,b.nickName,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id')
|
|
->group('a.id')
|
|
->order('a.id desc')
|
|
->paginate($page)
|
|
->toArray();
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2021-08-25 23:34
|
|
* @功能说明:佣金到账
|
|
*/
|
|
public function successCash($order_id){
|
|
|
|
$data = $this->dataInfo(['order_id'=>$order_id]);
|
|
|
|
if(!empty($data)&&$data['status']==1){
|
|
|
|
$res = $this->dataUpdate(['id'=>$data['id']],['status'=>2]);
|
|
|
|
if($res==0){
|
|
|
|
return $res;
|
|
}
|
|
|
|
$user_model = new User();
|
|
|
|
$user = $user_model->dataInfo(['id'=>$data['top_id']]);
|
|
|
|
$res = $user_model->where(['id'=>$data['top_id'],'balance'=>$user['balance']])->update(['balance'=>$user['balance']+$data['cash'],'cash'=>$user['cash']+$data['cash']]);
|
|
|
|
if($res==0){
|
|
|
|
return $res;
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2020-09-29 11:04
|
|
* @功能说明:添加
|
|
*/
|
|
public function dataAdd($data){
|
|
|
|
$data['create_time'] = time();
|
|
|
|
$res = $this->insert($data);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2020-09-29 11:05
|
|
* @功能说明:编辑
|
|
*/
|
|
public function dataUpdate($dis,$data){
|
|
|
|
$data['update_time'] = time();
|
|
|
|
$res = $this->where($dis)->update($data);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2020-09-29 11:06
|
|
* @功能说明:列表
|
|
*/
|
|
public function dataList($dis,$page=10){
|
|
|
|
$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-08-26 23:39
|
|
* @功能说明:添加佣金
|
|
*/
|
|
public function commissionAdd($order){
|
|
|
|
$user_model = new User();
|
|
//上级
|
|
$top = $user_model->where(['id'=>$order['user_id']])->value('pid');
|
|
|
|
if(!empty($top)){
|
|
|
|
$ser_model = new Service();
|
|
|
|
$com_mdoel = new Commission();
|
|
|
|
$com_goods_mdoel = new CommissionGoods();
|
|
|
|
foreach ($order['order_goods'] as $v){
|
|
//查看是否有分销
|
|
$ser = $ser_model->dataInfo(['id'=>$v['goods_id']]);
|
|
|
|
if(!empty($ser['com_balance'])){
|
|
|
|
$insert = [
|
|
|
|
'uniacid' => $order['uniacid'],
|
|
|
|
'user_id' => $order['user_id'],
|
|
|
|
'top_id' => $top,
|
|
|
|
'order_id'=> $order['id'],
|
|
|
|
'order_code' => $order['order_code'],
|
|
|
|
];
|
|
|
|
$find = $com_mdoel->dataInfo($insert);
|
|
|
|
$cash = $v['true_price']*$ser['com_balance']/100*$v['num'];
|
|
|
|
if(empty($find)){
|
|
|
|
$insert['cash'] = $cash;
|
|
|
|
$com_mdoel->dataAdd($insert);
|
|
|
|
$id = $com_mdoel->getLastInsID();
|
|
|
|
}else{
|
|
|
|
$id = $find['id'];
|
|
|
|
$update = [
|
|
|
|
'cash' => $find['cash']+$cash
|
|
];
|
|
//加佣金
|
|
$com_mdoel->dataUpdate(['id'=>$find['id']],$update);
|
|
|
|
}
|
|
|
|
$insert = [
|
|
|
|
'uniacid' => $order['uniacid'],
|
|
|
|
'order_goods_id' => $v['id'],
|
|
|
|
'commission_id' => $id,
|
|
|
|
'cash' => $cash,
|
|
|
|
'num' => $v['num'],
|
|
|
|
'balance' => $ser['com_balance']
|
|
];
|
|
//添加到自订单记录表
|
|
$res = $com_goods_mdoel->dataAdd($insert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2021-08-28 14:35
|
|
* @功能说明:佣金到账
|
|
*/
|
|
public function successCommission($order_id){
|
|
|
|
$comm = $this->dataInfo(['order_id'=>$order_id,'status'=>1]);
|
|
|
|
if(!empty($comm)){
|
|
|
|
$user_model = new User();
|
|
|
|
$user = $user_model->dataInfo(['id'=>$comm['top_id']]);
|
|
|
|
if(!empty($user)){
|
|
|
|
$update = [
|
|
|
|
'balance' => $user['balance']+$comm['cash'],
|
|
|
|
'cash' => $user['cash']+$comm['cash'],
|
|
];
|
|
|
|
$user_model->dataUpdate(['id'=>$comm['top_id']],$update);
|
|
|
|
$this->dataUpdate(['id'=>$comm['id']],['status'=>2,'cash_time'=>time()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2021-08-28 14:48
|
|
* @功能说明:退款的时候要减去分销
|
|
*/
|
|
public function refundComm($refund_id){
|
|
|
|
$refund_model = new RefundOrder();
|
|
|
|
$com_goods_mdoel = new CommissionGoods();
|
|
|
|
$refund_order = $refund_model->dataInfo(['id'=>$refund_id]);
|
|
|
|
if(!empty($refund_order)){
|
|
//查询这笔等待有无佣金
|
|
$comm = $this->dataInfo(['order_id'=>$refund_order['order_id'],'status'=>1]);
|
|
|
|
if(!empty($comm)){
|
|
|
|
foreach ($refund_order['order_goods'] as $v){
|
|
|
|
$comm_goods = $com_goods_mdoel->dataInfo(['commission_id'=>$comm['id'],'order_goods_id'=>$v['order_goods_id']]);
|
|
|
|
$comm_goods_cash = $comm_goods['cash']/$comm_goods['num'];
|
|
|
|
$true_num = $comm_goods['num'] - $v['num'];
|
|
|
|
$true_num = $true_num>0?$true_num:0;
|
|
|
|
$update = [
|
|
|
|
'num' => $true_num,
|
|
|
|
'cash'=> $comm_goods_cash*$true_num
|
|
];
|
|
|
|
$com_goods_mdoel->dataUpdate(['id'=>$comm_goods['id']],$update);
|
|
|
|
|
|
}
|
|
|
|
$total_cash = $com_goods_mdoel->where(['commission_id'=>$comm['id']])->sum('cash');
|
|
|
|
$update = [
|
|
|
|
'cash' => $total_cash,
|
|
|
|
'status' => $total_cash>0?1:-1
|
|
];
|
|
|
|
$this->dataUpdate(['id'=>$comm['id']],$update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |