StoreOrderServices.php 113 KB

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