| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\CouponModel;
- use App\Models\MeetingModel;
- use App\Models\MemberCouponModel;
- use App\Models\StoreModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 结算管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class SettleService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new AccountLogModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 商家收益结算
- * @param $storeId
- * @param $money 收益
- * @param $order 订单数据
- * @return array|false|int
- */
- public function storeBonus($storeId, $money, $order)
- {
- $orderNo = isset($order['order_no'])? $order['order_no'] : '';
- if($money<=0 && $storeId<=0){
- $this->error = '无企业佣金可结算';
- return false;
- }
- $storeInfo = StoreModel::where(['id'=> $storeId,'mark'=>1])->first();
- $balance = isset($storeInfo['bonus_total'])? $storeInfo['bonus_total'] : 0;
- $storeUserId = isset($storeInfo['user_id'])? $storeInfo['user_id'] : 0;
- if($storeUserId<=0){
- $this->error = '企业账号错误';
- return false;
- }
- if(!StoreModel::where(['id'=> $storeId])->update(['bonus_total'=>DB::raw("bonus_total + {$money}"),'update_time'=>time()])){
- $this->error = '收货错误,企业结算错误,请联系客服处理';
- return -1;
- }
- $log = [
- 'user_id'=> $storeUserId,
- 'store_id'=> $storeId,
- 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
- 'type'=> 7,
- 'money'=> $money,
- 'after_money'=> moneyFormat($balance+$money,2),
- 'date'=>date('Y-m-d'),
- 'create_time'=>time(),
- 'remark'=> '收益',
- 'status'=>1
- ];
- if(!$id = $this->model->insertGetId($log)){
- $this->error = '企业收益结算失败,请联系客服处理';
- return -1;
- }
- $result = ['id'=>$id,'store_id'=>$storeId,'bonus'=>$money];
- if(env('APP_DEBUG')){
- RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
- }
- return $result;
- }
- /**
- * 新人注册奖励
- * @param $userId
- * @return array|bool
- */
- public function registerReward($userId)
- {
- $rewardOpen = ConfigService::make()->getConfigByCode('register_award_coupon',0);
- $rewardCouponId = ConfigService::make()->getConfigByCode('register_reward_coupon_id',0);
- if($rewardCouponId<=0 || $rewardOpen!= 1){
- $this->error = '未开启或配置注册优惠券奖励';
- return true;
- }
- $couponInfo = CouponModel::where(['id'=>$rewardCouponId,'mark'=>1])
- ->first();
- $status = isset($couponInfo['status'])?$couponInfo['status']:0;
- if(empty($couponInfo) || $status != 1){
- $this->error = '配置的注册奖励优惠券不存在或无效';
- return true;
- }
- $data = [
- 'coupon_id'=> $rewardCouponId,
- 'user_id'=> $userId,
- 'store_id'=> isset($couponInfo['store_id'])?$couponInfo['store_id']:0,
- 'name'=> isset($couponInfo['name'])?$couponInfo['name']:'',
- 'coupon_type'=> isset($couponInfo['coupon_type'])?$couponInfo['coupon_type']: 20,
- 'reduce_price'=> isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0,
- 'discount'=> isset($couponInfo['discount'])?$couponInfo['discount']:0,
- 'min_price'=> isset($couponInfo['min_price'])?$couponInfo['min_price']:0,
- 'expire_day'=> isset($couponInfo['expire_day'])?$couponInfo['expire_day']:0,
- 'start_time'=> isset($couponInfo['start_time'])?$couponInfo['start_time']:0,
- 'end_time'=> isset($couponInfo['end_time'])?$couponInfo['end_time']:0,
- 'goods_ids'=> isset($couponInfo['goods_ids'])&&$couponInfo['goods_ids']?$couponInfo['goods_ids']:'',
- 'create_time'=> time(),
- 'status'=> 1,
- ];
- if(!$id = MemberCouponModel::insertGetId($data)){
- $this->error = '奖励注册优惠券失败';
- return true;
- }
- $data['id'] = $id;
- $data['discount'] = floatval($data['discount']);
- unset($data['create_time']);
- unset($data['status']);
- unset($data['goods_ids']);
- $this->error = '奖励注册优惠券成功';
- return $data;
- }
- }
|