StoreOrderServices.php 140 KB

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