Vrorder.php 16 KB

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