|
@@ -1,9 +1,13 @@
|
|
|
<?php
|
|
<?php
|
|
|
namespace app\api\logic;
|
|
namespace app\api\logic;
|
|
|
|
|
|
|
|
|
|
+use think\Exception;
|
|
|
|
|
+use think\facade\Db;
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\logic\BaseLogic;
|
|
|
|
|
+use app\common\model\service_area\ServiceArea;
|
|
|
|
|
+use app\common\model\group_activity\GroupOrder;
|
|
|
use app\common\model\group_activity\GroupActivity;
|
|
use app\common\model\group_activity\GroupActivity;
|
|
|
-use think\facade\Db;
|
|
|
|
|
|
|
+use app\common\model\group_activity\GroupUserOrder;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -18,136 +22,178 @@ class GroupActivityLogic extends BaseLogic
|
|
|
* @notes 拼团活动详情
|
|
* @notes 拼团活动详情
|
|
|
*/
|
|
*/
|
|
|
public static function detail($id){
|
|
public static function detail($id){
|
|
|
- $detail = GroupActivity::where(['id'=>$id])->visible([
|
|
|
|
|
|
|
+ $detail = GroupActivity::with('goods')->where(['id'=>$id])->visible([
|
|
|
'id','title','image','start_time','end_time','type','equity_id',
|
|
'id','title','image','start_time','end_time','type','equity_id',
|
|
|
- 'participant_num','origin_price','price','form_time_limit','is_simulate_form','simulate_num'
|
|
|
|
|
- ])->select()->toArray();
|
|
|
|
|
|
|
+ 'participant_num','origin_price','price','form_time_limit'
|
|
|
|
|
+ ])->findOrEmpty()->toArray();
|
|
|
|
|
|
|
|
if(!empty($detail)){
|
|
if(!empty($detail)){
|
|
|
$detail['price'] = explode(",",$detail['price']);
|
|
$detail['price'] = explode(",",$detail['price']);
|
|
|
$detail['participant_num'] = explode(",",$detail['participant_num']);
|
|
$detail['participant_num'] = explode(",",$detail['participant_num']);
|
|
|
- $detail['simulate_num'] = $detail['simulate_num'] ? explode(",",$detail['simulate_num']) : [];
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $detail;
|
|
return $detail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static function orderDetail($id,$userId){
|
|
|
|
|
- $goods = GroupOrder::where(['id'=>$id, 'user_id' => $userId])->visible([
|
|
|
|
|
- 'id','sn','group_activity_id','status','create_time','sell_num','base_service_fee',
|
|
|
|
|
- 'service_total','service_fee','service_image','fee_schedule','warranty_period','goods_payment_type','goods_category_id'
|
|
|
|
|
- ])->select()
|
|
|
|
|
- ->each(function (&$item, $key) use ($userId){
|
|
|
|
|
- //判定是否存在可使用的用户优惠券,存在的话直接更新展示价格
|
|
|
|
|
- if($userId!=0){
|
|
|
|
|
- $result = UserCouponLogic::categoryWithAmountLists(['user_id'=>$userId,'amount'=>$item['service_fee'],'goods_category_id'=>$item['goods_category_id'],'goods_id'=>$item['id']]);
|
|
|
|
|
- if(!empty($result[0]['amount'])){
|
|
|
|
|
- $item['base_service_fee'] = '券后'.($item['base_service_fee'] - $result[0]['amount']);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- })->toArray();
|
|
|
|
|
-
|
|
|
|
|
- if(!empty($goods)){
|
|
|
|
|
- $data = [];
|
|
|
|
|
- foreach ($goods as $key => $value) {
|
|
|
|
|
- if($value['goods_payment_type'] == '2'){
|
|
|
|
|
- $coupon = ($value['service_total']-$value['service_fee'])>0?'立减'.$value['service_total']-$value['service_fee']:'';
|
|
|
|
|
- }
|
|
|
|
|
- if($value['goods_payment_type'] == '3'){
|
|
|
|
|
- $coupon = '首付'.$value['service_fee'];
|
|
|
|
|
- }
|
|
|
|
|
- $data[] = [
|
|
|
|
|
- 'id'=>$value['id'],
|
|
|
|
|
- 'goods_image'=>$value['goods_image'],
|
|
|
|
|
- 'goods_name'=>$value['goods_name'],
|
|
|
|
|
- 'good_unit'=>$value['good_unit'],
|
|
|
|
|
- 'goods_banners'=>!empty($value['goods_banners']) ? $value['goods_banners'] : [],
|
|
|
|
|
- 'goods_payment_type'=>$value['goods_payment_type'],
|
|
|
|
|
- 'sell_num'=>$value['sell_num'],
|
|
|
|
|
- 'base_service_fee'=>$value['base_service_fee'],
|
|
|
|
|
- 'service_total'=>$value['service_total'],
|
|
|
|
|
- 'service_fee'=>$value['service_fee'],
|
|
|
|
|
- 'coupon'=>!empty($coupon)?$coupon:'',
|
|
|
|
|
- 'service_image'=>$value['service_image'],
|
|
|
|
|
- 'warranty_period'=>$value['warranty_period'],
|
|
|
|
|
- 'fee_schedule'=>$value['fee_schedule'],
|
|
|
|
|
- 'goods_category_id'=>$value['goods_category_id']
|
|
|
|
|
- ];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $goods = $data;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @notes 拼团订单详情
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function orderDetail($sn,$userId){
|
|
|
|
|
+ $detail = GroupOrder::where(['sn'=>$sn])->findOrEmpty()->toArray();
|
|
|
|
|
+ if(!empty($detail)){
|
|
|
|
|
+
|
|
|
|
|
+ $detail['activity'] = GroupActivity::with('goods')->where(['id'=>$detail['group_activity_id']])->findOrEmpty()->toArray();
|
|
|
|
|
+ $detail['users'] = GroupUserOrder::alias('a')
|
|
|
|
|
+ ->leftJoin('user b','a.user_id=b.id')
|
|
|
|
|
+ ->where(['a.sn'=>$sn,'a.status'=>1])
|
|
|
|
|
+ ->visible(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
|
|
|
|
|
+ ->select()
|
|
|
|
|
+ ->toArray();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return $goods;
|
|
|
|
|
|
|
+ return $detail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static function getHotData()
|
|
|
|
|
- {
|
|
|
|
|
- return Goods::where(['user_id'=>0,'is_hot'=>1,'platform_value'=>0])
|
|
|
|
|
- ->with('goodsCategory')
|
|
|
|
|
- ->visible(['id','goods_name','goods_image'])
|
|
|
|
|
- ->order(['top_weight' => 'desc', 'id' => 'desc'])
|
|
|
|
|
- ->select()->toArray();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @notes 用户订单详情
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function userOrderDetail($sn,$userId){
|
|
|
|
|
+ $detail = GroupUserOrder::where(['sn'=>$sn, 'user_id' => $userId])->findOrEmpty()->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ return $detail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static function sync(array $params,int $userId): bool
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 提交订单
|
|
|
|
|
+ * @param array $params
|
|
|
|
|
+ * @return array|false
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function submitOrder($params)
|
|
|
{
|
|
{
|
|
|
Db::startTrans();
|
|
Db::startTrans();
|
|
|
try {
|
|
try {
|
|
|
- if(isset($params['id'])){
|
|
|
|
|
- $model = Goods::findOrEmpty($params['id']);
|
|
|
|
|
- if($model->isEmpty()){
|
|
|
|
|
- throw new \Exception('数据异常');
|
|
|
|
|
|
|
+ // 订单位置是否在服务区内
|
|
|
|
|
+ $area = ServiceArea::serviceArea(['lon'=>$params['lon'],'lat'=>$params['lat']]);
|
|
|
|
|
+ if (!$area) {
|
|
|
|
|
+ throw new Exception('已超出服务区域!-1001');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(empty($params['mobile'])){
|
|
|
|
|
+ throw new Exception('请先补充您的联系方式后在提交订单');
|
|
|
|
|
+ }
|
|
|
|
|
+ $is_join = GroupUserOrder::where('status','<>',4)->where(['group_activity_id' => $params['group_activity_id'], 'user_id' => $params['user_id']])->value('id');
|
|
|
|
|
+ if ($is_join) {
|
|
|
|
|
+ throw new Exception('您已参加过该活动!');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //校验拼团活动
|
|
|
|
|
+ if (empty($params['sn'])) {
|
|
|
|
|
+ //新开团
|
|
|
|
|
+ $activity = GroupActivity::findOrEmpty($params['group_activity_id']); //活动详情
|
|
|
|
|
+ if ($activity->isEmpty()) {
|
|
|
|
|
+ throw new Exception('拼团活动不存在!'); //拼团活动不存在
|
|
|
|
|
+ }
|
|
|
|
|
+ //校验活动时间
|
|
|
|
|
+ if (time() < strtotime($activity['start_time'])) {
|
|
|
|
|
+ throw new Exception('拼团活动未开始!'); //拼团活动未开始
|
|
|
|
|
+ }
|
|
|
|
|
+ if (time() > strtotime($activity['end_time'])) {
|
|
|
|
|
+ throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $order_amount = explode(",",$activity['price'])[0];
|
|
|
|
|
+ //生成拼团单
|
|
|
|
|
+ $data = [
|
|
|
|
|
+ 'sn' => generate_sn(GroupOrder::class, 'sn'),
|
|
|
|
|
+ 'group_activity_id' => $params['group_activity_id'],
|
|
|
|
|
+ 'goods_id' => $activity['goods_id'],
|
|
|
|
|
+ 'user_id' => $params['user_id'],
|
|
|
|
|
+ 'origin_price' => $activity['origin_price'],
|
|
|
|
|
+ 'price' => $order_amount,
|
|
|
|
|
+ 'create_time' => time(),
|
|
|
|
|
+ 'end_time' => time() + $activity['form_time_limit'] * 60,
|
|
|
|
|
+ ];
|
|
|
|
|
+ $group_order = GroupOrder::create($data);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //加入已开的拼团单
|
|
|
|
|
+ $group_order = GroupOrder::where(['group_activity_id' => $params['group_activity_id'], 'sn' => $params['sn']])->findOrEmpty()->toArray();
|
|
|
|
|
+ if (empty($group_order)) {
|
|
|
|
|
+ throw new Exception('拼团订单不存在!'); //拼团活动不存在
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($group_order['status'] != 0) {
|
|
|
|
|
+ throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
|
}
|
|
}
|
|
|
- } else{
|
|
|
|
|
- $model = new Goods();
|
|
|
|
|
|
|
+ if (strtotime($group_order['end_time']) < time()) {
|
|
|
|
|
+ throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($group_order['num'] >= 100) {
|
|
|
|
|
+ throw new Exception('拼团人数已满!'); //拼团人数已满
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $order_amount = $group_order['price'];
|
|
|
}
|
|
}
|
|
|
- $params['goods_category_id'] = end($params['goods_category_ids']);
|
|
|
|
|
- $model->goods_category_id = $params['goods_category_id'];
|
|
|
|
|
- $model->category_type = GoodsCategory::where('id',$params['goods_category_id'])->value('category_type');
|
|
|
|
|
- $model->goods_category_ids = $params['goods_category_ids'];
|
|
|
|
|
- $model->goods_category_id = $params['goods_category_id'];
|
|
|
|
|
- $model->goods_name = $params['goods_name'];
|
|
|
|
|
- $model->goods_image = $params['goods_image'];
|
|
|
|
|
- $model->goods_video = $params['goods_video'];
|
|
|
|
|
- $model->goods_banners = $params['goods_banners'];
|
|
|
|
|
- $model->goods_number = $params['goods_number'];
|
|
|
|
|
- $model->good_unit = $params['good_unit'];
|
|
|
|
|
- $model->goods_size = $params['goods_size'];
|
|
|
|
|
- $model->goods_type = $params['goods_type'];
|
|
|
|
|
- $model->goods_brand = $params['goods_brand'];
|
|
|
|
|
- $model->install_guide = $params['install_guide'];
|
|
|
|
|
- $model->goods_payment_type = $params['goods_payment_type'];
|
|
|
|
|
- $model->base_service_fee = $params['base_service_fee'];
|
|
|
|
|
- $model->service_total = $params['service_total'];
|
|
|
|
|
- $model->service_fee = $params['service_fee'];
|
|
|
|
|
- $model->service_image = $params['service_image'];
|
|
|
|
|
- $model->warranty_period = $params['warranty_period'];
|
|
|
|
|
- $model->fee_schedule = $params['fee_schedule'];
|
|
|
|
|
- $model->goods_status = $params['goods_status'];
|
|
|
|
|
- $model->user_id = $userId;
|
|
|
|
|
- $model->save();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ //生成用户拼单订单
|
|
|
|
|
+ $data = [
|
|
|
|
|
+ 'sn' => $group_order['sn'],
|
|
|
|
|
+ 'group_order_id' => $group_order['id'],
|
|
|
|
|
+ 'group_activity_id' => $params['group_activity_id'],
|
|
|
|
|
+ 'user_id' => $params['user_id'],
|
|
|
|
|
+ 'real_name' => $params['real_name'],
|
|
|
|
|
+ 'mobile' => $params['mobile'],
|
|
|
|
|
+ 'address' => $params['address'],
|
|
|
|
|
+ 'lon' => $params['lon'],
|
|
|
|
|
+ 'lat' => $params['lat'],
|
|
|
|
|
+ 'province' => $areas['province']??0,
|
|
|
|
|
+ 'city' => $areas['city']??0,
|
|
|
|
|
+ 'area_name' => $areas['area_name']??'',
|
|
|
|
|
+ 'service_area_id' => $areas['id']??0,
|
|
|
|
|
+ 'order_amount' => $order_amount,
|
|
|
|
|
+ ];
|
|
|
|
|
+ $group_user_order = GroupUserOrder::create($data);
|
|
|
|
|
+
|
|
|
Db::commit();
|
|
Db::commit();
|
|
|
- return true;
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
Db::rollback();
|
|
|
self::setError($e->getMessage());
|
|
self::setError($e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'order_id' => (int)$group_user_order['id'],
|
|
|
|
|
+ 'sn' => $group_order['sn']
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static function firmGoodsDetail($params){
|
|
|
|
|
- try{
|
|
|
|
|
- $data= Goods::where('id',$params['id'])
|
|
|
|
|
- ->where('user_id',$params['user_id'])
|
|
|
|
|
- ->with('goodsCategory')
|
|
|
|
|
- ->withoutField(['is_recommend','recommend_weight','is_top','top_weight','is_hot','hot_weight'])
|
|
|
|
|
- ->findOrEmpty()->toArray();
|
|
|
|
|
- if(!empty($data['goods_category_ids'])) {
|
|
|
|
|
- $data['goods_category_ids'] = array_map("intval", $data['goods_category_ids']);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取消订单
|
|
|
|
|
+ * @param $params
|
|
|
|
|
+ * @return false|void
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function cancelOrder($params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Db::startTrans();
|
|
|
|
|
+ try {
|
|
|
|
|
+ $detail = GroupUserOrder::where([
|
|
|
|
|
+ 'user_id' => $params['user_id'],
|
|
|
|
|
+ 'sn'=>$params['sn']
|
|
|
|
|
+ ])->file('id,status')->findOrEmpty()->toArray();
|
|
|
|
|
+ if(empty($detail)){
|
|
|
|
|
+ throw new Exception('订单不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+ if($detail['status'] == 1){
|
|
|
|
|
+ throw new Exception('已支付订单不能取消');
|
|
|
}
|
|
}
|
|
|
- return $data;
|
|
|
|
|
- }catch(\Exception $e){
|
|
|
|
|
|
|
+ if($detail['status'] != 0){
|
|
|
|
|
+ throw new Exception('当前订单不能取消');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //将用户订单状态更新为已取消
|
|
|
|
|
+ GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
|
|
|
|
|
+
|
|
|
|
|
+ Db::commit();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (\Exception $e) {
|
|
|
|
|
+ Db::rollback();
|
|
|
self::setError($e->getMessage());
|
|
self::setError($e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|