StoreOrderServices.php 142 KB

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