StoreOrderServices.php 123 KB

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