StoreOrderServices.php 120 KB

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