StoreOrder.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\ebapi\model\store;
  8. use app\core\model\routine\RoutineTemplate;
  9. use app\core\util\ApiLogs;
  10. use app\ebapi\model\user\User;
  11. use app\ebapi\model\user\UserAddress;
  12. use app\core\model\user\UserBill;
  13. use app\ebapi\model\user\WechatUser;
  14. use basic\ModelBasic;
  15. use app\core\behavior\OrderBehavior;
  16. use app\core\behavior\GoodsBehavior;
  17. use app\core\behavior\UserBehavior;
  18. use app\core\behavior\PaymentBehavior;
  19. use service\HookService;
  20. use app\core\util\MiniProgramService;
  21. use app\core\util\SystemConfigService;
  22. use think\Cache;
  23. use think\Exception;
  24. use think\exception\PDOException;
  25. use traits\ModelTrait;
  26. class StoreOrder extends ModelBasic
  27. {
  28. use ModelTrait;
  29. protected $insert = ['add_time'];
  30. protected static $payType = ['weixin'=>'微信支付','yue'=>'余额支付','offline'=>'线下支付'];
  31. protected static $deliveryType = ['send'=>'商家配送','express'=>'快递配送'];
  32. protected function setAddTimeAttr()
  33. {
  34. return time();
  35. }
  36. protected function setCartIdAttr($value)
  37. {
  38. return is_array($value) ? json_encode($value) : $value;
  39. }
  40. protected function getCartIdAttr($value)
  41. {
  42. return json_decode($value,true);
  43. }
  44. /**获取订单组信息
  45. * @param $cartInfo
  46. * @return array
  47. */
  48. public static function getOrderPriceGroup($cartInfo)
  49. {
  50. $storePostage = floatval(SystemConfigService::get('store_postage'))?:0;//邮费基础价
  51. $storeFreePostage = floatval(SystemConfigService::get('store_free_postage'))?:0;//满额包邮
  52. $totalPrice = self::getOrderSumPrice($cartInfo,'truePrice');//获取订单总金额
  53. $costPrice = self::getOrderSumPrice($cartInfo,'costPrice');//获取订单成本价
  54. $vipPrice = self::getOrderSumPrice($cartInfo,'vip_truePrice');//获取订单会员优惠金额
  55. //如果满额包邮等于0
  56. if(!$storeFreePostage) {
  57. $storePostage = 0;
  58. }else{
  59. foreach ($cartInfo as $cart){
  60. if(!$cart['productInfo']['is_postage'])//若果产品不包邮
  61. $storePostage = bcadd($storePostage,$cart['productInfo']['postage'],2);
  62. }
  63. if($storeFreePostage <= $totalPrice) $storePostage = 0;//如果总价大于等于满额包邮 邮费等于0
  64. }
  65. // $totalPrice = bcadd($totalPrice,$storePostage,2);
  66. return compact('storePostage','storeFreePostage','totalPrice','costPrice','vipPrice');
  67. }
  68. /**获取某个字段总金额
  69. * @param $cartInfo
  70. * @param $key 键名
  71. * @return int|string
  72. */
  73. public static function getOrderSumPrice($cartInfo,$key='truePrice')
  74. {
  75. $SumPrice = 0;
  76. foreach ($cartInfo as $cart){
  77. $SumPrice = bcadd($SumPrice,bcmul($cart['cart_num'],$cart[$key],2),2);
  78. }
  79. return $SumPrice;
  80. }
  81. /**
  82. * 拼团
  83. * @param $cartInfo
  84. * @return array
  85. */
  86. public static function getCombinationOrderPriceGroup($cartInfo)
  87. {
  88. $storePostage = floatval(SystemConfigService::get('store_postage'))?:0;
  89. $storeFreePostage = floatval(SystemConfigService::get('store_free_postage'))?:0;
  90. $totalPrice = self::getCombinationOrderTotalPrice($cartInfo);
  91. $costPrice = self::getCombinationOrderTotalPrice($cartInfo);
  92. if(!$storeFreePostage) {
  93. $storePostage = 0;
  94. }else{
  95. foreach ($cartInfo as $cart){
  96. if(!StoreCombination::where('id',$cart['combination_id'])->value('is_postage'))
  97. $storePostage = bcadd($storePostage,StoreCombination::where('id',$cart['combination_id'])->value('postage'),2);
  98. }
  99. if($storeFreePostage <= $totalPrice) $storePostage = 0;
  100. }
  101. return compact('storePostage','storeFreePostage','totalPrice','costPrice');
  102. }
  103. /**
  104. * 拼团价格
  105. * @param $cartInfo
  106. * @return float
  107. */
  108. public static function getCombinationOrderTotalPrice($cartInfo)
  109. {
  110. $totalPrice = 0;
  111. foreach ($cartInfo as $cart){
  112. if($cart['combination_id']){
  113. $totalPrice = bcadd($totalPrice,bcmul($cart['cart_num'],StoreCombination::where('id',$cart['combination_id'])->value('price'),2),2);
  114. }
  115. }
  116. return (float)$totalPrice;
  117. }
  118. public static function cacheOrderInfo($uid,$cartInfo,$priceGroup,$other = [],$cacheTime = 600)
  119. {
  120. $key = md5(time());
  121. Cache::set('user_order_'.$uid.$key,compact('cartInfo','priceGroup','other'),$cacheTime);
  122. return $key;
  123. }
  124. public static function getCacheOrderInfo($uid,$key)
  125. {
  126. $cacheName = 'user_order_'.$uid.$key;
  127. if(!Cache::has($cacheName)) return null;
  128. return Cache::get($cacheName);
  129. }
  130. public static function clearCacheOrderInfo($uid,$key)
  131. {
  132. Cache::clear('user_order_'.$uid.$key);
  133. }
  134. /**生成订单
  135. * @param $uid
  136. * @param $key
  137. * @param $addressId
  138. * @param $payType
  139. * @param bool $useIntegral
  140. * @param int $couponId
  141. * @param string $mark
  142. * @param int $combinationId
  143. * @param int $pinkId
  144. * @param int $seckill_id
  145. * @param int $bargain_id
  146. * @return bool|object
  147. */
  148. public static function cacheKeyCreateOrder($uid,$key,$addressId,$payType,$useIntegral = false,$couponId = 0,$mark = '',$combinationId = 0,$pinkId = 0,$seckill_id=0,$bargain_id=0)
  149. {
  150. if(!array_key_exists($payType,self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  151. if(self::be(['unique'=>$key,'uid'=>$uid])) return self::setErrorInfo('请勿重复提交订单');
  152. $userInfo = User::getUserInfo($uid);
  153. if(!$userInfo) return self::setErrorInfo('用户不存在!');
  154. $cartGroup = self::getCacheOrderInfo($uid,$key);
  155. if(!$cartGroup) return self::setErrorInfo('订单已过期,请刷新当前页面!');
  156. $cartInfo = $cartGroup['cartInfo'];
  157. $priceGroup = $cartGroup['priceGroup'];
  158. $other = $cartGroup['other'];
  159. $payPrice = (float)$priceGroup['totalPrice'];
  160. $payPostage = $priceGroup['storePostage'];
  161. if(!$addressId) return self::setErrorInfo('请选择收货地址!');
  162. if(!UserAddress::be(['uid'=>$uid,'id'=>$addressId,'is_del'=>0]) || !($addressInfo = UserAddress::find($addressId)))
  163. return self::setErrorInfo('地址选择有误!');
  164. //使用优惠劵
  165. $res1 = true;
  166. if($couponId){
  167. $couponInfo = StoreCouponUser::validAddressWhere()->where('id',$couponId)->where('uid',$uid)->find();
  168. if(!$couponInfo) return self::setErrorInfo('选择的优惠劵无效!');
  169. if($couponInfo['use_min_price'] > $payPrice)
  170. return self::setErrorInfo('不满足优惠劵的使用条件!');
  171. $payPrice = (float)bcsub($payPrice,$couponInfo['coupon_price'],2);
  172. $res1 = StoreCouponUser::useCoupon($couponId);
  173. $couponPrice = $couponInfo['coupon_price'];
  174. }else{
  175. $couponId = 0;
  176. $couponPrice = 0;
  177. }
  178. if(!$res1) return self::setErrorInfo('使用优惠劵失败!');
  179. //是否包邮
  180. if((isset($other['offlinePostage']) && $other['offlinePostage'] && $payType == 'offline')) $payPostage = 0;
  181. $payPrice = (float)bcadd($payPrice,$payPostage,2);
  182. //积分抵扣
  183. $res2 = true;
  184. if($useIntegral && $userInfo['integral'] > 0){
  185. $deductionPrice = (float)bcmul($userInfo['integral'],$other['integralRatio'],2);
  186. if($deductionPrice < $payPrice){
  187. $payPrice = bcsub($payPrice,$deductionPrice,2);
  188. $usedIntegral = $userInfo['integral'];
  189. $res2 = false !== User::edit(['integral'=>0],$userInfo['uid'],'uid');
  190. }else{
  191. $deductionPrice = $payPrice;
  192. $usedIntegral = (float)bcdiv($payPrice,$other['integralRatio'],2);
  193. $res2 = false !== User::bcDec($userInfo['uid'],'integral',$usedIntegral,'uid');
  194. $payPrice = 0;
  195. }
  196. $res2 = $res2 && false != UserBill::expend('积分抵扣',$uid,'integral','deduction',$usedIntegral,$key,$userInfo['integral'],'购买商品使用'.floatval($usedIntegral).'积分抵扣'.floatval($deductionPrice).'元');
  197. }else{
  198. $deductionPrice = 0;
  199. $usedIntegral = 0;
  200. }
  201. if(!$res2) return self::setErrorInfo('使用积分抵扣失败!');
  202. $cartIds = [];
  203. $totalNum = 0;
  204. $gainIntegral = 0;
  205. foreach ($cartInfo as $cart){
  206. $cartIds[] = $cart['id'];
  207. $totalNum += $cart['cart_num'];
  208. $gainIntegral = bcadd($gainIntegral,isset($cart['productInfo']['give_integral']) ? bcmul($cart['productInfo']['give_integral'],$cart['cart_num'],2) : 0,2);
  209. }
  210. $orderInfo = [
  211. 'uid'=>$uid,
  212. 'order_id'=>self::getNewOrderId(),
  213. 'real_name'=>$addressInfo['real_name'],
  214. 'user_phone'=>$addressInfo['phone'],
  215. 'user_address'=>$addressInfo['province'].' '.$addressInfo['city'].' '.$addressInfo['district'].' '.$addressInfo['detail'],
  216. 'cart_id'=>$cartIds,
  217. 'total_num'=>$totalNum,
  218. 'total_price'=>$priceGroup['totalPrice'],
  219. 'total_postage'=>$priceGroup['storePostage'],
  220. 'coupon_id'=>$couponId,
  221. 'coupon_price'=>$couponPrice,
  222. 'pay_price'=>$payPrice,
  223. 'pay_postage'=>$payPostage,
  224. 'deduction_price'=>$deductionPrice,
  225. 'paid'=>0,
  226. 'pay_type'=>$payType,
  227. 'use_integral'=>$usedIntegral,
  228. 'gain_integral'=>$gainIntegral,
  229. 'mark'=>htmlspecialchars($mark),
  230. 'combination_id'=>$combinationId,
  231. 'pink_id'=>$pinkId,
  232. 'seckill_id'=>$seckill_id,
  233. 'bargain_id'=>$bargain_id,
  234. 'cost'=>$priceGroup['costPrice'],
  235. 'is_channel'=>1,
  236. 'unique'=>$key
  237. ];
  238. $order = self::set($orderInfo);
  239. if(!$order)return self::setErrorInfo('订单生成失败!');
  240. $res5 = true;
  241. foreach ($cartInfo as $cart)
  242. {
  243. //减库存加销量
  244. if($combinationId) $res5 = $res5 && StoreCombination::decCombinationStock($cart['cart_num'],$combinationId);
  245. else if($seckill_id) $res5 = $res5 && StoreSeckill::decSeckillStock($cart['cart_num'],$seckill_id);
  246. else if($bargain_id) $res5 = $res5 && StoreBargain::decBargainStock($cart['cart_num'],$bargain_id);
  247. else $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'],$cart['productInfo']['id'],isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique']:'');
  248. }
  249. //保存购物车商品信息
  250. $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'],$cartInfo);
  251. //购物车状态修改
  252. $res6 = false !== StoreCart::where('id','IN',$cartIds)->update(['is_pay'=>1]);
  253. if(!$res4 || !$res5 || !$res6) return self::setErrorInfo('订单生成失败!');
  254. try{
  255. HookService::listen('store_product_order_create',$order,compact('cartInfo','addressId'),false,GoodsBehavior::class);
  256. }catch (\Exception $e){
  257. return self::setErrorInfo($e->getMessage());
  258. }
  259. self::clearCacheOrderInfo($uid,$key);
  260. self::commitTrans();
  261. StoreOrderStatus::status($order['id'],'cache_key_create_order','订单生成');
  262. return $order;
  263. }
  264. /*
  265. * 回退积分
  266. * @param array $order 订单信息
  267. * @return boolean
  268. * */
  269. public static function RegressionIntegral($order)
  270. {
  271. if($order['paid'] || $order['status']==-2 || $order['is_del']) return true;
  272. if($order['use_integral'] <= 0) return true;
  273. if((int)$order['status']!=-2 && (int)$order['refund_status']!=2 && $order['back_integral'] >= $order['use_integral']) return true;
  274. $res=User::bcInc($order['uid'],'integral',$order['use_integral']);
  275. if(!$res) return self::setErrorInfo('回退积分增加失败');
  276. UserBill::income('积分回退',$order['uid'],'integral','deduction',$order['use_integral'],$order['unique'],User::where('uid',$order['uid'])->value('integral'),'购买商品失败,回退积分'.floatval($order['use_integral']));
  277. return false !== self::where('order_id',$order['order_id'])->update(['back_integral'=>$order['use_integral']]);
  278. }
  279. /*
  280. * 回退库存和销量
  281. * @param array $order 订单信息
  282. * @return boolean
  283. * */
  284. public static function RegressionStock($order)
  285. {
  286. if($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  287. $combinationId = $order['combination_id'];
  288. $seckill_id = $order['seckill_id'];
  289. $bargain_id = $order['bargain_id'];
  290. $res5 = true;
  291. $cartInfo=StoreOrderCartInfo::where('cart_id','in',$order['cart_id'])->select();
  292. foreach ($cartInfo as $cart)
  293. {
  294. //增库存减销量
  295. if($combinationId) $res5 = $res5 && StoreCombination::incCombinationStock($cart['cart_info']['cart_num'],$combinationId);
  296. else if($seckill_id) $res5 = $res5 && StoreSeckill::incSeckillStock($cart['cart_info']['cart_num'],$seckill_id);
  297. else if($bargain_id) $res5 = $res5 && StoreBargain::incBargainStock($cart['cart_info']['cart_num'],$bargain_id);
  298. else $res5 = $res5 && StoreProduct::incProductStock($cart['cart_info']['cart_num'],$cart['cart_info']['productInfo']['id'],isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique']:'');
  299. }
  300. return $res5;
  301. }
  302. /*
  303. * 回退优惠卷
  304. * @param array $order 订单信息
  305. * @return boolean
  306. * */
  307. public static function RegressionCoupon($order)
  308. {
  309. if($order['paid'] || $order['status']==-2 || $order['is_del']) return true;
  310. $res=true;
  311. if($order['coupon_id'] && StoreCouponUser::be(['id'=>$order['coupon_id'],'uid'=>$order['uid'],'status'=>1])){
  312. $res= $res && false !== StoreCouponUser::where(['id'=>$order['coupon_id'],'uid'=>$order['uid']])->update(['status'=>0,'use_time'=>0]);
  313. }
  314. return $res;
  315. }
  316. /*
  317. * 取消订单
  318. * @param string order_id 订单id
  319. * */
  320. public static function cancelOrder($order_id)
  321. {
  322. $order=self::where('order_id',$order_id)->find();
  323. if(!$order) return self::setErrorInfo('没有查到此订单');
  324. self::beginTrans();
  325. try{
  326. $res=self::RegressionIntegral($order) && self::RegressionStock($order) && self::RegressionCoupon($order);
  327. if($res){
  328. $order->is_del=1;
  329. $order->save();
  330. self::commitTrans();
  331. return true;
  332. }
  333. }catch (\Exception $e){
  334. self::rollbackTrans();
  335. return self::setErrorInfo(['line'=>$e->getLine(),'message'=>$e->getMessage()]);
  336. }
  337. }
  338. public static function getNewOrderId()
  339. {
  340. $count = (int) self::where('add_time',['>=',strtotime(date("Y-m-d"))],['<',strtotime(date("Y-m-d",strtotime('+1 day')))])->count();
  341. return 'wx'.date('YmdHis',time()).(10000+$count+1);
  342. }
  343. public static function changeOrderId($orderId)
  344. {
  345. $ymd = substr($orderId,2,8);
  346. $key = substr($orderId,16);
  347. return 'wx'.$ymd.date('His').$key;
  348. }
  349. //TODO JS支付
  350. public static function jsPay($orderId,$field = 'order_id')
  351. {
  352. if(is_string($orderId))
  353. $orderInfo = self::where($field,$orderId)->find();
  354. else
  355. $orderInfo = $orderId;
  356. if(!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  357. if($orderInfo['paid']) exception('支付已支付!');
  358. if($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  359. $openid = WechatUser::getOpenId($orderInfo['uid']);
  360. return MiniProgramService::jsPay($openid,$orderInfo['order_id'],$orderInfo['pay_price'],'productr',SystemConfigService::get('site_name'));
  361. }
  362. //TODO 余额支付
  363. public static function yuePay($order_id,$uid,$formId = '')
  364. {
  365. $orderInfo = self::where('uid',$uid)->where('order_id',$order_id)->where('is_del',0)->find();
  366. if(!$orderInfo) return self::setErrorInfo('订单不存在!');
  367. if($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  368. // if($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  369. $userInfo = User::getUserInfo($uid);
  370. if($userInfo['now_money'] < $orderInfo['pay_price'])
  371. return self::setErrorInfo(['status'=>'pay_deficiency','msg'=>'余额不足'.floatval($orderInfo['pay_price'])]);
  372. self::beginTrans();
  373. $res1 = false !== User::bcDec($uid,'now_money',$orderInfo['pay_price'],'uid');
  374. $res2 = UserBill::expend('购买商品',$uid,'now_money','pay_product',$orderInfo['pay_price'],$orderInfo['id'],$userInfo['now_money'],'余额支付'.floatval($orderInfo['pay_price']).'元购买商品');
  375. $res3 = self::paySuccess($order_id,'yue',$formId);//余额支付成功
  376. try{
  377. HookService::listen('yue_pay_product',$userInfo,$orderInfo,false,PaymentBehavior::class);
  378. }catch (\Exception $e){
  379. self::rollbackTrans();
  380. return self::setErrorInfo($e->getMessage());
  381. }
  382. $res = $res1 && $res2 && $res3;
  383. self::checkTrans($res);
  384. return $res;
  385. }
  386. /**
  387. * 微信支付 为 0元时
  388. * @param $order_id
  389. * @param $uid
  390. * @return bool
  391. */
  392. public static function jsPayPrice($order_id,$uid,$formId = ''){
  393. $orderInfo = self::where('uid',$uid)->where('order_id',$order_id)->where('is_del',0)->find();
  394. if(!$orderInfo) return self::setErrorInfo('订单不存在!');
  395. if($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  396. $userInfo = User::getUserInfo($uid);
  397. self::beginTrans();
  398. $res1 = UserBill::expend('购买商品',$uid,'now_money','pay_product',$orderInfo['pay_price'],$orderInfo['id'],$userInfo['now_money'],'微信支付'.floatval($orderInfo['pay_price']).'元购买商品');
  399. $res2 = self::paySuccess($order_id,'weixin',$formId);//微信支付为0时
  400. $res = $res1 && $res2;
  401. self::checkTrans($res);
  402. return $res;
  403. }
  404. /**
  405. * 用户申请退款
  406. * @param $uni
  407. * @param $uid
  408. * @param string $refundReasonWap
  409. * @return bool
  410. */
  411. public static function orderApplyRefund($uni, $uid,$refundReasonWap = '',$refundReasonWapExplain = '',$refundReasonWapImg = array())
  412. {
  413. $order = self::getUserOrderDetail($uid,$uni);
  414. if(!$order) return self::setErrorInfo('支付订单不存在!');
  415. if($order['refund_status'] == 2) return self::setErrorInfo('订单已退款!');
  416. if($order['refund_status'] == 1) return self::setErrorInfo('正在申请退款中!');
  417. if($order['status'] == 1) return self::setErrorInfo('订单当前无法退款!');
  418. self::beginTrans();
  419. $res1 = false !== StoreOrderStatus::status($order['id'],'apply_refund','用户申请退款,原因:'.$refundReasonWap);
  420. $res2 = false !== self::edit(['refund_status'=>1,'refund_reason_time'=>time(),'refund_reason_wap'=>$refundReasonWap,'refund_reason_wap_explain'=>$refundReasonWapExplain,'refund_reason_wap_img'=>json_encode($refundReasonWapImg)],$order['id'],'id');
  421. $res = $res1 && $res2;
  422. self::checkTrans($res);
  423. if(!$res)
  424. return self::setErrorInfo('申请退款失败!');
  425. else{
  426. $adminIds = SystemConfigService::get('site_store_admin_uids');
  427. if(!empty($adminIds)){
  428. try{
  429. if(!($adminList = array_unique(array_filter(explode(',',trim($adminIds)))))){
  430. self::setErrorInfo('申请退款成功,');
  431. return false;
  432. }
  433. RoutineTemplate::sendOrderRefundStatus($order,$refundReasonWap,$adminList);//小程序 发送模板消息
  434. }catch (\Exception $e){}
  435. }
  436. return true;
  437. }
  438. }
  439. /**
  440. * //TODO 支付成功后
  441. * @param $orderId
  442. * @param $paytype
  443. * @param $notify
  444. * @return bool
  445. */
  446. public static function paySuccess($orderId,$paytype='weixin',$formId = '')
  447. {
  448. $order = self::where('order_id',$orderId)->find();
  449. $resPink = true;
  450. $res1 = self::where('order_id',$orderId)->update(['paid'=>1,'pay_type'=>$paytype,'pay_time'=>time()]);//订单改为支付
  451. User::bcInc($order['uid'],'pay_count',1,'uid');
  452. if($order->combination_id && $res1 && !$order->refund_status) $resPink = StorePink::createPink($order);//创建拼团
  453. $oid = self::where('order_id',$orderId)->value('id');
  454. StoreOrderStatus::status($oid,'pay_success','用户付款成功');
  455. RoutineTemplate::sendOrderSuccess($formId,$orderId);
  456. HookService::afterListen('user_level',User::where('uid',$order['uid'])->find(),null,false,UserBehavior::class);
  457. //TODO 订单通知给管理员
  458. try{
  459. $pay_type = '微信支付';
  460. if($order['pay_type']=='weixin'){
  461. $pay_type = '微信支付';
  462. }else if($order['pay_type']=='yue'){
  463. $pay_type = '余额支付';
  464. }
  465. $nickname=User::where('uid',$order['uid'])->value('nickname');
  466. \app\core\util\WechatTemplateService::sendAdminNoticeTemplate([
  467. 'first'=>"亲,您有一个新订单 \n订单号:{$order['order_id']} \n支付金额:{$order['pay_price']} \n支付方式:{$pay_type} \n微信昵称:{$nickname}",
  468. 'keyword1'=>'新订单',
  469. 'keyword2'=>'已支付',
  470. 'keyword3'=>date('Y/m/d H:i',time()),
  471. 'remark'=>'用户备注:'.$order['mark'],
  472. ]);
  473. }catch(\Exception $e){}
  474. $res = $res1 && $resPink;
  475. return false !== $res;
  476. }
  477. /*
  478. * 线下支付消息通知
  479. * 待完善
  480. *
  481. * */
  482. public static function createOrderTemplate($order)
  483. {
  484. //$goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  485. // RoutineTemplateService::sendTemplate(WechatUser::getOpenId($order['uid']),RoutineTemplateService::ORDER_CREATE, [
  486. // 'first'=>'亲,您购买的商品已支付成功',
  487. // 'keyword1'=>date('Y/m/d H:i',$order['add_time']),
  488. // 'keyword2'=>implode(',',$goodsName),
  489. // 'keyword3'=>$order['order_id'],
  490. // 'remark'=>'点击查看订单详情'
  491. // ],Url::build('/wap/My/order',['uni'=>$order['order_id']],true,true));
  492. // RoutineTemplateService::sendAdminNoticeTemplate([
  493. // 'first'=>"亲,您有一个新订单 \n订单号:{$order['order_id']}",
  494. // 'keyword1'=>'新订单',
  495. // 'keyword2'=>'线下支付',
  496. // 'keyword3'=>date('Y/m/d H:i',time()),
  497. // 'remark'=>'请及时处理'
  498. // ]);
  499. }
  500. public static function getUserOrderDetail($uid,$key)
  501. {
  502. return self::where('order_id|unique',$key)->where('uid',$uid)->where('is_del',0)->find();
  503. }
  504. /**
  505. * TODO 订单发货
  506. * @param array $postageData 发货信息
  507. * @param string $oid orderID
  508. */
  509. public static function orderPostageAfter($postageData, $oid)
  510. {
  511. $order = self::where('id',$oid)->find();
  512. $url ='/pages/order_details/index?order_id='.$order['order_id'];
  513. $group = [
  514. 'first'=>'亲,您的订单已发货,请注意查收',
  515. 'remark'=>'点击查看订单详情'
  516. ];
  517. if($postageData['delivery_type'] == 'send'){//送货
  518. $goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  519. $group = array_merge($group,[
  520. 'keyword1'=>$goodsName,
  521. 'keyword2'=>$order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i',$order['pay_time']),
  522. 'keyword3'=>$order['user_address'],
  523. 'keyword4'=>$postageData['delivery_name'],
  524. 'keyword5'=>$postageData['delivery_id']
  525. ]);
  526. RoutineTemplate::sendOut('ORDER_DELIVER_SUCCESS',$order['uid'],$group,$url);
  527. }else if($postageData['delivery_type'] == 'express'){//发货
  528. $group = array_merge($group,[
  529. 'keyword1'=>$order['order_id'],
  530. 'keyword2'=>$postageData['delivery_name'],
  531. 'keyword3'=>$postageData['delivery_id']
  532. ]);
  533. RoutineTemplate::sendOut('ORDER_POSTAGE_SUCCESS',$order['uid'],$group,$url);
  534. }
  535. }
  536. /** 收货后发送模版消息
  537. * @param $order
  538. */
  539. public static function orderTakeAfter($order)
  540. {
  541. $title = '';
  542. $cartInfo = self::getDb('StoreOrderCartInfo')->where('oid', $order['id'])->column('cart_info');
  543. if(count($cartInfo)){
  544. foreach ($cartInfo as $key=>&$cart){
  545. $cart = json_decode($cart,true);
  546. $title .= $cart['productInfo']['store_name'].',';
  547. }
  548. }
  549. if(strlen(trim($title))) $title = substr($title,0,bcsub(strlen($title),1,0));
  550. else{
  551. $cartInfo = self::getDb('store_cart')->alias('a')->where('a.id','in',implode(',',json_decode($order['cart_id'],true)))->find();
  552. $title = StoreProduct::getProductField($cartInfo['product_id'],'store_name');
  553. }
  554. if($order['is_channel']){//小程序
  555. RoutineTemplate::sendOut('OREDER_TAKEVER',$order['uid'],[
  556. 'keyword1'=>$order['order_id'],
  557. 'keyword2'=>$title,
  558. 'keyword3'=>$order['pay_price'],
  559. 'keyword4'=>date('Y-m-d H:i:s',time()),
  560. ]);
  561. }else{
  562. $openid = WechatUser::where('uid',$order['uid'])->value('openid');
  563. \app\core\util\WechatTemplateService::sendTemplate($openid,\app\core\util\WechatTemplateService::ORDER_TAKE_SUCCESS,[
  564. 'first'=>'亲,您的订单已收货',
  565. 'keyword1'=>$order['order_id'],
  566. 'keyword2'=>'已收货',
  567. 'keyword3'=>date('Y-m-d H:i:s',time()),
  568. 'keyword4'=>$title,
  569. 'remark'=>'感谢您的光临!'
  570. ]);
  571. }
  572. }
  573. /**
  574. * 删除订单
  575. * @param $uni
  576. * @param $uid
  577. * @return bool
  578. */
  579. public static function removeOrder($uni, $uid)
  580. {
  581. $order = self::getUserOrderDetail($uid,$uni);
  582. if(!$order) return self::setErrorInfo('订单不存在!');
  583. $order = self::tidyOrder($order);
  584. if($order['_status']['_type'] != 0 && $order['_status']['_type']!= -2 && $order['_status']['_type'] != 4)
  585. return self::setErrorInfo('该订单无法删除!');
  586. if(false !== self::edit(['is_del'=>1],$order['id'],'id') && false !==StoreOrderStatus::status($order['id'],'remove_order','删除订单')) {
  587. //未支付和已退款的状态下才可以退积分退库存退优惠券
  588. if($order['_status']['_type']== 0 || $order['_status']['_type']== -2) {
  589. HookService::afterListen('store_order_regression_all',$order,null,false,OrderBehavior::class);
  590. }
  591. return true;
  592. }else
  593. return self::setErrorInfo('订单删除失败!');
  594. }
  595. /**
  596. * //TODO 用户确认收货
  597. * @param $uni
  598. * @param $uid
  599. */
  600. public static function takeOrder($uni, $uid)
  601. {
  602. $order = self::getUserOrderDetail($uid,$uni);
  603. if(!$order) return self::setErrorInfo('订单不存在!');
  604. $order = self::tidyOrder($order);
  605. if($order['_status']['_type'] != 2) return self::setErrorInfo('订单状态错误!');
  606. self::beginTrans();
  607. if(false !== self::edit(['status'=>2],$order['id'],'id') &&
  608. false !== StoreOrderStatus::status($order['id'],'user_take_delivery','用户已收货')){
  609. try{
  610. HookService::listen('store_product_order_user_take_delivery',$order,$uid,false,GoodsBehavior::class);
  611. }catch (\Exception $e){
  612. self::rollbackTrans();
  613. return self::setErrorInfo($e->getMessage());
  614. }
  615. self::commitTrans();
  616. return true;
  617. }else{
  618. self::rollbackTrans();
  619. return false;
  620. }
  621. }
  622. public static function tidyOrder($order,$detail = false,$isPic=false)
  623. {
  624. if($detail == true && isset($order['id'])){
  625. $cartInfo = self::getDb('StoreOrderCartInfo')->where('oid',$order['id'])->column('cart_info','unique')?:[];
  626. $info=[];
  627. foreach ($cartInfo as $k=>$cart){
  628. $cart=json_decode($cart, true);
  629. $cart['unique']=$k;
  630. //新增是否评价字段
  631. $cart['is_reply'] = self::getDb('store_product_reply')->where('unique',$k)->count();
  632. array_push($info,$cart);
  633. unset($cart);
  634. }
  635. $order['cartInfo'] = $info;
  636. }
  637. $status = [];
  638. if(!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2){
  639. $status['_type'] = 9;
  640. $status['_title'] = '线下付款';
  641. $status['_msg'] = '等待商家处理,请耐心等待';
  642. $status['_class'] = 'nobuy';
  643. }else if(!$order['paid']){
  644. $status['_type'] = 0;
  645. $status['_title'] = '未支付';
  646. //系统预设取消订单时间段
  647. $keyValue=['order_cancel_time','order_activity_time','order_bargain_time','order_seckill_time','order_pink_time'];
  648. //获取配置
  649. $systemValue=SystemConfigService::more($keyValue);
  650. //格式化数据
  651. $systemValue=self::setValeTime($keyValue,is_array($systemValue) ? $systemValue:[]);
  652. if($order['pink_id'] || $order['combination_id']){
  653. $order_pink_time=$systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  654. $time=bcadd($order['add_time'],$order_pink_time*3600,0);
  655. $status['_msg'] = '请在'.date('Y-m-d H:i:s',$time).'前完成支付!';
  656. }else if($order['seckill_id']){
  657. $order_seckill_time=$systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  658. $time=bcadd($order['add_time'],$order_seckill_time*3600,0);
  659. $status['_msg'] = '请在'.date('Y-m-d H:i:s',$time).'前完成支付!';
  660. }else if($order['bargain_id']){
  661. $order_bargain_time=$systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  662. $time=bcadd($order['add_time'],$order_bargain_time*3600,0);
  663. $status['_msg'] = '请在'.date('Y-m-d H:i:s',$time).'前完成支付!';
  664. }else{
  665. $time=bcadd($order['add_time'],$systemValue['order_cancel_time']*3600,0);
  666. $status['_msg'] = '请在'.date('Y-m-d H:i:s',$time).'前完成支付!';
  667. }
  668. $status['_class'] = 'nobuy';
  669. }else if($order['refund_status'] == 1){
  670. $status['_type'] = -1;
  671. $status['_title'] = '申请退款中';
  672. $status['_msg'] = '商家审核中,请耐心等待';
  673. $status['_class'] = 'state-sqtk';
  674. }else if($order['refund_status'] == 2){
  675. $status['_type'] = -2;
  676. $status['_title'] = '已退款';
  677. $status['_msg'] = '已为您退款,感谢您的支持';
  678. $status['_class'] = 'state-sqtk';
  679. }else if(!$order['status']){
  680. if($order['pink_id']){
  681. if(StorePink::where('id',$order['pink_id'])->where('status',1)->count()){
  682. $status['_type'] = 1;
  683. $status['_title'] = '拼团中';
  684. $status['_msg'] = '等待其他人参加拼团';
  685. $status['_class'] = 'state-nfh';
  686. }else{
  687. $status['_type'] = 1;
  688. $status['_title'] = '未发货';
  689. $status['_msg'] = '商家未发货,请耐心等待';
  690. $status['_class'] = 'state-nfh';
  691. }
  692. }else{
  693. $status['_type'] = 1;
  694. $status['_title'] = '未发货';
  695. $status['_msg'] = '商家未发货,请耐心等待';
  696. $status['_class'] = 'state-nfh';
  697. }
  698. }else if($order['status'] == 1){
  699. if($order['delivery_type'] == 'send'){//TODO 送货
  700. $status['_type'] = 2;
  701. $status['_title'] = '待收货';
  702. $status['_msg'] = date('m月d日H时i分',StoreOrderStatus::getTime($order['id'],'delivery')).'服务商已送货';
  703. $status['_class'] = 'state-ysh';
  704. }else if($order['delivery_type'] == 'express'){//TODO 发货
  705. $status['_type'] = 2;
  706. $status['_title'] = '待收货';
  707. $status['_msg'] = date('m月d日H时i分',StoreOrderStatus::getTime($order['id'],'delivery_goods')).'服务商已发货';
  708. $status['_class'] = 'state-ysh';
  709. }else if($order['delivery_type'] == 'fictitious'){
  710. $status['_type'] = 2;
  711. $status['_title'] = '虚拟发货';
  712. $status['_msg'] = date('m月d日H时i分',StoreOrderStatus::getTime($order['id'],'delivery_fictitious')).'服务商已发货';
  713. $status['_class'] = 'state-ysh';
  714. }
  715. }else if($order['status'] == 2){
  716. $status['_type'] = 3;
  717. $status['_title'] = '待评价';
  718. $status['_msg'] = '已收货,快去评价一下吧';
  719. $status['_class'] = 'state-ypj';
  720. }else if($order['status'] == 3){
  721. $status['_type'] = 4;
  722. $status['_title'] = '交易完成';
  723. $status['_msg'] = '交易完成,感谢您的支持';
  724. $status['_class'] = 'state-ytk';
  725. }
  726. if(isset($order['pay_type']))
  727. $status['_payType'] = isset(self::$payType[$order['pay_type']]) ? self::$payType[$order['pay_type']] : '其他方式';
  728. if(isset($order['delivery_type']))
  729. $status['_deliveryType'] = isset(self::$deliveryType[$order['delivery_type']]) ? self::$deliveryType[$order['delivery_type']] : '其他方式';
  730. $order['_status'] = $status;
  731. $order['_pay_time']=isset($order['pay_time']) && $order['pay_time'] != null ? date('Y-m-d H:i:s',$order['pay_time']) : date('Y-m-d H:i:s',$order['add_time']);
  732. $order['_add_time']=isset($order['add_time']) ? (strstr($order['add_time'],'-')===false ? date('Y-m-d H:i:s',$order['add_time']) : $order['add_time'] ): '';
  733. $order['status_pic']='';
  734. //获取产品状态图片
  735. if($isPic){
  736. $order_details_images=\app\core\util\GroupDataService::getData('order_details_images') ? : [];
  737. foreach ($order_details_images as $image){
  738. if(isset($image['order_status']) && $image['order_status']==$order['_status']['_type']){
  739. $order['status_pic']=$image['pic'];
  740. break;
  741. }
  742. }
  743. }
  744. return $order;
  745. }
  746. public static function statusByWhere($status,$uid=0,$model = null)
  747. {
  748. // $orderId = StorePink::where('uid',$uid)->where('status',1)->column('order_id','id');//获取正在拼团的订单编号
  749. if($model == null) $model = new self;
  750. if('' === $status)
  751. return $model;
  752. else if($status == 0)
  753. return $model->where('paid',0)->where('status',0)->where('refund_status',0);
  754. else if($status == 1)//待发货
  755. return $model->where('paid',1)->where('status',0)->where('refund_status',0);
  756. else if($status == 2)
  757. return $model->where('paid',1)->where('status',1)->where('refund_status',0);
  758. else if($status == 3)
  759. return $model->where('paid',1)->where('status',2)->where('refund_status',0);
  760. else if($status == 4)
  761. return $model->where('paid',1)->where('status',3)->where('refund_status',0);
  762. else if($status == -1)
  763. return $model->where('paid',1)->where('refund_status',1);
  764. else if($status == -2)
  765. return $model->where('paid',1)->where('refund_status',2);
  766. else if($status == -3)
  767. return $model->where('paid',1)->where('refund_status','IN','1,2');
  768. // else if($status == 11){
  769. // return $model->where('order_id','IN',implode(',',$orderId));
  770. // }
  771. else
  772. return $model;
  773. }
  774. public static function getUserOrderList($uid,$status = '',$page = 0,$limit = 8)
  775. {
  776. $list = self::statusByWhere($status,$uid)->where('is_del',0)->where('uid',$uid)
  777. ->field('add_time,seckill_id,bargain_id,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,pink_id,delivery_type,is_del')
  778. ->order('add_time DESC')->page((int)$page,(int)$limit)->select()->toArray();
  779. foreach ($list as $k=>$order){
  780. $list[$k] = self::tidyOrder($order,true);
  781. }
  782. return $list;
  783. }
  784. /**
  785. * 获取推广人地下用户的订单金额
  786. * @param string $uid
  787. * @param string $status
  788. * @return array
  789. */
  790. public static function getUserOrderCount($uid = '',$status = ''){
  791. $res = self::statusByWhere($status,$uid)->where('uid','IN',$uid)->column('pay_price');
  792. return $res;
  793. }
  794. public static function searchUserOrder($uid,$order_id)
  795. {
  796. $order = self::where('uid',$uid)->where('order_id',$order_id)->where('is_del',0)->field('seckill_id,bargain_id,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,delivery_type')
  797. ->order('add_time DESC')->find();
  798. if(!$order)
  799. return false;
  800. else
  801. return self::tidyOrder($order->toArray(),true);
  802. }
  803. public static function orderOver($oid)
  804. {
  805. $res = self::edit(['status'=>'3'],$oid,'id');
  806. if(!$res) exception('评价后置操作失败!');
  807. StoreOrderStatus::status($oid,'check_order_over','用户评价');
  808. }
  809. public static function checkOrderOver($oid)
  810. {
  811. $uniqueList = StoreOrderCartInfo::where('oid',$oid)->column('unique');
  812. if(StoreProductReply::where('unique','IN',$uniqueList)->where('oid',$oid)->count() == count($uniqueList)){
  813. HookService::listen('store_product_order_over',$oid,null,false,GoodsBehavior::class);
  814. self::orderOver($oid);
  815. }
  816. }
  817. public static function getOrderStatusNum($uid)
  818. {
  819. $noBuy = self::where('uid',$uid)->where('paid',0)->where('is_del',0)->where('pay_type','<>','offline')->count();
  820. $noPostageNoPink = self::where('o.uid',$uid)->alias('o')->where('o.paid',1)->where('o.pink_id',0)->where('o.is_del',0)->where('o.status',0)->where('o.pay_type','<>','offline')->count();
  821. $noPostageYesPink = self::where('o.uid',$uid)->alias('o')->join('StorePink p','o.pink_id = p.id')->where('p.status',2)->where('o.paid',1)->where('o.is_del',0)->where('o.status',0)->where('o.pay_type','<>','offline')->count();
  822. $noPostage = bcadd($noPostageNoPink,$noPostageYesPink);
  823. $noTake = self::where('uid',$uid)->where('paid',1)->where('is_del',0)->where('status',1)->where('pay_type','<>','offline')->count();
  824. $noReply = self::where('uid',$uid)->where('paid',1)->where('is_del',0)->where('status',2)->count();
  825. $noPink = self::where('o.uid',$uid)->alias('o')->join('StorePink p','o.pink_id = p.id')->where('p.status',1)->where('o.paid',1)->where('o.is_del',0)->where('o.status',0)->where('o.pay_type','<>','offline')->count();
  826. $noRefund = self::where('uid',$uid)->where('paid',1)->where('is_del',0)->where('refund_status','IN','1,2')->count();
  827. return compact('noBuy','noPostage','noTake','noReply','noPink','noRefund');
  828. }
  829. public static function gainUserIntegral($order)
  830. {
  831. if($order['gain_integral'] > 0){
  832. $userInfo = User::getUserInfo($order['uid']);
  833. ModelBasic::beginTrans();
  834. $res1 = false != User::where('uid',$userInfo['uid'])->update(['integral'=>bcadd($userInfo['integral'],$order['gain_integral'],2)]);
  835. $res2 = false != UserBill::income('购买商品赠送积分',$order['uid'],'integral','gain',$order['gain_integral'],$order['id'],$userInfo['integral'],'购买商品赠送'.floatval($order['gain_integral']).'积分');
  836. $res = $res1 && $res2;
  837. ModelBasic::checkTrans($res);
  838. return $res;
  839. }
  840. return true;
  841. }
  842. /**
  843. * 获取当前订单中有没有拼团存在
  844. * @param $pid
  845. * @return int|string
  846. */
  847. public static function getIsOrderPink($pid = 0 ,$uid = 0){
  848. return self::where('uid',$uid)->where('pink_id',$pid)->where('refund_status',0)->where('is_del',0)->count();
  849. }
  850. /**
  851. * 获取order_id
  852. * @param $pid
  853. * @return mixed
  854. */
  855. public static function getStoreIdPink($pid = 0 ,$uid = 0){
  856. return self::where('uid',$uid)->where('pink_id',$pid)->where('is_del',0)->value('order_id');
  857. }
  858. /**
  859. * 删除当前用户拼团未支付的订单
  860. */
  861. public static function delCombination(){
  862. self::where('combination','GT',0)->where('paid',0)->where('uid',User::getActiveUid())->delete();
  863. }
  864. public static function getUserPrice($uid =0){
  865. if(!$uid) return 0;
  866. $price = self::where('paid',1)->where('uid',$uid)->where('status',2)->where('refund_status',0)->column('pay_price','id');
  867. $count = 0;
  868. if($price){
  869. foreach ($price as $v){
  870. $count = bcadd($count,$v,2);
  871. }
  872. }
  873. return $count;
  874. }
  875. /*
  876. * 个人中心获取个人订单列表和订单搜索
  877. * @param int $uid 用户uid
  878. * @param int | string 查找订单类型
  879. * @param int $first 分页
  880. * @param int 每页显示多少条
  881. * @param string $search 订单号
  882. * @return array
  883. * */
  884. public static function getUserOrderSearchList($uid,$type,$page,$limit,$search)
  885. {
  886. if($search){
  887. $order = self::searchUserOrder($uid,$search)?:[];
  888. $list = $order == false ? [] : [$order];
  889. }else{
  890. $list = self::getUserOrderList($uid,$type,$page,$limit);
  891. }
  892. foreach ($list as $k=>$order){
  893. $list[$k] = self::tidyOrder($order,true);
  894. if($list[$k]['_status']['_type'] == 3){
  895. foreach ($order['cartInfo']?:[] as $key=>$product){
  896. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  897. $list[$k]['cartInfo'][$key]['add_time'] = date('Y-m-d H:i',$product['add_time']);
  898. }
  899. }
  900. }
  901. return $list;
  902. }
  903. /*
  904. * 获取用户下级的订单
  905. * @param int $xuid 下级用户用户uid
  906. * @param int $uid 用户uid
  907. * @param int $type 订单类型
  908. * @param int $first 截取行数
  909. * @param int $limit 展示条数
  910. * @return array
  911. * */
  912. public static function getSubordinateOrderlist($xUid,$uid,$type,$first,$limit)
  913. {
  914. $list = [];
  915. if(!$xUid){
  916. $arr = User::getOneSpreadUid($uid);
  917. foreach($arr as $v) $list = StoreOrder::getUserOrderList($v,$type,$first,$limit);
  918. }else $list = self::getUserOrderList($xUid,$type,$first,$limit);
  919. foreach ($list as $k=>$order){
  920. $list[$k] = self::tidyOrder($order,true);
  921. if($list[$k]['_status']['_type'] == 3){
  922. foreach ($order['cartInfo']?:[] as $key=>$product){
  923. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'],'product');
  924. }
  925. }
  926. }
  927. return $list;
  928. }
  929. /*
  930. * 获取某个用户的订单统计数据
  931. * @param int $uid 用户uid
  932. * */
  933. public static function getOrderData($uid)
  934. {
  935. $data['order_count']=self::where(['is_del'=>0,'paid'=>1,'uid'=>$uid,'refund_status'=>0])->count();
  936. $data['sum_price']=self::where(['is_del'=>0,'paid'=>1,'uid'=>$uid,'refund_status'=>0])->sum('pay_price');
  937. $data['unpaid_count']=self::statusByWhere(0,$uid)->where('is_del',0)->where('uid',$uid)->count();
  938. $data['unshipped_count']=self::statusByWhere(1,$uid)->where('is_del',0)->where('uid',$uid)->count();
  939. $data['received_count']=self::statusByWhere(2,$uid)->where('is_del',0)->where('uid',$uid)->count();
  940. $data['evaluated_count']=self::statusByWhere(3,$uid)->where('is_del',0)->where('uid',$uid)->count();
  941. $data['complete_count']=self::statusByWhere(4,$uid)->where('is_del',0)->where('uid',$uid)->count();
  942. return $data;
  943. }
  944. /*
  945. * 累计消费
  946. * **/
  947. public static function getOrderStatusSum($uid)
  948. {
  949. return self::where(['uid'=>$uid,'is_del'=>0,'paid'=>1])->sum('pay_price');
  950. }
  951. /**
  952. * 获取余额支付的金额
  953. * @param $uid
  954. * @return float|int
  955. */
  956. public static function getOrderStatusYueSum($uid)
  957. {
  958. return self::where(['uid'=>$uid,'is_del'=>0,'pay_type'=>'yue','paid'=>1])->sum('pay_price');
  959. }
  960. public static function getPinkOrderId($id){
  961. return self::where('id',$id)->value('order_id');
  962. }
  963. /*
  964. * 未支付订单自动取消
  965. * @param int $limit 分页截取条数
  966. * @param $prefid 缓存名称
  967. * @param $expire 缓存时间
  968. * */
  969. public static function orderUnpaidCancel($limit=10,$prefid=ApiLogs::ORDER_UNPAID_PAGE,$expire=3600)
  970. {
  971. //系统预设取消订单时间段
  972. $keyValue=['order_cancel_time','order_activity_time','order_bargain_time','order_seckill_time','order_pink_time'];
  973. //未支付查询条件
  974. $UnPaidwhere=['paid'=>0,'is_del'=>0,'status'=>0,'refund_status'=>0];
  975. //获取配置
  976. $systemValue=SystemConfigService::more($keyValue);
  977. //格式化数据
  978. $systemValue=self::setValeTime($keyValue,is_array($systemValue) ? $systemValue:[]);
  979. //检查是否有未支付的订单
  980. $unPidCount=self::where($UnPaidwhere)->count();
  981. if(!$unPidCount) return null;
  982. //总分页条数
  983. $pagesSum=ceil(bcdiv($unPidCount,$limit,2));
  984. if(Cache::has($prefid)){
  985. $pages=Cache::get($prefid);
  986. $pages++;
  987. Cache::set($prefid,$pages,$expire);
  988. }else{
  989. $pages=1;
  990. Cache::set($prefid,$pages,$expire);
  991. }
  992. if($pages > $pagesSum) Cache::set($prefid,0,$expire);
  993. self::beginTrans();
  994. try{
  995. $res=true;
  996. $orderList = self::where($UnPaidwhere)->field(['add_time','pink_id','order_id','seckill_id','bargain_id','combination_id','status','cart_id','use_integral',
  997. 'refund_status','uid','unique','back_integral','coupon_id','paid','is_del'])->page($pages,$limit)->select();
  998. foreach ($orderList as $order){
  999. if($order['seckill_id']){
  1000. //优先使用单独配置的过期时间
  1001. $order_seckill_time=$systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1002. $res=$res && self::RegressionAll($order_seckill_time,$order);
  1003. }else if($order['bargain_id']){
  1004. $order_bargain_time=$systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1005. $res=$res && self::RegressionAll($order_bargain_time,$order);
  1006. }else if($order['pink_id'] || $order['combination_id']){
  1007. $order_pink_time=$systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1008. $res=$res && self::RegressionAll($order_pink_time,$order);
  1009. }else{
  1010. $res=$res && self::RegressionAll($systemValue['order_cancel_time'],$order);
  1011. }
  1012. }
  1013. if($res) self::commitTrans();
  1014. }catch (PDOException $e){
  1015. self::rollbackTrans();
  1016. ApiLogs::writeLog(['file'=>$e->getFile(),'line'=>$e->getLine(),'message'=>$e->getMessage()],'s');
  1017. }catch (\think\Exception $e){
  1018. self::rollbackTrans();
  1019. ApiLogs::recodeErrorLog($e);
  1020. }
  1021. }
  1022. /*
  1023. * 未支付订单超过预设时间回退所有,如果不设置未支付过期时间,将不取消订单
  1024. * @param int $time 预设时间
  1025. * @param array $order 订单详情
  1026. * @return boolean
  1027. * */
  1028. protected static function RegressionAll($time,$order)
  1029. {
  1030. if($time==0) return true;
  1031. if(($order['add_time']+bcmul($time,3600,0)) < time()){
  1032. $res1=self::RegressionStock($order);
  1033. $res2=self::RegressionIntegral($order);
  1034. $res3=self::RegressionCoupon($order);
  1035. $res = $res1 && $res2 && $res3;
  1036. if($res) $res = false !== self::where(['order_id'=>$order['order_id']])->update(['is_del'=>1,'mark'=>'订单未支付已超过系统预设时间']);
  1037. return $res;
  1038. }else
  1039. return true;
  1040. }
  1041. /*
  1042. * 格式化数据
  1043. * @param $array 原本数据键
  1044. * @param $array 需要格式化的数据
  1045. * @param int $default 默认值
  1046. * @return array
  1047. * */
  1048. protected static function setValeTime(array $array,$value,$default=0)
  1049. {
  1050. foreach ($array as $item) {
  1051. if(!isset($value[$item]))
  1052. $value[$item]=$default;
  1053. else if(is_string($value[$item]))
  1054. $value[$item]=(float)$value[$item];
  1055. }
  1056. return $value;
  1057. }
  1058. }