Vrorder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\model\jarc\User;
  4. use think\facade\Db;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 逻辑层模型
  16. */
  17. class Vrorder
  18. {
  19. /**
  20. * 取消订单
  21. * @param array $order_info
  22. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  23. * @param string $msg 操作备注
  24. * @param boolean $if_queue 是否使用队列
  25. * @return array
  26. */
  27. public function changeOrderStateCancel($order_info, $role, $msg, $if_queue = true)
  28. {
  29. $vrorder_model = new \app\common\model\Vrorder();
  30. $ppintuanorder_model = model('ppintuanorder');
  31. if ($order_info['order_promotion_type'] == 2) {
  32. $condition = [];
  33. $condition[] = ['order_id', '=', $order_info['order_id']];
  34. $condition[] = ['pintuanorder_type', '=', 1];
  35. $condition[] = ['pintuanorder_state', '=', 1];
  36. $ppintuanorder_model->editPpintuanorder($condition, ['pintuanorder_state' => 0]);
  37. }
  38. //库存、销量变更
  39. if ($if_queue) {
  40. \mall\queue\QueueClient::push('cancelOrderUpdateStorage', [$order_info['goods_id'] => $order_info['goods_num']]);
  41. } else {
  42. \model('queue', 'logic')->cancelOrderUpdateStorage([$order_info['goods_id'] => $order_info['goods_num']]);
  43. }
  44. $predeposit_model = model('predeposit');
  45. //解冻充值卡
  46. $rcb_amount = floatval($order_info['rcb_amount']);
  47. $data_rcb = [];
  48. $data_rcb['member_id'] = $order_info['buyer_id'];
  49. $data_rcb['member_name'] = $order_info['buyer_name'];
  50. $data_rcb['amount'] = $rcb_amount;
  51. $data_rcb['order_sn'] = $order_info['order_sn'];
  52. //解冻预存款
  53. $pd_amount = floatval($order_info['pd_amount']);
  54. $data_pd = [];
  55. $data_pd['member_id'] = $order_info['buyer_id'];
  56. $data_pd['member_name'] = $order_info['buyer_name'];
  57. $data_pd['amount'] = $pd_amount;
  58. $data_pd['order_sn'] = $order_info['order_sn'];
  59. if ($order_info['order_state'] == ORDER_STATE_NEW) {
  60. if ($rcb_amount > 0) {
  61. $predeposit_model->changeRcb('order_cancel', $data_rcb);
  62. }
  63. if ($pd_amount > 0) {
  64. $predeposit_model->changePd('order_cancel', $data_pd);
  65. }
  66. }
  67. if ($order_info['order_state'] == ORDER_STATE_PAY) {
  68. $refundreturn_model = model('refundreturn');
  69. $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
  70. if ($order_info['order_promotion_type'] == 2) {//如果是拼团
  71. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  72. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  73. if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
  74. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->dec('pintuangroup_joined')->update();
  75. if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
  76. //拼团统计开团数量
  77. $condition = [];
  78. $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
  79. $condition[] = ['pintuan_count', '>', 0];
  80. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. //更新订单信息
  87. $update_order = [
  88. 'order_state' => ORDER_STATE_CANCEL, 'pd_amount' => 0, 'close_time' => TIMESTAMP, 'close_reason' => $msg,
  89. ];
  90. $update = $vrorder_model->editVrorder($update_order, ['order_id' => $order_info['order_id']]);
  91. if (!$update) {
  92. throw new \think\Exception('保存失败', 10006);
  93. }
  94. }
  95. /**
  96. * 返还金额
  97. */
  98. public function orderCancelBack($order_info, $member_info)
  99. {
  100. //返还卡券
  101. if (!empty($order_info['deduction_amount']) && $order_info['card_id'] != 0) {
  102. //这两个都满足的情况下,走退款 删除支付记录里的订单关联数据
  103. //1.回滚卡片余额
  104. $cardWhere = [];
  105. $cardWhere[] = ['id', '=', $order_info['card_id']];
  106. $cardWhere[] = ['member_id', '=', $member_info['member_id']];
  107. $cardInfo = Db::name('sub_card')->where($cardWhere)->find();
  108. if (($cardInfo['balance'] + $order_info['deduction_amount']) == $cardInfo['worth']) {
  109. $cardStatus = 1;
  110. } else {
  111. $cardStatus = 2;
  112. }
  113. $cardUpdate = [
  114. 'card_status' => $cardStatus,
  115. 'balance' => $cardInfo['balance'] + $order_info['deduction_amount'],
  116. 'freeze' => $cardInfo['freeze'] - $order_info['deduction_amount'],
  117. 'update_time' => time(),
  118. 'used_count' => $cardInfo['used_count'] - 1,
  119. ];
  120. $cardRes = Db::name('sub_card')->where($cardWhere)->update($cardUpdate);
  121. if ($cardRes) {
  122. //如果卡的数据恢复成功了 删除卡的消费记录
  123. Db::name('sub_card_expense')->where([
  124. ['card_id', '=', $cardInfo['id']],
  125. ['order_id', '=', $order_info['order_id']],
  126. ])->update(['expense_status' => 2]);
  127. }
  128. }
  129. //返还积分
  130. if ($order_info['point_amount'] > 0) {
  131. $userModel = new User();
  132. $point_ratio = config('app.point_ratio');
  133. $userModel->changeScore($order_info['point_amount'] * $point_ratio, '取消订单', '', $member_info['union_id']);
  134. }
  135. }
  136. /**
  137. * 支付订单
  138. * @param array $order_info
  139. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  140. * @param string $post
  141. * @return array
  142. */
  143. public function changeOrderStatePay($order_info, $role, $post)
  144. {
  145. try {
  146. $vrorder_model = model('vrorder');
  147. Db::startTrans();
  148. $predeposit_model = model('predeposit');
  149. //下单,支付被冻结的充值卡
  150. $rcb_amount = floatval($order_info['rcb_amount']);
  151. if ($rcb_amount > 0) {
  152. $data_pd = [];
  153. $data_pd['member_id'] = $order_info['buyer_id'];
  154. $data_pd['member_name'] = $order_info['buyer_name'];
  155. $data_pd['amount'] = $rcb_amount;
  156. $data_pd['order_sn'] = $order_info['order_sn'];
  157. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  158. }
  159. //下单,支付被冻结的预存款
  160. $pd_amount = floatval($order_info['pd_amount']);
  161. if ($pd_amount > 0) {
  162. $data_pd = [];
  163. $data_pd['member_id'] = $order_info['buyer_id'];
  164. $data_pd['member_name'] = $order_info['buyer_name'];
  165. $data_pd['amount'] = $pd_amount;
  166. $data_pd['order_sn'] = $order_info['order_sn'];
  167. $predeposit_model->changePd('order_comb_pay', $data_pd);
  168. }
  169. //更新订单状态
  170. $update_order = [];
  171. $update_order['order_state'] = 40;
  172. $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
  173. $update_order['payment_code'] = $post['payment_code'];
  174. $update_order['trade_no'] = $post['trade_no'];
  175. $update = $vrorder_model->editVrorder($update_order, ['order_id' => $order_info['order_id']]);
  176. if (!$update) {
  177. throw new \think\Exception(lang('ds_common_save_fail'), 10006);
  178. }
  179. //如果是拼团
  180. if ($order_info['order_promotion_type'] == 2) {
  181. $ppintuangroup_model = model('ppintuangroup');
  182. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  183. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  184. if ($ppintuangroup_info['pintuangroup_joined'] == 0) {
  185. //拼团统计开团数量
  186. $condition = [];
  187. $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
  188. Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
  189. }
  190. //开团统计新增人数
  191. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->inc('pintuangroup_joined')->update();
  192. if (($ppintuangroup_info['pintuangroup_joined'] + 1) >= $ppintuangroup_info['pintuangroup_limit_number']) {
  193. $condition = [];
  194. $condition[] = ['pintuangroup_id', '=', $order_info['promotions_id']];
  195. $ppintuangroup_model->successPpintuangroup($condition, $condition);
  196. $this->addVrorderCode($order_info);
  197. $condition = [];
  198. $condition[] = ['pintuan_id', '=', $ppintuangroup_info['pintuan_id']];
  199. Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
  200. }
  201. }
  202. } else {//虚拟商品拼团等拼团成功再发兑换码
  203. $this->addVrorderCode($order_info);
  204. }
  205. Db::commit();
  206. return ds_callback(true, '更新成功');
  207. } catch (Exception $e) {
  208. Db::rollback();
  209. return ds_callback(false, $e->getMessage());
  210. }
  211. }
  212. public function addVrorderCode($order_info)
  213. {
  214. $vrorder_model = model('vrorder');
  215. //发放兑换码
  216. $insert = $vrorder_model->addVrorderCode($order_info);
  217. if (!$insert) {
  218. throw new \think\Exception('兑换码发送失败', 10006);
  219. }
  220. // 支付成功发送买家消息
  221. $param = [];
  222. $param['code'] = 'order_payment_success';
  223. $param['member_id'] = $order_info['buyer_id'];
  224. //阿里短信参数
  225. $param['ali_param'] = [
  226. 'order_sn' => $order_info['order_sn'],
  227. ];
  228. $param['ten_param'] = [
  229. $order_info['order_sn'],
  230. ];
  231. $param['param'] = array_merge($param['ali_param'], [
  232. 'order_url' => HOME_SITE_URL . '/Membervrorder/show_order?order_id=' . $order_info['order_id'],
  233. ]);
  234. //微信模板消息
  235. $param['weixin_param'] = [
  236. 'url' => config('ds_config.h5_site_url') . '/member/vrorder_detail?order_id=' . $order_info['order_id'],
  237. 'data' => [
  238. "keyword1" => [
  239. "value" => $order_info['order_sn'],
  240. "color" => "#333",
  241. ],
  242. "keyword2" => [
  243. "value" => $order_info['goods_name'],
  244. "color" => "#333",
  245. ],
  246. "keyword3" => [
  247. "value" => $order_info['order_amount'],
  248. "color" => "#333",
  249. ],
  250. "keyword4" => [
  251. "value" => date('Y-m-d H:i', $order_info['add_time']),
  252. "color" => "#333",
  253. ],
  254. ],
  255. ];
  256. \mall\queue\QueueClient::push('sendMemberMsg', $param);
  257. // 支付成功发送店铺消息
  258. $param = [];
  259. $param['code'] = 'new_order';
  260. $param['store_id'] = $order_info['store_id'];
  261. $param['ali_param'] = [
  262. 'order_sn' => $order_info['order_sn'],
  263. ];
  264. $param['ten_param'] = [
  265. $order_info['order_sn'],
  266. ];
  267. $param['param'] = $param['ali_param'];
  268. $param['weixin_param'] = [
  269. 'url' => config('ds_config.h5_site_url') . '/seller/vrorder_detail?order_id=' . $order_info['order_id'],
  270. 'data' => [
  271. "keyword1" => [
  272. "value" => $order_info['order_sn'],
  273. "color" => "#333",
  274. ],
  275. "keyword2" => [
  276. "value" => $order_info['goods_name'],
  277. "color" => "#333",
  278. ],
  279. "keyword3" => [
  280. "value" => $order_info['order_amount'],
  281. "color" => "#333",
  282. ],
  283. "keyword4" => [
  284. "value" => date('Y-m-d H:i', $order_info['add_time']),
  285. "color" => "#333",
  286. ],
  287. ],
  288. ];
  289. \mall\queue\QueueClient::push('sendStoremsg', $param);
  290. //发送兑换码到手机
  291. $param = [
  292. 'order_id' => $order_info['order_id'], 'buyer_id' => $order_info['buyer_id'],
  293. 'buyer_phone' => $order_info['buyer_phone'],
  294. ];
  295. \mall\queue\QueueClient::push('sendVrCode', $param);
  296. }
  297. /**
  298. * 完成订单
  299. * @param int $order_id
  300. * @return array
  301. */
  302. public function changeOrderStateSuccess($order_id)
  303. {
  304. $vrorder_model = model('vrorder');
  305. $condition = [];
  306. $condition[] = ['vr_state', '=', 0];
  307. $condition[] = ['refund_lock', 'in', [0, 1]];
  308. $condition[] = ['order_id', '=', $order_id];
  309. $condition[] = ['vr_indate', '>', TIMESTAMP];
  310. $order_code_info = $vrorder_model->getVrordercodeInfo($condition, '*');
  311. if (empty($order_code_info)) {
  312. $update = $vrorder_model->editVrorder([
  313. 'order_state' => ORDER_STATE_SUCCESS, 'finnshed_time' => TIMESTAMP,
  314. ], ['order_id' => $order_id]);
  315. if (!$update) {
  316. ds_callback(false, '更新失败');
  317. }
  318. }
  319. $order_info = $vrorder_model->getVrorderInfo(['order_id' => $order_id]);
  320. //添加会员积分
  321. if (config('ds_config.points_isuse') == 1) {
  322. model('points')->savePointslog('order', [
  323. 'pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'],
  324. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  325. 'order_id' => $order_info['order_id'],
  326. ], true);
  327. }
  328. //添加会员经验值
  329. model('exppoints')->saveExppointslog('order', [
  330. 'explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'],
  331. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  332. 'order_id' => $order_info['order_id'],
  333. ], true);
  334. return ds_callback(true, '更新成功');
  335. }
  336. }