StoreOrderServices.php 125 KB

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