Vrorder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 逻辑层模型
  13. */
  14. class Vrorder
  15. {
  16. /**
  17. * 取消订单
  18. * @param array $order_info
  19. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  20. * @param string $msg 操作备注
  21. * @param boolean $if_queue 是否使用队列
  22. * @return array
  23. */
  24. public function changeOrderStateCancel($order_info, $role, $msg, $if_queue = true)
  25. {
  26. try {
  27. $vrorder_model = model('vrorder');
  28. Db::startTrans();
  29. $ppintuanorder_model = model('ppintuanorder');
  30. if ($order_info['order_promotion_type'] == 2) {
  31. $condition = array();
  32. $condition[] = array('order_id', '=', $order_info['order_id']);
  33. $condition[] = array('pintuanorder_type', '=', 1);
  34. $condition[] = array('pintuanorder_state', '=', 1);
  35. $ppintuanorder_model->editPpintuanorder($condition, array('pintuanorder_state' => 0));
  36. }
  37. //库存、销量变更
  38. model('goods')->cancelOrderUpdateStorage(array($order_info['goods_id'] => $order_info['goods_num']), $order_info['virtual_type'] > 1 ? true : false);
  39. $predeposit_model = model('predeposit');
  40. //解冻充值卡
  41. $rcb_amount = floatval($order_info['rcb_amount']);
  42. $data_rcb = array();
  43. $data_rcb['member_id'] = $order_info['buyer_id'];
  44. $data_rcb['member_name'] = $order_info['buyer_name'];
  45. $data_rcb['amount'] = $rcb_amount;
  46. $data_rcb['order_sn'] = $order_info['order_sn'];
  47. //解冻预存款
  48. $pd_amount = floatval($order_info['pd_amount']);
  49. $data_pd = array();
  50. $data_pd['member_id'] = $order_info['buyer_id'];
  51. $data_pd['member_name'] = $order_info['buyer_name'];
  52. $data_pd['amount'] = $pd_amount;
  53. $data_pd['order_sn'] = $order_info['order_sn'];
  54. if ($order_info['order_state'] == ORDER_STATE_NEW) {
  55. if ($rcb_amount > 0) {
  56. $predeposit_model->changeRcb('order_cancel', $data_rcb);
  57. }
  58. if ($pd_amount > 0) {
  59. $predeposit_model->changePd('order_cancel', $data_pd);
  60. }
  61. }
  62. if ($order_info['order_state'] == ORDER_STATE_PAY) {
  63. $refundreturn_model = model('refundreturn');
  64. $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
  65. if ($order_info['order_promotion_type'] == 2) { //如果是拼团
  66. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  67. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  68. if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
  69. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->dec('pintuangroup_joined')->update();
  70. if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
  71. //拼团统计开团数量
  72. $condition = array();
  73. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  74. $condition[] = array('pintuan_count', '>', 0);
  75. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  76. }
  77. }
  78. }
  79. }
  80. }
  81. //更新订单信息
  82. $update_order = array(
  83. 'order_state' => ORDER_STATE_CANCEL, 'pd_amount' => 0, 'close_time' => TIMESTAMP, 'close_reason' => $msg
  84. );
  85. $update = $vrorder_model->editVrorder($update_order, array('order_id' => $order_info['order_id']));
  86. if (!$update) {
  87. throw new \think\Exception('保存失败', 10006);
  88. }
  89. //分销佣金取消
  90. $condition = array();
  91. $condition[] = array('orderinviter_order_id', '=', $order_info['order_id']);
  92. $condition[] = array('orderinviter_valid', '=', 0);
  93. $condition[] = array('orderinviter_order_type', '=', 1);
  94. Db::name('orderinviter')->where($condition)->update(['orderinviter_valid' => 2]);
  95. Db::commit();
  96. return ds_callback(true, '更新成功');
  97. } catch (Exception $e) {
  98. Db::rollback();
  99. return ds_callback(false, $e->getMessage());
  100. }
  101. }
  102. /**
  103. * 支付订单
  104. * @param array $order_info
  105. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  106. * @param string $post
  107. * @return array
  108. */
  109. public function changeOrderStatePay($order_info, $role, $post)
  110. {
  111. try {
  112. $vrorder_model = model('vrorder');
  113. Db::startTrans();
  114. $predeposit_model = model('predeposit');
  115. //下单,支付被冻结的充值卡
  116. $rcb_amount = floatval($order_info['rcb_amount']);
  117. if ($rcb_amount > 0) {
  118. $data_pd = array();
  119. $data_pd['member_id'] = $order_info['buyer_id'];
  120. $data_pd['member_name'] = $order_info['buyer_name'];
  121. $data_pd['amount'] = $rcb_amount;
  122. $data_pd['order_sn'] = $order_info['order_sn'];
  123. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  124. }
  125. //下单,支付被冻结的预存款
  126. $pd_amount = floatval($order_info['pd_amount']);
  127. if ($pd_amount > 0) {
  128. $data_pd = array();
  129. $data_pd['member_id'] = $order_info['buyer_id'];
  130. $data_pd['member_name'] = $order_info['buyer_name'];
  131. $data_pd['amount'] = $pd_amount;
  132. $data_pd['order_sn'] = $order_info['order_sn'];
  133. $predeposit_model->changePd('order_comb_pay', $data_pd);
  134. }
  135. //更新订单状态
  136. $update_order = array();
  137. $update_order['order_state'] = ORDER_STATE_PAY;
  138. $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
  139. $update_order['payment_code'] = $post['payment_code'];
  140. $update_order['trade_no'] = $post['trade_no'];
  141. $update = $vrorder_model->editVrorder($update_order, array('order_id' => $order_info['order_id']));
  142. if (!$update) {
  143. throw new \think\Exception(lang('ds_common_save_fail'), 10006);
  144. }
  145. //如果是拼团
  146. if ($order_info['order_promotion_type'] == 2) {
  147. $ppintuangroup_model = model('ppintuangroup');
  148. $ppintuangroup_info = Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  149. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  150. if ($ppintuangroup_info['pintuangroup_joined'] == 0) {
  151. //拼团统计开团数量
  152. $condition = array();
  153. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  154. Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
  155. }
  156. //开团统计新增人数
  157. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->inc('pintuangroup_joined')->update();
  158. if (($ppintuangroup_info['pintuangroup_joined'] + 1) >= $ppintuangroup_info['pintuangroup_limit_number']) {
  159. $condition = array();
  160. $condition[] = array('pintuangroup_id', '=', $order_info['promotions_id']);
  161. $ppintuangroup_model->successPpintuangroup($condition, $condition);
  162. $this->addVrorderCode($order_info);
  163. $condition = array();
  164. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  165. Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
  166. }
  167. }
  168. } elseif ($order_info['virtual_type'] == 0) { //虚拟商品拼团等拼团成功再发兑换码
  169. $this->addVrorderCode($order_info);
  170. } else {
  171. $result = $this->changeOrderStateSuccess($order_info['order_id']);
  172. if (!$result['code']) {
  173. return $result;
  174. }
  175. }
  176. Db::commit();
  177. return ds_callback(true, '更新成功');
  178. } catch (Exception $e) {
  179. Db::rollback();
  180. return ds_callback(false, $e->getMessage());
  181. }
  182. }
  183. public function addVrorderCode($order_info)
  184. {
  185. $vrorder_model = model('vrorder');
  186. //发放兑换码
  187. $insert = $vrorder_model->addVrorderCode($order_info);
  188. if (!$insert) {
  189. throw new \think\Exception('兑换码发送失败', 10006);
  190. }
  191. // 支付成功发送买家消息
  192. $param = array();
  193. $param['code'] = 'order_payment_success';
  194. $param['member_id'] = $order_info['buyer_id'];
  195. //阿里短信参数
  196. $param['ali_param'] = array(
  197. 'order_sn' => $order_info['order_sn'],
  198. );
  199. $param['ten_param'] = array(
  200. $order_info['order_sn'],
  201. );
  202. $param['param'] = array_merge($param['ali_param'], array(
  203. 'order_url' => HOME_SITE_URL . '/Membervrorder/show_order?order_id=' . $order_info['order_id']
  204. ));
  205. //微信模板消息
  206. $param['weixin_param'] = array(
  207. 'url' => config('ds_config.h5_site_url') . '/pages/member/vrorder/OrderDetail?order_id=' . $order_info['order_id'],
  208. 'data' => array(
  209. "keyword1" => array(
  210. "value" => $order_info['order_sn'],
  211. "color" => "#333"
  212. ),
  213. "keyword2" => array(
  214. "value" => $order_info['goods_name'],
  215. "color" => "#333"
  216. ),
  217. "keyword3" => array(
  218. "value" => $order_info['order_amount'],
  219. "color" => "#333"
  220. ),
  221. "keyword4" => array(
  222. "value" => date('Y-m-d H:i', $order_info['add_time']),
  223. "color" => "#333"
  224. )
  225. ),
  226. );
  227. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendMemberMsg', 'cron_value' => serialize($param)));
  228. // 支付成功发送店铺消息
  229. $param = array();
  230. $param['code'] = 'new_order';
  231. $param['store_id'] = $order_info['store_id'];
  232. $param['ali_param'] = array(
  233. 'order_sn' => $order_info['order_sn']
  234. );
  235. $param['ten_param'] = array(
  236. $order_info['order_sn']
  237. );
  238. $param['param'] = $param['ali_param'];
  239. $param['weixin_param'] = array(
  240. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/vrorder/OrderDetail?order_id=' . $order_info['order_id'],
  241. 'data' => array(
  242. "keyword1" => array(
  243. "value" => $order_info['order_sn'],
  244. "color" => "#333"
  245. ),
  246. "keyword2" => array(
  247. "value" => $order_info['goods_name'],
  248. "color" => "#333"
  249. ),
  250. "keyword3" => array(
  251. "value" => $order_info['order_amount'],
  252. "color" => "#333"
  253. ),
  254. "keyword4" => array(
  255. "value" => date('Y-m-d H:i', $order_info['add_time']),
  256. "color" => "#333"
  257. )
  258. ),
  259. );
  260. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'sendStoremsg', 'cron_value' => serialize($param)));
  261. //发送兑换码到手机
  262. $param = array(
  263. 'order_id' => $order_info['order_id'], 'buyer_id' => $order_info['buyer_id'],
  264. 'buyer_phone' => $order_info['buyer_phone']
  265. );
  266. $vrorder_model->sendVrCode($param);
  267. }
  268. /**
  269. * 完成订单
  270. * @param int $order_id
  271. * @return array
  272. */
  273. public function changeOrderStateSuccess($order_id)
  274. {
  275. $vrorder_model = model('vrorder');
  276. $condition = array();
  277. $condition[] = array('vr_state', '=', 0);
  278. $condition[] = array('refund_lock', 'in', array(0, 1));
  279. $condition[] = array('order_id', '=', $order_id);
  280. $condition[] = array('vr_indate', '>', TIMESTAMP);
  281. $order_code_info = $vrorder_model->getVrordercodeInfo($condition, '*');
  282. if (empty($order_code_info)) {
  283. $update = $vrorder_model->editVrorder(array(
  284. 'order_state' => ORDER_STATE_SUCCESS, 'finnshed_time' => TIMESTAMP
  285. ), array('order_id' => $order_id));
  286. if (!$update) {
  287. ds_callback(false, '更新失败');
  288. }
  289. }
  290. $order_info = $vrorder_model->getVrorderInfo(array('order_id' => $order_id));
  291. //添加会员积分
  292. if (config('ds_config.points_isuse') == 1) {
  293. model('points')->savePointslog('order', array(
  294. 'pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'],
  295. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  296. 'order_id' => $order_info['order_id']
  297. ), true);
  298. }
  299. //添加会员经验值
  300. model('exppoints')->saveExppointslog('order', array(
  301. 'explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'],
  302. 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'],
  303. 'order_id' => $order_info['order_id']
  304. ), true);
  305. return ds_callback(true, '更新成功');
  306. }
  307. }