StoreOrderServices.php 139 KB

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