StoreOrderServices.php 123 KB

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