|
@@ -12,9 +12,12 @@ use app\model\Coupon;
|
|
|
use app\model\Device;
|
|
|
use app\model\Goods;
|
|
|
use app\model\GoodsComponent;
|
|
|
+use app\model\MemberAccount;
|
|
|
+use app\model\MemberBenefit;
|
|
|
use app\model\Order;
|
|
|
use app\model\OrderExpress;
|
|
|
use app\model\OrderSheet;
|
|
|
+use app\model\PayDetail;
|
|
|
use app\model\Supplier;
|
|
|
use app\model\SysSerial;
|
|
|
use support\Db;
|
|
@@ -190,6 +193,265 @@ class WholeController extends Curd
|
|
|
// return [$id, $data];
|
|
|
// }
|
|
|
|
|
|
+ public function insert(Request $request): Response
|
|
|
+ {
|
|
|
+ $params = $request->post();
|
|
|
+ try {
|
|
|
+ Db::beginTransaction();
|
|
|
+ // 余额支付
|
|
|
+ if ($params['pay_category'] == 'CASH') {
|
|
|
+ $account = MemberAccount::where('join_account_member_id', $params['join_order_member_id'])
|
|
|
+ ->where('member_account_classify', 'CASH')
|
|
|
+ ->where('member_account_status', 'ACTIVED')
|
|
|
+ ->first();
|
|
|
+ if (!$account) {
|
|
|
+ return json_fail('账户异常');
|
|
|
+ }
|
|
|
+ $amount = $account->member_account_surplus + $account->member_account_added;
|
|
|
+ if (!$account || $params['order_sheet_pay'] > $amount) {
|
|
|
+ return json_fail('账户余额不足');
|
|
|
+ }
|
|
|
+ if ($params['order_sheet_pay'] > $account->member_account_surplus) {
|
|
|
+ $cut = $account->member_account_added - ($params['order_sheet_pay'] - $account->member_account_surplus);
|
|
|
+ $account->member_account_surplus = 0;
|
|
|
+ $account->member_account_added = $cut;
|
|
|
+ } else {
|
|
|
+ $account->member_account_surplus = $account->member_account_surplus - $params['order_sheet_pay'];
|
|
|
+ }
|
|
|
+ $account->member_account_expend = $account->member_account_expend + $params['order_sheet_pay'];
|
|
|
+ $account->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ $params['orderId'] = 'OD' . date('YmdHis') . random_string(6, 'up');
|
|
|
+ $params['orderGroupId'] = 'OD' . date('YmdHis') . random_string(6, 'up');
|
|
|
+ $params['benefitId'] = 'BF' . date('YmdHis') . random_string(6, 'up');
|
|
|
+ $params['packageId'] = $params['join_sheet_goods_id'];
|
|
|
+
|
|
|
+ $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])
|
|
|
+ ->select('goods_id', 'goods_name', 'goods_classify')
|
|
|
+ ->first();
|
|
|
+ if (!$goods) {
|
|
|
+ return json_fail('产品数据异常');
|
|
|
+ }
|
|
|
+ $goods = $goods->toArray();
|
|
|
+
|
|
|
+ // 写入主订单
|
|
|
+ $this->insertMain($params);
|
|
|
+ // 订单详情
|
|
|
+ $this->insertSheet($params);
|
|
|
+ // 支付记录
|
|
|
+ $this->insertPayDetail($params);
|
|
|
+ // 买的单个服务
|
|
|
+ if ($goods['goods_classify'] == 'SERVICE') {
|
|
|
+ // 预约表
|
|
|
+ for ($i = 0; $i < intval($params['order_sheet_num']); $i++) {
|
|
|
+ $params['appointmentId'] = 'AP' . date('YmdHis') . random_string(6, 'up');
|
|
|
+ $this->insertAppointment($params);
|
|
|
+ }
|
|
|
+ $goods['skuId'] = $params['join_sheet_goods_sku_id'];
|
|
|
+ $goods['category'] = 'SERVICE';
|
|
|
+ // 权益表
|
|
|
+ $this->insertMemberBenefit($params, $goods);
|
|
|
+ } elseif ($goods['goods_classify'] == 'PACKAGE') { // 一个套餐买多个
|
|
|
+ $components = GoodsComponent::with([
|
|
|
+ 'goods' => function ($query) {
|
|
|
+ $query->select('goods_id', 'goods_name', 'goods_classify');
|
|
|
+ }
|
|
|
+ ])->where('join_component_master_goods_id', $params['join_sheet_goods_id'])
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+ $orderSheetNum = $params['order_sheet_num'];
|
|
|
+ foreach ($components as $component) {
|
|
|
+ $params['order_sheet_num'] = $orderSheetNum;
|
|
|
+ $componentJson = json_decode($component['goods_component_json'], true);
|
|
|
+ $params['join_sheet_goods_sku_id'] = $componentJson['sku_id'];
|
|
|
+ $params['join_sheet_goods_id'] = $component['join_component_goods_id'];
|
|
|
+ $params['benefitId'] = 'BF' . date('YmdHis') . random_string(6, 'up');
|
|
|
+ $params['order_sheet_num'] = $params['order_sheet_num'] * $componentJson['nbr'];
|
|
|
+ for ($i = 0; $i < intval($params['order_sheet_num']); $i++) {
|
|
|
+ $params['appointmentId'] = 'AP' . date('YmdHis') . random_string(8, 'up');
|
|
|
+ $this->insertAppointment($params);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $goods['goods_id'] = $component['join_component_goods_id'];
|
|
|
+ $goods['goods_name'] = $component['goods']['goods_name'];
|
|
|
+ $goods['goods_classify'] = $component['goods']['goods_classify'];
|
|
|
+ $goods['skuId'] = $params['join_sheet_goods_sku_id'];
|
|
|
+ $goods['category'] = 'SERVICE';
|
|
|
+ // 权益表
|
|
|
+ $this->insertMemberBenefit($params, $goods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+
|
|
|
+ return json_success('创建订单成功');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollBack();
|
|
|
+ dump($e->getMessage());
|
|
|
+ return json_fail('创建订单失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/6/7 10:30
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public function insertMain($params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $data = [
|
|
|
+ 'order_id' => $params['orderId'],
|
|
|
+ 'order_groupby' => $params['orderGroupId'],
|
|
|
+ 'join_order_member_id' => $params['join_order_member_id'],
|
|
|
+ 'order_name' => date('Y-m-d H:i:s') . '-订单',
|
|
|
+ 'order_amount_total' => $params['order_sheet_amount'],
|
|
|
+ 'order_amount_pay' => $params['order_sheet_pay'],
|
|
|
+ 'order_category' => 'NORMAL',
|
|
|
+ 'order_status_system' => $params['order_status_system'],
|
|
|
+ 'order_status_payment' => $params['order_status_payment'],
|
|
|
+ 'order_status_storage' => $params['order_status_storage'],
|
|
|
+ 'order_remark' => $params['order_remark'] ?? '',
|
|
|
+ 'order_addtimes' => time()
|
|
|
+ ];
|
|
|
+
|
|
|
+ Order::insert($data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getMessage());
|
|
|
+ throw new BusinessException('订单创建信息失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/6/7 10:25
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public function insertSheet($params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $data = [
|
|
|
+ 'join_sheet_member_id' => $params['join_order_member_id'],
|
|
|
+ 'join_sheet_order_id' => $params['orderId'],
|
|
|
+ 'join_sheet_goods_id' => $params['join_sheet_goods_id'],
|
|
|
+ 'join_sheet_goods_sku_id' => $params['join_sheet_goods_sku_id'],
|
|
|
+ 'order_sheet_status' => 'CONFIRM',
|
|
|
+ 'order_sheet_category' => 'NORMAL',
|
|
|
+ 'order_sheet_num' => $params['order_sheet_num'],
|
|
|
+ 'order_sheet_price' => $params['order_sheet_price'],
|
|
|
+ 'order_sheet_amount' => $params['order_sheet_amount'],
|
|
|
+ 'order_sheet_pay' => $params['order_sheet_pay'],
|
|
|
+ 'order_sheet_addtimes' => time()
|
|
|
+ ];
|
|
|
+ OrderSheet::insert($data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new BusinessException('订单创建失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/6/7 10:35
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public function insertPayDetail($params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $data = [
|
|
|
+ 'join_pay_member_id' => $params['join_order_member_id'],
|
|
|
+ 'join_pay_order_id' => $params['orderGroupId'],
|
|
|
+ 'pay_status' => 'SUCCESS',
|
|
|
+ 'pay_category' => $params['pay_category'],
|
|
|
+ 'pay_amount' => $params['order_sheet_pay'],
|
|
|
+ 'pay_prepayid' => 0,
|
|
|
+ 'pay_paytimes' => date('Y-m-d H:i:s'),
|
|
|
+ 'pay_addtimes' => time()
|
|
|
+ ];
|
|
|
+
|
|
|
+ PayDetail::insert($data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new BusinessException('创建支付记录失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/6/7 10:50
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public function insertAppointment($params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $data = [
|
|
|
+ 'appointment_id' => $params['appointmentId'],
|
|
|
+ 'join_appointment_member_id' => $params['join_order_member_id'],
|
|
|
+ 'join_appointment_goods_id' => $params['join_sheet_goods_id'],
|
|
|
+ 'join_appointment_goods_sku_id' => $params['join_sheet_goods_sku_id'],
|
|
|
+ 'join_appointment_order_id' => $params['orderId'],
|
|
|
+ 'join_appointment_member_benefit_id' => $params['benefitId'],
|
|
|
+ 'appointment_classify' => 'SERVICE',
|
|
|
+ 'appointment_status' => 'INIT',
|
|
|
+ 'appointment_category' => 'NORMAL',
|
|
|
+ 'appointment_addtimes' => time(),
|
|
|
+ ];
|
|
|
+ Appointment::insert($data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getMessage());
|
|
|
+ throw new BusinessException("创建权益记录失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Desc
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/6/7 11:05
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @param $goods
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public function insertMemberBenefit($params, $goods)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $data = [
|
|
|
+ 'member_benefit_id' => $params['benefitId'],
|
|
|
+ 'join_benefit_member_id' => $params['join_order_member_id'],
|
|
|
+ 'join_benefit_package_id' => $params['packageId'] ?? '',
|
|
|
+ 'join_benefit_goods_id' => $goods['goods_id'],
|
|
|
+ 'join_benefit_goods_sku_id' => $goods['skuId'] ?? '',
|
|
|
+ 'join_benefit_order_id' => $params['orderId'],
|
|
|
+ 'member_benefit_status' => 'ACTIVED',
|
|
|
+ 'member_benefit_category' => $goods['category'],
|
|
|
+ 'member_benefit_name' => $goods['goods_name'],
|
|
|
+ 'member_benefit_limit_count' => $params['order_sheet_num'],
|
|
|
+ 'member_benefit_addtimes' => time()
|
|
|
+ ];
|
|
|
+ MemberBenefit::insert($data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getMessage());
|
|
|
+ throw new BusinessException('创建会员权益失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public function delivery(Request $request)
|
|
|
{
|
|
|
if (!Order::where('order_id', $request->post('order_id'))->where('order_status_system', 'SENDING')->exists()) {
|