StoreOrder.php 110 KB

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