StoreOrderServices.php 130 KB

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