StoreOrderServices.php 117 KB

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