StoreOrder.php 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue;
  9. use app\admin\model\system\ShippingTemplatesFree;
  10. use app\admin\model\system\ShippingTemplatesRegion;
  11. use app\models\system\SystemAwardLevel;
  12. use app\models\system\SystemGroupLevel;
  13. use app\models\system\SystemStoreMember;
  14. use app\models\system\SystemStoreStock;
  15. use app\models\system\SystemUserLevel;
  16. use crmeb\basic\BaseModel;
  17. use think\Exception;
  18. use think\facade\Cache;
  19. use crmeb\traits\ModelTrait;
  20. use think\facade\Log;
  21. use app\models\system\SystemStore;
  22. use app\models\routine\RoutineTemplate;
  23. use app\models\user\{User, UserAddress, UserBill, UserCode, UserLevel, WechatUser};
  24. use crmeb\services\{JiaLieService, SystemConfigService, UtilService, WechatTemplateService, workerman\ChannelService};
  25. use crmeb\repositories\{
  26. GoodsRepository, PaymentRepositories, OrderRepository, ShortLetterRepositories, UserRepository
  27. };
  28. use app\admin\model\system\ShippingTemplates;
  29. /**
  30. * TODO 订单Model
  31. * Class StoreOrder
  32. * @package app\models\store
  33. */
  34. class StoreOrder extends BaseModel
  35. {
  36. /**
  37. * 数据表主键
  38. * @var string
  39. */
  40. protected $pk = 'id';
  41. /**
  42. * 模型名称
  43. * @var string
  44. */
  45. protected $name = 'store_order';
  46. use ModelTrait;
  47. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'consumer' => '消费券支付', 'jialie' => '嘉联支付'];
  48. protected static $deliveryType = ['send' => '商家配送', 'express' => '快递配送'];
  49. protected function setCartIdAttr($value)
  50. {
  51. return is_array($value) ? json_encode($value) : $value;
  52. }
  53. protected function getCartIdAttr($value)
  54. {
  55. return json_decode($value, true);
  56. }
  57. /**获取订单组信息
  58. * @param $cartInfo
  59. * @return array
  60. */
  61. public static function getOrderPriceGroup($cartInfo, $addr)
  62. {
  63. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮
  64. $totalPrice = self::getOrderSumPrice($cartInfo, 'truePrice');//获取订单总金额
  65. $costPrice = self::getOrderSumPrice($cartInfo, 'costPrice');//获取订单成本价
  66. $vipPrice = self::getOrderSumPrice($cartInfo, 'vip_truePrice');//获取订单会员优惠金额
  67. $use_integral = self::getOrderSumPrice($cartInfo, 'use_integral');//获取订单会员优惠金额
  68. //如果满额包邮等于0
  69. if (!$storeFreePostage) {
  70. $storePostage = 0;
  71. } else {
  72. if ($addr) {
  73. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  74. $temp_num = [];
  75. foreach ($cartInfo as $cart) {
  76. $temp = ShippingTemplates::get($cart['productInfo']['temp_id']);
  77. if (!$temp) $temp = ShippingTemplates::get(1);
  78. if ($temp->getData('type') == 1) {
  79. $num = $cart['cart_num'];
  80. } elseif ($temp->getData('type') == 2) {
  81. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  82. } else {
  83. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  84. }
  85. $region = ShippingTemplatesRegion::where('temp_id', $cart['productInfo']['temp_id'])->where('city_id', $addr['city_id'])->find();
  86. if (!$region) $region = ShippingTemplatesRegion::where('temp_id', $cart['productInfo']['temp_id'])->where('city_id', 0)->find();
  87. if (!$region) $region = ShippingTemplatesRegion::where('temp_id', 1)->where('city_id', 0)->find();
  88. if (!$region) {
  89. return self::setErrorInfo('运费模板不存在');
  90. }
  91. if (!isset($temp_num[$cart['productInfo']['temp_id']])) {
  92. $temp_num[$cart['productInfo']['temp_id']]['number'] = $num;
  93. $temp_num[$cart['productInfo']['temp_id']]['price'] = bcmul($cart['cart_num'], $cart['truePrice'], 2);
  94. $temp_num[$cart['productInfo']['temp_id']]['first'] = $region['first'];
  95. $temp_num[$cart['productInfo']['temp_id']]['first_price'] = $region['first_price'];
  96. $temp_num[$cart['productInfo']['temp_id']]['continue'] = $region['continue'];
  97. $temp_num[$cart['productInfo']['temp_id']]['continue_price'] = $region['continue_price'];
  98. $temp_num[$cart['productInfo']['temp_id']]['temp_id'] = $cart['productInfo']['temp_id'];
  99. $temp_num[$cart['productInfo']['temp_id']]['city_id'] = $addr['city_id'];
  100. } else {
  101. $temp_num[$cart['productInfo']['temp_id']]['number'] += $num;
  102. $temp_num[$cart['productInfo']['temp_id']]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  103. }
  104. }
  105. array_multisort(array_column($temp_num, 'first_price'), SORT_DESC, $temp_num);
  106. $type = $storePostage = 0;
  107. foreach ($temp_num as $k => $v) {
  108. if (ShippingTemplatesFree::where('temp_id', $v['temp_id'])->where('city_id', $v['city_id'])->where('number', '<=', $v['number'])->where('price', '<=', $v['price'])->find()) {
  109. unset($temp_num[$k]);
  110. }
  111. }
  112. foreach ($temp_num as $v) {
  113. if ($type == 0) {
  114. if ($v['number'] <= (float)$v['first']) {
  115. $storePostage = bcadd($storePostage, $v['first_price'], 2);
  116. } else {
  117. if ($v['continue'] <= 0) {
  118. $storePostage = $storePostage;
  119. } else {
  120. $storePostage = bcadd(bcadd($storePostage, $v['first_price'], 2), bcmul(ceil(bcdiv(bcsub($v['number'], $v['first'], 2), $v['continue'] ?? 0, 2)), $v['continue_price']), 2);
  121. }
  122. }
  123. $type = 1;
  124. } else {
  125. if ($v['continue'] <= 0) {
  126. $storePostage = $storePostage;
  127. } else {
  128. $storePostage = bcadd($storePostage, bcmul(ceil(bcdiv($v['number'], $v['continue'] ?? 0, 2)), $v['continue_price']), 2);
  129. }
  130. }
  131. }
  132. } else {
  133. $storePostage = 0;
  134. }
  135. if ($storeFreePostage <= $totalPrice) $storePostage = 0;//如果总价大于等于满额包邮 邮费等于0
  136. }
  137. return compact('use_integral', 'storePostage', 'storeFreePostage', 'totalPrice', 'costPrice', 'vipPrice');
  138. }
  139. /**获取某个字段总金额
  140. * @param $cartInfo
  141. * @param $key 键名
  142. * @return int|string
  143. */
  144. public static function getOrderSumPrice($cartInfo, $key = 'truePrice')
  145. {
  146. $SumPrice = 0;
  147. foreach ($cartInfo as $cart) {
  148. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'], $cart[$key], 2), 2);
  149. }
  150. return $SumPrice;
  151. }
  152. /**
  153. * 拼团
  154. * @param $cartInfo
  155. * @return array
  156. */
  157. public static function getCombinationOrderPriceGroup($cartInfo)
  158. {
  159. $storePostage = floatval(sys_config('store_postage')) ?: 0;
  160. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;
  161. $totalPrice = self::getCombinationOrderTotalPrice($cartInfo);
  162. $costPrice = self::getCombinationOrderTotalPrice($cartInfo);
  163. if (!$storeFreePostage) {
  164. $storePostage = 0;
  165. } else {
  166. foreach ($cartInfo as $cart) {
  167. if (!StoreCombination::where('id', $cart['combination_id'])->value('is_postage'))
  168. $storePostage = bcadd($storePostage, StoreCombination::where('id', $cart['combination_id'])->value('postage'), 2);
  169. }
  170. if ($storeFreePostage <= $totalPrice) $storePostage = 0;
  171. }
  172. return compact('storePostage', 'storeFreePostage', 'totalPrice', 'costPrice');
  173. }
  174. /**
  175. * 拼团价格
  176. * @param $cartInfo
  177. * @return float
  178. */
  179. public static function getCombinationOrderTotalPrice($cartInfo)
  180. {
  181. $totalPrice = 0;
  182. foreach ($cartInfo as $cart) {
  183. if ($cart['combination_id']) {
  184. $totalPrice = bcadd($totalPrice, bcmul($cart['cart_num'], StoreCombination::where('id', $cart['combination_id'])->value('price'), 2), 2);
  185. }
  186. }
  187. return (float)$totalPrice;
  188. }
  189. /**
  190. * 缓存订单信息
  191. * @param $uid
  192. * @param $cartInfo
  193. * @param $priceGroup
  194. * @param array $other
  195. * @param int $cacheTime
  196. * @return string
  197. * @throws \Psr\SimpleCache\InvalidArgumentException
  198. */
  199. public static function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  200. {
  201. $key = md5(time());
  202. Cache::set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  203. return $key;
  204. }
  205. /**
  206. * 获取订单缓存信息
  207. * @param $uid
  208. * @param $key
  209. * @return mixed|null
  210. * @throws \Psr\SimpleCache\InvalidArgumentException
  211. */
  212. public static function getCacheOrderInfo($uid, $key)
  213. {
  214. $cacheName = 'user_order_' . $uid . $key;
  215. if (!Cache::has($cacheName)) return null;
  216. return Cache::get($cacheName);
  217. }
  218. /**
  219. * 删除订单缓存
  220. * @param $uid
  221. * @param $key
  222. */
  223. public static function clearCacheOrderInfo($uid, $key)
  224. {
  225. Cache::delete('user_order_' . $uid . $key);
  226. }
  227. /**
  228. * 生成订单
  229. * @param $uid
  230. * @param $key
  231. * @param $addressId
  232. * @param $payType
  233. * @param bool $useIntegral
  234. * @param int $couponId
  235. * @param string $mark
  236. * @param int $combinationId
  237. * @param int $pinkId
  238. * @param int $seckill_id
  239. * @param int $bargain_id
  240. * @param bool $test
  241. * @param int $isChannel
  242. * @param int $shipping_type
  243. * @param string $real_name
  244. * @param string $phone
  245. * @return StoreOrder|bool|\think\Model
  246. * @throws \think\Exception
  247. * @throws \think\db\exception\DataNotFoundException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. * @throws \think\exception\DbException
  250. */
  251. public static function cacheKeyCreateOrder($uid, $key, $addressId, $payType, $useIntegral = false, $couponId = 0, $mark = '', $combinationId = 0, $pinkId = 0, $seckill_id = 0, $bargain_id = 0, $test = false, $isChannel = 0, $shipping_type = 1, $real_name = '', $phone = '', $storeId = 0, $is_consumer = 0)
  252. {
  253. self::beginTrans();
  254. try {
  255. $shipping_type = (int)$shipping_type;
  256. $offlinePayStatus = (int)sys_config('offline_pay_status') ?? (int)2;
  257. if ($offlinePayStatus == 2) unset(self::$payType['offline']);
  258. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true);
  259. if (self::be(['unique' => $key, 'uid' => $uid])) return self::setErrorInfo('请勿重复提交订单', true);
  260. $userInfo = User::getUserInfo($uid);
  261. if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
  262. $cartGroup = self::getCacheOrderInfo($uid, $key);
  263. if (!$cartGroup) return self::setErrorInfo('订单已过期,请刷新当前页面!', true);
  264. $cartInfo = $cartGroup['cartInfo'];
  265. $priceGroup = $cartGroup['priceGroup'];
  266. $other = $cartGroup['other'];
  267. $payPrice = (float)$priceGroup['totalPrice'];
  268. $useIntegrals = (float)$priceGroup['use_integral'];
  269. $addr = UserAddress::where('uid', $uid)->where('id', $addressId)->find();
  270. if ($payType == 'offline' && sys_config('offline_postage') == 1) {
  271. $payPostage = 0;
  272. } else {
  273. $payPostage = self::getOrderPriceGroup($cartInfo, $addr)['storePostage'];
  274. }
  275. if ($shipping_type === 1) {
  276. if (!$test && !$addressId) return self::setErrorInfo('请选择收货地址!', true);
  277. if (!$test && (!UserAddress::be(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]) || !($addressInfo = UserAddress::find($addressId))))
  278. return self::setErrorInfo('地址选择有误!', true);
  279. $addressInfo = UserAddress::find($addressId);
  280. if (!$test && !($other['suit'] ?? 0)) {
  281. if (empty($addressInfo['longitude']) || empty($addressInfo['latitude'])) {
  282. return self::setErrorInfo('收货地址经纬度不能为空!', true);
  283. }
  284. $rs = SystemStore::lst($addressInfo['latitude'], $addressInfo['longitude'], 1, 1);
  285. if (!$rs) {
  286. return self::setErrorInfo('系统没设置门店!', true);
  287. }
  288. if ($rs[0]['distance'] > sys_config('store_distance')) {
  289. return self::setErrorInfo('该区域没有门店,请到店自提!', true);
  290. }
  291. $storeId = $rs[0]['id'];
  292. }
  293. } else {
  294. if ((!$real_name || !$phone) && !$test) return self::setErrorInfo('请填写姓名和电话', true);
  295. $addressInfo['real_name'] = $real_name;
  296. $addressInfo['phone'] = $phone;
  297. $addressInfo['province'] = '';
  298. $addressInfo['city'] = '';
  299. $addressInfo['district'] = '';
  300. $addressInfo['detail'] = '';
  301. }
  302. $cartIds = [];
  303. $totalNum = 0;
  304. $gainIntegral = 0;
  305. $maxUseIntegral = 0;
  306. foreach ($cartInfo as $cart) {
  307. if (!$test && !self::checkProductStock($uid, $cart['product_id'], $cart['cart_num'], $cart['product_attr_unique'], $cart['combination_id'], $cart['seckill_id'], $cart['bargain_id'])) {
  308. return false;
  309. }
  310. $cartIds[] = $cart['id'];
  311. $totalNum += $cart['cart_num'];
  312. if (!$seckill_id) $seckill_id = $cart['seckill_id'];
  313. if (!$bargain_id) $bargain_id = $cart['bargain_id'];
  314. if (!$combinationId) $combinationId = $cart['combination_id'];
  315. $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul($cart['cart_num'], $cart['productInfo']['give_integral'], 2) : 0;
  316. $cartInfoMaxUseIntegral = isset($cart['productInfo']['max_use_integral']) ? bcmul($cart['cart_num'], $cart['productInfo']['max_use_integral'], 2) : 0;
  317. $gainIntegral = bcadd($gainIntegral, $cartInfoGainIntegral, 2);
  318. $maxUseIntegral = bcadd($maxUseIntegral, $cartInfoMaxUseIntegral, 2);
  319. }
  320. $deduction = $seckill_id || $bargain_id || $combinationId;
  321. if ($deduction) {
  322. $couponId = 0;
  323. $useIntegral = false;
  324. if (!$test) {
  325. unset(self::$payType['offline']);
  326. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('营销产品不能使用线下支付!', true);
  327. }
  328. }
  329. //使用优惠劵
  330. $res1 = true;
  331. if ($couponId) {
  332. $couponInfo = StoreCouponUser::validAddressWhere()->where('id', $couponId)->where('uid', $uid)->find();
  333. if (!$couponInfo) return self::setErrorInfo('选择的优惠劵无效!', true);
  334. $coupons = StoreCouponUser::getUsableCouponList($uid, ['valid' => $cartInfo], $payPrice);
  335. $flag = false;
  336. foreach ($coupons as $coupon) {
  337. if ($coupon['id'] == $couponId) {
  338. $flag = true;
  339. continue;
  340. }
  341. }
  342. if (!$flag)
  343. return self::setErrorInfo('不满足优惠劵的使用条件!', true);
  344. $payPrice = (float)bcsub($payPrice, $couponInfo['coupon_price'], 2);
  345. $res1 = StoreCouponUser::useCoupon($couponId);
  346. $couponPrice = $couponInfo['coupon_price'];
  347. } else {
  348. $couponId = 0;
  349. $couponPrice = 0;
  350. }
  351. if (!$res1) return self::setErrorInfo('使用优惠劵失败!', true);
  352. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  353. $store_self_mention = sys_config('store_self_mention') ?? 0;
  354. if (!$store_self_mention) $shipping_type = 1;
  355. if ($shipping_type === 1) {
  356. //是否包邮
  357. if ((isset($other['offlinePostage']) && $other['offlinePostage'] && $payType == 'offline')) $payPostage = 0;
  358. $payPrice = (float)bcadd($payPrice, $payPostage, 2);
  359. } else if ($shipping_type === 2) {
  360. //门店自提没有邮费支付
  361. $priceGroup['storePostage'] = 0;
  362. $payPostage = 0;
  363. if (!$storeId && !$test) {
  364. return self::setErrorInfo('请选择门店', true);
  365. }
  366. }
  367. // //积分抵扣
  368. // $res2 = true;
  369. // $SurplusIntegral = 0;
  370. // if ($useIntegral && $userInfo['integral'] > 0) {
  371. // if ($userInfo['integral'] > $maxUseIntegral) {
  372. // $useIntegral = $maxUseIntegral;
  373. // } else {
  374. // $useIntegral = $userInfo['integral'];
  375. // }
  376. // if ($useIntegral > 0) {
  377. // $deductionPrice = (float)bcmul($useIntegral, $other['integralRatio'], 2);
  378. // if ($deductionPrice < $payPrice) {
  379. // $payPrice = bcsub($payPrice, $deductionPrice, 2);
  380. // $usedIntegral = $useIntegral;
  381. // $SurplusIntegral = 0;
  382. // $res2 = false !== User::edit(['integral' => bcsub($userInfo['integral'], $usedIntegral, 2)], $userInfo['uid'], 'uid');
  383. // } else {
  384. // $deductionPrice = $payPrice;
  385. // $usedIntegral = (float)bcdiv($payPrice, $other['integralRatio'], 2);
  386. // $SurplusIntegral = bcsub($userInfo['integral'], $usedIntegral, 2);
  387. // $res2 = false !== User::bcDec($userInfo['uid'], 'integral', $usedIntegral, 'uid');
  388. // $payPrice = 0;
  389. // }
  390. // $res2 = $res2 && false != UserBill::expend('积分抵扣', $uid, 'integral', 'deduction', $usedIntegral, $key, $userInfo['integral'], '购买商品使用' . floatval($usedIntegral) . '积分抵扣' . floatval($deductionPrice) . '元');
  391. // } else {
  392. // $deductionPrice = 0;
  393. // $usedIntegral = 0;
  394. // }
  395. // } else {
  396. // $deductionPrice = 0;
  397. // $usedIntegral = 0;
  398. // }
  399. // if (!$res2) return self::setErrorInfo('使用积分抵扣失败!', true);
  400. //积分抵扣
  401. $deductionPrice = 0;
  402. $SurplusIntegral = 0;
  403. if ($useIntegrals > 0) {
  404. if ($userInfo['integral'] >= $useIntegrals) {
  405. $SurplusIntegral = bcsub($userInfo['integral'], $useIntegrals, 2);
  406. $res2 = false !== User::bcDec($userInfo['uid'], 'integral', $useIntegrals, 'uid');
  407. $res2 = $res2 && false != UserBill::expend('积分商品', $uid, 'integral', 'deduction', $useIntegrals, $key, $userInfo['integral'], '购买积分商品使用' . floatval($useIntegrals));
  408. } else {
  409. return self::setErrorInfo('积分不足' . $useIntegrals . '!', true);
  410. }
  411. if (!$res2) return self::setErrorInfo('使用积分抵扣失败!', true);
  412. }
  413. if ($payPrice <= 0) $payPrice = 0;
  414. if ($test) {
  415. self::rollbackTrans();
  416. return [
  417. 'total_price' => $priceGroup['totalPrice'],
  418. 'pay_price' => $payPrice,
  419. 'pay_postage' => $payPostage,
  420. 'coupon_price' => $couponPrice,
  421. 'deduction_price' => $deductionPrice,
  422. 'SurplusIntegral' => $SurplusIntegral,
  423. 'useIntegral' => $useIntegrals,
  424. ];
  425. }
  426. $id = self::getkeytoid('order_id');
  427. $orderInfo = [
  428. 'id' => $id,
  429. 'uid' => $uid,
  430. 'order_id' => $test ? 0 : self::getNewOrderId(),
  431. 'real_name' => $addressInfo['real_name'],
  432. 'user_phone' => $addressInfo['phone'],
  433. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  434. 'cart_id' => $cartIds,
  435. 'total_num' => $totalNum,
  436. 'total_price' => $priceGroup['totalPrice'],
  437. 'total_postage' => $priceGroup['storePostage'],
  438. 'coupon_id' => $couponId,
  439. 'coupon_price' => $couponPrice,
  440. 'pay_price' => $payPrice,
  441. 'pay_postage' => $payPostage,
  442. 'deduction_price' => $deductionPrice,
  443. 'paid' => 0,
  444. 'pay_type' => $payType,
  445. 'use_integral' => $useIntegrals,
  446. 'gain_integral' => $gainIntegral,
  447. 'mark' => htmlspecialchars($mark),
  448. 'combination_id' => $combinationId,
  449. 'pink_id' => $pinkId,
  450. 'seckill_id' => $seckill_id,
  451. 'bargain_id' => $bargain_id,
  452. 'cost' => $priceGroup['costPrice'],
  453. 'is_channel' => $isChannel,
  454. 'add_time' => date("Y-m-d H:i:s"),
  455. 'unique' => $key,
  456. 'shipping_type' => $shipping_type,
  457. 'is_consumer' => $is_consumer,
  458. 'reg_store_id' => $userInfo['reg_store_id'],
  459. 'suit' => $other['suit'] ?? 0,
  460. 'is_bind' => $other['is_bind'] ?? 0,
  461. 'store_bag' => $other['store_bag'] ?? 0,
  462. ];
  463. if ($shipping_type === 2) {
  464. $orderInfo['verify_code'] = self::getStoreCode();
  465. $orderInfo['store_id'] = SystemStore::getStoreDispose($storeId, 'id');
  466. if (!$orderInfo['store_id']) return self::setErrorInfo('暂无门店无法选择门店自提!', true);
  467. } else {
  468. $orderInfo['store_id'] = $storeId;
  469. }
  470. $create_store_id = intval(SystemStoreMember::where('uid', $uid)->value('reg_store_id'));
  471. if ($create_store_id == 0 && $orderInfo['store_id'] > 0) {
  472. $orderInfo['create_store_id'] = $orderInfo['store_id'];
  473. }
  474. $order = self::create($orderInfo);
  475. if (!$order) return self::setErrorInfo('订单生成失败-0!' . self::getlastsql(), true);
  476. $res5 = true;
  477. foreach ($cartInfo as $cart) {
  478. //减库存加销量
  479. if ($combinationId) $res5 = $res5 && StoreCombination::decCombinationStock($cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  480. else if ($seckill_id) $res5 = $res5 && StoreSeckill::decSeckillStock($cart['cart_num'], $seckill_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  481. else if ($bargain_id) $res5 = $res5 && StoreBargain::decBargainStock($cart['cart_num'], $bargain_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  482. else $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'], $cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  483. }
  484. //保存购物车商品信息
  485. $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'], $cartInfo);
  486. //购物车状态修改
  487. $res6 = false !== StoreCart::where('id', 'IN', $cartIds)->update(['is_pay' => 1]);
  488. if (!$res4 || !$res5 || !$res6) return self::setErrorInfo('订单生成失败-1!' . $res5 . '-' . $res4 . '-' . $res6, true);
  489. //自动设置默认地址
  490. UserRepository::storeProductOrderCreateEbApi($order, compact('cartInfo', 'addressId'));
  491. self::clearCacheOrderInfo($uid, $key);
  492. self::commitTrans();
  493. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  494. return $order;
  495. } catch (\PDOException $e) {
  496. self::rollbackTrans();
  497. return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  498. } catch (\Exception $e) {
  499. self::rollbackTrans();
  500. return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  501. }
  502. }
  503. /**
  504. * 回退积分
  505. * @param $order 订单信息
  506. * @return bool
  507. */
  508. public static function RegressionIntegral($order)
  509. {
  510. if ($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  511. if ($order['use_integral'] <= 0) return true;
  512. if ((int)$order['status'] != -2 && (int)$order['refund_status'] != 2 && $order['back_integral'] >= $order['use_integral']) return true;
  513. $res = User::bcInc($order['uid'], 'integral', $order['use_integral']);
  514. if (!$res) return self::setErrorInfo('回退积分增加失败');
  515. UserBill::income('积分回退', $order['uid'], 'integral', 'deduction', $order['use_integral'], $order['unique'], User::where('uid', $order['uid'])->value('integral'), '购买商品失败,回退积分' . floatval($order['use_integral']));
  516. return false !== self::where('order_id', $order['order_id'])->update(['back_integral' => $order['use_integral']]);
  517. }
  518. /**
  519. * 回退库存和销量
  520. * @param $order 订单信息
  521. * @return bool
  522. * @throws \think\db\exception\DataNotFoundException
  523. * @throws \think\db\exception\ModelNotFoundException
  524. * @throws \think\exception\DbException
  525. */
  526. public static function RegressionStock($order)
  527. {
  528. if ($order['status'] == -2 || $order['is_del']) return true;
  529. $combinationId = $order['combination_id'];
  530. $seckill_id = $order['seckill_id'];
  531. $bargain_id = $order['bargain_id'];
  532. $res5 = true;
  533. $cartInfo = StoreOrderCartInfo::where('cart_id', 'in', $order['cart_id'])->select()->toArray();
  534. foreach ($cartInfo as $cart) {
  535. //增库存减销量
  536. if ($combinationId) $res5 = $res5 && StoreCombination::incCombinationStock($cart['cart_info']['cart_num'], $combinationId, isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '');
  537. else if ($seckill_id) $res5 = $res5 && StoreSeckill::incSeckillStock($cart['cart_info']['cart_num'], $seckill_id, isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '');
  538. else if ($bargain_id) $res5 = $res5 && StoreBargain::incBargainStock($cart['cart_info']['cart_num'], $bargain_id, isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '');
  539. else {
  540. $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'] : '');
  541. SystemStoreStock::Refund($order, $cart['cart_info']);
  542. }
  543. }
  544. return $res5;
  545. }
  546. /**
  547. * 回退优惠卷
  548. * @param $order 订单信息
  549. * @return bool
  550. */
  551. public static function RegressionCoupon($order)
  552. {
  553. if ($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  554. $res = true;
  555. if ($order['coupon_id'] && StoreCouponUser::be(['id' => $order['coupon_id'], 'uid' => $order['uid'], 'status' => 1])) {
  556. $res = $res && false !== StoreCouponUser::where('id', $order['coupon_id'])->where('uid', $order['uid'])->update(['status' => 0, 'use_time' => 0]);
  557. }
  558. return $res;
  559. }
  560. /**
  561. * 取消订单
  562. * @param string order_id 订单id
  563. * @param $uid
  564. * @return bool
  565. * @throws \think\db\exception\DataNotFoundException
  566. * @throws \think\db\exception\ModelNotFoundException
  567. * @throws \think\exception\DbException
  568. */
  569. public static function cancelOrder($order_id, $uid)
  570. {
  571. $order = self::where('order_id', $order_id)->where('uid', $uid)->find();
  572. if (!$order) return self::setErrorInfo('没有查到此订单');
  573. self::beginTrans();
  574. try {
  575. $res = self::RegressionIntegral($order) && self::RegressionStock($order) && self::RegressionCoupon($order);
  576. $order->is_del = 1;
  577. if ($res && $order->save()) {
  578. self::commitTrans();
  579. return true;
  580. } else
  581. return false;
  582. } catch (\Exception $e) {
  583. self::rollbackTrans();
  584. return self::setErrorInfo(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  585. }
  586. }
  587. /**
  588. * 生成订单唯一id
  589. * @param $uid 用户uid
  590. * @return string
  591. */
  592. public static function getNewOrderId()
  593. {
  594. list($msec, $sec) = explode(' ', microtime());
  595. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  596. $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  597. if (self::be(['order_id' => $orderId])) $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  598. return $orderId;
  599. }
  600. /**
  601. * 修改订单号
  602. * @param $orderId
  603. * @return string
  604. */
  605. public static function changeOrderId($orderId)
  606. {
  607. $ymd = substr($orderId, 2, 8);
  608. $key = substr($orderId, 16);
  609. return 'wx' . $ymd . date('His') . $key;
  610. }
  611. /**
  612. * 查找购物车里的所有产品标题
  613. * @param $cartId 购物车id
  614. * @return bool|string
  615. */
  616. public static function getProductTitle($cartId)
  617. {
  618. $title = '';
  619. try {
  620. $orderCart = StoreOrderCartInfo::where('cart_id', 'in', $cartId)->field('cart_info')->select();
  621. foreach ($orderCart as $item) {
  622. if (isset($item['cart_info']['productInfo']['store_name'])) {
  623. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  624. }
  625. }
  626. unset($item);
  627. if (!$title) {
  628. $productIds = StoreCart::where('id', 'in', $cartId)->column('product_id');
  629. $productlist = ($productlist = StoreProduct::getProductField($productIds, 'store_name')) ? $productlist->toArray() : [];
  630. foreach ($productlist as $item) {
  631. if (isset($item['store_name'])) $title .= $item['store_name'] . '|';
  632. }
  633. }
  634. if ($title) $title = substr($title, 0, strlen($title) - 1);
  635. unset($item);
  636. } catch (\Exception $e) {
  637. }
  638. return $title;
  639. }
  640. /**
  641. * 获取门店自提唯一核销码
  642. * @return bool|string
  643. */
  644. public static function getStoreCode()
  645. {
  646. list($msec, $sec) = explode(' ', microtime());
  647. $num = bcadd(time(), mt_rand(10, 999999), 0) . '' . substr($msec, 2, 3);//生成随机数
  648. if (strlen($num) < 12)
  649. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  650. else
  651. $num = substr($num, 0, 12);
  652. if (self::be(['verify_code' => $num])) return self::getStoreCode();
  653. return $num;
  654. }
  655. /**
  656. * 余额支付
  657. * @param $order_id
  658. * @param $uid
  659. * @param string $formId
  660. * @return bool
  661. * @throws \think\Exception
  662. * @throws \think\db\exception\DataNotFoundException
  663. * @throws \think\db\exception\ModelNotFoundException
  664. * @throws \think\exception\DbException
  665. */
  666. public static function yuePay($order_id, $uid, $formId = '')
  667. {
  668. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  669. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  670. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  671. // if($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  672. $userInfo = User::getUserInfo($uid);
  673. if ($userInfo['now_money'] < $orderInfo['pay_price'])
  674. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])]);
  675. self::beginTrans();
  676. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  677. $res2 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  678. $res3 = self::paySuccess($order_id, 'yue', $formId);//余额支付成功
  679. try {
  680. PaymentRepositories::yuePayProduct($userInfo, $orderInfo);
  681. } catch (\Exception $e) {
  682. self::rollbackTrans();
  683. return self::setErrorInfo($e->getMessage());
  684. }
  685. $res = $res1 && $res2 && $res3;
  686. self::checkTrans($res);
  687. return $res;
  688. }
  689. /**
  690. * 消费券支付
  691. * @param $order_id
  692. * @param $uid
  693. * @param string $formId
  694. * @return bool
  695. * @throws \think\Exception
  696. * @throws \think\db\exception\DataNotFoundException
  697. * @throws \think\db\exception\ModelNotFoundException
  698. * @throws \think\exception\DbException
  699. */
  700. public static function consumerPay($order_id, $uid, $formId = '')
  701. {
  702. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  703. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  704. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  705. // if($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  706. $userInfo = User::getUserInfo($uid);
  707. if ($userInfo['consumer'] < $orderInfo['pay_price'])
  708. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '消费券不足' . floatval($orderInfo['pay_price'])]);
  709. self::beginTrans();
  710. $res1 = false !== User::bcDec($uid, 'consumer', $orderInfo['pay_price'], 'uid');
  711. $res2 = UserBill::expend('消费券商品', $uid, 'consumer', 'consumer_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['consumer'], '消费券支付' . floatval($orderInfo['pay_price']) . '购买商品');
  712. $res3 = self::paySuccess($order_id, 'consumer', $formId);//银币支付成功
  713. try {
  714. PaymentRepositories::yuePayProduct($userInfo, $orderInfo);
  715. } catch (\Exception $e) {
  716. self::rollbackTrans();
  717. return self::setErrorInfo($e->getMessage());
  718. }
  719. $res = $res1 && $res2 && $res3;
  720. self::checkTrans($res);
  721. return $res;
  722. }
  723. /**
  724. * 微信支付 为 0元时
  725. * @param $order_id
  726. * @param $uid
  727. * @param string $formId
  728. * @return bool
  729. * @throws \think\Exception
  730. * @throws \think\db\exception\DataNotFoundException
  731. * @throws \think\db\exception\ModelNotFoundException
  732. * @throws \think\exception\DbException
  733. */
  734. public static function jsPayPrice($order_id, $uid, $formId = '')
  735. {
  736. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  737. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  738. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  739. $userInfo = User::getUserInfo($uid);
  740. self::beginTrans();
  741. $res1 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  742. $res2 = self::paySuccess($order_id, 'weixin', $formId);//微信支付为0时
  743. $res = $res1 && $res2;
  744. self::checkTrans($res);
  745. return $res;
  746. }
  747. /**
  748. * 用户申请退款
  749. * @param $uni
  750. * @param $uid
  751. * @param string $refundReasonWap
  752. * @return bool
  753. */
  754. public static function orderApplyRefund($uni, $uid, $refundReasonWap = '', $refundReasonWapExplain = '', $refundReasonWapImg = [])
  755. {
  756. $order = self::getUserOrderDetail($uid, $uni);
  757. if (!$order) return self::setErrorInfo('支付订单不存在!');
  758. if ($order['refund_status'] == 2) return self::setErrorInfo('订单已退款!');
  759. if ($order['refund_status'] == 1) return self::setErrorInfo('正在申请退款中!');
  760. if ($order['status'] == 1) return self::setErrorInfo('订单当前无法退款!');
  761. self::beginTrans();
  762. $res1 = false !== StoreOrderStatus::status($order['id'], 'apply_refund', '用户申请退款,原因:' . $refundReasonWap);
  763. $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');
  764. $res = $res1 && $res2;
  765. self::checkTrans($res);
  766. if (!$res)
  767. return self::setErrorInfo('申请退款失败!');
  768. else {
  769. try {
  770. if (in_array($order['is_channel'], [0, 2])) {
  771. //公众号发送模板消息通知客服
  772. WechatTemplateService::sendAdminNoticeTemplate([
  773. 'first' => "亲,有个订单申请退款 \n订单号:{$order['order_id']}",
  774. 'keyword1' => '退款申请',
  775. 'keyword2' => '已支付',
  776. 'keyword3' => date('Y/m/d H:i', time()),
  777. 'remark' => '请及时处理'
  778. ]);
  779. }
  780. if (in_array($order['is_channel'], [1, 2])) {
  781. //小程序 发送模板消息
  782. RoutineTemplate::sendOrderRefundStatus($order, $refundReasonWap);
  783. }
  784. //通知后台消息提醒
  785. ChannelService::instance()->send('NEW_REFUND_ORDER', ['order_id' => $order['order_id']]);
  786. } catch (\Exception $e) {
  787. }
  788. //发送短信
  789. event('ShortMssageSend', [$order['order_id'], 'AdminRefund']);
  790. return true;
  791. }
  792. }
  793. /**
  794. * //TODO 支付成功后
  795. * @param $orderId
  796. * @param $paytype
  797. * @param $notify
  798. * @return bool
  799. */
  800. public static function paySuccess($orderId, $paytype = 'weixin', $formId = '')
  801. {
  802. $order = self::where('order_id', $orderId)->find();
  803. $resPink = true;
  804. $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => date("Y-m-d H:i:s")]);//订单改为支付
  805. if ($order->combination_id && $res1 && !$order->refund_status) $resPink = StorePink::createPink($order);//创建拼团
  806. $oid = self::where('order_id', $orderId)->value('id');
  807. StoreOrderStatus::status($oid, 'pay_success', '用户付款成功');
  808. $now_money = User::where('uid', $order['uid'])->value('now_money');
  809. UserBill::expend('购买商品', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元购买商品');
  810. //支付成功后
  811. $res2 = true;
  812. $user = User::where('uid', $order['uid'])->find();
  813. if (($order['suit'] > 0 || $order['is_bind'] > 0) && !$user['lock_spread'] && $user['spread_uid']) User::where('uid', $order['uid'])->update(['lock_spread' => 1]);
  814. $res2 = $res2 && self::addSuit($order);
  815. $res2 = $res2 && StoreOrder::sendSuit($order);
  816. $res2 = $res2 && StoreOrder::sendGroupAward($order);
  817. $res2 = $res2 && StoreOrder::sendAllSystemAward($order);
  818. $res2 = $res2 && SystemGroupLevel::checkLevel($order['uid']);
  819. $res2 = $res2 && SystemAwardLevel::checkLevel($order['uid']);
  820. event('OrderPaySuccess', [$order, $formId]);
  821. $res = $res1 && $resPink && $res2;
  822. return false !== $res;
  823. }
  824. public static function addSuit($order)
  825. {
  826. if ($order['suit'] <= 0) return true;
  827. $userInfo = User::getUserInfo($order['uid']);
  828. $spread = User::getUserInfo($userInfo['spread_uid']);
  829. if (StoreOrder::where('uid', $order['uid'])->where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('is_system_del', 0)
  830. ->where('id', '<>', $order['id'])->where('suit', '>', 0)->find()) {
  831. $spread = $userInfo;
  832. }
  833. $re = true;
  834. $res = true;
  835. while ($spread) {
  836. if ($re) {
  837. $res = $res && User::where('uid', $spread['uid'])->inc('recommend_suit_num', $order['suit'])
  838. ->inc('group_suit_num', $order['suit'])->update();
  839. $re = false;
  840. } else {
  841. $res = $res && User::where('uid', $spread['uid'])->inc('group_suit_num', $order['suit'])->update();
  842. }
  843. $spread = User::getUserInfo($spread['spread_uid']);
  844. }
  845. return $res;
  846. }
  847. /*
  848. * 线下支付消息通知
  849. * 待完善
  850. *
  851. * */
  852. public static function createOrderTemplate($order)
  853. {
  854. //$goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  855. // RoutineTemplateService::sendTemplate(WechatUser::getOpenId($order['uid']),RoutineTemplateService::ORDER_CREATE, [
  856. // 'first'=>'亲,您购买的商品已支付成功',
  857. // 'keyword1'=>date('Y/m/d H:i',$order['add_time']),
  858. // 'keyword2'=>implode(',',$goodsName),
  859. // 'keyword3'=>$order['order_id'],
  860. // 'remark'=>'点击查看订单详情'
  861. // ],Url::build('/wap/My/order',['uni'=>$order['order_id']],true,true));
  862. // RoutineTemplateService::sendAdminNoticeTemplate([
  863. // 'first'=>"亲,您有一个新订单 \n订单号:{$order['order_id']}",
  864. // 'keyword1'=>'新订单',
  865. // 'keyword2'=>'线下支付',
  866. // 'keyword3'=>date('Y/m/d H:i',time()),
  867. // 'remark'=>'请及时处理'
  868. // ]);
  869. }
  870. /**
  871. * 获取订单详情
  872. * @param $uid
  873. * @param $key
  874. * @return array|\think\Model|null
  875. * @throws \think\db\exception\DataNotFoundException
  876. * @throws \think\db\exception\ModelNotFoundException
  877. * @throws \think\exception\DbException
  878. */
  879. public static function getUserOrderDetail($uid, $key)
  880. {
  881. return self::where('order_id|unique', $key)->where('uid', $uid)->where('is_del', 0)->find();
  882. }
  883. /**
  884. * TODO 订单发货
  885. * @param array $postageData 发货信息
  886. * @param string $oid orderID
  887. */
  888. public static function orderPostageAfter($postageData, $oid)
  889. {
  890. $order = self::where('id', $oid)->find();
  891. if ($postageData['delivery_type'] == 'send') {//送货
  892. RoutineTemplate::sendOrderPostage($order);
  893. } else if ($postageData['delivery_type'] == 'express') {//发货
  894. RoutineTemplate::sendOrderPostage($order, 1);
  895. }
  896. }
  897. /** 收货后发送模版消息
  898. * @param $order
  899. */
  900. public static function orderTakeAfter($order)
  901. {
  902. $title = self::getProductTitle($order['cart_id']);
  903. if ($order['is_channel'] == 1) {//小程序
  904. RoutineTemplate::sendOrderTakeOver($order, $title);
  905. } else {
  906. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  907. \crmeb\services\WechatTemplateService::sendTemplate($openid, \crmeb\services\WechatTemplateService::ORDER_TAKE_SUCCESS, [
  908. 'first' => '亲,您的订单已收货',
  909. 'keyword1' => $order['order_id'],
  910. 'keyword2' => '已收货',
  911. 'keyword3' => date('Y-m-d H:i:s', time()),
  912. 'keyword4' => $title,
  913. 'remark' => '感谢您的光临!'
  914. ]);
  915. }
  916. }
  917. /**
  918. * 删除订单
  919. * @param $uni
  920. * @param $uid
  921. * @return bool
  922. */
  923. public static function removeOrder($uni, $uid)
  924. {
  925. $order = self::getUserOrderDetail($uid, $uni);
  926. if (!$order) return self::setErrorInfo('订单不存在!');
  927. $order = self::tidyOrder($order);
  928. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  929. return self::setErrorInfo('该订单无法删除!');
  930. if (false !== self::edit(['is_del' => 1], $order['id'], 'id') && false !== StoreOrderStatus::status($order['id'], 'remove_order', '删除订单')) {
  931. //未支付和已退款的状态下才可以退积分退库存退优惠券
  932. if ($order['_status']['_type'] == 0 || $order['_status']['_type'] == -2) {
  933. event('StoreOrderRegressionAllAfter', [$order]);
  934. }
  935. event('UserOrderRemoved', $uni);
  936. return true;
  937. } else
  938. return self::setErrorInfo('订单删除失败!');
  939. }
  940. /**
  941. * //TODO 用户确认收货
  942. * @param $uni
  943. * @param $uid
  944. */
  945. public static function takeOrder($uni, $uid)
  946. {
  947. $order = self::getUserOrderDetail($uid, $uni);
  948. if (!$order) return self::setErrorInfo('订单不存在!');
  949. $order = self::tidyOrder($order);
  950. if ($order['_status']['_type'] != 2) return self::setErrorInfo('订单状态错误!');
  951. self::beginTrans();
  952. if (false !== self::edit(['status' => 2], $order['id'], 'id') &&
  953. false !== StoreOrderStatus::status($order['id'], 'user_take_delivery', '用户已收货')) {
  954. try {
  955. OrderRepository::storeProductOrderUserTakeDelivery($order, $uid);
  956. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  957. } catch (\Exception $e) {
  958. self::rollbackTrans();
  959. return self::setErrorInfo($e->getMessage());
  960. }
  961. self::commitTrans();
  962. event('UserLevelAfter', [User::get($uni)]);
  963. event('UserOrderTake', $uni);
  964. //短信通知
  965. event('ShortMssageSend', [$order['order_id'], ['Receiving', 'AdminConfirmTakeOver']]);
  966. return true;
  967. } else {
  968. self::rollbackTrans();
  969. return false;
  970. }
  971. }
  972. /**
  973. * 获取订单状态购物车等信息
  974. * @param $order
  975. * @param bool $detail 是否获取订单购物车详情
  976. * @param bool $isPic 是否获取订单状态图片
  977. * @return mixed
  978. * @throws \think\db\exception\DataNotFoundException
  979. * @throws \think\db\exception\ModelNotFoundException
  980. * @throws \think\exception\DbException
  981. */
  982. public static function tidyOrder($order, $detail = false, $isPic = false)
  983. {
  984. if ($detail == true && isset($order['id'])) {
  985. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'unique') ?: [];
  986. $info = [];
  987. foreach ($cartInfo as $k => $cart) {
  988. $cart = json_decode($cart, true);
  989. $cart['unique'] = $k;
  990. //新增是否评价字段
  991. $cart['is_reply'] = StoreProductReply::where('unique', $k)->count();
  992. array_push($info, $cart);
  993. unset($cart);
  994. }
  995. $order['cartInfo'] = $info;
  996. }
  997. $status = [];
  998. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  999. $status['_type'] = 9;
  1000. $status['_title'] = '线下付款';
  1001. $status['_msg'] = '商家处理中,请耐心等待';
  1002. $status['_class'] = 'nobuy';
  1003. } else if (!$order['paid']) {
  1004. $status['_type'] = 0;
  1005. $status['_title'] = '未支付';
  1006. //系统预设取消订单时间段
  1007. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1008. //获取配置
  1009. $systemValue = SystemConfigService::more($keyValue);
  1010. //格式化数据
  1011. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1012. if ($order['pink_id'] || $order['combination_id']) {
  1013. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1014. $time = bcadd(strtotime($order['add_time']), $order_pink_time * 3600, 0);
  1015. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1016. } else if ($order['seckill_id']) {
  1017. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1018. $time = bcadd(strtotime($order['add_time']), $order_seckill_time * 3600, 0);
  1019. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1020. } else if ($order['bargain_id']) {
  1021. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1022. $time = bcadd(strtotime($order['add_time']), $order_bargain_time * 3600, 0);
  1023. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1024. } else {
  1025. $time = bcadd(strtotime($order['add_time']), $systemValue['order_cancel_time'] * 3600, 0);
  1026. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1027. }
  1028. $status['_class'] = 'nobuy';
  1029. } else if ($order['refund_status'] == 1) {
  1030. $status['_type'] = -1;
  1031. $status['_title'] = '申请退款中';
  1032. $status['_msg'] = '商家审核中,请耐心等待';
  1033. $status['_class'] = 'state-sqtk';
  1034. } else if ($order['refund_status'] == 2) {
  1035. $status['_type'] = -2;
  1036. $status['_title'] = '已退款';
  1037. $status['_msg'] = '已为您退款,感谢您的支持';
  1038. $status['_class'] = 'state-sqtk';
  1039. } else if (!$order['status']) {
  1040. if ($order['pink_id']) {
  1041. if (StorePink::where('id', $order['pink_id'])->where('status', 1)->count()) {
  1042. $status['_type'] = 1;
  1043. $status['_title'] = '拼团中';
  1044. $status['_msg'] = '等待其他人参加拼团';
  1045. $status['_class'] = 'state-nfh';
  1046. } else {
  1047. $status['_type'] = 1;
  1048. $status['_title'] = '未发货';
  1049. $status['_msg'] = '商家未发货,请耐心等待';
  1050. $status['_class'] = 'state-nfh';
  1051. }
  1052. } else {
  1053. if ($order['shipping_type'] === 1) {
  1054. $status['_type'] = 1;
  1055. $status['_title'] = '未发货';
  1056. $status['_msg'] = '商家未发货,请耐心等待';
  1057. $status['_class'] = 'state-nfh';
  1058. } else {
  1059. $status['_type'] = 1;
  1060. $status['_title'] = '待核销';
  1061. $status['_msg'] = '待核销,请到核销点进行核销';
  1062. $status['_class'] = 'state-nfh';
  1063. }
  1064. }
  1065. } else if ($order['status'] == 1) {
  1066. if ($order['delivery_type'] == 'send') {//TODO 送货
  1067. $status['_type'] = 2;
  1068. $status['_title'] = '待收货';
  1069. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($order['id'], 'delivery')) . '服务商已送货';
  1070. $status['_class'] = 'state-ysh';
  1071. } else {//TODO 发货
  1072. $status['_type'] = 2;
  1073. $status['_title'] = '待收货';
  1074. if ($order['delivery_type'] == 'fictitious')
  1075. $_time = StoreOrderStatus::getTime($order['id'], 'delivery_fictitious');
  1076. else
  1077. $_time = StoreOrderStatus::getTime($order['id'], 'delivery_goods');
  1078. $status['_msg'] = date('m月d日H时i分', $_time) . '服务商已发货';
  1079. $status['_class'] = 'state-ysh';
  1080. }
  1081. } else if ($order['status'] == 2) {
  1082. $status['_type'] = 3;
  1083. $status['_title'] = '待评价';
  1084. $status['_msg'] = '已收货,快去评价一下吧';
  1085. $status['_class'] = 'state-ypj';
  1086. } else if ($order['status'] == 3) {
  1087. $status['_type'] = 4;
  1088. $status['_title'] = '交易完成';
  1089. $status['_msg'] = '交易完成,感谢您的支持';
  1090. $status['_class'] = 'state-ytk';
  1091. }
  1092. if (isset($order['pay_type']))
  1093. $status['_payType'] = isset(self::$payType[$order['pay_type']]) ? self::$payType[$order['pay_type']] : '其他方式';
  1094. if (isset($order['delivery_type']))
  1095. $status['_deliveryType'] = isset(self::$deliveryType[$order['delivery_type']]) ? self::$deliveryType[$order['delivery_type']] : '其他方式';
  1096. $order['_status'] = $status;
  1097. $order['_pay_time'] = isset($order['pay_time']) && $order['pay_time'] != null ? $order['pay_time'] : $order['add_time'];
  1098. $order['_add_time'] = $order['add_time'];
  1099. $order['status_pic'] = '';
  1100. //获取产品状态图片
  1101. if ($isPic) {
  1102. $order_details_images = sys_data('order_details_images') ?: [];
  1103. foreach ($order_details_images as $image) {
  1104. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  1105. $order['status_pic'] = $image['pic'];
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. $order['offlinePayStatus'] = (int)sys_config('offline_pay_status') ?? (int)2;
  1111. return $order;
  1112. }
  1113. /**
  1114. * 设置订单查询状态
  1115. * @param $status
  1116. * @param int $uid
  1117. * @param null $model
  1118. * @return StoreOrder|null
  1119. */
  1120. public static function statusByWhere($status, $uid = 0, $model = null)
  1121. {
  1122. // $orderId = StorePink::where('uid',$uid)->where('status',1)->column('order_id','id');//获取正在拼团的订单编号
  1123. if ($model == null) $model = new self;
  1124. if ('' === $status)
  1125. return $model;
  1126. else if ($status == 0)//未支付
  1127. return $model->where('paid', 0)->where('status', 0)->where('refund_status', 0);
  1128. else if ($status == 1)//待发货
  1129. return $model->where('paid', 1)->where('status', 0)->where('refund_status', 0);
  1130. else if ($status == 2)//待收货
  1131. return $model->where('paid', 1)->where('status', 1)->where('refund_status', 0);
  1132. else if ($status == 3)//待评价
  1133. return $model->where('paid', 1)->where('status', 2)->where('refund_status', 0);
  1134. else if ($status == 4)//已完成
  1135. return $model->where('paid', 1)->where('status', 3)->where('refund_status', 0);
  1136. else if ($status == -1)//退款中
  1137. return $model->where('paid', 1)->where('refund_status', 1);
  1138. else if ($status == -2)//已退款
  1139. return $model->where('paid', 1)->where('refund_status', 2);
  1140. else if ($status == -3)//退款
  1141. return $model->where('paid', 1)->where('refund_status', 'IN', '1,2');
  1142. // else if($status == 11){
  1143. // return $model->where('order_id','IN',implode(',',$orderId));
  1144. // }
  1145. else
  1146. return $model;
  1147. }
  1148. /**
  1149. * 获取订单并分页
  1150. * @param $uid
  1151. * @param string $status
  1152. * @param int $page
  1153. * @param int $limit
  1154. * @return mixed
  1155. * @throws \think\db\exception\DataNotFoundException
  1156. * @throws \think\db\exception\ModelNotFoundException
  1157. * @throws \think\exception\DbException
  1158. */
  1159. public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8)
  1160. {
  1161. if ($page) $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid', $uid)
  1162. ->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,shipping_type')
  1163. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  1164. else $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid', $uid)
  1165. ->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,shipping_type')
  1166. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  1167. foreach ($list as $k => $order) {
  1168. $list[$k] = self::tidyOrder($order, true);
  1169. }
  1170. return $list;
  1171. }
  1172. /**
  1173. * 获取推广人地下用户的订单金额
  1174. * @param string $uid
  1175. * @param string $status
  1176. * @return array
  1177. */
  1178. public static function getUserOrderCount($uid = '', $status = '')
  1179. {
  1180. $res = self::statusByWhere($status, $uid)->where('uid', 'IN', $uid)->column('pay_price');
  1181. return $res;
  1182. }
  1183. /**
  1184. * 搜索某个订单详细信息
  1185. * @param $uid
  1186. * @param $order_id
  1187. * @return bool|mixed
  1188. * @throws \think\db\exception\DataNotFoundException
  1189. * @throws \think\db\exception\ModelNotFoundException
  1190. * @throws \think\exception\DbException
  1191. */
  1192. public static function searchUserOrder($uid, $order_id)
  1193. {
  1194. $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,shipping_type')
  1195. ->order('add_time DESC')->find();
  1196. if (!$order)
  1197. return false;
  1198. else
  1199. return self::tidyOrder($order->toArray(), true);
  1200. }
  1201. /**
  1202. * 订单评价信息记录
  1203. * @param $oid
  1204. * @return StoreOrderStatus|\think\Model
  1205. * @throws \Exception
  1206. */
  1207. public static function orderOver($oid)
  1208. {
  1209. $res = self::edit(['status' => '3'], $oid, 'id');
  1210. if (!$res) exception('评价后置操作失败!');
  1211. return StoreOrderStatus::status($oid, 'check_order_over', '用户评价');
  1212. }
  1213. /**
  1214. * 设置订单产品评价完毕事件
  1215. * @param $oid
  1216. * @return StoreOrderStatus|\think\Model
  1217. * @throws \Exception
  1218. */
  1219. public static function checkOrderOver($oid)
  1220. {
  1221. $uniqueList = StoreOrderCartInfo::where('oid', $oid)->column('unique', 'unique');
  1222. //订单产品全部评价完成
  1223. if (StoreProductReply::where('unique', 'IN', $uniqueList)->where('oid', $oid)->count() == count($uniqueList)) {
  1224. event('StoreProductOrderOver', [$oid]);
  1225. return self::orderOver($oid);
  1226. }
  1227. }
  1228. public static function getOrderStatusNum($uid)
  1229. {
  1230. $noBuy = (int)self::where('uid', $uid)->where('paid', 0)->where('is_del', 0)->where('pay_type', '<>', 'offline')->count();
  1231. $noPostageNoPink = (int)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();
  1232. $noPostageYesPink = (int)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();
  1233. $noPostage = (int)bcadd($noPostageNoPink, $noPostageYesPink, 0);
  1234. $noTake = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('status', 1)->where('pay_type', '<>', 'offline')->count();
  1235. $noReply = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('status', 2)->count();
  1236. $noPink = (int)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();
  1237. $noRefund = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('refund_status', 'IN', '1,2')->count();
  1238. return compact('noBuy', 'noPostage', 'noTake', 'noReply', 'noPink', 'noRefund');
  1239. }
  1240. /**
  1241. * 购买商品赠送积分
  1242. * @param $order
  1243. * @return bool
  1244. * @throws \think\Exception
  1245. * @throws \think\db\exception\DataNotFoundException
  1246. * @throws \think\db\exception\ModelNotFoundException
  1247. * @throws \think\exception\DbException
  1248. */
  1249. public static function gainUserIntegral($order)
  1250. {
  1251. if ($order['gain_integral'] > 0) {
  1252. $userInfo = User::getUserInfo($order['uid']);
  1253. BaseModel::beginTrans();
  1254. $res1 = false != User::where('uid', $userInfo['uid'])->update(['integral' => bcadd($userInfo['integral'], $order['gain_integral'], 2)]);
  1255. $res2 = false != UserBill::income('购买商品赠送积分', $order['uid'], 'integral', 'gain', $order['gain_integral'], $order['id'], $userInfo['integral'], '购买商品赠送' . floatval($order['gain_integral']) . '积分');
  1256. $res = $res1 && $res2;
  1257. BaseModel::checkTrans($res);
  1258. return $res;
  1259. }
  1260. return true;
  1261. }
  1262. /**
  1263. * 获取当前订单中有没有拼团存在
  1264. * @param $pid
  1265. * @return int|string
  1266. */
  1267. public static function getIsOrderPink($pid = 0, $uid = 0)
  1268. {
  1269. return self::where('uid', $uid)->where('pink_id', $pid)->where('refund_status', 0)->where('is_del', 0)->count();
  1270. }
  1271. /**
  1272. * 获取order_id
  1273. * @param $pid
  1274. * @return mixed
  1275. */
  1276. public static function getStoreIdPink($pid = 0, $uid = 0)
  1277. {
  1278. return self::where('uid', $uid)->where('pink_id', $pid)->where('is_del', 0)->value('order_id');
  1279. }
  1280. /**
  1281. * 删除当前用户拼团未支付的订单
  1282. */
  1283. public static function delCombination()
  1284. {
  1285. self::where('combination', '>', 0)->where('paid', 0)->where('uid', User::getActiveUid())->delete();
  1286. }
  1287. public static function getUserPrice($uid = 0)
  1288. {
  1289. if (!$uid) return 0;
  1290. $price = self::where('paid', 1)->where('uid', $uid)->where('status', 2)->where('refund_status', 0)->column('pay_price', 'id');
  1291. $count = 0;
  1292. if ($price) {
  1293. foreach ($price as $v) {
  1294. $count = bcadd($count, $v, 2);
  1295. }
  1296. }
  1297. return $count;
  1298. }
  1299. /**
  1300. * 个人中心获取个人订单列表和订单搜索
  1301. * @param int $uid 用户uid
  1302. * @param int | string 查找订单类型
  1303. * @param int $first 分页
  1304. * @param int 每页显示多少条
  1305. * @param string $search 订单号
  1306. * @return array
  1307. * */
  1308. public static function getUserOrderSearchList($uid, $type, $page, $limit, $search)
  1309. {
  1310. if ($search) {
  1311. $order = self::searchUserOrder($uid, $search) ?: [];
  1312. $list = $order == false ? [] : [$order];
  1313. } else {
  1314. $list = self::getUserOrderList($uid, $type, $page, $limit);
  1315. }
  1316. foreach ($list as $k => $order) {
  1317. $list[$k] = self::tidyOrder($order, true);
  1318. if ($list[$k]['_status']['_type'] == 3) {
  1319. foreach ($order['cartInfo'] ?: [] as $key => $product) {
  1320. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'], 'product');
  1321. //$list[$k]['cartInfo'][$key]['add_time'] = $product['add_time'];
  1322. }
  1323. }
  1324. }
  1325. return $list;
  1326. }
  1327. /**
  1328. * 获取用户下级的订单
  1329. * @param int $xuid 下级用户用户uid
  1330. * @param int $uid 用户uid
  1331. * @param int $type 订单类型
  1332. * @param int $first 截取行数
  1333. * @param int $limit 展示条数
  1334. * @return array
  1335. * */
  1336. public static function getSubordinateOrderlist($xUid, $uid, $type, $first, $limit)
  1337. {
  1338. $list = [];
  1339. if (!$xUid) {
  1340. $arr = User::getOneSpreadUid($uid);
  1341. foreach ($arr as $v) $list = StoreOrder::getUserOrderList($v, $type, $first, $limit);
  1342. } else $list = self::getUserOrderList($xUid, $type, $first, $limit);
  1343. foreach ($list as $k => $order) {
  1344. $list[$k] = self::tidyOrder($order, true);
  1345. if ($list[$k]['_status']['_type'] == 3) {
  1346. foreach ($order['cartInfo'] ?: [] as $key => $product) {
  1347. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'], 'product');
  1348. }
  1349. }
  1350. }
  1351. return $list;
  1352. }
  1353. /**
  1354. * 获取 今日 昨日 本月 订单金额
  1355. * @return mixed
  1356. */
  1357. public static function getOrderTimeData($store_id = 0, $is_consumer = 0)
  1358. {
  1359. $where = [];
  1360. if ($store_id > 0) {
  1361. $where['store_id'] = $store_id;
  1362. }
  1363. $where['is_consumer'] = $is_consumer;
  1364. $to_day = strtotime(date('Y-m-d'));//今日
  1365. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
  1366. $now_month = strtotime(date('Y-m'));//本月
  1367. //今日成交额
  1368. // $data['todayPrice'] = (float)number_format(self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->value('sum(pay_price)'), 2) ?? 0;
  1369. $data['todayPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->where($where)->value('sum(pay_price)'), 2) ?? 0;
  1370. //今日订单数
  1371. $data['todayCount'] = self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->where($where)->count();
  1372. //昨日成交额
  1373. $data['proPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '<', $to_day)->where('pay_time', '>=', $pre_day)->where('paid', 1)->where('refund_status', 0)->where($where)->value('sum(pay_price)'), 2) ?? 0;
  1374. //昨日订单数
  1375. $data['proCount'] = self::where('is_del', 0)->where('pay_time', '<', $to_day)->where('pay_time', '>=', $pre_day)->where('paid', 1)->where('refund_status', 0)->where($where)->count();
  1376. //本月成交额
  1377. $data['monthPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '>=', $now_month)->where('paid', 1)->where('refund_status', 0)->where($where)->value('sum(pay_price)'), 2) ?? 0;
  1378. //本月订单数
  1379. $data['monthCount'] = self::where('is_del', 0)->where('pay_time', '>=', $now_month)->where('paid', 1)->where('refund_status', 0)->where($where)->count();
  1380. return $data;
  1381. }
  1382. /**
  1383. * 获取某个用户的订单统计数据
  1384. * @param $uid
  1385. * @return mixed
  1386. */
  1387. public static function getOrderData($uid)
  1388. {
  1389. //订单支付没有退款 数量
  1390. $data['order_count'] = self::where('is_del', 0)->where('paid', 1)->where('uid', $uid)->where('refund_status', 0)->count();
  1391. //订单支付没有退款 支付总金额
  1392. $data['sum_price'] = self::where('is_del', 0)->where('paid', 1)->where('uid', $uid)->where('refund_status', 0)->sum('pay_price');
  1393. //订单待支付 数量
  1394. $data['unpaid_count'] = self::statusByWhere(0, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1395. //订单待发货 数量
  1396. $data['unshipped_count'] = self::statusByWhere(1, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1397. //订单待收货 数量
  1398. $data['received_count'] = self::statusByWhere(2, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1399. //订单待评价 数量
  1400. $data['evaluated_count'] = self::statusByWhere(3, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1401. //订单已完成 数量
  1402. $data['complete_count'] = self::statusByWhere(4, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1403. //订单退款
  1404. $data['refund_count'] = self::statusByWhere(-1, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1405. return $data;
  1406. }
  1407. /**
  1408. * 获取订单统计数据
  1409. * @param $uid
  1410. * @return mixed
  1411. */
  1412. public static function getOrderDataAdmin($store_id = 0, $is_consumer = 0)
  1413. {
  1414. $where = [];
  1415. if ($store_id > 0) {
  1416. $where['store_id'] = $store_id;
  1417. }
  1418. $where['is_consumer'] = $is_consumer;
  1419. //订单支付没有退款 数量
  1420. $data['order_count'] = self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where($where)->count();
  1421. //订单支付没有退款 支付总金额
  1422. $data['sum_price'] = self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where($where)->sum('pay_price');
  1423. //订单待支付 数量
  1424. $data['unpaid_count'] = self::statusByWhere(0, 0)->where('is_del', 0)->where($where)->count();
  1425. //订单待发货 数量
  1426. $data['unshipped_count'] = self::statusByWhere(1, 0)->where('is_del', 0)->where($where)->count();
  1427. //订单待收货 数量
  1428. $data['received_count'] = self::statusByWhere(2, 0)->where('is_del', 0)->where($where)->count();
  1429. //订单待评价 数量
  1430. $data['evaluated_count'] = self::statusByWhere(3, 0)->where('is_del', 0)->where($where)->count();
  1431. //订单已完成 数量
  1432. $data['complete_count'] = self::statusByWhere(4, 0)->where('is_del', 0)->where($where)->count();
  1433. //订单退款 数量
  1434. $data['refund_count'] = self::statusByWhere(-3, 0)->where('is_del', 0)->where($where)->count();
  1435. return $data;
  1436. }
  1437. /**
  1438. * 累计消费
  1439. * @param $uid
  1440. * @return float
  1441. */
  1442. public static function getOrderStatusSum($uid)
  1443. {
  1444. return self::where('uid', $uid)->where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
  1445. }
  1446. public static function getPinkOrderId($id)
  1447. {
  1448. return self::where('id', $id)->value('order_id');
  1449. }
  1450. /**
  1451. * 未支付订单自动取消
  1452. * @param int $limit 分页截取条数
  1453. * @param string $prefid 缓存名称
  1454. * @param int $expire 缓存时间
  1455. * @return string|null
  1456. * @throws \Psr\SimpleCache\InvalidArgumentException
  1457. */
  1458. public static function orderUnpaidCancel()
  1459. {
  1460. //系统预设取消订单时间段
  1461. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1462. //获取配置
  1463. $systemValue = SystemConfigService::more($keyValue);
  1464. //格式化数据
  1465. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1466. //检查是否有未支付的订单 未支付查询条件
  1467. $unPidCount = self::where('paid', 0)->where('pay_type', '<>', 'offline')->where('is_del', 0)->where('status', 0)->where('refund_status', 0)->count();
  1468. if (!$unPidCount) return null;
  1469. try {
  1470. $res = true;
  1471. // 未支付查询条件
  1472. $orderList = self::where('paid', 0)->where('pay_type', '<>', 'offline')->where('is_del', 0)->where('status', 0)->where('refund_status', 0)->field('add_time,pink_id,order_id,seckill_id,bargain_id,combination_id,status,cart_id,use_integral,refund_status,uid,unique,back_integral,coupon_id,paid,is_del')->select();
  1473. foreach ($orderList as $order) {
  1474. if ($order['seckill_id']) {
  1475. //优先使用单独配置的过期时间
  1476. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1477. $res = $res && self::RegressionAll($order_seckill_time, $order);
  1478. unset($order_seckill_time);
  1479. } else if ($order['bargain_id']) {
  1480. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1481. $res = $res && self::RegressionAll($order_bargain_time, $order);
  1482. unset($order_bargain_time);
  1483. } else if ($order['pink_id'] || $order['combination_id']) {
  1484. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1485. $res = $res && self::RegressionAll($order_pink_time, $order);
  1486. unset($order_pink_time);
  1487. } else {
  1488. $res = $res && self::RegressionAll($systemValue['order_cancel_time'], $order);
  1489. }
  1490. }
  1491. if (!$res) throw new \Exception('更新错误');
  1492. unset($orderList, $res, $pages);
  1493. return null;
  1494. } catch (PDOException $e) {
  1495. Log::error('未支付自动取消时发生数据库查询错误,错误原因为:' . $e->getMessage());
  1496. throw new \Exception($e->getMessage());
  1497. } catch (\think\Exception $e) {
  1498. Log::error('未支付自动取消时发生系统错误,错误原因为:' . $e->getMessage());
  1499. throw new \Exception($e->getMessage());
  1500. }
  1501. }
  1502. /**
  1503. * 未支付订单超过预设时间回退所有,如果不设置未支付过期时间,将不取消订单
  1504. * @param $time 预设时间
  1505. * @param $order 订单详情
  1506. * @return bool
  1507. * @throws \think\db\exception\DataNotFoundException
  1508. * @throws \think\db\exception\ModelNotFoundException
  1509. * @throws \think\exception\DbException
  1510. */
  1511. protected static function RegressionAll($time, $order)
  1512. {
  1513. if ($time == 0) return true;
  1514. if ((strtotime($order['add_time']) + bcmul($time, 3600, 0)) < time()) {
  1515. $res1 = self::RegressionStock($order);
  1516. $res2 = self::RegressionIntegral($order);
  1517. $res3 = self::RegressionCoupon($order);
  1518. $res = $res1 && $res2 && $res3;
  1519. if ($res) $res = false !== self::where('order_id', $order['order_id'])->update(['is_del' => 1, 'mark' => '订单未支付已超过系统预设时间']);
  1520. unset($res1, $res2, $res3);
  1521. return $res;
  1522. } else
  1523. return true;
  1524. }
  1525. /**
  1526. * 格式化数据
  1527. * @param array $array 原本数据键
  1528. * @param $value 需要格式化的数据
  1529. * @param int $default 默认值
  1530. * @return mixed
  1531. */
  1532. protected static function setValeTime(array $array, $value, $default = 0)
  1533. {
  1534. foreach ($array as $item) {
  1535. if (!isset($value[$item]))
  1536. $value[$item] = $default;
  1537. else if (is_string($value[$item]))
  1538. $value[$item] = (float)$value[$item];
  1539. }
  1540. return $value;
  1541. }
  1542. public static function getOrderTotalPrice($cartInfo)
  1543. {
  1544. $totalPrice = 0;
  1545. foreach ($cartInfo as $cart) {
  1546. $totalPrice = bcadd($totalPrice, bcmul($cart['cart_num'], $cart['truePrice'], 2), 2);
  1547. }
  1548. return $totalPrice;
  1549. }
  1550. public static function getOrderCostPrice($cartInfo)
  1551. {
  1552. $costPrice = 0;
  1553. foreach ($cartInfo as $cart) {
  1554. $costPrice = bcadd($costPrice, bcmul($cart['cart_num'], $cart['costPrice'], 2), 2);
  1555. }
  1556. return $costPrice;
  1557. }
  1558. public static function getCombinationOrderCostPrice($cartInfo)
  1559. {
  1560. $costPrice = 0;
  1561. foreach ($cartInfo as $cart) {
  1562. if ($cart['combination_id']) {
  1563. $costPrice = bcadd($costPrice, bcmul($cart['cart_num'], StoreCombination::where('id', $cart['combination_id'])->value('price'), 2), 2);
  1564. }
  1565. }
  1566. return (float)$costPrice;
  1567. }
  1568. public static function yueRefundAfter($order)
  1569. {
  1570. }
  1571. /**
  1572. * 获取余额支付的金额
  1573. * @param $uid
  1574. * @return float|int
  1575. */
  1576. public static function getOrderStatusYueSum($uid)
  1577. {
  1578. return self::where('uid', $uid)->where('is_del', 0)->where('is_del', 0)->where('pay_type', 'yue')->where('paid', 1)->sum('pay_price');
  1579. }
  1580. /**
  1581. * 砍价支付成功订单数量
  1582. * @param $bargain
  1583. * @return int
  1584. */
  1585. public static function getBargainPayCount($bargain)
  1586. {
  1587. return self::where('bargain_id', $bargain)->where(['paid' => 1, 'refund_status' => 0])->count();
  1588. }
  1589. /**
  1590. * 7天自动收货
  1591. * @return bool
  1592. */
  1593. public static function startTakeOrder()
  1594. {
  1595. //7天前时间戳
  1596. $systemDeliveryTime = SystemConfigService::get('system_delivery_time') ?? 0;
  1597. //0为取消自动收货功能
  1598. if ($systemDeliveryTime == 0) return true;
  1599. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  1600. $model = new self;
  1601. $model = $model->alias('o');
  1602. $model = $model->join('StoreOrderStatus s', 's.oid=o.id');
  1603. $model = $model->where('o.paid', 1);
  1604. $model = $model->where('s.change_type', 'IN', ['delivery_goods', 'delivery_fictitious', 'delivery']);
  1605. $model = $model->where('s.change_time', '<', $sevenDay);
  1606. $model = $model->where('o.status', 1);
  1607. $model = $model->where('o.refund_status', 0);
  1608. $model = $model->where('o.is_del', 0);
  1609. $orderInfo = $model->column('id', 'id');
  1610. if (!count($orderInfo)) return true;
  1611. $res = true;
  1612. foreach ($orderInfo as $key => &$item) {
  1613. $order = self::get($item);
  1614. if ($order['status'] == 2) continue;
  1615. if ($order['paid'] == 1 && $order['status'] == 1) $data['status'] = 2;
  1616. else if ($order['pay_type'] == 'offline') $data['status'] = 2;
  1617. else continue;
  1618. if (!self::edit($data, $item, 'id')) continue;
  1619. try {
  1620. OrderRepository::storeProductOrderTakeDeliveryAdmin($order, $item);
  1621. $res = $res && true;
  1622. } catch (\Exception $e) {
  1623. $res = $res && false;
  1624. }
  1625. $res = $res && StoreOrderStatus::status($item, 'take_delivery', '已收货[自动收货]');
  1626. }
  1627. if (!$res) {
  1628. throw new \Exception('');
  1629. }
  1630. }
  1631. /**
  1632. * 获取订单信息
  1633. * @param $id
  1634. * @param string $field
  1635. * @return array|null|\think\Model
  1636. * @throws \think\db\exception\DataNotFoundException
  1637. * @throws \think\db\exception\ModelNotFoundException
  1638. * @throws \think\exception\DbException
  1639. */
  1640. public static function getOrderInfo($id, $field = 'order_id')
  1641. {
  1642. return self::where('id', $id)->field($field)->find();
  1643. }
  1644. /**
  1645. * 订单每月统计数据
  1646. * @param $page
  1647. * @param $limit
  1648. * @return array
  1649. */
  1650. public static function getOrderDataPriceCount($page, $limit, $start, $stop)
  1651. {
  1652. if (!$limit) return [];
  1653. $model = new self;
  1654. if ($start != '' && $stop != '') $model = $model->where('pay_time', '>', $start)->where('pay_time', '<=', $stop);
  1655. $model = $model->field('sum(pay_price) as price,count(id) as count,FROM_UNIXTIME(pay_time, \'%m-%d\') as time');
  1656. $model = $model->where('is_del', 0);
  1657. $model = $model->where('paid', 1);
  1658. $model = $model->where('refund_status', 0);
  1659. $model = $model->group("FROM_UNIXTIME(pay_time, '%Y-%m-%d')");
  1660. $model = $model->order('pay_time DESC');
  1661. if ($page) $model = $model->page($page, $limit);
  1662. return $model->select();
  1663. }
  1664. /**
  1665. * 每月订单汇总
  1666. * @param $store_id
  1667. * @param $start
  1668. * @param $end
  1669. * @return array
  1670. * @throws \think\db\exception\DataNotFoundException
  1671. * @throws \think\db\exception\DbException
  1672. * @throws \think\db\exception\ModelNotFoundException
  1673. */
  1674. public static function getordermonth($store_id, $start, $end)
  1675. {
  1676. $model = new self;
  1677. $model = $model->where('pay_time', '>', $start)->where('pay_time', '<=', $end);
  1678. $model = $model->where('is_del', 0);
  1679. $model = $model->where('paid', 1);
  1680. $model = $model->where('refund_status', 0);
  1681. $model = $model->where('store_id', $store_id);
  1682. $data[] = $model->where('is_consumer', 0)->where('use_integral', 0)->field("sum(pay_price) as price,count(id) as count,'普通订单' as order_type")->find()->toArray();
  1683. $data[] = $model->where('is_consumer', 1)->field("sum(pay_price) as price,count(id) as count,'消费券订单' as order_type")->find()->toArray();
  1684. $data[] = $model->where('is_consumer', 0)->where('use_integral', '>', 0)->field("sum(pay_price) as price,count(id) as count,'积分订单' as order_type")->find()->toArray();
  1685. $day = self::where('paid', 1)->where('refund_status', 0)->where('store_id', $store_id)->where('pay_time', '>', date("y-m-d"))->field('sum(pay_price) as price,count(id) as count')->find();
  1686. return compact('day', 'data');
  1687. }
  1688. /**
  1689. * 前台订单管理订单列表获取
  1690. * @param $where
  1691. * @return mixed
  1692. */
  1693. public static function orderList($where)
  1694. {
  1695. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.id,a.order_id,a.add_time,a.status,a.total_num,a.total_price,a.total_postage,a.pay_price,a.pay_postage,a.paid,a.refund_status,a.remark,a.pay_type')->where('is_del', 0);
  1696. if ($where['order'] != '') {
  1697. $model = $model->order(self::setOrder($where['order']));
  1698. } else {
  1699. $model = $model->order('a.id desc');
  1700. }
  1701. $count = $model->count('id');
  1702. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  1703. $data = self::tidyAdminOrder($data);
  1704. return compact('count', 'data');
  1705. }
  1706. /**
  1707. * 前台订单管理 订单信息设置
  1708. * @param $data
  1709. * @param bool $status
  1710. * @return mixed
  1711. * @throws \think\db\exception\DataNotFoundException
  1712. * @throws \think\db\exception\ModelNotFoundException
  1713. * @throws \think\exception\DbException
  1714. */
  1715. public static function tidyAdminOrder($data, $status = false)
  1716. {
  1717. foreach ($data as &$item) {
  1718. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select()->toArray();
  1719. foreach ($_info as $k => $v) {
  1720. if (!is_array($v['cart_info']))
  1721. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  1722. }
  1723. foreach ($_info as $k => $v) {
  1724. unset($_info[$k]['cart_info']['type'], $_info[$k]['cart_info']['product_id'], $_info[$k]['cart_info']['combination_id'], $_info[$k]['cart_info']['seckill_id'], $_info[$k]['cart_info']['bargain_id'], $_info[$k]['cart_info']['bargain_id'], $_info[$k]['cart_info']['truePrice'], $_info[$k]['cart_info']['vip_truePrice'], $_info[$k]['cart_info']['trueStock'], $_info[$k]['cart_info']['costPrice'], $_info[$k]['cart_info']['productInfo']['id'], $_info[$k]['cart_info']['productInfo']['vip_price'], $_info[$k]['cart_info']['productInfo']['postage'], $_info[$k]['cart_info']['productInfo']['give_integral'], $_info[$k]['cart_info']['productInfo']['sales'], $_info[$k]['cart_info']['productInfo']['stock'], $_info[$k]['cart_info']['productInfo']['unit_name'], $_info[$k]['cart_info']['productInfo']['is_postage'], $_info[$k]['cart_info']['productInfo']['slider_image'], $_info[$k]['cart_info']['productInfo']['cost'], $_info[$k]['cart_info']['productInfo']['mer_id'], $_info[$k]['cart_info']['productInfo']['cate_id'], $_info[$k]['cart_info']['productInfo']['is_show'], $_info[$k]['cart_info']['productInfo']['store_info'], $_info[$k]['cart_info']['productInfo']['is_del'], $_info[$k]['cart_info']['is_pay'], $_info[$k]['cart_info']['is_del'], $_info[$k]['cart_info']['is_new'], $_info[$k]['cart_info']['add_time'], $_info[$k]['cart_info']['id'], $_info[$k]['cart_info']['uid'], $_info[$k]['cart_info']['product_attr_unique']);
  1725. $_info[$k]['cart_info']['productInfo']['suk'] = '';
  1726. if (isset($v['cart_info']['productInfo']['attrInfo'])) {
  1727. $_info[$k]['cart_info']['productInfo']['image'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['image'];
  1728. $_info[$k]['cart_info']['productInfo']['price'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['price'];
  1729. $_info[$k]['cart_info']['productInfo']['suk'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['suk'];
  1730. unset($_info[$k]['cart_info']['productInfo']['attrInfo']);
  1731. }
  1732. if (!isset($v['cart_info']['productInfo']['ot_price'])) {
  1733. $_info[$k]['cart_info']['productInfo']['ot_price'] = $v['cart_info']['productInfo']['price'];
  1734. }
  1735. }
  1736. $item['_info'] = $_info;
  1737. // if($item['pink_id'] || $item['combination_id']){
  1738. // $pinkStatus = StorePink::where('order_id_key',$item['id'])->value('status');
  1739. // switch ($pinkStatus){
  1740. // case 1:
  1741. // $item['pink_name'] = '[拼团订单]正在进行中';
  1742. // $item['color'] = '#f00';
  1743. // break;
  1744. // case 2:
  1745. // $item['pink_name'] = '[拼团订单]已完成';
  1746. // $item['color'] = '#00f';
  1747. // break;
  1748. // case 3:
  1749. // $item['pink_name'] = '[拼团订单]未完成';
  1750. // $item['color'] = '#f0f';
  1751. // break;
  1752. // default:
  1753. // $item['pink_name'] = '[拼团订单]历史订单';
  1754. // $item['color'] = '#457856';
  1755. // break;
  1756. // }
  1757. // }elseif ($item['seckill_id']){
  1758. // $item['pink_name'] = '[秒杀订单]';
  1759. // $item['color'] = '#32c5e9';
  1760. // }elseif ($item['bargain_id']){
  1761. // $item['pink_name'] = '[砍价订单]';
  1762. // $item['color'] = '#12c5e9';
  1763. // }else{
  1764. // $item['pink_name'] = '[普通订单]';
  1765. // $item['color'] = '#895612';
  1766. // }
  1767. // if($item['paid']==1){
  1768. // switch ($item['pay_type']){
  1769. // case 'weixin':
  1770. // $item['pay_type_name']='微信支付';
  1771. // break;
  1772. // case 'yue':
  1773. // $item['pay_type_name']='余额支付';
  1774. // break;
  1775. // case 'offline':
  1776. // $item['pay_type_name']='线下支付';
  1777. // break;
  1778. // default:
  1779. // $item['pay_type_name']='其他支付';
  1780. // break;
  1781. // }
  1782. // }else{
  1783. // switch ($item['pay_type']){
  1784. // default:
  1785. // $item['pay_type_name']='未支付';
  1786. // break;
  1787. // case 'offline':
  1788. // $item['pay_type_name']='线下支付';
  1789. // $item['pay_type_info']=1;
  1790. // break;
  1791. // }
  1792. // }
  1793. if ($status) {
  1794. $status = [];
  1795. if (!$item['paid'] && $item['pay_type'] == 'offline' && !$item['status'] >= 2) {
  1796. $status['_type'] = 9;
  1797. $status['_title'] = '线下付款';
  1798. $status['_msg'] = '商家处理中,请耐心等待';
  1799. $status['_class'] = 'nobuy';
  1800. } else if (!$item['paid']) {
  1801. $status['_type'] = 0;
  1802. $status['_title'] = '未支付';
  1803. //系统预设取消订单时间段
  1804. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1805. //获取配置
  1806. $systemValue = SystemConfigService::more($keyValue);
  1807. //格式化数据
  1808. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1809. if ($item['pink_id'] || $item['combination_id']) {
  1810. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1811. $time = bcadd(strtotime($item['add_time']), $order_pink_time * 3600, 0);
  1812. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1813. } else if ($item['seckill_id']) {
  1814. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1815. $time = bcadd(strtotime($item['add_time']), $order_seckill_time * 3600, 0);
  1816. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1817. } else if ($item['bargain_id']) {
  1818. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1819. $time = bcadd(strtotime($item['add_time']), $order_bargain_time * 3600, 0);
  1820. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1821. } else {
  1822. $time = bcadd(strtotime($item['add_time']), $systemValue['order_cancel_time'] * 3600, 0);
  1823. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1824. }
  1825. $status['_class'] = 'nobuy';
  1826. } else if ($item['refund_status'] == 1) {
  1827. $status['_type'] = -1;
  1828. $status['_title'] = '申请退款中';
  1829. $status['_msg'] = '商家审核中,请耐心等待';
  1830. $status['_class'] = 'state-sqtk';
  1831. } else if ($item['refund_status'] == 2) {
  1832. $status['_type'] = -2;
  1833. $status['_title'] = '已退款';
  1834. $status['_msg'] = '已为您退款,感谢您的支持';
  1835. $status['_class'] = 'state-sqtk';
  1836. } else if (!$item['status']) {
  1837. if ($item['pink_id']) {
  1838. if (StorePink::where('id', $item['pink_id'])->where('status', 1)->count()) {
  1839. $status['_type'] = 11;
  1840. $status['_title'] = '拼团中';
  1841. $status['_msg'] = '等待其他人参加拼团';
  1842. $status['_class'] = 'state-nfh';
  1843. } else {
  1844. $status['_type'] = 1;
  1845. $status['_title'] = '未发货';
  1846. $status['_msg'] = '商家未发货,请耐心等待';
  1847. $status['_class'] = 'state-nfh';
  1848. }
  1849. } else {
  1850. $status['_type'] = 1;
  1851. $status['_title'] = '未发货';
  1852. $status['_msg'] = '商家未发货,请耐心等待';
  1853. $status['_class'] = 'state-nfh';
  1854. }
  1855. } else if ($item['status'] == 1) {
  1856. if ($item['delivery_type'] == 'send') {//TODO 送货
  1857. $status['_type'] = 2;
  1858. $status['_title'] = '待收货';
  1859. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($item['id'], 'delivery')) . '服务商已送货';
  1860. $status['_class'] = 'state-ysh';
  1861. } else {//TODO 发货
  1862. $status['_type'] = 2;
  1863. $status['_title'] = '待收货';
  1864. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($item['id'], 'delivery_goods')) . '服务商已发货';
  1865. $status['_class'] = 'state-ysh';
  1866. }
  1867. } else if ($item['status'] == 2) {
  1868. $status['_type'] = 3;
  1869. $status['_title'] = '待评价';
  1870. $status['_msg'] = '已收货,快去评价一下吧';
  1871. $status['_class'] = 'state-ypj';
  1872. } else if ($item['status'] == 3) {
  1873. $status['_type'] = 4;
  1874. $status['_title'] = '交易完成';
  1875. $status['_msg'] = '交易完成,感谢您的支持';
  1876. $status['_class'] = 'state-ytk';
  1877. }
  1878. if (isset($item['pay_type']))
  1879. $status['_payType'] = isset(self::$payType[$item['pay_type']]) ? self::$payType[$item['pay_type']] : '其他方式';
  1880. if (isset($item['delivery_type']))
  1881. $status['_deliveryType'] = isset(self::$deliveryType[$item['delivery_type']]) ? self::$deliveryType[$item['delivery_type']] : '其他方式';
  1882. $item['_status'] = $status;
  1883. } else {
  1884. if ($item['paid'] == 0 && $item['status'] == 0) {
  1885. $item['status_name'] = '未支付';
  1886. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  1887. $item['status_name'] = '未发货';
  1888. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  1889. $item['status_name'] = '待收货';
  1890. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  1891. $item['status_name'] = '待评价';
  1892. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  1893. $item['status_name'] = '已完成';
  1894. }
  1895. }
  1896. // unset($item['refund_status']);
  1897. // else if($item['paid']==1 && $item['refund_status']==1){
  1898. // $item['status_name']=<<<HTML
  1899. //<b style="color:#f124c7">申请退款</b><br/>
  1900. //<span>退款原因:{$item['refund_reason_wap']}</span>
  1901. //HTML;
  1902. // }else if($item['paid']==1 && $item['refund_status']==2){
  1903. // $item['status_name']='已退款';
  1904. // }
  1905. // if($item['paid']==0 && $item['status']==0 && $item['refund_status']==0){
  1906. // $item['_status']=1;
  1907. // }else if($item['paid']==1 && $item['status']==0 && $item['refund_status']==0){
  1908. // $item['_status']=2;
  1909. // }else if($item['paid']==1 && $item['refund_status']==1){
  1910. // $item['_status']=3;
  1911. // }else if($item['paid']==1 && $item['status']==1 && $item['refund_status']==0){
  1912. // $item['_status']=4;
  1913. // }else if($item['paid']==1 && $item['status']==2 && $item['refund_status']==0){
  1914. // $item['_status']=5;
  1915. // }else if($item['paid']==1 && $item['status']==3 && $item['refund_status']==0){
  1916. // $item['_status']=6;
  1917. // }else if($item['paid']==1 && $item['refund_status']==2){
  1918. // $item['_status']=7;
  1919. // }
  1920. }
  1921. return $data;
  1922. }
  1923. /**
  1924. * 处理where条件
  1925. * @param $where
  1926. * @param $model
  1927. * @param string $aler
  1928. * @param string $join
  1929. * @return StoreOrder|null
  1930. */
  1931. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  1932. {
  1933. if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler);
  1934. if (isset($where['is_del']) && $where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler . 'is_del', $where['is_del']);
  1935. if (isset($where['combination_id'])) {
  1936. if ($where['combination_id'] == '普通订单') {
  1937. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  1938. }
  1939. if ($where['combination_id'] == '拼团订单') {
  1940. $model = $model->where($aler . 'combination_id', ">", 0)->where($aler . 'pink_id', ">", 0);
  1941. }
  1942. if ($where['combination_id'] == '秒杀订单') {
  1943. $model = $model->where($aler . 'seckill_id', ">", 0);
  1944. }
  1945. if ($where['combination_id'] == '砍价订单') {
  1946. $model = $model->where($aler . 'bargain_id', ">", 0);
  1947. }
  1948. }
  1949. if (isset($where['type'])) {
  1950. switch ($where['type']) {
  1951. case 1:
  1952. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  1953. break;
  1954. case 2:
  1955. $model = $model->where($aler . 'combination_id', ">", 0);
  1956. break;
  1957. case 3:
  1958. $model = $model->where($aler . 'seckill_id', ">", 0);
  1959. break;
  1960. case 4:
  1961. $model = $model->where($aler . 'bargain_id', ">", 0);
  1962. break;
  1963. }
  1964. }
  1965. if (isset($where['real_name']) && $where['real_name'] != '')
  1966. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%");
  1967. if (isset($where['store_id']) && $where['store_id'] > 0) {
  1968. $model = $model->where($aler . 'store_id', "=", $where['store_id']);
  1969. }
  1970. if (isset($where['data']) && $where['data'] !== '') {
  1971. list($startTime, $endTime) = explode(' - ', $where['data']);
  1972. $model = $model->where($aler . 'add_time', '>', $startTime);
  1973. $model = $model->where($aler . 'add_time', '<', date("Y-m-d H:i:s", bcadd(strtotime($endTime), 86400, 0)));
  1974. }
  1975. return $model;
  1976. }
  1977. /**
  1978. * 设置where条件
  1979. * @param $status
  1980. * @param null $model
  1981. * @param string $alert
  1982. * @return StoreOrder|null
  1983. */
  1984. public static function statusWhere($status, $model = null, $alert = '')
  1985. {
  1986. if ($model == null) $model = new self;
  1987. if ('' === $status)
  1988. return $model;
  1989. else if ($status == 0)//未支付
  1990. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  1991. else if ($status == 1)//已支付 未发货
  1992. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  1993. else if ($status == 2)//已支付 待收货
  1994. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  1995. else if ($status == 3)// 已支付 已收货 待评价
  1996. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2)->where($alert . 'refund_status', 0);
  1997. else if ($status == 4)// 交易完成
  1998. return $model->where($alert . 'paid', 1)->where($alert . 'status', 3)->where($alert . 'refund_status', 0);
  1999. else if ($status == -1)//退款中
  2000. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1);
  2001. else if ($status == -2)//已退款
  2002. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2);
  2003. else if ($status == -3)//退款
  2004. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 'in', '1,2');
  2005. else
  2006. return $model;
  2007. }
  2008. /**
  2009. * 订单详情 管理员
  2010. * @param $orderId
  2011. * @param string $field
  2012. * @return array|null|\think\Model
  2013. * @throws \think\db\exception\DataNotFoundException
  2014. * @throws \think\db\exception\ModelNotFoundException
  2015. * @throws \think\exception\DbException
  2016. */
  2017. public static function getAdminOrderDetail($orderId, $field = '*')
  2018. {
  2019. return self::where('order_id', $orderId)->field($field)->find();
  2020. }
  2021. /**
  2022. * 获取指定时间区间的支付金额 管理员
  2023. * @param $start
  2024. * @param $stop
  2025. * @return float
  2026. */
  2027. public static function getOrderTimeBusinessVolumePrice($start, $stop)
  2028. {
  2029. return self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where('add_time', '>=', date("Y-m-d H:i:s", $start))->where('add_time', '<', date("Y-m-d H:i:s", $stop))->sum('pay_price');
  2030. }
  2031. /**
  2032. * 获取指定时间区间的支付订单数量 管理员
  2033. * @param $start
  2034. * @param $stop
  2035. * @return float
  2036. */
  2037. public static function getOrderTimeBusinessVolumeNumber($start, $stop)
  2038. {
  2039. return self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where('add_time', '>=', date("Y-m-d H:i:s", $start))->where('add_time', '<', date("Y-m-d H:i:s", $stop))->count();
  2040. }
  2041. /**
  2042. * 获取当前时间到指定时间的支付金额 管理员
  2043. * @param $start 开始时间
  2044. * @param $stop 结束时间
  2045. * @return mixed
  2046. */
  2047. public static function chartTimePrice($start, $stop)
  2048. {
  2049. $model = new self;
  2050. $model = $model->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time');
  2051. $model = $model->where('is_del', 0);
  2052. $model = $model->where('paid', 1);
  2053. $model = $model->where('refund_status', 0);
  2054. $model = $model->where('add_time', '>=', $start);
  2055. $model = $model->where('add_time', '<', $stop);
  2056. $model = $model->group("TO_DAYS(add_time, '%Y-%m-%d')");
  2057. $model = $model->order('add_time ASC');
  2058. return $model->select();
  2059. }
  2060. /**
  2061. * 获取当前时间到指定时间的支付订单数 管理员
  2062. * @param $start 开始时间
  2063. * @param $stop 结束时间
  2064. * @return mixed
  2065. */
  2066. public static function chartTimeNumber($start, $stop)
  2067. {
  2068. $model = new self;
  2069. $model = $model->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time');
  2070. $model = $model->where('is_del', 0);
  2071. $model = $model->where('paid', 1);
  2072. $model = $model->where('refund_status', 0);
  2073. $model = $model->where('add_time', '>=', date("Y-m-d H:i:s", $start));
  2074. $model = $model->where('add_time', '<', date("Y-m-d H:i:s", $stop));
  2075. $model = $model->group("TO_DAYS(add_time, '%Y-%m-%d')");
  2076. $model = $model->order('add_time ASC');
  2077. return $model->select();
  2078. }
  2079. /**
  2080. * 修改支付方式为线下支付
  2081. * @param $orderId
  2082. * @return bool
  2083. */
  2084. public static function setOrderTypePayOffline($orderId)
  2085. {
  2086. return self::edit(['pay_type' => 'offline'], $orderId, 'order_id');
  2087. }
  2088. /**
  2089. * 线下付款
  2090. * @param $id
  2091. * @return $this
  2092. */
  2093. public static function updateOffline($id)
  2094. {
  2095. $count = self::where('id', $id)->count();
  2096. if (!$count) return self::setErrorInfo('订单不存在');
  2097. $count = self::where('id', $id)->where('paid', 0)->count();
  2098. if (!$count) return self::setErrorInfo('订单已支付');
  2099. $res = self::where('id', $id)->update(['paid' => 1, 'pay_time' => time()]);
  2100. return $res;
  2101. }
  2102. /**
  2103. * 处理订单
  2104. * @param $order
  2105. * @throws \think\db\exception\DataNotFoundException
  2106. * @throws \think\db\exception\DbException
  2107. * @throws \think\db\exception\ModelNotFoundException
  2108. * @throws \think\exception\DbException
  2109. */
  2110. public static function handle_order($order)
  2111. {
  2112. //会员升级
  2113. $newsum = StoreOrderCartInfo::getProductcount($order['id']);
  2114. $cartlist = StoreOrderCartInfo::getProductList($order['id']);
  2115. $user = User::where('uid', $order['uid'])->field('uid,nickname,level,spread_uid')->find();
  2116. $spreaduser = User::where('uid', $order['uid'])->field('uid,nickname,level,spread_uid')->find();
  2117. $upgrade = 0;
  2118. $code_stock_right = 0;
  2119. foreach ($cartlist as $v) {
  2120. if (isset($v['attrInfo']['upgrade'])) {
  2121. $upgrade = bcsub($v['attrInfo']['upgrade'], $upgrade) > 0 ? $v['attrInfo']['upgrade'] : $upgrade;
  2122. }
  2123. if (isset($v['attrInfo']['upgrade'])) {
  2124. $code_stock_right += intval($v['attrInfo']['stock_right']);
  2125. }
  2126. }
  2127. $level = User::where('uid', $order['uid'])->value('level');
  2128. if ($upgrade > $level) {
  2129. UserLevel::setUserLevel($order['uid'], $upgrade);
  2130. if ($upgrade == 6) {
  2131. UserCode::add_code($order['uid'], $cartlist, $newsum);
  2132. if (intval($spreaduser['level']) == 6 || User::where('spread_uid', $spreaduser['uid'])->where('level', 6)->count() > 1) {
  2133. $spreaduser->where('uid', $spreaduser['uid'])->inc('brokerage_price', sys_config('code_recommend', 60000))->update();
  2134. $str = sprintf("推荐%s用户升级为总代,获得推荐奖:%d", $user['nickname'], sys_config('code_recommend', 60000));
  2135. UserBill::income('总代推荐奖', $spreaduser['uid'], 'now_money', 'brokerage', sys_config('code_recommend', 60000), $order['id'], bcadd($spreaduser['brokerage_price'], sys_config('code_recommend', 60000), 0), $str, 1);
  2136. }
  2137. } elseif ($upgrade == 5) {
  2138. User::where('uid', $v['uid'])->setField('give_stock_right', 20);
  2139. }
  2140. }
  2141. //处理团队单数
  2142. User::where('uid', $user['uid'])->inc('team_cts', $newsum);
  2143. User::where('uid', $spreaduser['uid'])->inc('team_cts', $newsum);
  2144. UserLevel::setLevelComplete($spreaduser['uid']);
  2145. if (intval($spreaduser['level']) == 5 && intval($spreaduser['give_stock_right']) == 0) {
  2146. User::where('uid', $spreaduser['uid'])->setField('give_stock_right', 10);
  2147. }
  2148. //处理极差
  2149. $users = User::field('uid,level,spread_uid')->select();
  2150. $rs = UtilService::getparentid($users, $spreaduser['uid']);
  2151. $levels = SystemUserLevel::where('is_del', 0)->where('is_show', 1)->field('id,name,range_award')->select();
  2152. $levela = [];
  2153. $current_level = $spreaduser['level'];
  2154. $system_range = sys_config('system_range');
  2155. $range_award = 0;
  2156. foreach ($levels as $v) {
  2157. $levela[$v['id']]['bl'] = bcsub($v['id'], $spreaduser['level'], 0) < 0 ? true : false;
  2158. $levela[$v['id']]['sbl'] = true;
  2159. $levela[$v['id']]['s_range'] = sys_config('PeerAward', 1);
  2160. $levela[$v['id']]['range_award'] = $v['range_award'];
  2161. $levela[$v['id']]['use'] = 0;
  2162. $levela[$v['id']]['team_cts'] = 0;
  2163. }
  2164. foreach ($rs as $v) {
  2165. if ($v['level'] >= $current_level) {
  2166. //处理第一次
  2167. if ($levela[$v['level']]['use'] == 0) {
  2168. $level_award = $range_award == 0 ? $levela[$v['level']]['range_award'] : bcsub($levela[$v['level']]['range_award'], $range_award, 0);
  2169. $levela[$v['level']]['use'] = 1;
  2170. $levela[$v['id']]['team_cts'] = $v['team_cts'];
  2171. $range_award = $levela[$v['level']]['range_award'];
  2172. } else {
  2173. if ($levela[$v['id']]['sbl'] && bcsub($levela[$v['id']]['team_cts'], $v['team_cts'])) {
  2174. $level_award = $levela[$v['level']]['s_range'];
  2175. $levela[$v['id']]['sbl'] = false;
  2176. $range_award = bcadd($range_award, $level_award, 0);
  2177. }
  2178. }
  2179. if ($v['level'] > $current_level) {
  2180. $current_level = $v['level'];
  2181. } else {
  2182. if ($v['level'] == $current_level && $levela[$v['id']]['sbl'] == 1) {
  2183. $current_level++;
  2184. }
  2185. }
  2186. $proportion = bcmul(bcsub($order['total_price'], $order['cost'], 2), bcdiv($level_award, 100, 2), 2);
  2187. $mark = "订单号" . $order['order_id'] . '产生极差奖,奖励推广佣金' . $proportion;
  2188. //TODO 添加推广记录
  2189. $res1 = UserBill::income('极差奖', $v['uid'], 'now_money', 'brokerage', $proportion, $order['id'], $proportion, $mark);
  2190. //TODO 添加用户余额
  2191. $res2 = User::where('uid', $v['uid'])->inc('brokerage', $proportion)->update();
  2192. }
  2193. }
  2194. }
  2195. /**
  2196. * 向创建订单10分钟未付款的用户发送短信
  2197. */
  2198. public static function sendTen()
  2199. {
  2200. $switch = sys_config('unpaid_order_switch') ? true : false;
  2201. if ($switch) {
  2202. $list = self::where('paid', 0)
  2203. ->where('is_del', 0)
  2204. ->where('is_system_del', 0)
  2205. ->where('add_time', 'exp', 'DATE_SUB()')
  2206. ->where('add_time', '<', time() - 600)
  2207. ->field('order_id,user_phone')
  2208. ->select();
  2209. if ($list) {
  2210. $list = $list->toArray();
  2211. foreach ($list as $phone) {
  2212. ShortLetterRepositories::send(true, $phone['user_phone'], ['order_id' => $phone['order_id']], 'ORDER_PAY_FALSE');
  2213. }
  2214. }
  2215. }
  2216. }
  2217. public function productInfo()
  2218. {
  2219. return $this->hasMany(StoreProductReply::class, 'oid', 'id');
  2220. }
  2221. public static function setOrderProductReplyWhere($where)
  2222. {
  2223. $model = self::where('status', 3)->order('add_time desc')->whereIn('id', function ($query) {
  2224. $query->name('store_order')->alias('o')->join('store_product_reply a', 'a.oid = o.id')->group('o.id')->field('o.id')->select();
  2225. })->with('productInfo', function ($query) use ($where) {
  2226. $alias = '';
  2227. if (isset($where['title']) && $where['title'] != '')
  2228. $query->where("{$alias}comment", 'LIKE', "%$where[title]%");
  2229. if (isset($where['is_reply']) && $where['is_reply'] != '') {
  2230. if ($where['is_reply'] >= 0) {
  2231. $query->where("{$alias}is_reply", $where['is_reply']);
  2232. } else {
  2233. $query->where("{$alias}is_reply", '>', 0);
  2234. }
  2235. }
  2236. if (isset($where['producr_id']) && $where['producr_id'] != 0)
  2237. $query->where($alias . 'product_id', $where['producr_id']);
  2238. $query->where("{$alias}is_del", 0);
  2239. });
  2240. return $model;
  2241. }
  2242. public static function getOrderProductReplyList($where)
  2243. {
  2244. $list = self::setOrderProductReplyWhere($where)->page((int)$where['message_page'], (int)$where['limit'])->select();
  2245. $list = count($list) ? $list->toArray() : [];
  2246. foreach ($list as $key => $item) {
  2247. if (isset($item['productInfo']) && is_array($item['productInfo']) && count($item['productInfo'])) {
  2248. foreach ($item['productInfo'] as $k => $v) {
  2249. if (!$v['nickname'] && $v['uid']) {
  2250. $v['nickname'] = User::where('uid', $v['uid'])->value('nickname');
  2251. $v['avatar'] = User::where('uid', $v['uid'])->value('avatar');
  2252. }
  2253. $v['image'] = '';
  2254. $v['store_name'] = '';
  2255. if ($v['product_id']) {
  2256. $product = StoreProduct::where('id', $v['product_id'])->field(['image', 'store_name'])->find();
  2257. if ($product) {
  2258. $v['image'] = $product['image'];
  2259. $v['store_name'] = $product['store_name'];
  2260. }
  2261. }
  2262. $list[$key]['productInfo'][$k] = $v;
  2263. }
  2264. }
  2265. }
  2266. $count = self::setOrderProductReplyWhere($where)->count();
  2267. return compact('list', 'count');
  2268. }
  2269. /**
  2270. * 验证加入购物车、生成订单时商品得库存以及是否能购买情况
  2271. * @param $uid
  2272. * @param $product_id
  2273. * @param int $cart_num
  2274. * @param string $product_attr_unique
  2275. * @param int $combination_id
  2276. * @param int $seckill_id
  2277. * @param int $bargain_id
  2278. * @return bool
  2279. * @throws \think\db\exception\DataNotFoundException
  2280. * @throws \think\db\exception\DbException
  2281. * @throws \think\db\exception\ModelNotFoundException
  2282. */
  2283. public static function checkProductStock($uid, $product_id, $cart_num = 1, $product_attr_unique = '', $combination_id = 0, $seckill_id = 0, $bargain_id = 0)
  2284. {
  2285. if ($cart_num < 1) $cart_num = 1;
  2286. if (!$product_attr_unique) return self::setErrorInfo('请选择商品属性');
  2287. if ($seckill_id) {
  2288. if (!StoreSeckill::isSeckillEnd($seckill_id))
  2289. return self::setErrorInfo('活动已结束');
  2290. $StoreSeckillinfo = StoreSeckill::getValidProduct($seckill_id);
  2291. if (!$StoreSeckillinfo)
  2292. return self::setErrorInfo('该产品已下架或删除');
  2293. $userbuycount = StoreOrder::where('uid', $uid)->where('paid', 1)->where('seckill_id', $seckill_id)->sum('total_num');
  2294. if ($StoreSeckillinfo['num'] <= $userbuycount || $StoreSeckillinfo['num'] < $cart_num)
  2295. return self::setErrorInfo('每人限购' . $StoreSeckillinfo['num'] . '件');
  2296. $res = StoreProductAttrValue::where('product_id', $seckill_id)->where('unique', $product_attr_unique)->where('type', 1)->field('suk,quota')->find();
  2297. if ($cart_num > $res['quota'])
  2298. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2299. $product_stock = StoreProductAttrValue::where('product_id', $StoreSeckillinfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  2300. if ($product_stock < $cart_num)
  2301. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2302. } elseif ($bargain_id) {
  2303. if (!StoreBargain::validBargain($bargain_id))
  2304. return self::setErrorInfo('该产品已下架或删除');
  2305. $StoreBargainInfo = StoreBargain::getBargain($bargain_id);
  2306. $res = StoreProductAttrValue::where('product_id', $bargain_id)->where('type', 2)->field('suk,quota')->find();
  2307. if ($cart_num > $res['quota'])
  2308. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2309. $product_stock = StoreProductAttrValue::where('product_id', $StoreBargainInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  2310. if ($product_stock < $cart_num)
  2311. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2312. } elseif ($combination_id) {//拼团
  2313. $StoreCombinationInfo = StoreCombination::getCombinationOne($combination_id);
  2314. if (!$StoreCombinationInfo)
  2315. return self::setErrorInfo('该产品已下架或删除');
  2316. $userbuycount = StoreOrder::where('uid', $uid)->where('paid', 1)->where('combination_id', $combination_id)->count();
  2317. if ($StoreCombinationInfo['num'] <= $userbuycount || $StoreCombinationInfo['num'] < $cart_num)
  2318. return self::setErrorInfo('每人限购' . $StoreCombinationInfo['num'] . '件');
  2319. $res = StoreProductAttrValue::where('product_id', $combination_id)->where('unique', $product_attr_unique)->where('type', 3)->field('suk,quota')->find();
  2320. if ($cart_num > $res['quota'])
  2321. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2322. $product_stock = StoreProductAttrValue::where('product_id', $StoreCombinationInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  2323. if ($product_stock < $cart_num)
  2324. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2325. } else {
  2326. if (!StoreProduct::isValidProduct($product_id))
  2327. return self::setErrorInfo('该产品已下架或删除');
  2328. if (!StoreProductAttr::issetProductUnique($product_id, $product_attr_unique))
  2329. return self::setErrorInfo('请选择有效的产品属性');
  2330. if (StoreProduct::getProductStock($product_id, $product_attr_unique) < $cart_num)
  2331. return self::setErrorInfo('该产品库存不足' . $cart_num);
  2332. }
  2333. return true;
  2334. }
  2335. public static function do_jialie()
  2336. {
  2337. $rs = self::where('paid', 0)->where('jialie_do', 1)->where('jialie_count', '<', 31)->select()->toArray();
  2338. try {
  2339. foreach ($rs as $v) {
  2340. self::edit(['jialie_do' => 2], $v['id']);
  2341. if (empty($v['re_order_id'])) {
  2342. $res = JiaLieService::initialize()::paymentquery(JiaLieService::QUERY, ['out_trade_no' => $v['order_id'], 'nonce_str' => uniqid() . mt_rand(1000, 9999)]);
  2343. } else {
  2344. $res = JiaLieService::initialize()::paymentquery(JiaLieService::QUERY, ['out_trade_no' => $v['re_order_id'], 'nonce_str' => uniqid() . mt_rand(1000, 9999)]);
  2345. }
  2346. if ($res['ret_code'] == 0 && $res['status'] == 2) {
  2347. self::paySuccess($v['order_id'], 'jialie');
  2348. self::edit(['jialie_do' => 0, 'is_del' => 0, 'status' => 2], $v['id']);
  2349. OrderRepository::storeProductOrderTakeDeliveryAdmin($v);
  2350. StoreOrderStatus::status($v['id'], 'take_delivery', '已核销');
  2351. } else {
  2352. self::where('id', $v['id'])->inc('jialie_count', 1)->update();
  2353. self::edit(['jialie_do' => 1], $v['id']);
  2354. }
  2355. }
  2356. } catch (Exception $e) {
  2357. @file_put_contents("jialie.txt", $e->getLine() . '--' . $e->getMessage() . "-end\r\n", 8);
  2358. }
  2359. return true;
  2360. }
  2361. public static function sendGroupAward($order): bool
  2362. {
  2363. // return true;
  2364. //TODO 营销产品不返佣金
  2365. if (isset($order['combination_id']) && $order['combination_id']) return true;
  2366. if (isset($order['seckill_id']) && $order['seckill_id']) return true;
  2367. if (isset($order['bargain_id']) && $order['bargain_id']) return true;
  2368. $userInfo = User::getUserInfo($order['uid']);
  2369. $spread = User::getUserInfo($userInfo['spread_uid']);
  2370. if (StoreOrder::where('uid', $order['uid'])->where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('is_system_del', 0)
  2371. ->where('id', '<>', $order['id'])->where('suit', '>', 0)->find()) {
  2372. $spread = $userInfo;
  2373. }
  2374. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->column('cart_info');
  2375. $suit = 0;
  2376. $suit_exit = 0;
  2377. $suit_award = 0;
  2378. $suit_price = 0;
  2379. $extra_price = 0;
  2380. foreach ($cartInfo as $value) {
  2381. $product = json_decode($value, true);
  2382. if (isset($product['productInfo'])) {
  2383. $productInfo = $product['productInfo'];
  2384. if (($productInfo['attrInfo']['brokerage_suit'] ?? 0) > 0) {
  2385. $suit_award += $product['productInfo']['attrInfo']['brokerage_suit'] * $product['cart_num'];
  2386. $suit_price += ($productInfo['attrInfo']['price'] ?? $product['price']) * $product['cart_num'];
  2387. if ($product['is_suit']) $suit_exit += $product['cart_num'] ?? 0;
  2388. }
  2389. if ($product['is_suit']) {
  2390. $suit += $product['cart_num'] ?? 0;
  2391. $suit_price += ($productInfo['attrInfo']['price'] ?? $product['price']) * $product['cart_num'];
  2392. }
  2393. if (!$product['is_award']) {
  2394. $extra_price += ($productInfo['attrInfo']['price'] ?? $product['price']) * $product['cart_num'];
  2395. }
  2396. }
  2397. }
  2398. $res = true;
  2399. $suit_recommend = sys_config('suit_recommend', 0, true);
  2400. if ($spread && ($suit_recommend || $suit_award > 0)) {
  2401. //推荐奖
  2402. $brokeragePrice = bcmul($suit_recommend, $suit - $suit_exit, 2);
  2403. $brokeragePrice = bcadd($suit_award, $brokeragePrice, 2);
  2404. if ($brokeragePrice > 0) {
  2405. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得推荐奖励' . $brokeragePrice;
  2406. $res = $res && UserBill::income('套装推荐奖', $spread['uid'], 'now_money', 'brokerage', $brokeragePrice, $order['id'], bcadd($spread['brokerage_price'], $brokeragePrice, 2), $mark);
  2407. $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $brokeragePrice)->update();
  2408. $spread['brokerage_price'] = bcadd($spread['brokerage_price'], $brokeragePrice, 2);
  2409. }
  2410. }
  2411. $send = 0;
  2412. $send_normal = 0;
  2413. while ($spread) {
  2414. $group_level = SystemGroupLevel::get($spread['group_level']);
  2415. $award_level = SystemGroupLevel::get($spread['award_level']);
  2416. if ($suit) {
  2417. $award_group = $group_level['suit_award'] ?? 0;
  2418. $award_award = $award_level['suit_award'] ?? 0;
  2419. $big = $award_group > $award_award ? $group_level['id'] : $award_level['id'];
  2420. $brokeragePrice_a = bcmul($award_award, $suit, 2);
  2421. $brokeragePrice_g = bcmul($award_group, $suit, 2);
  2422. $brokeragePrice_big = $brokeragePrice_a > $brokeragePrice_g ? $brokeragePrice_a : $brokeragePrice_g;
  2423. $brokeragePrice_small = $brokeragePrice_a > $brokeragePrice_g ? $brokeragePrice_g : $brokeragePrice_a;
  2424. if ($brokeragePrice_big == $brokeragePrice_small) {
  2425. $brokeragePrice = $brokeragePrice_big;
  2426. if ($brokeragePrice > $send) {
  2427. $real_send = bcsub($brokeragePrice, $send, 2);
  2428. if ($real_send > 0) {
  2429. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得极差奖励' . $real_send;
  2430. $res = $res && UserBill::income('套装极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], bcadd($spread['brokerage_price'], $real_send, 2), $mark);
  2431. $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $real_send)->update();
  2432. $send = $brokeragePrice;
  2433. }
  2434. }
  2435. } else {
  2436. $brokeragePrice = $brokeragePrice_small;
  2437. if ($brokeragePrice > $send) {
  2438. $real_send = bcsub($brokeragePrice, $send, 2);
  2439. if ($real_send > 0) {
  2440. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得极差奖励' . $real_send;
  2441. $res = $res && UserBill::income('套装极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], bcadd($spread['brokerage_price'], $real_send, 2), $mark);
  2442. $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $real_send)->update();
  2443. $send = $brokeragePrice;
  2444. }
  2445. }
  2446. $brokeragePrice = $brokeragePrice_big;
  2447. if ($brokeragePrice > $send) {
  2448. $real_send = bcsub($brokeragePrice, $send, 2);
  2449. if ($real_send > 0) {
  2450. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得极差奖励' . $real_send;
  2451. $res = $res && UserBill::income('套装极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], bcadd($spread['brokerage_price'], $real_send, 2), $mark, 0, $big);
  2452. $send = $brokeragePrice;
  2453. }
  2454. }
  2455. }
  2456. // if ($group_level) {
  2457. // if (!$award_level) {
  2458. // $award = SystemGroupLevel::min('suit_award');
  2459. // $brokeragePrice = bcmul($award, $suit, 2);
  2460. // if ($brokeragePrice > $send) {
  2461. // $real_send = bcsub($brokeragePrice, $send, 2);
  2462. // $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得极差奖励' . $real_send;
  2463. // $res = $res && UserBill::income('套装极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], $spread['brokerage_price'], $mark, 0);
  2464. // $send = $brokeragePrice;
  2465. // }
  2466. // } else {
  2467. // $award = $group_level['suit_award'] > $award_level['suit_award'] ? $award_level['suit_award'] : $group_level['suit_award'];
  2468. // $brokeragePrice = bcmul($award, $suit, 2);
  2469. // if ($brokeragePrice > $send) {
  2470. // $real_send = bcsub($brokeragePrice, $send, 2);
  2471. // if ($real_send > 0) {
  2472. // $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得极差奖励' . $real_send;
  2473. // $res = $res && UserBill::income('套装极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], bcadd($spread['brokerage_price'], $real_send, 2), $mark);
  2474. // $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $real_send)->update();
  2475. // $send = $brokeragePrice;
  2476. // }
  2477. // }
  2478. // }
  2479. // }
  2480. }
  2481. // if ($group_level) {
  2482. if (!$group_level) {
  2483. $group_award_basic = sys_config('group_award_basic', 0);
  2484. $integral_group_award_basic = sys_config('integral_group_award_basic', 0);
  2485. $brokeragePrice = bcmul(bcdiv($group_award_basic, 100, 4), bcsub(bcsub($order['pay_price'], $suit_price, 2), $extra_price, 2), 2);
  2486. if ($order['use_integral'] > 0) $brokeragePrice = bcmul(bcdiv($integral_group_award_basic, 100, 4), bcsub(bcsub($order['pay_price'], $suit_price, 2), $extra_price, 2), 2);
  2487. } else {
  2488. $brokeragePrice = bcmul(bcdiv($group_level['group_award'], 100, 4), bcsub(bcsub($order['pay_price'], $suit_price, 2), $extra_price, 2), 2);
  2489. if ($order['use_integral'] > 0) $brokeragePrice = bcmul(bcdiv($group_level['integral_group_award'], 100, 4), bcsub(bcsub($order['pay_price'], $suit_price, 2), $extra_price, 2), 2);
  2490. }
  2491. if ($brokeragePrice > $send_normal) {
  2492. $real_send = bcsub($brokeragePrice, $send_normal, 2);
  2493. if ($real_send > 0) {
  2494. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买' . ($order['use_integral'] > 0 ? '积分' : '普通') . '商品,获得极差奖励' . $real_send;
  2495. $res = $res && UserBill::income('极差奖', $spread['uid'], 'now_money', 'brokerage', $real_send, $order['id'], bcadd($spread['brokerage_price'], $real_send, 2), $mark);
  2496. $res = $res && User::where('uid', $spread['uid'])->inc('brokerage_price', $real_send)->update();
  2497. $send_normal = $brokeragePrice;
  2498. }
  2499. }
  2500. // }
  2501. $spread = User::getUserInfo($spread['spread_uid']);
  2502. }
  2503. return $res;
  2504. }
  2505. public static function sendAllSystemAward($order): bool
  2506. {
  2507. // return true;
  2508. //TODO 营销产品不返佣金
  2509. if (isset($order['combination_id']) && $order['combination_id']) return true;
  2510. if (isset($order['seckill_id']) && $order['seckill_id']) return true;
  2511. if (isset($order['bargain_id']) && $order['bargain_id']) return true;
  2512. $userInfo = User::getUserInfo($order['uid']);
  2513. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->column('cart_info');
  2514. $suit = 0;
  2515. $suit_price = 0;
  2516. foreach ($cartInfo as $value) {
  2517. $product = json_decode($value, true);
  2518. if (isset($product['productInfo'])) {
  2519. $productInfo = $product['productInfo'];
  2520. if ($product['is_suit']) {
  2521. $suit += $product['cart_num'] ?? 0;
  2522. $suit_price += ($productInfo['attrInfo']['price'] ?? $product['price']) * $product['cart_num'];
  2523. }
  2524. }
  2525. }
  2526. $levels = SystemGroupLevel::where('system_award', 1)->column('id');
  2527. $levels2 = SystemAwardLevel::where('system_award', 1)->column('id');
  2528. if (count($levels) && count($levels2)) {
  2529. $users = User::where('group_level', 'in', $levels)->whereOr('award_level', 'in', $levels2)
  2530. ->select();
  2531. } else if (count($levels)) {
  2532. $users = User::where('group_level', 'in', $levels)
  2533. ->select();
  2534. } else if (count($levels2)) {
  2535. $users = User::where('award_level', 'in', $levels2)
  2536. ->select();
  2537. } else {
  2538. $users = [];
  2539. }
  2540. $res = true;
  2541. $award = bcdiv(bcmul(100, $suit, 2), count($users) ?: 1, 2);
  2542. if ($suit && $award && count($users)) {
  2543. foreach ($users as $v) {
  2544. $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '购买套装商品,获得董事分红' . $award;
  2545. $res = $res && UserBill::income('董事加权分红', $v['uid'], 'now_money', 'brokerage', $award, $order['id'], bcadd($v['brokerage_price'], $award, 2), $mark);
  2546. $res = $res && User::where('uid', $v['uid'])->inc('brokerage_price', $award)->update();
  2547. }
  2548. }
  2549. return $res;
  2550. }
  2551. public static function sendSuit($order): bool
  2552. {
  2553. // return true;
  2554. //TODO 营销产品不返佣金
  2555. if (isset($order['combination_id']) && $order['combination_id']) return true;
  2556. if (isset($order['seckill_id']) && $order['seckill_id']) return true;
  2557. if (isset($order['bargain_id']) && $order['bargain_id']) return true;
  2558. $userInfo = User::getUserInfo($order['uid']);
  2559. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->column('cart_info');
  2560. $suit = 0;
  2561. foreach ($cartInfo as $value) {
  2562. $product = json_decode($value, true);
  2563. if (isset($product['productInfo'])) {
  2564. if ($product['is_suit']) {
  2565. $suit += $product['cart_num'] ?? 0;
  2566. }
  2567. }
  2568. }
  2569. $res = true;
  2570. $suit_give_integral = sys_config('suit_give_integral', 0, true);
  2571. $suit_give_gf = sys_config('suit_give_gf', 0, true);
  2572. if ($suit_give_integral > 0) {
  2573. //获得积分
  2574. $brokeragePrice = bcmul($suit_give_integral, $suit, 2);
  2575. if ($brokeragePrice > 0) {
  2576. $mark = '购买套装商品,获得积分' . $brokeragePrice;
  2577. $res = $res && UserBill::income('购买套装赠送', $userInfo['uid'], 'integral', 'suit_gain', $brokeragePrice, $order['id'], bcadd($userInfo['integral'], $brokeragePrice, 2), $mark);
  2578. $res = $res && User::where('uid', $userInfo['uid'])->inc('integral', $brokeragePrice)->update();
  2579. }
  2580. }
  2581. // if ($suit_give_gf > 0 && SystemStoreMember::be(['uid' => $userInfo['uid']])) {
  2582. // //获得股份
  2583. // $brokeragePrice = bcmul($suit_give_gf, $suit, 2);
  2584. // if ($brokeragePrice > 0) {
  2585. // $res = $res && SystemStoreMember::where('uid', $userInfo['uid'])->inc('consume_rights', $brokeragePrice)->update();
  2586. // }
  2587. // }
  2588. return $res;
  2589. }
  2590. }