StoreOrder.php 132 KB

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