StoreOrderServices.php 117 KB

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