StoreOrderServices.php 121 KB

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