|
@@ -3,6 +3,7 @@
|
|
|
namespace app\common\service;
|
|
|
|
|
|
use app\common\model\PreOrder;
|
|
|
+use fast\Random;
|
|
|
use GuzzleHttp\Client;
|
|
|
use think\Db;
|
|
|
use think\Exception;
|
|
@@ -279,4 +280,67 @@ class UserService
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 套餐卡券购买
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function packageCoupons($params=[])
|
|
|
+ {
|
|
|
+ $result = [
|
|
|
+ 'status' => 1,
|
|
|
+ 'msg' => '',
|
|
|
+ 'data' => [],
|
|
|
+ ];
|
|
|
+ try {
|
|
|
+ $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
|
|
|
+ $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
|
|
|
+ $userId = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ $payOrderId= isset($params['payorder_id']) ? $params['payorder_id'] : 0;
|
|
|
+ //赠送优惠券
|
|
|
+ $pg = 'package_gift';
|
|
|
+ $c = 'coupons';
|
|
|
+ $field = $pg.'.*,'.$c.'.name,'.$c.'.info,'.$c.'.days';
|
|
|
+ $packageGiftWhere['package_id'] = $packageId;
|
|
|
+ $packageGiftWhere[$c.'.status'] = 1;
|
|
|
+ $rechargeGift = Db::name($pg)->alias($pg)->field($field)
|
|
|
+ ->join($c,$c.'.id = '.$pg.'.coupon_id','LEFT')->where($packageGiftWhere)->select();
|
|
|
+ $time = time();
|
|
|
+ foreach ($rechargeGift as $k => $v) {
|
|
|
+ $checkCode[] = Random::alnum(8);
|
|
|
+ }
|
|
|
+ $orderService = new OrderService();//防重复
|
|
|
+ $checkCodeRes = $orderService->getCheckCoupons($checkCode);
|
|
|
+ foreach ($rechargeGift as $key => $value) {
|
|
|
+ $endtime = $value['days'] == 0 ? 0 : $time + 86400 * $value['days'];
|
|
|
+ if ($checkCodeRes['status']) {
|
|
|
+ $checkCodeStr = isset($checkCodeRes['data'][$key]) ? $checkCodeRes['data'][$key] : '';
|
|
|
+ } else {
|
|
|
+ $checkCodeStr = Random::alnum(8);
|
|
|
+ }
|
|
|
+ $userCouponsData[] = [
|
|
|
+ 'check_code' => $checkCodeStr,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'company_id' => $companyId,
|
|
|
+ 'coupons_id' => $value['coupon_id'],
|
|
|
+ 'coupon_name' => $value['name'],
|
|
|
+ 'coupon_info' => $value['info'],
|
|
|
+ 'createtime' => $time,
|
|
|
+ 'endtime' => $endtime,
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'remain' => $value['number'],
|
|
|
+ 'payorder_id' => $payOrderId,
|
|
|
+ 'getfrom' => '购买套餐',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $userCouponsRes = Db::name('user_coupons')->insertAll($userCouponsData);
|
|
|
+ if (!$userCouponsRes) {
|
|
|
+ throw new Exception('套餐券获取失败');
|
|
|
+ }
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $result['status'] = 0;
|
|
|
+ $result['msg'] = $e->getMessage();
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
}
|