| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace app\api\logic;
- use think\Exception;
- use think\facade\Db;
- use app\common\enum\RefundEnum;
- use app\common\logic\BaseLogic;
- use app\common\logic\RefundLogic;
- use app\common\model\equity\UserEquity;
- use app\common\model\refund\RefundRecord;
- use app\common\model\equity\UserEquityLog;
- 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\GroupUserOrder;
- /**
- * 拼团活动逻辑处理
- * Class GroupActivityLogic
- * @package app\api\logic
- */
- class GroupActivityLogic extends BaseLogic
- {
- /**
- * @notes 拼团活动详情
- */
- public static function detail($id){
- $detail = GroupActivity::with('goods')->where(['id'=>$id])->visible([
- 'id','title','image','start_time','end_time','type','equity_id',
- 'participant_num','origin_price','price','form_time_limit'
- ])->findOrEmpty()->toArray();
- if(!empty($detail)){
- $detail['price'] = explode(",",$detail['price']);
- $detail['participant_num'] = explode(",",$detail['participant_num']);
- }
- return $detail;
- }
- /**
- * @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']])->visible([
- 'id','title','image','start_time','end_time','type','equity_id',
- 'participant_num','origin_price','price','form_time_limit'
- ])->findOrEmpty()->toArray();
- $detail['users'] = GroupUserOrder::alias('a')
- ->leftJoin('user b','a.user_id=b.id')
- ->where(['a.sn'=>$sn,'a.status'=>1])
- ->field(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
- ->order('a.create_time','asc')
- ->select()
- ->toArray();
- }
- return $detail;
- }
- /**
- * @notes 用户订单详情
- */
- public static function userOrderDetail($sn,$userId){
- $detail = GroupUserOrder::where(['sn'=>$sn, 'user_id' => $userId])->findOrEmpty()->toArray();
-
- return $detail;
- }
- /**
- * 提交订单
- * @param array $params
- * @return array|false
- */
- public static function submitOrder($params)
- {
- Db::startTrans();
- try {
- // 订单位置是否在服务区内
- $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 * 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['num'] >= 100) {
- throw new Exception('拼团人数已满!'); //拼团人数已满
- }
- if ($group_order['status'] == 1 ) {
- throw new Exception('订单已支付!');
- }
- if ($group_order['status'] >= 1 ) {
- throw new Exception('订单已取消!');
- }
- if (strtotime($group_order['end_time']) < time()) {
- throw new Exception('拼团活动已结束!'); //拼团活动已结束
- }
- $order_amount = $group_order['price'];
- }
- //生成用户拼单订单
- $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,
- 'order_terminal' => $params['terminal'],
- ];
- $group_user_order = GroupUserOrder::create($data);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- return [
- 'order_id' => (int)$group_user_order['id'],
- 'sn' => $group_order['sn']
- ];
- }
- /**
- * 取消订单
- * @param $params
- * @return false|void
- */
- public static function cancelOrder($params)
- {
- Db::startTrans();
- try {
- $detail = GroupUserOrder::where([
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->field('id,status')->findOrEmpty()->toArray();
- if(empty($detail)){
- throw new Exception('订单不存在');
- }
- if($detail['status'] == 1){
- throw new Exception('已支付订单不支持取消');
- }
- if($detail['status'] != 0){
- throw new Exception('当前订单不支持取消');
- }
- //将用户订单状态更新为已取消
- GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
-
- Db::commit();
- return true;
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 订单退款
- * @param $params
- * @return false|void
- */
- public static function refundOrder($params)
- {
- Db::startTrans();
- try {
- $order = GroupUserOrder::where([
- 'user_id' => $params['user_id'],
- 'sn'=>$params['sn']
- ])->field('id,sn,status,order_amount,pay_status,pay_way,user_equity_id,user_id')->findOrEmpty()->toArray();
- if(empty($order)){
- throw new Exception('订单不存在');
- }
- if($order['status'] != 1 || $order['pay_status'] != 1){
- throw new Exception('当前订单不支持退款');
- }
- if ($order['user_equity_id']) {
- //判断权益卡是否已使用
- $used = UserEquityLog::where(['user_equity_id' => $order['user_equity_id'],'user_id' => $params['user_id']])->findOrEmpty();
- if ($used) {
- throw new Exception('当前权益卡已使用,不支持退款');
- }
- }
- //将用户订单状态更新为申请退款
- GroupUserOrder::where('id',$order['id'])->update(['status' => 2,'refund_status' => 1]);
- // 生成退款记录
- $recordSn = generate_sn(RefundRecord::class, 'sn');
- $record = RefundRecord::create([
- 'sn' => $recordSn,
- 'user_id' => $order['user_id'],
- 'order_id' => $order['id'],
- 'order_sn' => $order['sn'],
- 'order_type' => RefundEnum::ORDER_TYPE_GROUP,
- 'order_amount' => $order['order_amount'],
- 'refund_amount' => $order['order_amount'],
- 'refund_type' => RefundEnum::TYPE_ADMIN,
- 'transaction_id' => $order['transaction_id'] ?? '',
- 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
- ]);
- // 退款
- RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
-
- Db::commit();
- return true;
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function deleteOrder($params):bool
- {
- try{
- $order = GroupUserOrder::where([
- 'status' => 0,
- 'user_id' => $params['worker_iuser_idd'],
- 'sn' => $params['sn']
- ])->findOrEmpty();
- if($order->isEmpty()){
- throw new \Exception('订单不存在');
- }
- $order->delete();
- return true;
- } catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|