Vrorder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 逻辑层模型
  14. */
  15. class Vrorder
  16. {
  17. /**
  18. * 取消订单
  19. * @param array $order_info
  20. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  21. * @param string $msg 操作备注
  22. * @param boolean $if_queue 是否使用队列
  23. * @return array
  24. */
  25. public function changeOrderStateCancel($order_info, $role, $msg, $if_queue = true)
  26. {
  27. try {
  28. $vrorder_model = model('vrorder');
  29. Db::startTrans();
  30. $ppintuanorder_model=model('ppintuanorder');
  31. if($order_info['order_promotion_type']==2){
  32. $condition=array();
  33. $condition[]=array('order_id','=',$order_info['order_id']);
  34. $condition[]=array('pintuanorder_type','=',1);
  35. $condition[]=array('pintuanorder_state','=',1);
  36. $ppintuanorder_model->editPpintuanorder($condition, array('pintuanorder_state'=>0));
  37. }
  38. //库存、销量变更
  39. model('goods')->cancelOrderUpdateStorage(array($order_info['goods_id'] => $order_info['goods_num']),$order_info['virtual_type']>1?true:false);
  40. $predeposit_model = model('predeposit');
  41. //解冻充值卡
  42. $rcb_amount = floatval($order_info['rcb_amount']);
  43. $data_rcb = array();
  44. $data_rcb['member_id'] = $order_info['buyer_id'];
  45. $data_rcb['member_name'] = $order_info['buyer_name'];
  46. $data_rcb['amount'] = $rcb_amount;
  47. $data_rcb['order_sn'] = $order_info['order_sn'];
  48. //解冻预存款
  49. $pd_amount = floatval($order_info['pd_amount']);
  50. $data_pd = array();
  51. $data_pd['member_id'] = $order_info['buyer_id'];
  52. $data_pd['member_name'] = $order_info['buyer_name'];
  53. $data_pd['amount'] = $pd_amount;
  54. $data_pd['order_sn'] = $order_info['order_sn'];
  55. if ($order_info['order_state'] == ORDER_STATE_NEW) {
  56. if ($rcb_amount > 0) {
  57. $predeposit_model->changeRcb('order_cancel', $data_rcb);
  58. }
  59. if ($pd_amount > 0) {
  60. $predeposit_model->changePd('order_cancel', $data_pd);
  61. }
  62. }
  63. if ($order_info['order_state'] == ORDER_STATE_PAY) {
  64. $refundreturn_model=model('refundreturn');
  65. $refundreturn_model->refundAmount($order_info, $order_info['order_amount']);
  66. if($order_info['order_promotion_type']==2){//如果是拼团
  67. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  68. if ($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state'] == 1) {
  69. if ($ppintuangroup_info['pintuangroup_joined'] > 0) {
  70. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->dec('pintuangroup_joined')->update();
  71. if ($ppintuangroup_info['pintuangroup_joined'] == 1) {
  72. //拼团统计开团数量
  73. $condition = array();
  74. $condition[] = array('pintuan_id', '=', $ppintuangroup_info['pintuan_id']);
  75. $condition[] = array('pintuan_count', '>', 0);
  76. Db::name('ppintuan')->where($condition)->dec('pintuan_count')->update();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. //更新订单信息
  83. $update_order = array(
  84. 'order_state' => ORDER_STATE_CANCEL, 'pd_amount' => 0, 'close_time' => TIMESTAMP, 'close_reason' => $msg
  85. );
  86. $update = $vrorder_model->editVrorder($update_order, array('order_id' => $order_info['order_id']));
  87. if (!$update) {
  88. throw new \think\Exception('保存失败', 10006);
  89. }
  90. //分销佣金取消
  91. $condition=array();
  92. $condition[]=array('orderinviter_order_id','=',$order_info['order_id']);
  93. $condition[]=array('orderinviter_valid','=',0);
  94. $condition[]=array('orderinviter_order_type','=',1);
  95. Db::name('orderinviter')->where($condition)->update(['orderinviter_valid' => 2]);
  96. Db::commit();
  97. return ds_callback(true, '更新成功');
  98. } catch (Exception $e) {
  99. Db::rollback();
  100. return ds_callback(false, $e->getMessage());
  101. }
  102. }
  103. /**
  104. * 支付订单
  105. * @param array $order_info
  106. * @param string $role 操作角色 buyer、seller、admin、system 分别代表买家、商家、管理员、系统
  107. * @param string $post
  108. * @return array
  109. */
  110. public function changeOrderStatePay($order_info, $role, $post)
  111. {
  112. try {
  113. $vrorder_model = model('vrorder');
  114. Db::startTrans();
  115. $predeposit_model = model('predeposit');
  116. //下单,支付被冻结的充值卡
  117. $rcb_amount = floatval($order_info['rcb_amount']);
  118. if ($rcb_amount > 0) {
  119. $data_pd = array();
  120. $data_pd['member_id'] = $order_info['buyer_id'];
  121. $data_pd['member_name'] = $order_info['buyer_name'];
  122. $data_pd['amount'] = $rcb_amount;
  123. $data_pd['order_sn'] = $order_info['order_sn'];
  124. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  125. }
  126. //下单,支付被冻结的预存款
  127. $pd_amount = floatval($order_info['pd_amount']);
  128. if ($pd_amount > 0) {
  129. $data_pd = array();
  130. $data_pd['member_id'] = $order_info['buyer_id'];
  131. $data_pd['member_name'] = $order_info['buyer_name'];
  132. $data_pd['amount'] = $pd_amount;
  133. $data_pd['order_sn'] = $order_info['order_sn'];
  134. $predeposit_model->changePd('order_comb_pay', $data_pd);
  135. }
  136. //更新订单状态
  137. $update_order = array();
  138. $update_order['order_state'] = ORDER_STATE_PAY;
  139. $update_order['payment_time'] = isset($post['payment_time']) ? strtotime($post['payment_time']) : TIMESTAMP;
  140. $update_order['payment_code'] = $post['payment_code'];
  141. $update_order['trade_no'] = $post['trade_no'];
  142. $update = $vrorder_model->editVrorder($update_order, array('order_id' => $order_info['order_id']));
  143. if (!$update) {
  144. throw new \think\Exception(lang('ds_common_save_fail'), 10006);
  145. }
  146. //如果是拼团
  147. if ($order_info['order_promotion_type']==2) {
  148. $ppintuangroup_model=model('ppintuangroup');
  149. $ppintuangroup_info=Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->lock(true)->find();
  150. if($ppintuangroup_info && $ppintuangroup_info['pintuangroup_state']==1){
  151. if($ppintuangroup_info['pintuangroup_joined']==0){
  152. //拼团统计开团数量
  153. $condition=array();
  154. $condition[]=array('pintuan_id','=', $ppintuangroup_info['pintuan_id']);
  155. Db::name('ppintuan')->where($condition)->inc('pintuan_count')->update();
  156. }
  157. //开团统计新增人数
  158. Db::name('ppintuangroup')->where('pintuangroup_id', $order_info['promotions_id'])->inc('pintuangroup_joined')->update();
  159. if(($ppintuangroup_info['pintuangroup_joined']+1)>=$ppintuangroup_info['pintuangroup_limit_number']){
  160. $condition = array();
  161. $condition[] = array('pintuangroup_id','=', $order_info['promotions_id']);
  162. $ppintuangroup_model->successPpintuangroup($condition,$condition);
  163. $this->addVrorderCode($order_info);
  164. $condition=array();
  165. $condition[]=array('pintuan_id','=', $ppintuangroup_info['pintuan_id']);
  166. Db::name('ppintuan')->where($condition)->inc('pintuan_ok_count')->update();
  167. }
  168. }
  169. }elseif($order_info['virtual_type']==0){//虚拟商品拼团等拼团成功再发兑换码
  170. $this->addVrorderCode($order_info);
  171. }else{
  172. $result = $this->changeOrderStateSuccess($order_info['order_id']);
  173. if (!$result['code']) {
  174. return $result;
  175. }
  176. }
  177. Db::commit();
  178. return ds_callback(true, '更新成功');
  179. } catch (Exception $e) {
  180. Db::rollback();
  181. return ds_callback(false, $e->getMessage());
  182. }
  183. }
  184. public function addVrorderCode($order_info) {
  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. }