123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- namespace app\common\logic;
- use app\common\model\jarc\User;
- use think\facade\Db;
- /**
- * ============================================================================
- * DSMall多用户商城
- * ============================================================================
- * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
- * 网站地址: http://www.csdeshang.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
- * 不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * 逻辑层模型
- */
- class Vrorder
- {
- /**
- * 取消订单
- * @param array $order_info
- * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
- * @param string $msg 操作备注
- * @param boolean $if_queue 是否使用队列
- * @return array
- */
- public function changeOrderStateCancel($order_info, $role, $msg, $if_queue = true)
- {
- $vrorder_model = new \app\common\model\Vrorder();
- $ppintuanorder_model = model('ppintuanorder');
- if ($order_info['order_promotion_type'] == 2) {
- $condition = [];
- $condition[] = ['order_id', '=', $order_info['order_id']];
- $condition[] = ['pintuanorder_type', '=', 1];
- $condition[] = ['pintuanorder_state', '=', 1];
- $ppintuanorder_model->editPpintuanorder($condition, ['pintuanorder_state' => 0]);
- }
- //库存、销量变更
- if ($if_queue) {
- \mall\queue\QueueClient::push('cancelOrderUpdateStorage', [$order_info['goods_id'] => $order_info['goods_num']]);
- } else {
- \model('queue', 'logic')->cancelOrderUpdateStorage([$order_info['goods_id'] => $order_info['goods_num']]);
- }
- $predeposit_model = model('predeposit');
- //解冻充值卡
- $rcb_amount = floatval($order_info['rcb_amount']);
- $data_rcb = [];
- $data_rcb['member_id'] = $order_info['buyer_id'];
- $data_rcb['member_name'] = $order_info['buyer_name'];
- $data_rcb['amount'] = $rcb_amount;
- $data_rcb['order_sn'] = $order_info['order_sn'];
- //解冻预存款
- $pd_amount = floatval($order_info['pd_amount']);
- $data_pd = [];
- $data_pd['member_id'] = $order_info['buyer_id'];
- $data_pd['member_name'] = $order_info['buyer_name'];
- $data_pd['amount'] = $pd_amount;
- $data_pd['order_sn'] = $order_info['order_sn'];
- if ($order_info['order_state'] == ORDER_STATE_NEW) {
- if ($rcb_amount > 0) {
- $predeposit_model->changeRcb('order_cancel', $data_rcb);
- }
- if ($pd_amount > 0) {
- $predeposit_model->changePd('order_cancel', $data_pd);
- }
- }
- if ($order_info['order_state'] == ORDER_STATE_PAY) {
- $refundreturn_model = model('refundreturn');
- $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
- if ($order_info['order_promotion_type'] == 2) {//如果是拼团
- $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
- if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
- if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
- Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->dec('pintuangroup_joined')->update();
- if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
- //拼团统计开团数量
- $condition = [];
- $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
- $condition[] = ['pintuan_count', '>', 0];
- Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
- }
- }
- }
- }
- }
- //更新订单信息
- $update_order = [
- 'order_state' => ORDER_STATE_CANCEL, 'pd_amount' => 0, 'close_time' => TIMESTAMP, 'close_reason' => $msg,
- ];
- $update = $vrorder_model->editVrorder($update_order, ['order_id' => $order_info['order_id']]);
- if (!$update) {
- throw new \think\Exception('保存失败', 10006);
- }
- }
- /**
- * 返还金额
- */
- public function orderCancelBack($order_info, $member_info)
- {
- //返还卡券
- if (!empty($order_info['deduction_amount']) && $order_info['card_id'] != 0) {
- //这两个都满足的情况下,走退款 删除支付记录里的订单关联数据
- //1.回滚卡片余额
- $cardWhere = [];
- $cardWhere[] = ['id', '=', $order_info['card_id']];
- $cardWhere[] = ['member_id', '=', $member_info['member_id']];
- $cardInfo = Db::name('sub_card')->where($cardWhere)->find();
- if (($cardInfo['balance'] + $order_info['deduction_amount']) == $cardInfo['worth']) {
- $cardStatus = 1;
- } else {
- $cardStatus = 2;
- }
- $cardUpdate = [
- 'card_status' => $cardStatus,
- 'balance' => $cardInfo['balance'] + $order_info['deduction_amount'],
- 'freeze' => $cardInfo['freeze'] - $order_info['deduction_amount'],
- 'update_time' => time(),
- 'used_count' => $cardInfo['used_count'] - 1,
- ];
- $cardRes = Db::name('sub_card')->where($cardWhere)->update($cardUpdate);
- if ($cardRes) {
- //如果卡的数据恢复成功了 删除卡的消费记录
- Db::name('sub_card_expense')->where([
- ['card_id', '=', $cardInfo['id']],
- ['order_id', '=', $order_info['order_id']],
- ])->update(['expense_status' => 2]);
- }
- }
- //返还积分
- if ($order_info['point_amount'] > 0) {
- $userModel = new User();
- $point_ratio = config('app.point_ratio');
- $userModel->changeScore($order_info['point_amount'] * $point_ratio, '取消订单', '', $member_info['union_id']);
- }
- }
- /**
- * 支付订单
- * @param array $order_info
- * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
- * @param string $post
- * @return array
- */
- public function changeOrderStatePay($order_info, $role, $post)
- {
- try {
- $vrorder_model = model('vrorder');
- Db::startTrans();
- $predeposit_model = model('predeposit');
- //下单,支付被冻结的充值卡
- $rcb_amount = floatval($order_info['rcb_amount']);
- if ($rcb_amount > 0) {
- $data_pd = [];
- $data_pd['member_id'] = $order_info['buyer_id'];
- $data_pd['member_name'] = $order_info['buyer_name'];
- $data_pd['amount'] = $rcb_amount;
- $data_pd['order_sn'] = $order_info['order_sn'];
- $predeposit_model->changeRcb('order_comb_pay', $data_pd);
- }
- //下单,支付被冻结的预存款
- $pd_amount = floatval($order_info['pd_amount']);
- if ($pd_amount > 0) {
- $data_pd = [];
- $data_pd['member_id'] = $order_info['buyer_id'];
- $data_pd['member_name'] = $order_info['buyer_name'];
- $data_pd['amount'] = $pd_amount;
- $data_pd['order_sn'] = $order_info['order_sn'];
- $predeposit_model->changePd('order_comb_pay', $data_pd);
- }
- //更新订单状态
- $update_order = [];
- $update_order['order_state'] = 40;
- $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
- $update_order['payment_code'] = $post['payment_code'];
- $update_order['trade_no'] = $post['trade_no'];
- $update = $vrorder_model->editVrorder($update_order, ['order_id' => $order_info['order_id']]);
- if (!$update) {
- throw new \think\Exception(lang('ds_common_save_fail'), 10006);
- }
- //如果是拼团
- if ($order_info['order_promotion_type'] == 2) {
- $ppintuangroup_model = model('ppintuangroup');
- $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
- if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
- if ($ppintuangroup_info['pintuangroup_joined'] == 0) {
- //拼团统计开团数量
- $condition = [];
- $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
- Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
- }
- //开团统计新增人数
- Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->inc('pintuangroup_joined')->update();
- if (($ppintuangroup_info['pintuangroup_joined'] + 1) >= $ppintuangroup_info['pintuangroup_limit_number']) {
- $condition = [];
- $condition[] = ['pintuangroup_id', '=', $order_info['promotions_id']];
- $ppintuangroup_model->successPpintuangroup($condition, $condition);
- $this->addVrorderCode($order_info);
- $condition = [];
- $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
- Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
- }
- }
- } else {//虚拟商品拼团等拼团成功再发兑换码
- $this->addVrorderCode($order_info);
- }
- Db::commit();
- return ds_callback(true, '更新成功');
- } catch (Exception $e) {
- Db::rollback();
- return ds_callback(false, $e->getMessage());
- }
- }
- public function addVrorderCode($order_info)
- {
- $vrorder_model = model('vrorder');
- //发放兑换码
- $insert = $vrorder_model->addVrorderCode($order_info);
- if (!$insert) {
- throw new \think\Exception('兑换码发送失败', 10006);
- }
- // 支付成功发送买家消息
- $param = [];
- $param['code'] = 'order_payment_success';
- $param['member_id'] = $order_info['buyer_id'];
- //阿里短信参数
- $param['ali_param'] = [
- 'order_sn' => $order_info['order_sn'],
- ];
- $param['ten_param'] = [
- $order_info['order_sn'],
- ];
- $param['param'] = array_merge($param['ali_param'], [
- 'order_url' => HOME_SITE_URL . '/Membervrorder/show_order?order_id=' . $order_info['order_id'],
- ]);
- //微信模板消息
- $param['weixin_param'] = [
- 'url' => config('ds_config.h5_site_url') . '/member/vrorder_detail?order_id=' . $order_info['order_id'],
- 'data' => [
- "keyword1" => [
- "value" => $order_info['order_sn'],
- "color" => "#333",
- ],
- "keyword2" => [
- "value" => $order_info['goods_name'],
- "color" => "#333",
- ],
- "keyword3" => [
- "value" => $order_info['order_amount'],
- "color" => "#333",
- ],
- "keyword4" => [
- "value" => date('Y-m-d H:i', $order_info['add_time']),
- "color" => "#333",
- ],
- ],
- ];
- \mall\queue\QueueClient::push('sendMemberMsg', $param);
- // 支付成功发送店铺消息
- $param = [];
- $param['code'] = 'new_order';
- $param['store_id'] = $order_info['store_id'];
- $param['ali_param'] = [
- 'order_sn' => $order_info['order_sn'],
- ];
- $param['ten_param'] = [
- $order_info['order_sn'],
- ];
- $param['param'] = $param['ali_param'];
- $param['weixin_param'] = [
- 'url' => config('ds_config.h5_site_url') . '/seller/vrorder_detail?order_id=' . $order_info['order_id'],
- 'data' => [
- "keyword1" => [
- "value" => $order_info['order_sn'],
- "color" => "#333",
- ],
- "keyword2" => [
- "value" => $order_info['goods_name'],
- "color" => "#333",
- ],
- "keyword3" => [
- "value" => $order_info['order_amount'],
- "color" => "#333",
- ],
- "keyword4" => [
- "value" => date('Y-m-d H:i', $order_info['add_time']),
- "color" => "#333",
- ],
- ],
- ];
- \mall\queue\QueueClient::push('sendStoremsg', $param);
- //发送兑换码到手机
- $param = [
- 'order_id' => $order_info['order_id'], 'buyer_id' => $order_info['buyer_id'],
- 'buyer_phone' => $order_info['buyer_phone'],
- ];
- \mall\queue\QueueClient::push('sendVrCode', $param);
- }
- /**
- * 完成订单
- * @param int $order_id
- * @return array
- */
- public function changeOrderStateSuccess($order_id)
- {
- $vrorder_model = model('vrorder');
- $condition = [];
- $condition[] = ['vr_state', '=', 0];
- $condition[] = ['refund_lock', 'in', [0, 1]];
- $condition[] = ['order_id', '=', $order_id];
- $condition[] = ['vr_indate', '>', TIMESTAMP];
- $order_code_info = $vrorder_model->getVrordercodeInfo($condition, '*');
- if (empty($order_code_info)) {
- $update = $vrorder_model->editVrorder([
- 'order_state' => ORDER_STATE_SUCCESS, 'finnshed_time' => TIMESTAMP,
- ], ['order_id' => $order_id]);
- if (!$update) {
- ds_callback(false, '更新失败');
- }
- }
- $order_info = $vrorder_model->getVrorderInfo(['order_id' => $order_id]);
- //添加会员积分
- if (config('ds_config.points_isuse') == 1) {
- model('points')->savePointslog('order', [
- 'pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'],
- 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
- 'order_id' => $order_info['order_id'],
- ], true);
- }
- //添加会员经验值
- model('exppoints')->saveExppointslog('order', [
- 'explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'],
- 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
- 'order_id' => $order_info['order_id'],
- ], true);
- return ds_callback(true, '更新成功');
- }
- }
|