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