StoreOrderServices.php 139 KB

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