StoreOrderServices.php 113 KB

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