StoreOrderServices.php 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\jobs\AutoCommentJob;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\activity\coupon\StoreCouponUserServices;
  16. use app\services\activity\seckill\StoreSeckillServices;
  17. use app\services\BaseServices;
  18. use app\services\other\PosterServices;
  19. use app\services\other\QrcodeServices;
  20. use app\services\other\UploadService;
  21. use app\services\pay\OrderPayServices;
  22. use app\services\pay\PayServices;
  23. use app\services\product\product\StoreProductLogServices;
  24. use app\services\product\product\StoreProductServices;
  25. use app\services\serve\ServeServices;
  26. use app\services\system\attachment\SystemAttachmentServices;
  27. use app\services\system\store\SystemStoreServices;
  28. use app\services\system\SystemTicketServices;
  29. use app\services\user\UserInvoiceServices;
  30. use app\services\user\UserRechargeServices;
  31. use app\services\user\UserServices;
  32. use app\services\product\product\StoreProductReplyServices;
  33. use app\services\user\UserAddressServices;
  34. use app\services\user\UserBillServices;
  35. use app\services\user\UserLevelServices;
  36. use app\services\wechat\WechatUserServices;
  37. use crmeb\exceptions\AdminException;
  38. use crmeb\exceptions\ApiException;
  39. use crmeb\exceptions\PayException;
  40. use crmeb\services\app\MiniProgramService;
  41. use crmeb\services\CacheService;
  42. use crmeb\services\easywechat\orderShipping\MiniOrderService;
  43. use crmeb\services\FormBuilder as Form;
  44. use crmeb\services\printer\Printer;
  45. use crmeb\services\SystemConfigService;
  46. use crmeb\utils\Arr;
  47. use Guzzle\Http\EntityBody;
  48. use think\facade\Log;
  49. /**
  50. * Class StoreOrderServices
  51. * @package app\services\order
  52. * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
  53. * @method StoreOrderDao getUserOrderDetail(string $key, int $uid, array $with) 获取订单详情
  54. * @method chartTimePrice($start, $stop) 获取当前时间到指定时间的支付金额 管理员
  55. * @method chartTimeNumber($start, $stop) 获取当前时间到指定时间的支付订单数 管理员
  56. * @method together(array $where, string $field, string $together = 'sum') 聚合查询
  57. * @method getBuyCount($uid, $type, $typeId) 获取用户已购买此活动商品的个数
  58. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  59. * @method getTrendData($time, $type, $timeType, $str) 用户趋势
  60. * @method getRegion($time, $channelType) 地域统计
  61. * @method getProductTrend($time, $timeType, $field, $str) 商品趋势
  62. * @method getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  63. */
  64. class StoreOrderServices extends BaseServices
  65. {
  66. /**
  67. * 发货类型
  68. * @var string[]
  69. */
  70. public $deliveryType = [
  71. 'send' => '商家配送',
  72. 'express' => '快递配送',
  73. 'fictitious' => '虚拟发货',
  74. 'delivery_part_split' => '拆分部分发货',
  75. 'delivery_split' => '拆分发货完成'
  76. ];
  77. /**
  78. * StoreOrderProductServices constructor.
  79. * @param StoreOrderDao $dao
  80. */
  81. public function __construct(StoreOrderDao $dao)
  82. {
  83. $this->dao = $dao;
  84. }
  85. /**
  86. * 获取列表
  87. * @param array $where
  88. * @param array $field
  89. * @param array $with
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function getOrderList(array $where, array $field = ['*'], array $with = [])
  96. {
  97. [$page, $limit] = $this->getPageValue();
  98. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  99. $count = $this->dao->count($where, false);
  100. $data = $this->tidyOrderList($data);
  101. foreach ($data as &$item) {
  102. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  103. $cart_num = 0;
  104. $vipTruePrice = 0;
  105. foreach ($item['_info'] as $items) {
  106. $cart_num += $items['cart_info']['cart_num'];
  107. $vipTruePrice = bcadd((string)$vipTruePrice, bcmul((string)$items['cart_info']['vip_truePrice'], (string)$items['cart_info']['cart_num'], 2), 2);
  108. }
  109. $item['total_price'] = bcadd($item['total_price'], $vipTruePrice, 2);
  110. $item['is_all_refund'] = $refund_num == $cart_num;
  111. $item['pay_price'] = (float)$item['pay_price'];
  112. }
  113. return compact('data', 'count');
  114. }
  115. /**
  116. * 前端订单列表
  117. * @param array $where
  118. * @param array|string[] $field
  119. * @param array $with
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getOrderApiList(array $where, array $field = ['*'], array $with = [])
  126. {
  127. [$page, $limit] = $this->getPageValue();
  128. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  129. foreach ($data as &$item) {
  130. $item = $this->tidyOrder($item, true);
  131. foreach ($item['cartInfo'] ?: [] as $key => $product) {
  132. if ($item['_status']['_type'] == 3) {
  133. $item['cartInfo'][$key]['add_time'] = isset($product['add_time']) ? date('Y-m-d H:i', (int)$product['add_time']) : '时间错误';
  134. }
  135. $item['cartInfo'][$key]['productInfo']['price'] = $product['truePrice'] ?? 0;
  136. }
  137. if (count($item['refund'])) {
  138. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  139. $cart_num = array_sum(array_column($item['cartInfo'], 'cart_num'));
  140. $item['is_all_refund'] = $refund_num == $cart_num ? true : false;
  141. } else {
  142. $item['is_all_refund'] = false;
  143. }
  144. }
  145. return $data;
  146. }
  147. /**
  148. * 获取订单数量
  149. * @param int $uid
  150. * @return array
  151. * @throws \ReflectionException
  152. */
  153. public function getOrderData(int $uid = 0)
  154. {
  155. $data['order_count'] = (string)$this->dao->count(['uid' => $uid, 'refund_status' => [0, 3], 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0]);
  156. $data['sum_price'] = (string)$this->dao->sum([
  157. ['uid', '=', $uid],
  158. ['paid', '=', 1],
  159. ['refund_status', '=', 0],
  160. ['pid', '>=', 0]
  161. ], 'pay_price', false);
  162. $countWhere = ['is_del' => 0, 'is_system_del' => 0];
  163. if ($uid) {
  164. $countWhere['uid'] = $uid;
  165. }
  166. $data['unpaid_count'] = (string)$this->dao->count(['status' => 0] + $countWhere);
  167. $data['unshipped_count'] = (string)$this->dao->count(['status' => 1] + $countWhere + ['pid' => 0]);
  168. $data['received_count'] = (string)$this->dao->count(['status' => 2] + $countWhere + ['pid' => 0]);
  169. $data['evaluated_count'] = (string)$this->dao->count(['status' => 3] + $countWhere + ['pid' => 0]);
  170. $data['complete_count'] = (string)$this->dao->count(['status' => 4] + $countWhere + ['pid' => 0]);
  171. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  172. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  173. $refund_where = ['is_cancel' => 0];
  174. if ($uid) $refund_where['uid'] = $uid;
  175. $data['refunding_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => [1, 2, 4, 5]]);
  176. $data['no_refund_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => 3]);
  177. $data['refunded_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => 6]);
  178. $data['refund_count'] = bcadd(bcadd($data['refunding_count'], $data['refunded_count'], 0), $data['no_refund_count'], 0);
  179. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  180. $data['pc_order_count'] = $data['order_count'] + $data['refunding_count'] + $data['refunded_count'];
  181. $data['pay_weixin_open'] = sys_config('pay_weixin_open', '0') != '0';//微信支付 1 开启 0 关闭
  182. $data['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  183. $data['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  184. return $data;
  185. }
  186. /**
  187. * 订单详情数据格式化
  188. * @param $order
  189. * @param bool $detail 是否需要订单商品详情
  190. * @param bool $isPic 是否需要订单状态图片
  191. * @return mixed
  192. */
  193. public function tidyOrder($order, bool $detail = false, $isPic = false)
  194. {
  195. if ($detail == true && isset($order['id'])) {
  196. /** @var StoreOrderCartInfoServices $cartServices */
  197. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  198. $cartInfos = $cartServices->getCartColunm(['oid' => $order['id']], 'cart_num,surplus_num,cart_info,refund_num', 'unique');
  199. $info = [];
  200. /** @var StoreProductReplyServices $replyServices */
  201. $replyServices = app()->make(StoreProductReplyServices::class);
  202. foreach ($cartInfos as $k => $cartInfo) {
  203. $cart = json_decode($cartInfo['cart_info'], true);
  204. $cart['cart_num'] = $cartInfo['cart_num'];
  205. $cart['surplus_num'] = $cartInfo['surplus_num'];
  206. $cart['refund_num'] = $cartInfo['refund_num'];
  207. $cart['surplus_refund_num'] = $cartInfo['surplus_num'] - $cartInfo['refund_num'];
  208. $cart['unique'] = $k;
  209. //新增是否评价字段
  210. $cart['is_reply'] = $replyServices->count(['unique' => $k]);
  211. if (isset($cart['productInfo']['attrInfo'])) {
  212. $cart['productInfo']['attrInfo'] = get_thumb_water($cart['productInfo']['attrInfo']);
  213. }
  214. $cart['productInfo'] = get_thumb_water($cart['productInfo']);
  215. //一种商品买多件 计算总优惠
  216. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ? $cart['cart_num'] : 1, 2);
  217. $cart['is_valid'] = 1;
  218. array_push($info, $cart);
  219. unset($cart);
  220. }
  221. $order['cartInfo'] = $info;
  222. }
  223. /** @var StoreOrderStatusServices $statusServices */
  224. $statusServices = app()->make(StoreOrderStatusServices::class);
  225. $status = [];
  226. if ($order['is_cancel']) {
  227. $status['_type'] = 4;
  228. $status['_title'] = '已取消';
  229. $status['_msg'] = '您已取消订单,感谢您的使用';
  230. $status['_class'] = 'nobuy';
  231. } else {
  232. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  233. $status['_type'] = 9;
  234. $status['_title'] = '线下付款,未支付';
  235. $status['_msg'] = '等待商家处理,请耐心等待';
  236. $status['_class'] = 'nobuy';
  237. } else if (!$order['paid']) {
  238. $status['_type'] = 0;
  239. $status['_title'] = '未支付';
  240. //系统预设取消订单时间段
  241. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  242. //获取配置
  243. $systemValue = SystemConfigService::more($keyValue);
  244. //格式化数据
  245. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  246. if ($order['pink_id'] || $order['combination_id']) {
  247. $order_pink_time = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  248. $time = $order['add_time'] + $order_pink_time * 3600;
  249. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  250. } else if ($order['seckill_id']) {
  251. $order_seckill_time = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  252. $time = $order['add_time'] + $order_seckill_time * 3600;
  253. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  254. } else if ($order['bargain_id']) {
  255. $order_bargain_time = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  256. $time = $order['add_time'] + $order_bargain_time * 3600;
  257. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  258. } else {
  259. $time = $order['add_time'] + $systemValue['order_cancel_time'] * 3600;
  260. $status['_msg'] = '请在' . date('m-d H:i:s', (int)$time) . '前完成支付!';
  261. }
  262. $status['_class'] = 'nobuy';
  263. } else if ($order['status'] == 4) {
  264. if ($order['delivery_type'] == 'send') {//TODO 送货
  265. $status['_type'] = 1;
  266. $status['_title'] = '待收货';
  267. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  268. $status['_class'] = 'state-ysh';
  269. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  270. $status['_type'] = 1;
  271. $status['_title'] = '待收货';
  272. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  273. $status['_class'] = 'state-ysh';
  274. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  275. $status['_type'] = 1;
  276. $status['_title'] = '待收货';
  277. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_part_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  278. $status['_class'] = 'state-ysh';
  279. } else {
  280. $status['_type'] = 1;
  281. $status['_title'] = '待收货';
  282. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  283. $status['_class'] = 'state-ysh';
  284. }
  285. } else if ($order['refund_status'] == 1) {
  286. if (in_array($order['refund_type'], [0, 1, 2])) {
  287. $status['_type'] = -1;
  288. $status['_title'] = '申请退款中';
  289. $status['_msg'] = '商家审核中,请耐心等待';
  290. $status['_class'] = 'state-sqtk';
  291. } elseif ($order['refund_type'] == 4) {
  292. $status['_type'] = -1;
  293. $status['_title'] = '申请退款中';
  294. $status['_msg'] = '商家同意退款,请填写退货订单号';
  295. $status['_class'] = 'state-sqtk';
  296. $status['refund_name'] = sys_config('refund_name', '');
  297. $status['refund_phone'] = sys_config('refund_phone', '');
  298. $status['refund_address'] = sys_config('refund_address', '');
  299. } elseif ($order['refund_type'] == 5) {
  300. $status['_type'] = -1;
  301. $status['_title'] = '申请退款中';
  302. $status['_msg'] = '等待商家收货';
  303. $status['_class'] = 'state-sqtk';
  304. $status['refund_name'] = sys_config('refund_name', '');
  305. $status['refund_phone'] = sys_config('refund_phone', '');
  306. $status['refund_address'] = sys_config('refund_address', '');
  307. }
  308. } else if ($order['refund_status'] == 2 || $order['refund_type'] == 6) {
  309. $status['_type'] = -2;
  310. $status['_title'] = '已退款';
  311. $status['_msg'] = '已为您退款,感谢您的支持';
  312. $status['_class'] = 'state-sqtk';
  313. } else if ($order['refund_status'] == 3) {
  314. $status['_type'] = -1;
  315. $status['_title'] = '部分退款(子订单)';
  316. $status['_msg'] = '拆分发货,部分退款';
  317. $status['_class'] = 'state-sqtk';
  318. } else if ($order['refund_status'] == 4) {
  319. $status['_type'] = -1;
  320. $status['_title'] = '子订单已全部申请退款中';
  321. $status['_msg'] = '拆分发货,全部退款';
  322. $status['_class'] = 'state-sqtk';
  323. } else if (!$order['status']) {
  324. if ($order['pink_id']) {
  325. /** @var StorePinkServices $pinkServices */
  326. $pinkServices = app()->make(StorePinkServices::class);
  327. if ($pinkServices->getCount(['id' => $order['pink_id'], 'status' => 1])) {
  328. $status['_type'] = 1;
  329. $status['_title'] = '拼团中';
  330. $status['_msg'] = '等待其他人参加拼团';
  331. $status['_class'] = 'state-nfh';
  332. } else {
  333. $status['_type'] = 1;
  334. $status['_title'] = '未发货';
  335. $status['_msg'] = '商家未发货,请耐心等待';
  336. $status['_class'] = 'state-nfh';
  337. }
  338. } else {
  339. if ($order['shipping_type'] === 1) {
  340. $status['_type'] = 1;
  341. $status['_title'] = '未发货';
  342. if ($order['advance_id']) {
  343. $status['_msg'] = date('Y-m-d', $order['cartInfo'][0]['productInfo']['presale_end_time']) . '预售结束后' . $order['cartInfo'][0]['productInfo']['presale_day'] . '天内发货,请耐心等待';
  344. } else {
  345. $status['_msg'] = '商家未发货,请耐心等待';
  346. }
  347. $status['_class'] = 'state-nfh';
  348. } elseif ($order['shipping_type'] === 2) {
  349. $status['_type'] = 1;
  350. $status['_title'] = '待核销';
  351. $status['_msg'] = '待核销,请到核销点进行核销';
  352. $status['_class'] = 'state-nfh';
  353. } else {
  354. $status['_type'] = 1;
  355. $status['_title'] = '待领取';
  356. $status['_msg'] = '待领取,将礼品转赠给好友吧!';
  357. $status['_class'] = 'state-nfh';
  358. }
  359. }
  360. } else if ($order['status'] == 1) {
  361. if ($order['delivery_type'] == 'send') {//TODO 送货
  362. $status['_type'] = 2;
  363. $status['_title'] = '待收货';
  364. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  365. $status['_class'] = 'state-ysh';
  366. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  367. $status['_type'] = 2;
  368. $status['_title'] = '待收货';
  369. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  370. $status['_class'] = 'state-ysh';
  371. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  372. $status['_type'] = 2;
  373. $status['_title'] = '待收货';
  374. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  375. $status['_class'] = 'state-ysh';
  376. } else {
  377. $status['_type'] = 2;
  378. $status['_title'] = '待收货';
  379. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  380. $status['_class'] = 'state-ysh';
  381. }
  382. } else if ($order['status'] == 2) {
  383. $status['_type'] = 3;
  384. $status['_title'] = '待评价';
  385. $status['_msg'] = '已收货,快去评价一下吧';
  386. $status['_class'] = 'state-ypj';
  387. } else if ($order['status'] == 3) {
  388. $status['_type'] = 4;
  389. $status['_title'] = '交易完成';
  390. $status['_msg'] = '交易完成,感谢您的支持';
  391. $status['_class'] = 'state-ytk';
  392. }
  393. }
  394. if (isset($order['pay_type']))
  395. $status['_payType'] = $status['_type'] == 0 ? '' : PayServices::PAY_TYPE[$order['pay_type']] ?? '其他方式';
  396. if (isset($order['delivery_type']))
  397. $status['_deliveryType'] = $this->deliveryType[$order['delivery_type']] ?? '其他方式';
  398. $order['_status'] = $status;
  399. $order['_pay_time'] = isset($order['pay_time']) && $order['pay_time'] != null ? date('Y-m-d H:i:s', $order['pay_time']) : '';
  400. $order['_add_time'] = isset($order['add_time']) ? (strstr((string)$order['add_time'], '-') === false ? date('Y-m-d H:i:s', $order['add_time']) : $order['add_time']) : '';
  401. //系统预设取消订单时间段
  402. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  403. //获取配置
  404. $systemValue = SystemConfigService::more($keyValue);
  405. //格式化数据
  406. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  407. if ($order['seckill_id']) {
  408. $secs = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  409. } elseif ($order['bargain_id']) {
  410. $secs = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  411. } elseif ($order['combination_id']) {
  412. $secs = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  413. } else {
  414. $secs = $systemValue['order_cancel_time'];
  415. }
  416. $order['stop_time'] = $secs * 3600 + $order['add_time'];
  417. $order['status_pic'] = '';
  418. //获取商品状态图片
  419. if ($isPic) {
  420. $order_details_images = sys_data('order_details_images') ?: [];
  421. foreach ($order_details_images as $image) {
  422. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  423. $order['status_pic'] = $image['pic'];
  424. break;
  425. }
  426. }
  427. }
  428. if ($order['seckill_id'] || $order['bargain_id'] || $order['combination_id'] || $order['advance_id']) {
  429. if ($order['seckill_id']) $order['type'] = 1;
  430. if ($order['bargain_id']) $order['type'] = 2;
  431. if ($order['combination_id']) $order['type'] = 3;
  432. if ($order['advance_id']) $order['type'] = 4;
  433. }
  434. $order['offlinePayStatus'] = (int)sys_config('offline_pay_status') ?? (int)2;
  435. $log = $statusServices->getColumn(['oid' => $order['id']], 'change_time', 'change_type');
  436. if (isset($log['delivery'])) {
  437. $delivery = date('Y-m-d', $log['delivery']);
  438. } elseif (isset($log['delivery_goods'])) {
  439. $delivery = date('Y-m-d', $log['delivery_goods']);
  440. } elseif (isset($log['delivery_fictitious'])) {
  441. $delivery = date('Y-m-d', $log['delivery_fictitious']);
  442. } else {
  443. $delivery = '';
  444. }
  445. $order['order_log'] = [
  446. 'create' => isset($log['cache_key_create_order']) ? date('Y-m-d', $log['cache_key_create_order']) : '',
  447. 'pay' => isset($log['pay_success']) ? date('Y-m-d', $log['pay_success']) : '',
  448. 'delivery' => $delivery,
  449. 'take' => isset($log['take_delivery']) ? date('Y-m-d', $log['take_delivery']) : '',
  450. 'complete' => isset($log['check_order_over']) ? date('Y-m-d', $log['check_order_over']) : '',
  451. ];
  452. $order['gift_user_info'] = [
  453. 'gift_uid' => $order['gift_uid'],
  454. 'gift_nickname' => '',
  455. 'gift_avatar' => '',
  456. ];
  457. if ($order['gift_uid'] != 0) {
  458. /** @var UserServices $userServices */
  459. $userServices = app()->make(UserServices::class);
  460. $giftUser = $userServices->get($order['gift_uid'], ['nickname', 'avatar']);
  461. $order['gift_user_info'] = [
  462. 'gift_uid' => $order['gift_uid'],
  463. 'gift_nickname' => $giftUser['nickname'],
  464. 'gift_avatar' => $giftUser['avatar'],
  465. ];
  466. }
  467. return $order;
  468. }
  469. /**
  470. * 数据转换
  471. * @param array $data
  472. * @return array
  473. */
  474. public function tidyOrderList(array $data)
  475. {
  476. /** @var StoreOrderCartInfoServices $services */
  477. $services = app()->make(StoreOrderCartInfoServices::class);
  478. foreach ($data as &$item) {
  479. $item['_info'] = $services->getOrderCartInfo((int)$item['id']);
  480. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  481. $item['_refund_time'] = isset($item['refund_reason_time']) && $item['refund_reason_time'] ? date('Y-m-d H:i:s', $item['refund_reason_time']) : '';
  482. $item['_pay_time'] = isset($item['pay_time']) && $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  483. if (($item['pink_id'] || $item['combination_id']) && isset($item['pinkStatus'])) {
  484. switch ($item['pinkStatus']) {
  485. case 1:
  486. $item['pink_name'] = '[拼团订单]正在进行中';
  487. $item['color'] = '#f00';
  488. break;
  489. case 2:
  490. $item['pink_name'] = '[拼团订单]已完成';
  491. $item['color'] = '#00f';
  492. break;
  493. case 3:
  494. $item['pink_name'] = '[拼团订单]未完成';
  495. $item['color'] = '#f0f';
  496. break;
  497. default:
  498. $item['pink_name'] = '[拼团订单]历史订单';
  499. $item['color'] = '#FF7D00';
  500. break;
  501. }
  502. } elseif ($item['combination_id']) {
  503. $item['pink_name'] = '[拼团订单]';
  504. $item['color'] = '#FF7D00';
  505. } elseif ($item['seckill_id']) {
  506. $item['pink_name'] = '[秒杀订单]';
  507. $item['color'] = '#3491FA';
  508. } elseif ($item['bargain_id']) {
  509. $item['pink_name'] = '[砍价订单]';
  510. $item['color'] = '#F7BA1E';
  511. } elseif ($item['advance_id']) {
  512. $item['pink_name'] = '[预售订单]';
  513. $item['color'] = '#B27FEB';
  514. } else {
  515. if ($item['shipping_type'] == 1) {
  516. $item['pink_name'] = '[普通订单]';
  517. $item['color'] = '#333';
  518. } else if ($item['shipping_type'] == 2) {
  519. $item['pink_name'] = '[核销订单]';
  520. $item['color'] = '#8956E8';
  521. }
  522. }
  523. if ($item['paid'] == 1) {
  524. switch ($item['pay_type']) {
  525. case PayServices::WEIXIN_PAY:
  526. $item['pay_type_name'] = '微信支付';
  527. break;
  528. case PayServices::YUE_PAY:
  529. $item['pay_type_name'] = '余额支付';
  530. break;
  531. case PayServices::OFFLINE_PAY:
  532. $item['pay_type_name'] = '线下支付';
  533. break;
  534. case PayServices::ALIAPY_PAY:
  535. $item['pay_type_name'] = '支付宝支付';
  536. break;
  537. case PayServices::ALLIN_PAY:
  538. $item['pay_type_name'] = '通联支付';
  539. break;
  540. default:
  541. $item['pay_type_name'] = '其他支付';
  542. break;
  543. }
  544. } else {
  545. switch ($item['pay_type']) {
  546. case 'offline':
  547. $item['pay_type_name'] = '线下支付';
  548. $item['pay_type_info'] = 1;
  549. break;
  550. default:
  551. $item['pay_type_name'] = '';
  552. break;
  553. }
  554. }
  555. $status_name = ['status_name' => '', 'pics' => []];
  556. if ($item['paid'] == 0 && $item['status'] == 0) {
  557. $status_name['status_name'] = '未支付';
  558. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  559. $status_name['status_name'] = $item['combination_id'] && isset($item['pinkStatus']) && $item['pinkStatus'] == 1 ? '未发货(拼团中)' : '未发货';
  560. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  561. $status_name['status_name'] = '部分发货';
  562. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  563. $status_name['status_name'] = '未核销';
  564. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  565. $status_name['status_name'] = '待收货';
  566. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  567. $status_name['status_name'] = '未核销';
  568. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  569. $status_name['status_name'] = '待评价';
  570. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  571. $status_name['status_name'] = '已完成';
  572. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  573. $refundReasonTime = date('Y-m-d H:i', $item['refund_reason_time']);
  574. $refundReasonWapImg = json_decode($item['refund_reason_wap_img'], true);
  575. $refundReasonWapImg = $refundReasonWapImg ?: [];
  576. $img = [];
  577. if (count($refundReasonWapImg)) {
  578. foreach ($refundReasonWapImg as $itemImg) {
  579. if (strlen(trim($itemImg)))
  580. $img[] = $itemImg;
  581. }
  582. }
  583. $status_name['status_name'] = '退款中';
  584. $status_name['pics'] = $img;
  585. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  586. $status_name['status_name'] = '已退款';
  587. } else if ($item['paid'] == 1 && $item['refund_status'] == 3) {
  588. $status_name['status_name'] = <<<HTML
  589. <b style="color:#f124c7">部分退款</b><br/>
  590. HTML;
  591. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  592. $status_name['status_name'] = <<<HTML
  593. <b style="color:#f124c7">退款中</b><br/>
  594. HTML;
  595. }
  596. $item['status_name'] = $status_name;
  597. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  598. $item['_status'] = 1;//未支付
  599. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  600. $item['_status'] = 2;//已支付 未发货
  601. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['refund_status'] == 0) {
  602. $item['_status'] = 8;//已支付 部分发货
  603. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  604. $item['_status'] = 3;//已支付 申请退款中
  605. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  606. $item['_status'] = 4;//已支付 待收货
  607. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  608. $item['_status'] = 5;//已支付 待评价
  609. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  610. $item['_status'] = 6;//已支付 已完成
  611. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  612. $item['_status'] = 7;//已支付 已退款
  613. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 4) {
  614. $item['_status'] = 9;//拆单发货 部分申请退款
  615. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  616. $item['_status'] = 10;//拆单发货 已全部申请退款
  617. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 0) {
  618. $item['_status'] = 11;//拆单退款 未发货
  619. }
  620. if ($item['clerk_id'] == 0 && !isset($item['clerk_name'])) {
  621. $item['clerk_name'] = '总平台';
  622. }
  623. if ($item['store_id']) {
  624. $store = app()->make(SystemStoreServices::class);
  625. $storeOne = $store->value(['id' => $item['store_id']], 'name');
  626. if ($storeOne) $item['store_name'] = $storeOne;
  627. }
  628. //根据核销员更改store_name
  629. if ($item['clerk_id'] && isset($item['staff_store_id']) && $item['staff_store_id']) {
  630. /** @var SystemStoreServices $store */
  631. $store = app()->make(SystemStoreServices::class);
  632. $storeOne = $store->value(['id' => $item['staff_store_id']], 'name');
  633. if ($storeOne) $item['store_name'] = $storeOne;
  634. }
  635. }
  636. return $data;
  637. }
  638. /**
  639. * 处理订单金额
  640. * @param $where
  641. * @return array
  642. */
  643. public function getOrderPrice($where)
  644. {
  645. if (isset($where['refund_type']) && $where['refund_type']) unset($where['refund_type']);
  646. $where['is_del'] = 0;//删除订单不统计
  647. $price['today_pay_price'] = 0;//今日支付金额
  648. $price['pay_price'] = 0;//支付金额
  649. $price['refund_price'] = 0;//退款金额
  650. $price['pay_price_wx'] = 0;//微信支付金额
  651. $price['pay_price_yue'] = 0;//余额支付金额
  652. $price['pay_price_offline'] = 0;//线下支付金额
  653. $price['pay_price_other'] = 0;//其他支付金额
  654. $price['use_integral'] = 0;//用户使用积分
  655. $price['back_integral'] = 0;//退积分总数
  656. $price['deduction_price'] = 0;//抵扣金额
  657. $price['total_num'] = 0; //商品总数
  658. $price['today_count_sum'] = 0; //今日订单总数
  659. $price['count_sum'] = 0; //订单总数
  660. $price['brokerage'] = 0;
  661. $price['pay_postage'] = 0;
  662. $whereData = ['is_del' => 0];
  663. if ($where['status'] == '' && $where['pay_type'] != 3) {
  664. $whereData['paid'] = 1;
  665. }
  666. $ids = $this->dao->column($where + $whereData, 'id');
  667. if (count($ids)) {
  668. /** @var UserBillServices $services */
  669. $services = app()->make(UserBillServices::class);
  670. $price['brokerage'] = $services->getBrokerageNumSum($ids);
  671. }
  672. $price['refund_price'] = $this->dao->together($where + ['is_del' => 0, 'paid' => 1, 'refund_status' => 2], 'refund_price');
  673. $sumNumber = $this->dao->search($where + $whereData)->field([
  674. 'sum(total_num) as sum_total_num',
  675. 'count(id) as count_sum',
  676. 'sum(pay_price) as sum_pay_price',
  677. 'sum(pay_postage) as sum_pay_postage',
  678. 'sum(use_integral) as sum_use_integral',
  679. 'sum(back_integral) as sum_back_integral',
  680. 'sum(deduction_price) as sum_deduction_price'
  681. ])->find();
  682. if ($sumNumber) {
  683. $price['count_sum'] = $sumNumber['count_sum'];
  684. $price['total_num'] = $sumNumber['sum_total_num'];
  685. $price['pay_price'] = $sumNumber['sum_pay_price'];
  686. $price['pay_postage'] = $sumNumber['sum_pay_postage'];
  687. $price['use_integral'] = $sumNumber['sum_use_integral'];
  688. $price['back_integral'] = $sumNumber['sum_back_integral'];
  689. $price['deduction_price'] = $sumNumber['sum_deduction_price'];
  690. }
  691. $list = $this->dao->column($where + $whereData, 'sum(pay_price) as sum_pay_price,pay_type', 'id', 'pay_type');
  692. foreach ($list as $v) {
  693. if ($v['pay_type'] == 'weixin') {
  694. $price['pay_price_wx'] = $v['sum_pay_price'];
  695. } elseif ($v['pay_type'] == 'yue') {
  696. $price['pay_price_yue'] = $v['sum_pay_price'];
  697. } elseif ($v['pay_type'] == 'offline') {
  698. $price['pay_price_offline'] = $v['sum_pay_price'];
  699. } else {
  700. $price['pay_price_other'] = $v['sum_pay_price'];
  701. }
  702. }
  703. $where['time'] = 'today';
  704. $sumNumber = $this->dao->search($where + $whereData)->field([
  705. 'count(id) as today_count_sum',
  706. 'sum(pay_price) as today_pay_price',
  707. ])->find();
  708. if ($sumNumber) {
  709. $price['today_count_sum'] = $sumNumber['today_count_sum'];
  710. $price['today_pay_price'] = $where['status'] !== 0 ? $sumNumber['today_pay_price'] : 0;
  711. }
  712. return $price;
  713. }
  714. /**
  715. * 获取订单列表页面统计数据
  716. * @param $where
  717. * @return array
  718. */
  719. public function getBadge($where)
  720. {
  721. $price = $this->getOrderPrice($where);
  722. return [
  723. [
  724. 'name' => '订单数量',
  725. 'field' => '件',
  726. 'count' => $price['count_sum'],
  727. 'className' => 'md-basket',
  728. 'col' => 6
  729. ],
  730. [
  731. 'name' => '订单金额',
  732. 'field' => '元',
  733. 'count' => $price['pay_price'],
  734. 'className' => 'md-pricetags',
  735. 'col' => 6
  736. ],
  737. [
  738. 'name' => '今日订单数量',
  739. 'field' => '件',
  740. 'count' => $price['today_count_sum'],
  741. 'className' => 'ios-chatbubbles',
  742. 'col' => 6
  743. ],
  744. [
  745. 'name' => '今日支付金额',
  746. 'field' => '元',
  747. 'count' => $price['today_pay_price'],
  748. 'className' => 'ios-cash',
  749. 'col' => 6
  750. ],
  751. ];
  752. }
  753. /**
  754. *
  755. * @param array $where
  756. * @return mixed
  757. */
  758. /**
  759. * @param array $where
  760. * @return array
  761. * @throws \ReflectionException
  762. * @author wuhaotian
  763. * @email 442384644@qq.com
  764. * @date 2024/3/14
  765. */
  766. public function orderCount(array $where)
  767. {
  768. $where['is_system_del'] = 0;
  769. $where['pid'] = 0;
  770. $data['un_paid'] = $this->dao->count($where + ['status' => 0], false);
  771. $data['un_send'] = $this->dao->count($where + ['status' => 1, 'shipping_type' => 1], false);
  772. return $data;
  773. }
  774. /**
  775. * 创建修改订单表单
  776. * @param int $id
  777. * @return array
  778. * @throws \FormBuilder\Exception\FormBuilderException
  779. */
  780. public function updateForm(int $id)
  781. {
  782. $product = $this->dao->get($id);
  783. if (!$product) {
  784. throw new AdminException(100026);
  785. }
  786. $f = [];
  787. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(true);
  788. $f[] = Form::hidden('total_price', (float)$product->getData('total_price'));
  789. $f[] = Form::hidden('pay_postage', (float)$product->getData('pay_postage') ?: 0);
  790. $f[] = Form::number('pay_price', '实际支付金额', (float)$product->getData('pay_price'))->min(0);
  791. $f[] = Form::number('gain_integral', '赠送积分', (float)$product->getData('gain_integral') ?: 0)->min(0);
  792. return create_form('修改订单', $f, $this->url('/order/update/' . $id), 'PUT');
  793. }
  794. /**
  795. * 修改订单
  796. * @param int $id
  797. * @param array $data
  798. * @return mixed
  799. * @throws \Exception
  800. */
  801. public function updateOrder(int $id, array $data)
  802. {
  803. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  804. if (!$order) {
  805. throw new AdminException(400118);
  806. }
  807. /** @var StoreOrderCreateServices $createServices */
  808. $createServices = app()->make(StoreOrderCreateServices::class);
  809. $data['order_id'] = $createServices->getNewOrderId('cp');
  810. if (sys_config('user_brokerage_type') == 1) {
  811. $percent = $order['pay_price'] != 0 ? bcdiv((string)$data['pay_price'], (string)$order['pay_price'], 6) : $order['pay_price'];
  812. if ($order['one_brokerage'] > 0) {
  813. $data['one_brokerage'] = bcmul((string)$order['one_brokerage'], $percent, 2);
  814. }
  815. if ($order['two_brokerage'] > 0) {
  816. $data['two_brokerage'] = bcmul((string)$order['two_brokerage'], $percent, 2);
  817. }
  818. if ($order['staff_brokerage'] > 0) {
  819. $data['staff_brokerage'] = bcmul((string)$order['staff_brokerage'], $percent, 2);
  820. }
  821. if ($order['agent_brokerage'] > 0) {
  822. $data['agent_brokerage'] = bcmul((string)$order['agent_brokerage'], $percent, 2);
  823. }
  824. if ($order['division_brokerage'] > 0) {
  825. $data['division_brokerage'] = bcmul((string)$order['division_brokerage'], $percent, 2);
  826. }
  827. }
  828. /** @var StoreOrderStatusServices $services */
  829. $services = app()->make(StoreOrderStatusServices::class);
  830. return $this->transaction(function () use ($id, $data, $services) {
  831. $res = $this->dao->update($id, $data);
  832. $res = $res && $services->save([
  833. 'oid' => $id,
  834. 'change_type' => 'order_edit',
  835. 'change_time' => time(),
  836. 'change_message' => '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']
  837. ]);
  838. if (isset($data['gain_integral'])) {
  839. $res = $res && $services->save([
  840. 'oid' => $id,
  841. 'change_type' => 'order_edit',
  842. 'change_time' => time(),
  843. 'change_message' => '修改订单赠送积分为:' . $data['gain_integral']
  844. ]);
  845. }
  846. if ($res) {
  847. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  848. //改价短信提醒
  849. event('NoticeListener', [['order' => $order, 'pay_price' => $data['pay_price']], 'price_revision']);
  850. //自定义消息-订单改价
  851. $order['change_price'] = $data['pay_price'];
  852. event('NoticeListener', [$order['uid'], $order, 'price_change_price']);
  853. //自定义事件-订单改价
  854. event('CustomEventListener', ['admin_order_change', [
  855. 'uid' => $order['uid'],
  856. 'order_id' => $data['order_id'],
  857. 'pay_price' => $data['pay_price'],
  858. 'gain_integral' => $data['gain_integral'] ?? $order['gain_integral'],
  859. 'change_time' => date('Y-m-d H:i:s'),
  860. ]]);
  861. return $data['order_id'];
  862. } else {
  863. throw new AdminException(100007);
  864. }
  865. });
  866. }
  867. /**
  868. * 订单图表
  869. * @param $cycle
  870. * @return array
  871. */
  872. public function orderCharts($cycle)
  873. {
  874. $datalist = [];
  875. switch ($cycle) {
  876. case 'thirtyday':
  877. $datebefor = date('Y-m-d', strtotime('-30 day'));
  878. $dateafter = date('Y-m-d 23:59:59');
  879. //上期
  880. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  881. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  882. for ($i = -29; $i <= 0; $i++) {
  883. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  884. }
  885. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, '30');
  886. if (empty($order_list)) return ['yAxis' => [], 'legend' => [], 'xAxis' => [], 'serise' => [], 'pre_cycle' => [], 'cycle' => []];
  887. foreach ($order_list as $k => &$v) {
  888. $order_list[$v['day']] = $v;
  889. }
  890. $cycle_list = [];
  891. foreach ($datalist as $dk => $dd) {
  892. if (!empty($order_list[$dd])) {
  893. $cycle_list[$dd] = $order_list[$dd];
  894. } else {
  895. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  896. }
  897. }
  898. $chartdata = [];
  899. $data = [];//临时
  900. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  901. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  902. foreach ($cycle_list as $k => $v) {
  903. $data['day'][] = $v['day'];
  904. $data['count'][] = $v['count'];
  905. $data['price'][] = round($v['price'], 2);
  906. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  907. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  908. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  909. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  910. }
  911. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  912. $chartdata['xAxis'] = $data['day'];//X轴值
  913. $series1 = ['normal' => ['color' => [
  914. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  915. 'colorStops' => [
  916. [
  917. 'offset' => 0,
  918. 'color' => '#69cdff'
  919. ],
  920. [
  921. 'offset' => 0.5,
  922. 'color' => '#3eb3f7'
  923. ],
  924. [
  925. 'offset' => 1,
  926. 'color' => '#1495eb'
  927. ]
  928. ]
  929. ]]
  930. ];
  931. $series2 = ['normal' => ['color' => [
  932. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  933. 'colorStops' => [
  934. [
  935. 'offset' => 0,
  936. 'color' => '#6fdeab'
  937. ],
  938. [
  939. 'offset' => 0.5,
  940. 'color' => '#44d693'
  941. ],
  942. [
  943. 'offset' => 1,
  944. 'color' => '#2cc981'
  945. ]
  946. ]
  947. ]]
  948. ];
  949. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['price']];//分类1值
  950. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['count'], 'yAxisIndex' => 1];//分类2值
  951. //统计总数上期
  952. $pre_total = $this->dao->preTotalFind($pre_datebefor, $pre_dateafter);
  953. if ($pre_total) {
  954. $chartdata['pre_cycle']['count'] = [
  955. 'data' => $pre_total['count'] ?: 0
  956. ];
  957. $chartdata['pre_cycle']['price'] = [
  958. 'data' => $pre_total['price'] ?: 0
  959. ];
  960. }
  961. //统计总数
  962. $total = $this->dao->preTotalFind($datebefor, $dateafter);
  963. if ($total) {
  964. $cha_count = intval($pre_total['count']) - intval($total['count']);
  965. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  966. $chartdata['cycle']['count'] = [
  967. 'data' => $total['count'] ?: 0,
  968. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  969. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  970. ];
  971. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  972. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  973. $chartdata['cycle']['price'] = [
  974. 'data' => $total['price'] ?: 0,
  975. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  976. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  977. ];
  978. }
  979. return $chartdata;
  980. case 'week':
  981. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  982. $datebefor = date('Y-m-d', strtotime('-1 week Monday'));
  983. $dateafter = date('Y-m-d', strtotime('-1 week Sunday'));
  984. // $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'week');
  985. //数据查询重新处理
  986. $new_order_list = [];
  987. // foreach ($order_list as $k => $v) {
  988. // $new_order_list[$v['day']] = $v;
  989. // }
  990. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  991. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  992. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'week');
  993. //数据查询重新处理 key 变为当前值
  994. $new_now_order_list = [];
  995. foreach ($now_order_list as $k => $v) {
  996. $new_now_order_list[$v['day']] = $v;
  997. }
  998. foreach ($weekarray as $dk => $dd) {
  999. if (!empty($new_order_list[$dk])) {
  1000. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1001. } else {
  1002. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1003. }
  1004. if (!empty($new_now_order_list[$dk])) {
  1005. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1006. } else {
  1007. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1008. }
  1009. }
  1010. $chartdata = [];
  1011. $data = [];//临时
  1012. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1013. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1014. foreach ($weekarray as $k => $v) {
  1015. $data['day'][] = $v[0];
  1016. $data['pre']['count'][] = $v['pre']['count'];
  1017. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1018. $data['now']['count'][] = $v['now']['count'];
  1019. $data['now']['price'][] = round($v['now']['price'], 2);
  1020. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1021. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1022. }
  1023. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1024. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1025. }
  1026. }
  1027. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  1028. $chartdata['xAxis'] = $data['day'];//X轴值
  1029. $series1 = ['normal' => ['color' => [
  1030. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1031. 'colorStops' => [
  1032. [
  1033. 'offset' => 0,
  1034. 'color' => '#69cdff'
  1035. ],
  1036. [
  1037. 'offset' => 0.5,
  1038. 'color' => '#3eb3f7'
  1039. ],
  1040. [
  1041. 'offset' => 1,
  1042. 'color' => '#1495eb'
  1043. ]
  1044. ]
  1045. ]]
  1046. ];
  1047. $series2 = ['normal' => ['color' => [
  1048. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1049. 'colorStops' => [
  1050. [
  1051. 'offset' => 0,
  1052. 'color' => '#6fdeab'
  1053. ],
  1054. [
  1055. 'offset' => 0.5,
  1056. 'color' => '#44d693'
  1057. ],
  1058. [
  1059. 'offset' => 1,
  1060. 'color' => '#2cc981'
  1061. ]
  1062. ]
  1063. ]]
  1064. ];
  1065. $series3 = ['normal' => ['color' => [
  1066. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1067. 'colorStops' => [
  1068. [
  1069. 'offset' => 0,
  1070. 'color' => '#69cdff'
  1071. ],
  1072. [
  1073. 'offset' => 0.5,
  1074. 'color' => '#3eb3f7'
  1075. ],
  1076. [
  1077. 'offset' => 1,
  1078. 'color' => '#1495eb'
  1079. ]
  1080. ]
  1081. ]]
  1082. ];
  1083. $series4 = ['normal' => ['color' => [
  1084. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1085. 'colorStops' => [
  1086. [
  1087. 'offset' => 0,
  1088. 'color' => '#6fdeab'
  1089. ],
  1090. [
  1091. 'offset' => 0.5,
  1092. 'color' => '#44d693'
  1093. ],
  1094. [
  1095. 'offset' => 1,
  1096. 'color' => '#2cc981'
  1097. ]
  1098. ]
  1099. ]]
  1100. ];
  1101. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1102. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1103. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1104. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1105. //统计总数上期
  1106. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1107. if ($pre_total) {
  1108. $chartdata['pre_cycle']['count'] = [
  1109. 'data' => $pre_total['count'] ?: 0
  1110. ];
  1111. $chartdata['pre_cycle']['price'] = [
  1112. 'data' => $pre_total['price'] ?: 0
  1113. ];
  1114. }
  1115. //统计总数
  1116. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1117. if ($total) {
  1118. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1119. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1120. $chartdata['cycle']['count'] = [
  1121. 'data' => $total['count'] ?: 0,
  1122. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1123. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1124. ];
  1125. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1126. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1127. $chartdata['cycle']['price'] = [
  1128. 'data' => $total['price'] ?: 0,
  1129. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1130. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1131. ];
  1132. }
  1133. return $chartdata;
  1134. case 'month':
  1135. $weekarray = array('01' => ['1'], '02' => ['2'], '03' => ['3'], '04' => ['4'], '05' => ['5'], '06' => ['6'], '07' => ['7'], '08' => ['8'], '09' => ['9'], '10' => ['10'], '11' => ['11'], '12' => ['12'], '13' => ['13'], '14' => ['14'], '15' => ['15'], '16' => ['16'], '17' => ['17'], '18' => ['18'], '19' => ['19'], '20' => ['20'], '21' => ['21'], '22' => ['22'], '23' => ['23'], '24' => ['24'], '25' => ['25'], '26' => ['26'], '27' => ['27'], '28' => ['28'], '29' => ['29'], '30' => ['30'], '31' => ['31']);
  1136. $datebefor = date('Y-m-01', strtotime('-1 month'));
  1137. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  1138. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, "month");
  1139. //数据查询重新处理
  1140. $new_order_list = [];
  1141. foreach ($order_list as $k => $v) {
  1142. $new_order_list[$v['day']] = $v;
  1143. }
  1144. $now_datebefor = date('Y-m-01');
  1145. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  1146. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, "month");
  1147. //数据查询重新处理 key 变为当前值
  1148. $new_now_order_list = [];
  1149. foreach ($now_order_list as $k => $v) {
  1150. $new_now_order_list[$v['day']] = $v;
  1151. }
  1152. foreach ($weekarray as $dk => $dd) {
  1153. if (!empty($new_order_list[$dk])) {
  1154. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1155. } else {
  1156. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1157. }
  1158. if (!empty($new_now_order_list[$dk])) {
  1159. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1160. } else {
  1161. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1162. }
  1163. }
  1164. $chartdata = [];
  1165. $data = [];//临时
  1166. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1167. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1168. foreach ($weekarray as $k => $v) {
  1169. $data['day'][] = $v[0];
  1170. $data['pre']['count'][] = $v['pre']['count'];
  1171. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1172. $data['now']['count'][] = $v['now']['count'];
  1173. $data['now']['price'][] = round($v['now']['price'], 2);
  1174. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1175. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1176. }
  1177. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1178. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1179. }
  1180. }
  1181. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  1182. $chartdata['xAxis'] = $data['day'];//X轴值
  1183. $series1 = ['normal' => ['color' => [
  1184. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1185. 'colorStops' => [
  1186. [
  1187. 'offset' => 0,
  1188. 'color' => '#69cdff'
  1189. ],
  1190. [
  1191. 'offset' => 0.5,
  1192. 'color' => '#3eb3f7'
  1193. ],
  1194. [
  1195. 'offset' => 1,
  1196. 'color' => '#1495eb'
  1197. ]
  1198. ]
  1199. ]]
  1200. ];
  1201. $series2 = ['normal' => ['color' => [
  1202. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1203. 'colorStops' => [
  1204. [
  1205. 'offset' => 0,
  1206. 'color' => '#6fdeab'
  1207. ],
  1208. [
  1209. 'offset' => 0.5,
  1210. 'color' => '#44d693'
  1211. ],
  1212. [
  1213. 'offset' => 1,
  1214. 'color' => '#2cc981'
  1215. ]
  1216. ]
  1217. ]]
  1218. ];
  1219. $series3 = ['normal' => ['color' => [
  1220. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1221. 'colorStops' => [
  1222. [
  1223. 'offset' => 0,
  1224. 'color' => '#69cdff'
  1225. ],
  1226. [
  1227. 'offset' => 0.5,
  1228. 'color' => '#3eb3f7'
  1229. ],
  1230. [
  1231. 'offset' => 1,
  1232. 'color' => '#1495eb'
  1233. ]
  1234. ]
  1235. ]]
  1236. ];
  1237. $series4 = ['normal' => ['color' => [
  1238. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1239. 'colorStops' => [
  1240. [
  1241. 'offset' => 0,
  1242. 'color' => '#6fdeab'
  1243. ],
  1244. [
  1245. 'offset' => 0.5,
  1246. 'color' => '#44d693'
  1247. ],
  1248. [
  1249. 'offset' => 1,
  1250. 'color' => '#2cc981'
  1251. ]
  1252. ]
  1253. ]]
  1254. ];
  1255. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1256. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1257. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1258. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1259. //统计总数上期
  1260. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1261. if ($pre_total) {
  1262. $chartdata['pre_cycle']['count'] = [
  1263. 'data' => $pre_total['count'] ?: 0
  1264. ];
  1265. $chartdata['pre_cycle']['price'] = [
  1266. 'data' => $pre_total['price'] ?: 0
  1267. ];
  1268. }
  1269. //统计总数
  1270. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1271. if ($total) {
  1272. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1273. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1274. $chartdata['cycle']['count'] = [
  1275. 'data' => $total['count'] ?: 0,
  1276. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1277. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1278. ];
  1279. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1280. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1281. $chartdata['cycle']['price'] = [
  1282. 'data' => $total['price'] ?: 0,
  1283. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1284. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1285. ];
  1286. }
  1287. return $chartdata;
  1288. case 'year':
  1289. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  1290. $datebefor = date('Y-01-01', strtotime('-1 year'));
  1291. $dateafter = date('Y-12-31', strtotime('-1 year'));
  1292. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'year');
  1293. //数据查询重新处理
  1294. $new_order_list = [];
  1295. foreach ($order_list as $k => $v) {
  1296. $new_order_list[$v['day']] = $v;
  1297. }
  1298. $now_datebefor = date('Y-01-01');
  1299. $now_dateafter = date('Y-12-31 23:59:59');
  1300. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'year');
  1301. //数据查询重新处理 key 变为当前值
  1302. $new_now_order_list = [];
  1303. foreach ($now_order_list as $k => $v) {
  1304. $new_now_order_list[$v['day']] = $v;
  1305. }
  1306. foreach ($weekarray as $dk => $dd) {
  1307. if (!empty($new_order_list[$dk])) {
  1308. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1309. } else {
  1310. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1311. }
  1312. if (!empty($new_now_order_list[$dk])) {
  1313. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1314. } else {
  1315. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1316. }
  1317. }
  1318. $chartdata = [];
  1319. $data = [];//临时
  1320. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1321. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1322. foreach ($weekarray as $k => $v) {
  1323. $data['day'][] = $v[0];
  1324. $data['pre']['count'][] = $v['pre']['count'];
  1325. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1326. $data['now']['count'][] = $v['now']['count'];
  1327. $data['now']['price'][] = round($v['now']['price'], 2);
  1328. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1329. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1330. }
  1331. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1332. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1333. }
  1334. }
  1335. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  1336. $chartdata['xAxis'] = $data['day'];//X轴值
  1337. $series1 = ['normal' => ['color' => [
  1338. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1339. 'colorStops' => [
  1340. [
  1341. 'offset' => 0,
  1342. 'color' => '#69cdff'
  1343. ],
  1344. [
  1345. 'offset' => 0.5,
  1346. 'color' => '#3eb3f7'
  1347. ],
  1348. [
  1349. 'offset' => 1,
  1350. 'color' => '#1495eb'
  1351. ]
  1352. ]
  1353. ]]
  1354. ];
  1355. $series2 = ['normal' => ['color' => [
  1356. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1357. 'colorStops' => [
  1358. [
  1359. 'offset' => 0,
  1360. 'color' => '#6fdeab'
  1361. ],
  1362. [
  1363. 'offset' => 0.5,
  1364. 'color' => '#44d693'
  1365. ],
  1366. [
  1367. 'offset' => 1,
  1368. 'color' => '#2cc981'
  1369. ]
  1370. ]
  1371. ]]
  1372. ];
  1373. $series3 = ['normal' => ['color' => [
  1374. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1375. 'colorStops' => [
  1376. [
  1377. 'offset' => 0,
  1378. 'color' => '#69cdff'
  1379. ],
  1380. [
  1381. 'offset' => 0.5,
  1382. 'color' => '#3eb3f7'
  1383. ],
  1384. [
  1385. 'offset' => 1,
  1386. 'color' => '#1495eb'
  1387. ]
  1388. ]
  1389. ]]
  1390. ];
  1391. $series4 = ['normal' => ['color' => [
  1392. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1393. 'colorStops' => [
  1394. [
  1395. 'offset' => 0,
  1396. 'color' => '#6fdeab'
  1397. ],
  1398. [
  1399. 'offset' => 0.5,
  1400. 'color' => '#44d693'
  1401. ],
  1402. [
  1403. 'offset' => 1,
  1404. 'color' => '#2cc981'
  1405. ]
  1406. ]
  1407. ]]
  1408. ];
  1409. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1410. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1411. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1412. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1413. //统计总数上期
  1414. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1415. if ($pre_total) {
  1416. $chartdata['pre_cycle']['count'] = [
  1417. 'data' => $pre_total['count'] ?: 0
  1418. ];
  1419. $chartdata['pre_cycle']['price'] = [
  1420. 'data' => $pre_total['price'] ?: 0
  1421. ];
  1422. }
  1423. //统计总数
  1424. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1425. if ($total) {
  1426. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1427. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1428. $chartdata['cycle']['count'] = [
  1429. 'data' => $total['count'] ?: 0,
  1430. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1431. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1432. ];
  1433. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1434. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1435. $chartdata['cycle']['price'] = [
  1436. 'data' => $total['price'] ?: 0,
  1437. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1438. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1439. ];
  1440. }
  1441. return $chartdata;
  1442. default:
  1443. break;
  1444. }
  1445. }
  1446. /**
  1447. * 获取订单数量
  1448. * @return int
  1449. */
  1450. public function storeOrderCount()
  1451. {
  1452. return $this->dao->storeOrderCount();
  1453. }
  1454. /**
  1455. * 新订单ID
  1456. * @param $status
  1457. * @return array
  1458. */
  1459. public function newOrderId($status)
  1460. {
  1461. return $this->dao->search(['status' => $status, 'is_remind' => 0])->column('order_id', 'id');
  1462. }
  1463. /**
  1464. * 新订单修改
  1465. * @param $newOrderId
  1466. * @return \crmeb\basic\BaseModel
  1467. */
  1468. public function newOrderUpdate($newOrderId)
  1469. {
  1470. return $this->dao->newOrderUpdates($newOrderId);
  1471. }
  1472. /**
  1473. * 增长率
  1474. * @param $left
  1475. * @param $right
  1476. * @return int|string
  1477. */
  1478. public function growth($nowValue, $lastValue)
  1479. {
  1480. if ($lastValue == 0 && $nowValue == 0) return 0;
  1481. if ($lastValue == 0) return bcmul((string)$nowValue, '100', 2);
  1482. if ($nowValue == 0) return bcdiv(bcsub($nowValue, $lastValue, 2), $lastValue, 4) * 100;
  1483. return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 4), 100, 2);
  1484. }
  1485. /**
  1486. * 后台首页顶部统计
  1487. * @return array
  1488. * @author 吴汐
  1489. * @email 442384644@qq.com
  1490. * @date 2023/04/03
  1491. */
  1492. public function homeStatics()
  1493. {
  1494. /** @var UserServices $userService */
  1495. $userService = app()->make(UserServices::class);
  1496. /** @var StoreProductLogServices $productLogServices */
  1497. $productLogServices = app()->make(StoreProductLogServices::class);
  1498. //TODO 销售额
  1499. //今日销售额
  1500. $today_sales = $this->dao->todaySales('today');
  1501. //昨日销售额
  1502. $yesterday_sales = $this->dao->todaySales('yesterday');
  1503. //日同比
  1504. $sales_today_ratio = $this->growth($today_sales, $yesterday_sales);
  1505. //总销售额
  1506. $total_sales = $this->dao->totalSales('month');
  1507. $sales = [
  1508. 'today' => $today_sales,
  1509. 'yesterday' => $yesterday_sales,
  1510. 'today_ratio' => $sales_today_ratio,
  1511. 'total' => $total_sales . '元',
  1512. 'date' => '今日'
  1513. ];
  1514. //TODO:用户访问量
  1515. //今日访问量
  1516. $today_visits = $productLogServices->count(['time' => 'today', 'type' => 'visit']);
  1517. //昨日访问量
  1518. $yesterday_visits = $productLogServices->count(['time' => 'yesterday', 'type' => 'visit']);
  1519. //日同比
  1520. $visits_today_ratio = $this->growth($today_visits, $yesterday_visits);
  1521. //总访问量
  1522. $total_visits = $productLogServices->count(['time' => 'month', 'type' => 'visit']);
  1523. $visits = [
  1524. 'today' => $today_visits,
  1525. 'yesterday' => $yesterday_visits,
  1526. 'today_ratio' => $visits_today_ratio,
  1527. 'total' => $total_visits . 'Pv',
  1528. 'date' => '今日'
  1529. ];
  1530. //TODO 订单量
  1531. //今日订单量
  1532. $today_order = $this->dao->todayOrderVisit('today', 1);
  1533. //昨日订单量
  1534. $yesterday_order = $this->dao->todayOrderVisit('yesterday', 1);
  1535. //订单日同比
  1536. $order_today_ratio = $this->growth($today_order, $yesterday_order);
  1537. //总订单量
  1538. $total_order = $this->dao->count(['time' => 'month', 'paid' => 1, 'refund_status' => 0, 'pid' => 0]);
  1539. $order = [
  1540. 'today' => $today_order,
  1541. 'yesterday' => $yesterday_order,
  1542. 'today_ratio' => $order_today_ratio,
  1543. 'total' => $total_order . '单',
  1544. 'date' => '今日'
  1545. ];
  1546. //TODO 用户
  1547. //今日新增用户
  1548. $today_user = $userService->todayAddVisits('today', 1);
  1549. //昨日新增用户
  1550. $yesterday_user = $userService->todayAddVisits('yesterday', 1);
  1551. //新增用户日同比
  1552. $user_today_ratio = $this->growth($today_user, $yesterday_user);
  1553. //所有用户
  1554. $total_user = $userService->count(['time' => 'month']);
  1555. $user = [
  1556. 'today' => $today_user,
  1557. 'yesterday' => $yesterday_user,
  1558. 'today_ratio' => $user_today_ratio,
  1559. 'total' => $total_user . '人',
  1560. 'date' => '今日'
  1561. ];
  1562. //TODO 礼包产品
  1563. $lb_radio = sys_config('gift_product_bonus',5);
  1564. //今日礼包产品
  1565. $today_lb_sales = $this->dao->todayLbSales('today');
  1566. $today_lb_sales = bcmul($today_lb_sales,bcdiv($lb_radio,100,2),2);
  1567. //昨日礼包产品
  1568. $yesterday_lb_sales = $this->dao->todayLbSales('yesterday');
  1569. $yesterday_lb_sales = bcmul($yesterday_lb_sales,bcdiv($lb_radio,100,2),2);
  1570. //日同比
  1571. $sales_today_lb_ratio = $this->growth($today_sales, $yesterday_sales);
  1572. //总礼包产品
  1573. $total_lb_sales = $this->dao->totalLbSales('month');
  1574. $total_lb_sales = bcmul($total_lb_sales,bcdiv($lb_radio,100,2),2);
  1575. $lb_sales = [
  1576. 'today' => $today_lb_sales,
  1577. 'yesterday' => $yesterday_lb_sales,
  1578. 'today_ratio' => $sales_today_lb_ratio,
  1579. 'total' => $total_lb_sales . '元',
  1580. 'date' => '今日'
  1581. ];
  1582. //TODO 老积分
  1583. $points_radio = sys_config('gift_points',5);
  1584. //今日老积分
  1585. $today_points = $this->dao->todayLbSales('today');
  1586. $today_points = bcmul($today_points,bcdiv($points_radio,100,2),2);
  1587. //昨日老积分
  1588. $yesterday_points = $this->dao->todayLbSales('yesterday');
  1589. $yesterday_points = bcmul($yesterday_points,bcdiv($points_radio,100,2),2);
  1590. //日同比
  1591. $points_today__ratio = $this->growth($today_sales, $yesterday_sales);
  1592. //总老积分
  1593. $total_points = $this->dao->totalLbSales('month');
  1594. $total_points = bcmul($total_points,bcdiv($points_radio,100,2),2);
  1595. $points = [
  1596. 'today' => $today_points,
  1597. 'yesterday' => $yesterday_points,
  1598. 'today_ratio' => $points_today__ratio,
  1599. 'total' => $total_points . '积分',
  1600. 'date' => '今日'
  1601. ];
  1602. $info = array_values(compact('sales', 'visits', 'order', 'user','lb_sales','points'));
  1603. $info[0]['title'] = '销售额';
  1604. $info[1]['title'] = '用户访问量';
  1605. $info[2]['title'] = '订单量';
  1606. $info[3]['title'] = '新增用户';
  1607. $info[4]['title'] = '礼包产品绩效';
  1608. $info[5]['title'] = '老积分';
  1609. $info[0]['total_name'] = '本月销售额';
  1610. $info[1]['total_name'] = '本月访问量';
  1611. $info[2]['total_name'] = '本月订单量';
  1612. $info[3]['total_name'] = '本月新增用户';
  1613. $info[4]['total_name'] = '总礼包产品绩效';
  1614. $info[5]['total_name'] = '总老积分';
  1615. return $info;
  1616. }
  1617. /**
  1618. * 订单小票打印
  1619. * @param int $id
  1620. * @param bool $start
  1621. * @return bool|void
  1622. * @throws \think\db\exception\DataNotFoundException
  1623. * @throws \think\db\exception\DbException
  1624. * @throws \think\db\exception\ModelNotFoundException
  1625. * @throws \Exception
  1626. */
  1627. public function orderPrintTicket(int $id, $print_type)
  1628. {
  1629. $order = $this->get($id);
  1630. if (!$order) {
  1631. throw new AdminException(400118);
  1632. }
  1633. /** @var StoreOrderCartInfoServices $cartServices */
  1634. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1635. $product = $cartServices->getCartInfoPrintProduct($order['id']);
  1636. if (!$product) {
  1637. throw new AdminException(400463);
  1638. }
  1639. // $switch = (bool)sys_config('pay_success_printing_switch');
  1640. // if (!$switch) {
  1641. // throw new AdminException(400464);
  1642. // }
  1643. app()->make(SystemTicketServices::class)->startPrint(
  1644. is_object($order) ? $order->toArray() : $order,
  1645. $product,
  1646. $print_type
  1647. );
  1648. return true;
  1649. // if (sys_config('print_type', 1) == 1) {
  1650. // $name = 'yi_lian_yun';
  1651. // $configData = [
  1652. // 'clientId' => sys_config('printing_client_id', ''),
  1653. // 'apiKey' => sys_config('printing_api_key', ''),
  1654. // 'partner' => sys_config('develop_id', ''),
  1655. // 'terminal' => sys_config('terminal_number', '')
  1656. // ];
  1657. // if (!$configData['clientId'] || !$configData['apiKey'] || !$configData['partner'] || !$configData['terminal']) {
  1658. // throw new AdminException(400465);
  1659. // }
  1660. // } else {
  1661. // $name = 'fei_e_yun';
  1662. // $configData = [
  1663. // 'feyUser' => sys_config('fey_user', ''),
  1664. // 'feyUkey' => sys_config('fey_ukey', ''),
  1665. // 'feySn' => sys_config('fey_sn', '')
  1666. // ];
  1667. // if (!$configData['feyUser'] || !$configData['feyUkey'] || !$configData['feySn']) {
  1668. // throw new AdminException(400465);
  1669. // }
  1670. // }
  1671. // $printer = new Printer($name, $configData);
  1672. // $res = $printer->setPrinterContent([
  1673. // 'name' => sys_config('site_name'),
  1674. // 'url' => sys_config('site_url'),
  1675. // 'orderInfo' => is_object($order) ? $order->toArray() : $order,
  1676. // 'product' => $product
  1677. // ])->startPrinter();
  1678. // if (!$res) {
  1679. // throw new AdminException($printer->getError());
  1680. // }
  1681. // return true;
  1682. }
  1683. /**
  1684. * 获取订单确认数据
  1685. * @param array $user
  1686. * @param $cartId
  1687. * @param bool $new
  1688. * @param int $addressId
  1689. * @param int $shipping_type
  1690. * @return array
  1691. * @throws \Psr\SimpleCache\InvalidArgumentException
  1692. * @throws \think\db\exception\DataNotFoundException
  1693. * @throws \think\db\exception\DbException
  1694. * @throws \think\db\exception\ModelNotFoundException
  1695. */
  1696. public function getOrderConfirmData(array $user, $cartId, bool $new, int $addressId, int $shipping_type = 1, int $is_gift = 0)
  1697. {
  1698. $addr = [];
  1699. /** @var UserAddressServices $addressServices */
  1700. $addressServices = app()->make(UserAddressServices::class);
  1701. if ($addressId) {
  1702. $addr = $addressServices->getAddress($addressId);
  1703. }
  1704. //没传地址id或地址已删除未找到 ||获取默认地址
  1705. if (!$addr) {
  1706. $addr = $addressServices->getUserDefaultAddress((int)$user['uid']);
  1707. }
  1708. if ($addr) {
  1709. $addr = $addr->toArray();
  1710. } else {
  1711. $addr = [];
  1712. }
  1713. if ($shipping_type == 2) $addr = [];
  1714. if ($is_gift == 1) {
  1715. $addr = [];
  1716. $shipping_type = 0;
  1717. }
  1718. /** @var StoreCartServices $cartServices */
  1719. $cartServices = app()->make(StoreCartServices::class);
  1720. $cartGroup = $cartServices->getUserProductCartListV1($user['uid'], $cartId, $new, $addr, $shipping_type, $is_gift);
  1721. $data = [];
  1722. $data['storeFreePostage'] = $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  1723. $validCartInfo = $cartGroup['valid'];
  1724. /** @var StoreOrderComputedServices $computedServices */
  1725. $computedServices = app()->make(StoreOrderComputedServices::class);
  1726. $priceGroup = $computedServices->getOrderPriceGroup($storeFreePostage, $validCartInfo, $addr, $user, $shipping_type, $is_gift);
  1727. $validCartInfo = $priceGroup['cartInfo'] ?? $validCartInfo;
  1728. $other = [
  1729. 'offlinePostage' => sys_config('offline_postage'),
  1730. 'integralRatio' => sys_config('integral_ratio')
  1731. ];
  1732. $cartIdA = explode(',', $cartId);
  1733. $is_lb= 0 ;
  1734. $is_no_lb =0;
  1735. foreach ($cartGroup['cartInfo'] as $v) {
  1736. // $product_id = $cartGroup['cartInfo'][$cartIdItem]['product_id'] ?? 0;
  1737. // 检测是否是礼包商品
  1738. // $storeServices = app()->make(StoreProductServices::class);
  1739. // $is_lb = $storeServices->value(['id' => $product_id], 'is_lb');
  1740. if ($v['productInfo']['is_lb'] == 1) {
  1741. $is_lb = $is_lb + 1;
  1742. }else{
  1743. $is_no_lb = $is_no_lb + 1;
  1744. }
  1745. }
  1746. if ($is_lb > 1) {
  1747. throw new ApiException('礼包商品不支持混合购买');
  1748. }
  1749. if ($is_lb>0 && $is_no_lb > 0) {
  1750. throw new ApiException('礼包商品不支持混合购买');
  1751. }
  1752. $seckill_id = 0;
  1753. $combination_id = 0;
  1754. $bargain_id = 0;
  1755. $advance_id = 0;
  1756. if (count($cartIdA) == 1) {
  1757. $seckill_id = $cartGroup['deduction']['seckill_id'] ?? 0;
  1758. $combination_id = $cartGroup['deduction']['combination_id'] ?? 0;
  1759. $bargain_id = $cartGroup['deduction']['bargain_id'] ?? 0;
  1760. $advance_id = $cartGroup['deduction']['advance_id'] ?? 0;
  1761. }
  1762. $data['valid_count'] = count($validCartInfo);
  1763. $data['virtual_type'] = $data['valid_count'] ? (int)$validCartInfo[0]['productInfo']['virtual_type'] > 0 : 0;
  1764. $data['deduction'] = $seckill_id || $combination_id || $bargain_id || $advance_id;
  1765. $data['addressInfo'] = $addr;
  1766. $data['seckill_id'] = $seckill_id;
  1767. $data['combination_id'] = $combination_id;
  1768. $data['bargain_id'] = $bargain_id;
  1769. $data['advance_id'] = $advance_id;
  1770. $data['cartInfo'] = $cartGroup['cartInfo'];
  1771. $data['custom_form'] = json_decode($cartGroup['cartInfo'][0]['productInfo']['custom_form'], true) ?? [];
  1772. if (!is_array($data['custom_form'])) $data['custom_form'] = [];
  1773. $data['priceGroup'] = $priceGroup;
  1774. $data['orderKey'] = $this->cacheOrderInfo($user['uid'], $validCartInfo, $priceGroup, $other);
  1775. $data['offlinePostage'] = $other['offlinePostage'];
  1776. /** @var UserLevelServices $levelServices */
  1777. $levelServices = app()->make(UserLevelServices::class);
  1778. $userLevel = $levelServices->getUerLevelInfoByUid($user['uid']);
  1779. if (isset($user['pwd'])) unset($user['pwd']);
  1780. $user['vip'] = $userLevel !== false;
  1781. if ($user['vip']) {
  1782. $user['vip_id'] = $userLevel['id'] ?? 0;
  1783. $user['discount'] = $userLevel['discount'] ?? 0;
  1784. }
  1785. $data['userInfo'] = $user;
  1786. $data['integralRatio'] = $other['integralRatio'];
  1787. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  1788. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  1789. $data['pay_weixin_open'] = sys_config('pay_weixin_open', '0') != '0';//微信支付 1 开启 0 关闭
  1790. $data['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  1791. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  1792. /** @var SystemStoreServices $systemStoreServices */
  1793. $systemStoreServices = app()->make(SystemStoreServices::class);
  1794. $store_count = $systemStoreServices->count(['type' => 0]);
  1795. $data['store_self_mention'] = $data['store_self_mention'] && $store_count;
  1796. $data['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  1797. $data['system_store'] = [];//门店信息
  1798. /** @var UserInvoiceServices $userInvoice */
  1799. $userInvoice = app()->make(UserInvoiceServices::class);
  1800. $invoice_func = $userInvoice->invoiceFuncStatus();
  1801. $data['invoice_func'] = $invoice_func['invoice_func'];
  1802. $data['special_invoice'] = $invoice_func['special_invoice'];
  1803. /** @var UserBillServices $userBillServices */
  1804. $userBillServices = app()->make(UserBillServices::class);
  1805. $data['usable_integral'] = bcsub((string)$user['integral'], (string)$userBillServices->getBillSum(['uid' => $user['uid'], 'is_frozen' => 1]), 0);
  1806. $data['integral_open'] = sys_config('integral_ratio', 0) > 0;
  1807. //自动领取优惠券
  1808. app()->make(StoreCouponUserServices::class)->autoReceiveCoupon($user['uid'], $cartGroup);
  1809. return $data;
  1810. }
  1811. /**
  1812. * 缓存订单信息
  1813. * @param $uid
  1814. * @param $cartInfo
  1815. * @param $priceGroup
  1816. * @param array $other
  1817. * @param int $cacheTime
  1818. * @return string
  1819. * @throws \Psr\SimpleCache\InvalidArgumentException
  1820. */
  1821. public function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  1822. {
  1823. $key = $this->getCacheKey();
  1824. CacheService::set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  1825. return $key;
  1826. }
  1827. /**
  1828. * 使用雪花算法生成订单ID
  1829. * @return string
  1830. * @throws \Exception
  1831. */
  1832. public function getCacheKey(string $prefix = '')
  1833. {
  1834. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  1835. //32位
  1836. if (PHP_INT_SIZE == 4) {
  1837. $id = abs($snowflake->id());
  1838. } else {
  1839. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  1840. }
  1841. return $prefix . $id;
  1842. }
  1843. /**获取用户购买活动产品的次数
  1844. * @param $uid
  1845. * @param $seckill_id
  1846. * @return int
  1847. */
  1848. public function activityProductCount(array $where)
  1849. {
  1850. return $this->dao->count($where);
  1851. }
  1852. /**
  1853. * 获取订单缓存信息
  1854. * @param int $uid
  1855. * @param string $key
  1856. * @return |null
  1857. */
  1858. public function getCacheOrderInfo(int $uid, string $key)
  1859. {
  1860. $cacheName = 'user_order_' . $uid . $key;
  1861. if (!CacheService::has($cacheName)) return null;
  1862. return CacheService::get($cacheName);
  1863. }
  1864. /**
  1865. * 获取拼团的订单id
  1866. * @param int $pid
  1867. * @param int $uid
  1868. * @return mixed
  1869. */
  1870. public function getStoreIdPink(int $pid, int $uid)
  1871. {
  1872. return $this->dao->value(['uid' => $uid, 'pink_id' => $pid, 'is_del' => 0], 'order_id');
  1873. }
  1874. /**
  1875. * 判断当前订单中是否有拼团
  1876. * @param int $pid
  1877. * @param int $uid
  1878. * @return int
  1879. */
  1880. public function getIsOrderPink($pid = 0, $uid = 0)
  1881. {
  1882. return $this->dao->count(['uid' => $uid, 'pink_id' => $pid, 'refund_status' => 0, 'is_del' => 0]);
  1883. }
  1884. /**
  1885. * 判断支付方式是否开启
  1886. * @param $payType
  1887. * @return bool
  1888. */
  1889. public function checkPaytype(string $payType)
  1890. {
  1891. $res = false;
  1892. switch ($payType) {
  1893. case PayServices::WEIXIN_PAY:
  1894. $res = sys_config('pay_weixin_open', '0') != '0';
  1895. break;
  1896. case PayServices::YUE_PAY:
  1897. $res = sys_config('balance_func_status') && sys_config('yue_pay_status') == 1;
  1898. break;
  1899. case 'offline':
  1900. $res = sys_config('offline_pay_status') == 1;
  1901. break;
  1902. case PayServices::ALIAPY_PAY:
  1903. $res = sys_config('ali_pay_status', '0') != '0';
  1904. break;
  1905. case PayServices::FRIEND:
  1906. $res = sys_config('friend_pay_status', 1) == 1;
  1907. break;
  1908. case PayServices::ALLIN_PAY:
  1909. $res = sys_config('allin_pay_status') == 1;
  1910. }
  1911. return $res;
  1912. }
  1913. /**
  1914. * 修改支付方式为线下支付
  1915. * @param string $orderId
  1916. * @return bool|\crmeb\basic\BaseModel
  1917. */
  1918. public function setOrderTypePayOffline(string $orderId)
  1919. {
  1920. if (($count = strpos($orderId, '_')) !== false) {
  1921. $orderId = substr($orderId, $count + 1);
  1922. }
  1923. if (sys_config('offline_postage', 0) == 1) {
  1924. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  1925. $cartInfoService = app()->make(StoreOrderCartInfoServices::class);
  1926. $cartInfo = $cartInfoService->getColumn(['oid' => $orderInfo['id']], 'cart_info', 'id');
  1927. foreach ($cartInfo as $key => &$item) {
  1928. $item_arr = json_decode($item, true);
  1929. $item_arr['postage_price'] = $item_arr['origin_postage_price'] = 0;
  1930. $cartInfoService->update(['id' => $key], ['cart_info' => json_encode($item_arr)]);
  1931. }
  1932. return $this->dao->update($orderId, [
  1933. 'pay_type' => 'offline',
  1934. 'pay_price' => bcsub((string)$orderInfo['pay_price'], (string)$orderInfo['pay_postage'], 2),
  1935. 'pay_postage' => 0
  1936. ], 'order_id');
  1937. }
  1938. return $this->dao->update($orderId, ['pay_type' => 'offline'], 'order_id');
  1939. }
  1940. /**
  1941. * 删除订单
  1942. * @param string $uni
  1943. * @param int $uid
  1944. * @return bool
  1945. * @throws \Psr\SimpleCache\InvalidArgumentException
  1946. */
  1947. public function removeOrder(string $uni, int $uid)
  1948. {
  1949. $order = $this->getUserOrderDetail($uni, $uid);
  1950. if (!$order) {
  1951. throw new ApiException(410173);
  1952. }
  1953. $order = $this->tidyOrder($order);
  1954. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  1955. throw new ApiException(410256);
  1956. $order->is_del = 1;
  1957. /** @var StoreOrderStatusServices $statusService */
  1958. $statusService = app()->make(StoreOrderStatusServices::class);
  1959. $res = $statusService->save([
  1960. 'oid' => $order['id'],
  1961. 'change_type' => 'remove_order',
  1962. 'change_message' => '删除订单',
  1963. 'change_time' => time()
  1964. ]);
  1965. if ($order->save() && $res) {
  1966. return true;
  1967. } else
  1968. throw new ApiException(100020);
  1969. }
  1970. /**
  1971. * 取消订单
  1972. * @param $order_id
  1973. * @param $uid
  1974. * @return bool|void
  1975. * @throws \think\db\exception\DataNotFoundException
  1976. * @throws \think\db\exception\DbException
  1977. * @throws \think\db\exception\ModelNotFoundException
  1978. */
  1979. public function cancelOrder($order_id, int $uid)
  1980. {
  1981. $order = $this->dao->getOne(['order_id' => $order_id, 'uid' => $uid, 'is_del' => 0]);
  1982. if (!$order) {
  1983. throw new ApiException(410173);
  1984. }
  1985. if ($order->paid) {
  1986. throw new ApiException(410257);
  1987. }
  1988. /** @var StoreOrderCartInfoServices $cartServices */
  1989. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1990. $cartInfo = $cartServices->getOrderCartInfo($order['id']);
  1991. /** @var StoreOrderRefundServices $refundServices */
  1992. $refundServices = app()->make(StoreOrderRefundServices::class);
  1993. $this->transaction(function () use ($refundServices, $order) {
  1994. $res = $refundServices->integralAndCouponBack($order, 'cancel') && $refundServices->regressionStock($order);
  1995. $order->is_cancel = 1;
  1996. if (!($res && $order->save())) {
  1997. throw new ApiException(100020);
  1998. }
  1999. });
  2000. //自定义事件-订单取消
  2001. event('CustomEventListener', ['order_cancel', [
  2002. 'uid' => $uid,
  2003. 'id' => $order['id'],
  2004. 'order_id' => $order_id,
  2005. 'real_name' => $order['id'],
  2006. 'user_phone' => $order['id'],
  2007. 'user_address' => $order['id'],
  2008. 'total_num' => $order['id'],
  2009. 'pay_price' => $order['id'],
  2010. 'deduction_price' => $order['id'],
  2011. 'coupon_price' => $order['id'],
  2012. 'cancel_time' => date('Y-m-d H:i:s'),
  2013. ]]);
  2014. return true;
  2015. }
  2016. /**
  2017. * 判断订单完成
  2018. * @param StoreProductReplyServices $replyServices
  2019. * @param array $uniqueList
  2020. * @param $oid
  2021. * @return mixed
  2022. */
  2023. public function checkOrderOver($replyServices, array $uniqueList, $oid)
  2024. {
  2025. //订单商品全部评价完成
  2026. $replyServices->count(['unique' => $uniqueList, 'oid' => $oid]);
  2027. if ($replyServices->count(['unique' => $uniqueList, 'oid' => $oid]) >= count($uniqueList)) {
  2028. $res = $this->dao->update(['id' => $oid, 'status' => 2], ['status' => 3]);
  2029. if (!$res) throw new ApiException(100007);
  2030. /** @var StoreOrderStatusServices $statusService */
  2031. $statusService = app()->make(StoreOrderStatusServices::class);
  2032. $statusService->save([
  2033. 'oid' => $oid,
  2034. 'change_type' => 'check_order_over',
  2035. 'change_message' => '用户评价',
  2036. 'change_time' => time()
  2037. ]);
  2038. $order = $this->dao->get((int)$oid, ['id,pid,status']);
  2039. if ($order && $order['pid'] > 0) {
  2040. $p_order = $this->dao->get((int)$order['pid'], ['id,pid,status']);
  2041. //主订单全部收货 且子订单没有待评价 有已完成
  2042. if ($p_order['status'] == 2 && !$this->dao->count(['pid' => $order['pid'], 'status' => 3]) && $this->dao->count(['pid' => $order['pid'], 'status' => 4])) {
  2043. $this->dao->update($p_order['id'], ['status' => 3]);
  2044. $statusService->save([
  2045. 'oid' => $p_order['id'],
  2046. 'change_type' => 'check_order_over',
  2047. 'change_message' => '用户评价',
  2048. 'change_time' => time()
  2049. ]);
  2050. }
  2051. }
  2052. }
  2053. }
  2054. /**
  2055. * 某个用户订单
  2056. * @param int $uid
  2057. * @param UserServices $userServices
  2058. * @return array
  2059. * @throws \think\db\exception\DataNotFoundException
  2060. * @throws \think\db\exception\DbException
  2061. * @throws \think\db\exception\ModelNotFoundException
  2062. */
  2063. public function getUserOrderList(int $uid)
  2064. {
  2065. /** @var UserServices $userServices */
  2066. $userServices = app()->make(UserServices::class);
  2067. $user = $userServices->getUserInfo($uid, 'uid');
  2068. if (!$user) {
  2069. throw new AdminException(100026);
  2070. }
  2071. [$page, $limit] = $this->getPageValue();
  2072. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  2073. $list = $this->dao->getStairOrderList($where, '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', $page, $limit);
  2074. $count = $this->dao->count($where);
  2075. return compact('list', 'count');
  2076. }
  2077. /**
  2078. * 获取推广订单列表
  2079. * @param int $uid
  2080. * @param $where
  2081. * @return array
  2082. * @throws \think\db\exception\DataNotFoundException
  2083. * @throws \think\db\exception\DbException
  2084. * @throws \think\db\exception\ModelNotFoundException
  2085. */
  2086. public function getUserStairOrderList(int $uid, $where)
  2087. {
  2088. $where_data = [];
  2089. if (isset($where['type'])) {
  2090. switch ((int)$where['type']) {
  2091. case 1:
  2092. $where_data['spread_uid'] = $uid;
  2093. break;
  2094. case 2:
  2095. $where_data['spread_two_uid'] = $uid;
  2096. break;
  2097. case 3:
  2098. $where_data['division_id'] = $uid;
  2099. break;
  2100. case 4:
  2101. $where_data['agent_id'] = $uid;
  2102. break;
  2103. default:
  2104. $where_data['all_spread'] = $uid;
  2105. break;
  2106. }
  2107. }
  2108. if (isset($where['data']) && $where['data']) {
  2109. $where_data['time'] = $where['data'];
  2110. }
  2111. if (isset($where['order_id']) && $where['order_id']) {
  2112. $where_data['order_id'] = $where['order_id'];
  2113. }
  2114. //推广订单只显示支付过并且未退款的订单
  2115. $where_data['paid'] = 1;
  2116. $where_data['refund_status'] = 0;
  2117. [$page, $limit] = $this->getPageValue();
  2118. $list = $this->dao->getStairOrderList($where_data, '*', $page, $limit);
  2119. $count = $this->dao->count($where_data);
  2120. return compact('list', 'count');
  2121. }
  2122. /**
  2123. * 订单导出
  2124. * @param array $where
  2125. * @return array
  2126. * @throws \think\db\exception\DataNotFoundException
  2127. * @throws \think\db\exception\DbException
  2128. * @throws \think\db\exception\ModelNotFoundException
  2129. */
  2130. public function getExportList(array $where)
  2131. {
  2132. $list = $this->dao->search($where)->order('id desc')->select()->toArray();
  2133. foreach ($list as &$item) {
  2134. /** @var StoreOrderCartInfoServices $orderCart */
  2135. $orderCart = app()->make(StoreOrderCartInfoServices::class);
  2136. $_info = $orderCart->getCartColunm(['oid' => $item['id']], 'cart_info', 'unique');
  2137. foreach ($_info as $k => $v) {
  2138. $cart_info = is_string($v) ? json_decode($v, true) : $v;
  2139. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  2140. $_info[$k] = $cart_info;
  2141. unset($cart_info);
  2142. }
  2143. $item['_info'] = $_info;
  2144. /** @var WechatUserServices $wechatUserService */
  2145. $wechatUserService = app()->make(WechatUserServices::class);
  2146. $item['sex'] = $wechatUserService->value(['uid' => $item['uid']], 'sex');
  2147. if ($item['pink_id'] || $item['combination_id']) {
  2148. /** @var StorePinkServices $pinkService */
  2149. $pinkService = app()->make(StorePinkServices::class);
  2150. $pinkStatus = $pinkService->value(['order_id_key' => $item['id']], 'status');
  2151. switch ($pinkStatus) {
  2152. case 1:
  2153. $item['pink_name'] = '[拼团订单]正在进行中';
  2154. $item['color'] = '#f00';
  2155. break;
  2156. case 2:
  2157. $item['pink_name'] = '[拼团订单]已完成';
  2158. $item['color'] = '#00f';
  2159. break;
  2160. case 3:
  2161. $item['pink_name'] = '[拼团订单]未完成';
  2162. $item['color'] = '#f0f';
  2163. break;
  2164. default:
  2165. $item['pink_name'] = '[拼团订单]历史订单';
  2166. $item['color'] = '#457856';
  2167. break;
  2168. }
  2169. } elseif ($item['seckill_id']) {
  2170. $item['pink_name'] = '[秒杀订单]';
  2171. $item['color'] = '#32c5e9';
  2172. } elseif ($item['bargain_id']) {
  2173. $item['pink_name'] = '[砍价订单]';
  2174. $item['color'] = '#12c5e9';
  2175. } else {
  2176. if ($item['shipping_type'] == 1) {
  2177. $item['pink_name'] = '[普通订单]';
  2178. $item['color'] = '#895612';
  2179. } else if ($item['shipping_type'] == 2) {
  2180. $item['pink_name'] = '[核销订单]';
  2181. $item['color'] = '#8956E8';
  2182. }
  2183. }
  2184. }
  2185. return $list;
  2186. }
  2187. /**
  2188. * 自动取消订单
  2189. * @return bool
  2190. * @throws \think\db\exception\DataNotFoundException
  2191. * @throws \think\db\exception\DbException
  2192. * @throws \think\db\exception\ModelNotFoundException
  2193. */
  2194. public function orderUnpaidCancel()
  2195. {
  2196. //系统预设取消订单时间段
  2197. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  2198. //获取配置
  2199. $systemValue = SystemConfigService::more($keyValue);
  2200. //格式化数据
  2201. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  2202. $list = $this->dao->getOrderUnPaidList();
  2203. /** @var StoreOrderRefundServices $refundServices */
  2204. $refundServices = app()->make(StoreOrderRefundServices::class);
  2205. foreach ($list as $order) {
  2206. if ($order['pink_id'] || $order['combination_id']) {
  2207. $secs = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  2208. } elseif ($order['seckill_id']) {
  2209. $secs = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  2210. } elseif ($order['bargain_id']) {
  2211. $secs = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  2212. } else {
  2213. $secs = $systemValue['order_cancel_time'];
  2214. }
  2215. if ($secs == 0) return true;
  2216. if (($order['add_time'] + bcmul($secs, '3600', 0)) < time()) {
  2217. try {
  2218. $this->transaction(function () use ($order, $refundServices) {
  2219. //回退积分和优惠卷
  2220. $res = $refundServices->integralAndCouponBack($order, 'cancel');
  2221. //回退库存和销量
  2222. $res = $res && $refundServices->regressionStock($order);
  2223. //修改订单状态
  2224. $res = $res && $this->dao->update($order['id'], ['is_cancel' => 1, 'mark' => '订单未支付已超过系统预设时间']);
  2225. if (!$res) {
  2226. Log::error('订单号' . $order['order_id'] . '自动取消订单失败');
  2227. }
  2228. return true;
  2229. });
  2230. /** @var StoreOrderCartInfoServices $cartServices */
  2231. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  2232. $cartInfo = $cartServices->getOrderCartInfo((int)$order['id']);
  2233. } catch (\Throwable $e) {
  2234. Log::error('自动取消订单失败,失败原因:' . $e->getMessage(), $e->getTrace());
  2235. }
  2236. }
  2237. }
  2238. }
  2239. /**根据时间获取当天或昨天订单营业额
  2240. * @param array $where
  2241. * @return float|int
  2242. */
  2243. public function getOrderMoneyByWhere(array $where, string $sum_field, string $selectType, string $group = "")
  2244. {
  2245. switch ($selectType) {
  2246. case "sum" :
  2247. return $this->dao->getDayTotalMoney($where, $sum_field);
  2248. case "group" :
  2249. return $this->dao->getDayGroupMoney($where, $sum_field, $group);
  2250. }
  2251. }
  2252. /**统计时间段订单数
  2253. * @param array $where
  2254. * @param string $sum_field
  2255. */
  2256. public function getOrderCountByWhere(array $where)
  2257. {
  2258. return $this->dao->getDayOrderCount($where);
  2259. }
  2260. /**分组统计时间段订单数
  2261. * @param $where
  2262. * @return mixed
  2263. */
  2264. public function getOrderGroupCountByWhere($where)
  2265. {
  2266. return $this->dao->getOrderGroupCount($where);
  2267. }
  2268. /** 时间段支付订单人数
  2269. * @param $where
  2270. * @return mixed
  2271. */
  2272. public function getPayOrderPeopleByWhere($where)
  2273. {
  2274. return $this->dao->getPayOrderPeople($where);
  2275. }
  2276. /**时间段分组统计支付订单人数
  2277. * @param $where
  2278. * @return mixed
  2279. */
  2280. public function getPayOrderGroupPeopleByWhere($where)
  2281. {
  2282. return $this->dao->getPayOrderGroupPeople($where);
  2283. }
  2284. /**
  2285. * 退款订单列表
  2286. * @param array $where
  2287. * @return array
  2288. * @throws \think\db\exception\DataNotFoundException
  2289. * @throws \think\db\exception\DbException
  2290. * @throws \think\db\exception\ModelNotFoundException
  2291. */
  2292. public function refundList(array $where)
  2293. {
  2294. [$page, $limit] = $this->getPageValue();
  2295. if ($where['refund_reason_time'] != '') $where['refund_reason_time'] = explode('-', $where['refund_reason_time']);
  2296. $data = $this->dao->getRefundList($where, $page, $limit);
  2297. if ($data['list']) $data['list'] = $this->tidyOrderList($data['list']);
  2298. $data['num'] = [
  2299. 0 => ['name' => '全部', 'num' => $this->dao->count(['refund_type' => 0, 'is_system_del' => 0])],
  2300. 1 => ['name' => '仅退款', 'num' => $this->dao->count(['refund_type' => 1, 'is_system_del' => 0])],
  2301. 2 => ['name' => '退货退款', 'num' => $this->dao->count(['refund_type' => 2, 'is_system_del' => 0])],
  2302. 3 => ['name' => '拒绝退款', 'num' => $this->dao->count(['refund_type' => 3, 'is_system_del' => 0])],
  2303. 4 => ['name' => '商品待退货', 'num' => $this->dao->count(['refund_type' => 4, 'is_system_del' => 0])],
  2304. 5 => ['name' => '退货待收货', 'num' => $this->dao->count(['refund_type' => 5, 'is_system_del' => 0])],
  2305. 6 => ['name' => '已退款', 'num' => $this->dao->count(['refund_type' => 6, 'is_system_del' => 0])]
  2306. ];
  2307. return $data;
  2308. }
  2309. /**
  2310. * 商家同意退款,等待客户退货
  2311. * @param $order_id
  2312. * @return bool
  2313. */
  2314. public function agreeRefund($order_id)
  2315. {
  2316. $res = $this->dao->update(['id' => $order_id], ['refund_type' => 4]);
  2317. /** @var StoreOrderStatusServices $statusService */
  2318. $statusService = app()->make(StoreOrderStatusServices::class);
  2319. $statusService->save([
  2320. 'oid' => $order_id,
  2321. 'change_type' => 'refund_express',
  2322. 'change_message' => '等待用户退货',
  2323. 'change_time' => time()
  2324. ]);
  2325. if ($res) return true;
  2326. throw new AdminException(100005);
  2327. }
  2328. /**
  2329. * @param array $where
  2330. * @param array|string[] $field
  2331. * @param array $with
  2332. * @param int $page
  2333. * @param int $limit
  2334. * @param string $order
  2335. * @return array
  2336. * @throws \think\db\exception\DataNotFoundException
  2337. * @throws \think\db\exception\DbException
  2338. * @throws \think\db\exception\ModelNotFoundException
  2339. */
  2340. public function getSplitOrderList(array $where, array $field = ['*'], array $with = [], $page = 0, $limit = 0, $order = 'pay_time DESC,id DESC')
  2341. {
  2342. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with, $order);
  2343. if ($data) {
  2344. $data = $this->tidyOrderList($data);
  2345. }
  2346. return $data;
  2347. }
  2348. /**
  2349. * 代付详情
  2350. * @param $orderId
  2351. * @param $uid
  2352. * @return array
  2353. * @throws \think\db\exception\DataNotFoundException
  2354. * @throws \think\db\exception\DbException
  2355. * @throws \think\db\exception\ModelNotFoundException
  2356. */
  2357. public function getFriendDetail($orderId, $uid)
  2358. {
  2359. $orderInfo = $this->dao->getOne(['id' => $orderId, 'is_del' => 0]);
  2360. if ($orderInfo) {
  2361. $orderInfo = $orderInfo->toArray();
  2362. } else {
  2363. throw new ApiException(410173);
  2364. }
  2365. $orderInfo = $this->tidyOrder($orderInfo, true);
  2366. /** @var UserServices $userServices */
  2367. $userServices = app()->make(UserServices::class);
  2368. $userInfo = $userServices->get($orderInfo['uid']);
  2369. $friendInfo = $userServices->get($orderInfo['pay_uid']);
  2370. $info = [
  2371. 'id' => $orderInfo['id'],
  2372. 'order_id' => $orderInfo['order_id'],
  2373. 'uid' => $orderInfo['uid'],
  2374. 'avatar' => $userInfo['avatar'],
  2375. 'nickname' => $userInfo['nickname'],
  2376. 'cartInfo' => $orderInfo['cartInfo'],
  2377. 'paid' => $orderInfo['paid'],
  2378. 'total_num' => $orderInfo['total_num'],
  2379. 'pay_price' => $orderInfo['pay_price'],
  2380. 'type' => $uid == $orderInfo['uid'] ? 0 : 1,
  2381. 'pay_uid' => isset($friendInfo) ? $friendInfo['uid'] : 0,
  2382. 'pay_nickname' => isset($friendInfo) ? $friendInfo['nickname'] : '',
  2383. 'pay_avatar' => isset($friendInfo) ? $friendInfo['avatar'] : '',
  2384. ];
  2385. return $info;
  2386. }
  2387. /**
  2388. * 获取退货商品列表
  2389. * @param array $cart_ids
  2390. * @param int $id
  2391. * @return array
  2392. * @throws \think\db\exception\DataNotFoundException
  2393. * @throws \think\db\exception\DbException
  2394. * @throws \think\db\exception\ModelNotFoundException
  2395. */
  2396. public function refundCartInfoList(array $cart_ids = [], int $id = 0)
  2397. {
  2398. $orderInfo = $this->dao->get($id);
  2399. if (!$orderInfo) {
  2400. throw new ApiException(410173);
  2401. }
  2402. $orderInfo = $this->tidyOrder($orderInfo, true);
  2403. $cartInfo = $orderInfo['cartInfo'] ?? [];
  2404. $data = [];
  2405. if ($cart_ids) {
  2406. foreach ($cart_ids as $cart) {
  2407. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num'] || $cart['cart_num'] <= 0) {
  2408. throw new ApiException(410223);
  2409. }
  2410. }
  2411. $cart_ids = array_combine(array_column($cart_ids, 'cart_id'), $cart_ids);
  2412. $i = 0;
  2413. foreach ($cartInfo as $item) {
  2414. if (isset($cart_ids[$item['id']])) {
  2415. $data['cartInfo'][$i] = $item;
  2416. if (isset($cart_ids[$item['id']]['cart_num'])) $data['cartInfo'][$i]['cart_num'] = $cart_ids[$item['id']]['cart_num'];
  2417. $i++;
  2418. }
  2419. }
  2420. }
  2421. $data['_status'] = $orderInfo['_status'] ?? [];
  2422. $data['_status']['_is_back'] = $orderInfo['delivery_type'] != 'fictitious' && $orderInfo['virtual_type'] == 0;
  2423. $data['cartInfo'] = $data['cartInfo'] ?? $cartInfo;
  2424. return $data;
  2425. }
  2426. /**
  2427. * 再次下单
  2428. * @param string $uni
  2429. * @param int $uid
  2430. * @return array
  2431. */
  2432. public function againOrder(StoreCartServices $services, string $uni, int $uid): array
  2433. {
  2434. if (!$uni) throw new ApiException(100100);
  2435. $order = $this->getUserOrderDetail($uni, $uid);
  2436. if (!$order) throw new ApiException(410173);
  2437. $order = $this->tidyOrder($order, true);
  2438. $cateId = [];
  2439. foreach ($order['cartInfo'] as $v) {
  2440. if ($v['combination_id']) throw new ApiException(410258);
  2441. elseif ($v['bargain_id']) throw new ApiException(410259);
  2442. elseif ($v['seckill_id']) throw new ApiException(410260);
  2443. elseif ($v['advance_id']) throw new ApiException(410261);
  2444. else $cateId[] = $services->setCart($uid, (int)$v['product_id'], (int)$v['cart_num'], $v['productInfo']['attrInfo']['unique'] ?? '', '0', true);
  2445. }
  2446. if (!$cateId) throw new ApiException(410262);
  2447. return $cateId;
  2448. }
  2449. /**
  2450. * 支付宝单独支付
  2451. * @param OrderPayServices $payServices
  2452. * @param OtherOrderServices $services
  2453. * @param string $key
  2454. * @param string $quitUrl
  2455. * @return array|string
  2456. * @throws \think\db\exception\DataNotFoundException
  2457. * @throws \think\db\exception\DbException
  2458. * @throws \think\db\exception\ModelNotFoundException
  2459. */
  2460. public function aliPayOrder(OrderPayServices $payServices, OtherOrderServices $services, string $key, string $quitUrl)
  2461. {
  2462. if (!$key) {
  2463. throw new ApiException(100100);
  2464. }
  2465. if (!$quitUrl) {
  2466. throw new ApiException(100100);
  2467. }
  2468. $orderCache = CacheService::get($key);
  2469. if (!$orderCache || !isset($orderCache['order_id'])) {
  2470. throw new ApiException(410263);
  2471. }
  2472. $payType = isset($orderCache['other_pay_type']) && $orderCache['other_pay_type'] == true;
  2473. if ($payType) {
  2474. $orderInfo = $services->getOne(['order_id' => $orderCache['order_id'], 'is_del' => 0, 'paid' => 0]);
  2475. } else {
  2476. $orderInfo = $this->get(['order_id' => $orderCache['order_id'], 'paid' => 0, 'is_del' => 0]);
  2477. }
  2478. if (!$orderInfo) {
  2479. throw new ApiException(410264);
  2480. }
  2481. return $payServices->beforePay($orderInfo->toArray(), PayServices::ALIAPY_PAY, ['quitUrl' => $quitUrl]);
  2482. }
  2483. /**
  2484. * 用户订单信息
  2485. * @param StoreOrderEconomizeServices $services
  2486. * @param string $uni
  2487. * @param int $uid
  2488. * @return void
  2489. * @throws \think\db\exception\DataNotFoundException
  2490. * @throws \think\db\exception\DbException
  2491. * @throws \think\db\exception\ModelNotFoundException
  2492. */
  2493. public function getUserOrderByKey(StoreOrderEconomizeServices $services, string $uni, int $uid): array
  2494. {
  2495. $order = $this->getUserOrderDetail($uni, $uid, ['split', 'invoice', 'user']);
  2496. if (!$order) throw new ApiException(410294);
  2497. $order = $order->toArray();
  2498. $splitNum = [];
  2499. //是否开启门店自提
  2500. $store_self_mention = sys_config('store_self_mention');
  2501. //关闭门店自提后 订单隐藏门店信息
  2502. if ($store_self_mention == 0) $order['shipping_type'] = 1;
  2503. if ($order['verify_code']) {
  2504. $verify_code = $order['verify_code'];
  2505. $verify[] = substr($verify_code, 0, 4);
  2506. $verify[] = substr($verify_code, 4, 4);
  2507. $verify[] = substr($verify_code, 8);
  2508. $order['_verify_code'] = implode(' ', $verify);
  2509. }
  2510. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  2511. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  2512. $order['system_store'] = false;
  2513. if ($order['store_id']) {
  2514. /** @var SystemStoreServices $storeServices */
  2515. $storeServices = app()->make(SystemStoreServices::class);
  2516. $order['system_store'] = $storeServices->getStoreDispose($order['store_id']);
  2517. }
  2518. $order['code'] = '';
  2519. if (($order['shipping_type'] === 2 || $order['delivery_uid'] != 0) && $order['verify_code']) {
  2520. // $name = $order['verify_code'] . '.jpg';
  2521. // /** @var SystemAttachmentServices $attachmentServices */
  2522. // $attachmentServices = app()->make(SystemAttachmentServices::class);
  2523. // $imageInfo = $attachmentServices->getInfo(['name' => $name]);
  2524. // $siteUrl = sys_config('site_url');
  2525. // if (!$imageInfo) {
  2526. // $imageInfo = PosterServices::getQRCodePath($order['verify_code'], $name);
  2527. // if (is_array($imageInfo)) {
  2528. // $attachmentServices->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  2529. // $url = $imageInfo['dir'];
  2530. // } else
  2531. // $url = '';
  2532. // } else $url = $imageInfo['att_dir'];
  2533. // if (isset($imageInfo['image_type']) && $imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  2534. // $order['code'] = $url;
  2535. try {
  2536. $verifyName = 'verify_code_' . $order['verify_code'] . '.jpg';
  2537. $data = 'verify_code=' . $order['verify_code'];
  2538. /** @var SystemAttachmentServices $systemAttachmentService */
  2539. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  2540. $imageInfo = $systemAttachmentService->getOne(['name' => $verifyName]);
  2541. $siteUrl = sys_config('site_url');
  2542. if (!$imageInfo) {
  2543. $res = MiniProgramService::appCodeUnlimitService($data, 'pages/admin/order_cancellation/index', 280);
  2544. if (!$res) throw new ApiException('小程序核销码生成失败');
  2545. $uploadType = (int)sys_config('upload_type', 1);
  2546. $upload = UploadService::init();
  2547. $res = (string)EntityBody::factory($res);
  2548. $res = $upload->to('routine/product')->validate()->setAuthThumb(false)->stream($res, $verifyName);
  2549. if ($res === false) throw new ApiException('小程序核销码生成失败');
  2550. $imageInfo = $upload->getUploadInfo();
  2551. $imageInfo['image_type'] = $uploadType;
  2552. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  2553. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  2554. if (!$remoteImage['status']) throw new ApiException('小程序核销码生成失败');
  2555. $systemAttachmentService->save([
  2556. 'name' => $imageInfo['name'],
  2557. 'att_dir' => $imageInfo['dir'],
  2558. 'satt_dir' => $imageInfo['thumb_path'],
  2559. 'att_size' => $imageInfo['size'],
  2560. 'att_type' => $imageInfo['type'],
  2561. 'image_type' => $imageInfo['image_type'],
  2562. 'module_type' => 2,
  2563. 'time' => time(),
  2564. 'pid' => 1,
  2565. 'type' => 2
  2566. ]);
  2567. $url = $imageInfo['dir'];
  2568. } else $url = $imageInfo['att_dir'];
  2569. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  2570. $order['code'] = $url;
  2571. } catch (\Exception $e) {
  2572. }
  2573. }
  2574. $order['mapKey'] = sys_config('tengxun_map_key');
  2575. $order['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  2576. $order['pay_weixin_open'] = sys_config('pay_weixin_open') != '0';//微信支付 1 开启 0 关闭
  2577. $order['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  2578. $order['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  2579. $orderData = $this->tidyOrder($order, true, true);
  2580. $vipTruePrice = $memberPrice = $levelPrice = 0;
  2581. foreach ($orderData['cartInfo'] ?? [] as $key => $cart) {
  2582. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  2583. if ($cart['price_type'] == 'member') $memberPrice = bcadd((string)$memberPrice, (string)$cart['vip_sum_truePrice'], 2);
  2584. if ($cart['price_type'] == 'level') $levelPrice = bcadd((string)$levelPrice, (string)$cart['vip_sum_truePrice'], 2);
  2585. if (isset($splitNum[$cart['id']])) {
  2586. $orderData['cartInfo'][$key]['cart_num'] = $cart['cart_num'] - $splitNum[$cart['id']];
  2587. if ($orderData['cartInfo'][$key]['cart_num'] == 0) unset($orderData['cartInfo'][$key]);
  2588. }
  2589. }
  2590. $orderData['cartInfo'] = array_merge($orderData['cartInfo']);
  2591. $orderData['vip_true_price'] = $vipTruePrice;
  2592. $orderData['levelPrice'] = $levelPrice;
  2593. $orderData['memberPrice'] = $memberPrice;
  2594. $economize = $services->get(['order_id' => $order['order_id']], ['postage_price', 'member_price']);
  2595. if ($economize) {
  2596. $orderData['postage_price'] = $economize['postage_price'];
  2597. $orderData['member_price'] = $economize['member_price'];
  2598. } else {
  2599. $orderData['postage_price'] = 0;
  2600. $orderData['member_price'] = 0;
  2601. }
  2602. $orderData['routine_contact_type'] = sys_config('routine_contact_type', 0);
  2603. /** @var UserInvoiceServices $userInvoice */
  2604. $userInvoice = app()->make(UserInvoiceServices::class);
  2605. $invoice_func = $userInvoice->invoiceFuncStatus();
  2606. $orderData['invoice_func'] = $invoice_func['invoice_func'];
  2607. $orderData['special_invoice'] = $invoice_func['special_invoice'];
  2608. $orderData['refund_cartInfo'] = $orderData['cartInfo'];
  2609. $orderData['refund_total_num'] = $orderData['total_num'];
  2610. $orderData['refund_pay_price'] = $orderData['pay_price'];
  2611. $orderData['is_apply_refund'] = true;
  2612. $orderData['help_info'] = [
  2613. 'pay_uid' => $orderData['pay_uid'],
  2614. 'pay_nickname' => '',
  2615. 'pay_avatar' => '',
  2616. 'help_status' => 0
  2617. ];
  2618. $orderData['gift_user_info'] = [
  2619. 'gift_uid' => $orderData['gift_uid'],
  2620. 'gift_nickname' => '',
  2621. 'gift_avatar' => '',
  2622. ];
  2623. if ($orderData['uid'] != $orderData['pay_uid']) {
  2624. /** @var UserServices $userServices */
  2625. $userServices = app()->make(UserServices::class);
  2626. $payUser = $userServices->get($orderData['pay_uid'], ['nickname', 'avatar']);
  2627. $orderData['help_info'] = [
  2628. 'pay_uid' => $orderData['pay_uid'],
  2629. 'pay_nickname' => $payUser['nickname'],
  2630. 'pay_avatar' => $payUser['avatar'],
  2631. 'help_status' => 1
  2632. ];
  2633. }
  2634. if ($orderData['gift_uid'] != 0) {
  2635. /** @var UserServices $userServices */
  2636. $userServices = app()->make(UserServices::class);
  2637. $giftUser = $userServices->get($orderData['gift_uid'], ['nickname', 'avatar']);
  2638. $orderData['gift_user_info'] = [
  2639. 'gift_uid' => $orderData['gift_uid'],
  2640. 'gift_nickname' => $giftUser['nickname'],
  2641. 'gift_avatar' => $giftUser['avatar'],
  2642. ];
  2643. }
  2644. // 判断是否开启小程序订单管理
  2645. $orderData['order_shipping_open'] = false;
  2646. if (sys_config('order_shipping_open', 0) && $order['pay_price'] > 0 && $order['is_channel'] == 1 && $order['pay_type'] == 'weixin' && MiniOrderService::isManaged()) {
  2647. // 判断是否存在子未收货子订单
  2648. if ($order['pid'] > 0) {
  2649. if ($this->checkSubOrderNotTake((int)$order['pid'], (int)$order['id'])) {
  2650. $orderData['order_shipping_open'] = true;
  2651. }
  2652. } else {
  2653. $orderData['order_shipping_open'] = true;
  2654. }
  2655. }
  2656. $orderData['is_refund_available'] = $this->isRefundAvailable((int)$order['id']);
  2657. $orderData['gift_key'] = $orderData['gift_code'] = '';
  2658. if ($order['is_gift'] == 1) {
  2659. $orderData['gift_key'] = md5($order['id'] . '_' . $order['order_id'] . '_' . $order['uid']);
  2660. /** @var QrcodeServices $qrcodeService */
  2661. $qrcodeService = app()->make(QrcodeServices::class);
  2662. $orderData['gift_code'] = $qrcodeService->getRoutineQrcodePath($order['id'], $order['uid'], 7, ['gift_key' => $orderData['gift_key']]);
  2663. }
  2664. $orderData['avatar'] = set_file_url($orderData['avatar']);
  2665. return $orderData;
  2666. }
  2667. /**
  2668. * 检测订单是否能退款
  2669. * @param $oid
  2670. * @return bool
  2671. * @author: 吴汐
  2672. * @email: 442384644@qq.com
  2673. * @date: 2023/10/11
  2674. */
  2675. public function isRefundAvailable($oid)
  2676. {
  2677. $refundTimeAvailable = (int)sys_config('refund_time_available');
  2678. if ($refundTimeAvailable == 0) return true;
  2679. $statusInfo = app()->make(StoreOrderStatusServices::class)->get(['oid' => $oid, 'change_type' => 'take_delivery']);
  2680. if (!$statusInfo) return true;
  2681. $changeTime = preg_match('/^\d+$/', $statusInfo['change_time']) ? intval($statusInfo['change_time']) : strtotime($statusInfo['change_time']);
  2682. if (($changeTime + ($refundTimeAvailable * 86400)) < time()) {
  2683. return false;
  2684. }
  2685. return true;
  2686. }
  2687. /**
  2688. * 获取确认订单页面是否展示快递配送和到店自提
  2689. * @param $uid
  2690. * @param $cartIds
  2691. * @param $new
  2692. * @return array
  2693. * @throws \Psr\SimpleCache\InvalidArgumentException
  2694. */
  2695. public function checkShipping($uid, $cartIds, $new)
  2696. {
  2697. if ($new) {
  2698. $cartIds = explode(',', $cartIds);
  2699. $cartInfo = [];
  2700. foreach ($cartIds as $key) {
  2701. $info = CacheService::get($key);
  2702. if ($info) {
  2703. $cartInfo[] = $info;
  2704. }
  2705. }
  2706. } else {
  2707. /** @var StoreCartServices $cartServices */
  2708. $cartServices = app()->make(StoreCartServices::class);
  2709. $cartInfo = $cartServices->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  2710. }
  2711. if (!$cartInfo) {
  2712. throw new ApiException(100026);
  2713. }
  2714. $arr = [];
  2715. foreach ($cartInfo as $item) {
  2716. $arr[] = $item['productInfo']['logistics'];
  2717. }
  2718. $res = array_unique(explode(',', implode(',', $arr)));
  2719. if (count($res) == 2) {
  2720. return ['type' => 0];
  2721. } else {
  2722. if ($res[0] == 2 && sys_config('store_self_mention') == 0) {
  2723. return ['type' => 1];
  2724. }
  2725. return ['type' => (int)$res[0]];
  2726. }
  2727. }
  2728. /**
  2729. * 自动评价
  2730. * @return bool
  2731. */
  2732. public function autoComment()
  2733. {
  2734. //自动评价天数
  2735. $systemCommentTime = sys_config('system_comment_time', 0);
  2736. //0为取消自动默认好评功能
  2737. if ($systemCommentTime == 0) {
  2738. return true;
  2739. }
  2740. $sevenDay = bcsub((string)time(), bcmul((string)$systemCommentTime, '86400'));
  2741. /** @var StoreOrderStoreOrderStatusServices $service */
  2742. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  2743. $orderList = $service->getTakeOrderIds([
  2744. 'change_time' => $sevenDay,
  2745. 'is_del' => 0,
  2746. 'paid' => 1,
  2747. 'status' => 2,
  2748. 'change_type' => ['take_delivery', 'user_take_delivery']
  2749. ], 30);
  2750. foreach ($orderList as $item) {
  2751. AutoCommentJob::dispatch([$item['id'], $item['cart_id']]);
  2752. }
  2753. return true;
  2754. }
  2755. /**
  2756. * @param int $uid
  2757. * @param string $orderId
  2758. * @param string $type
  2759. * @return array
  2760. * @throws \think\db\exception\DataNotFoundException
  2761. * @throws \think\db\exception\DbException
  2762. * @throws \think\db\exception\ModelNotFoundException
  2763. * @author 等风来
  2764. * @email 136327134@qq.com
  2765. * @date 2023/2/13
  2766. */
  2767. public function getCashierInfo(int $uid, string $orderId, string $type)
  2768. {
  2769. //支付类型开关
  2770. $data = [
  2771. 'ali_pay_status' => sys_config('ali_pay_status', '0') != '0',
  2772. 'wechat_pay_status' => sys_config('pay_weixin_open', '0') != '0',
  2773. 'offline_pay_status' => (int)sys_config('offline_pay_status') == 1,
  2774. 'friend_pay_status' => (int)sys_config('friend_pay_status') == 1,
  2775. 'yue_pay_status' => (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1,
  2776. ];
  2777. $data['order_id'] = $orderId;
  2778. $data['pay_price'] = '0';
  2779. $data['now_money'] = app()->make(UserServices::class)->value(['uid' => $uid], 'now_money');
  2780. switch ($type) {
  2781. case 'order':
  2782. $info = $this->dao->get(['order_id' => $orderId], ['id', 'pay_price', 'add_time', 'combination_id', 'seckill_id', 'bargain_id', 'pay_postage', 'is_gift']);
  2783. if (!$info) {
  2784. throw new PayException('您支付的订单不存在');
  2785. }
  2786. $orderCancelTime = sys_config('order_cancel_time', 0);
  2787. $orderActivityTime = sys_config('order_activity_time', 0);
  2788. if ($info->combination_id) {
  2789. $time = (sys_config('order_pink_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2790. } else if ($info->seckill_id) {
  2791. $time = (sys_config('order_seckill_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2792. } else if ($info->bargain_id) {
  2793. $time = (sys_config('order_bargain_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2794. } else {
  2795. $time = $orderCancelTime * 60 * 60 + ((int)$info->add_time);
  2796. }
  2797. if ($time < 0) {
  2798. $time = 0;
  2799. }
  2800. $data['pay_price'] = $info['pay_price'];
  2801. $data['pay_postage'] = $info['pay_postage'];
  2802. $data['offline_postage'] = (int)sys_config('offline_postage', 0);
  2803. $data['invalid_time'] = $time;
  2804. $data['oid'] = $info['id'];
  2805. $data['is_gift'] = $info['is_gift'];
  2806. break;
  2807. case 'svip':
  2808. $info = app()->make(OtherOrderServices::class)->get(['order_id' => $orderId], ['id', 'pay_price', 'add_time']);
  2809. if (!$info) {
  2810. throw new PayException('您支付的订单不存在');
  2811. }
  2812. $data['pay_price'] = $info['pay_price'];
  2813. $data['invalid_time'] = $info->add_time + 86400;
  2814. break;
  2815. case 'recharge':
  2816. $info = app()->make(UserRechargeServices::class)->get(['order_id' => $orderId], ['id', 'price', 'add_time']);
  2817. if (!$info) {
  2818. throw new PayException('您支付的订单不存在');
  2819. }
  2820. $data['pay_price'] = $info['price'];
  2821. $data['invalid_time'] = $info->add_time + 86400;
  2822. break;
  2823. default:
  2824. throw new PayException('暂不支持其他类型订单支付');
  2825. }
  2826. return $data;
  2827. }
  2828. /**
  2829. * 取消商家寄件
  2830. * @param int $id
  2831. * @param string $msg
  2832. * @return array|mixed
  2833. * @throws \think\db\exception\DataNotFoundException
  2834. * @throws \think\db\exception\DbException
  2835. * @throws \think\db\exception\ModelNotFoundException
  2836. * @author 等风来
  2837. * @email 136327134@qq.com
  2838. * @date 2023/5/15
  2839. */
  2840. public function shipmentCancelOrder(int $id, string $msg)
  2841. {
  2842. $orderInfo = $this->dao->get($id);
  2843. if (!$orderInfo) {
  2844. throw new AdminException('取消的订单不存在');
  2845. }
  2846. if (!$orderInfo->kuaidi_task_id || !$orderInfo->kuaidi_order_id) {
  2847. throw new AdminException('商家寄件订单信息不存在,无法取消');
  2848. }
  2849. if ($orderInfo->is_stock_up != 1) {
  2850. throw new AdminException('订单状态不正确,无法取消寄件');
  2851. }
  2852. //发起取消商家寄件
  2853. app()->make(ServeServices::class)->express()->shipmentCancelOrder([
  2854. 'task_id' => $orderInfo->kuaidi_task_id,
  2855. 'order_id' => $orderInfo->kuaidi_order_id,
  2856. 'cancel_msg' => $msg,
  2857. ]);
  2858. //订单返回原状态
  2859. $this->transaction(function () use ($id, $msg, $orderInfo) {
  2860. app()->make(StoreOrderStatusServices::class)->save([
  2861. 'oid' => $id,
  2862. 'change_time' => time(),
  2863. 'change_type' => 'delivery_goods_cancel',
  2864. 'change_message' => '已取消发货,取消原因:' . $msg
  2865. ]);
  2866. $orderInfo->status = 0;
  2867. $orderInfo->is_stock_up = 0;
  2868. $orderInfo->kuaidi_task_id = '';
  2869. $orderInfo->kuaidi_order_id = '';
  2870. $orderInfo->express_dump = '';
  2871. $orderInfo->kuaidi_label = '';
  2872. $orderInfo->delivery_id = '';
  2873. $orderInfo->delivery_code = '';
  2874. $orderInfo->delivery_name = '';
  2875. $orderInfo->delivery_type = '';
  2876. $orderInfo->save();
  2877. });
  2878. return true;
  2879. }
  2880. /**
  2881. * 判断订单是否全部发货
  2882. * @param int $pid
  2883. * @param int $order_id
  2884. * @return bool
  2885. * @author: 吴汐
  2886. * @email: 442384644@qq.com
  2887. * @date: 2023/8/31
  2888. */
  2889. public function checkSubOrderNotSend(int $pid, int $order_id)
  2890. {
  2891. $order_count = $this->dao->getSubOrderNotSend($pid, $order_id);
  2892. if ($order_count > 0) {
  2893. return false;
  2894. } else {
  2895. return true;
  2896. }
  2897. }
  2898. /**
  2899. * 判断是否存在子未收货子订单
  2900. * @param int $pid
  2901. * @param int $order_id
  2902. * @return bool
  2903. * @author: 吴汐
  2904. * @email: 442384644@qq.com
  2905. * @date: 2023/8/31
  2906. */
  2907. public function checkSubOrderNotTake(int $pid, int $order_id)
  2908. {
  2909. $order_count = $this->dao->getSubOrderNotTake($pid, $order_id);
  2910. if ($order_count > 0) {
  2911. return false;
  2912. } else {
  2913. return true;
  2914. }
  2915. }
  2916. /**
  2917. * 配货单数据
  2918. * @param $oid
  2919. * @return array
  2920. * @throws \think\db\exception\DataNotFoundException
  2921. * @throws \think\db\exception\DbException
  2922. * @throws \think\db\exception\ModelNotFoundException
  2923. * @author: 吴汐
  2924. * @email: 442384644@qq.com
  2925. * @date: 2023/10/11
  2926. */
  2927. public function printShippingData($order_id)
  2928. {
  2929. $orderInfo = $this->dao->get(['order_id' => $order_id]);
  2930. if (!$orderInfo) {
  2931. throw new AdminException('订单不存在');
  2932. }
  2933. $orderInfo = $this->tidyOrder($orderInfo->toArray(), true);
  2934. $data['user_name'] = $orderInfo['real_name'];
  2935. $data['user_phone'] = $orderInfo['user_phone'];
  2936. $data['user_address'] = $orderInfo['user_address'];
  2937. $data['order_id'] = $orderInfo['order_id'];
  2938. $data['pay_time'] = $orderInfo['_pay_time'];
  2939. $data['pay_type'] = $orderInfo['_status']['_payType'];
  2940. $data['pay_price'] = $orderInfo['pay_price'];
  2941. $data['pay_postage'] = $orderInfo['pay_postage'];
  2942. $data['deduction_price'] = $orderInfo['deduction_price'];
  2943. $data['coupon_price'] = $orderInfo['coupon_price'];
  2944. $data['mark'] = $orderInfo['mark'];
  2945. $data['product_info'] = [];
  2946. $data['vip_price'] = 0;
  2947. foreach ($orderInfo['cartInfo'] as $item) {
  2948. $data['product_info'][] = [
  2949. 'name' => $item['productInfo']['store_name'],
  2950. 'sku' => $item['attrInfo']['suk'],
  2951. 'price' => $item['sum_price'],
  2952. 'num' => $item['cart_num'],
  2953. 'sum_price' => bcmul((string)$item['sum_price'], (string)$item['cart_num'], 2)
  2954. ];
  2955. $data['vip_price'] = bcadd((string)$data['vip_price'], $item['vip_sum_truePrice'], 2);
  2956. }
  2957. return $data;
  2958. }
  2959. public function giftDetail($oid)
  2960. {
  2961. $orderInfo = $this->dao->getOne(['id' => $oid, 'is_del' => 0]);
  2962. if ($orderInfo) {
  2963. $orderInfo = $orderInfo->toArray();
  2964. } else {
  2965. throw new ApiException('订单不存在');
  2966. }
  2967. $orderInfo = $this->tidyOrder($orderInfo, true);
  2968. /** @var UserServices $userServices */
  2969. $userServices = app()->make(UserServices::class);
  2970. $userInfo = $userServices->get($orderInfo['uid']);
  2971. $arr = [];
  2972. foreach ($orderInfo['cartInfo'] as $cartInfo) {
  2973. $arr[] = $cartInfo['productInfo']['logistics'];
  2974. }
  2975. $res = array_unique(explode(',', implode(',', $arr)));
  2976. if (count($res) == 2) {
  2977. $type = 0;
  2978. } else {
  2979. if ($res[0] == 2 && sys_config('store_self_mention') == 0) {
  2980. $type = 1;
  2981. } else {
  2982. $type = (int)$res[0];
  2983. }
  2984. }
  2985. return [
  2986. 'id' => $orderInfo['id'],
  2987. 'order_id' => $orderInfo['order_id'],
  2988. 'uid' => $orderInfo['uid'],
  2989. 'avatar' => set_file_url($userInfo['avatar']),
  2990. 'nickname' => $userInfo['nickname'],
  2991. 'cartInfo' => $orderInfo['cartInfo'],
  2992. 'paid' => $orderInfo['paid'],
  2993. 'total_num' => $orderInfo['total_num'],
  2994. 'pay_price' => $orderInfo['pay_price'],
  2995. 'gift_key' => md5($orderInfo['id'] . '_' . $orderInfo['order_id'] . '_' . $orderInfo['uid']),
  2996. 'gift_mark' => $orderInfo['gift_mark'],
  2997. 'gift_uid' => $orderInfo['gift_uid'],
  2998. 'refund_status' => $orderInfo['refund_status'],
  2999. 'type' => $type,
  3000. 'store_self_mention' => (int)sys_config('store_self_mention') ?? 0,//门店自提是否开启
  3001. ];
  3002. }
  3003. public function receiveGift($uid, $oid, $gift_key, $shipping_type, $name, $phone, $address_id = 0, $store_id = 0)
  3004. {
  3005. $orderInfo = $this->dao->get($oid);
  3006. if (!$orderInfo) {
  3007. throw new AdminException('订单不存在');
  3008. }
  3009. if ($gift_key != md5($orderInfo['id'] . '_' . $orderInfo['order_id'] . '_' . $orderInfo['uid'])) {
  3010. throw new AdminException('领取失败');
  3011. }
  3012. if ($orderInfo['refund_status'] != 0) {
  3013. throw new AdminException('订单已退款');
  3014. }
  3015. if ($orderInfo['uid'] == $uid) {
  3016. throw new AdminException('不能领取自己的礼物');
  3017. }
  3018. if ($orderInfo['gift_uid'] != 0 && $orderInfo['gift_uid'] != $uid) {
  3019. return false;
  3020. }
  3021. $address = '';
  3022. if ($shipping_type == 1 && $address_id) {
  3023. $addressInfo = app()->make(UserAddressServices::class)->getOne(['uid' => $uid, 'id' => $address_id, 'is_del' => 0]);
  3024. $name = $addressInfo['real_name'];
  3025. $phone = $addressInfo['phone'];
  3026. $address = $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'];
  3027. }
  3028. $verify_code = '';
  3029. if ($shipping_type == 2 && $store_id) {
  3030. $store_id = app()->make(SystemStoreServices::class)->getStoreDispose($store_id, 'id');
  3031. if (!$store_id) throw new ApiException('门店选择错误');
  3032. $verify_code = app()->make(StoreOrderCreateServices::class)->getStoreCode();
  3033. }
  3034. $orderData = [
  3035. 'gift_uid' => $uid,
  3036. 'real_name' => $name,
  3037. 'user_phone' => $phone,
  3038. 'user_address' => $address,
  3039. 'shipping_type' => $shipping_type,
  3040. 'store_id' => $store_id,
  3041. 'verify_code' => $verify_code,
  3042. ];
  3043. $this->dao->update($oid, $orderData);
  3044. return true;
  3045. }
  3046. }