123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\controller;
- use app\api\model\Order as OrderModel;
- use app\api\model\Setting as SettingModel;
- use app\api\model\Express as ExpressModel;
- use app\api\model\Receipt as ReceiptModel;
- use app\api\model\shop\Shops;
- use app\common\enum\order\DeliveryType;
- use app\common\enum\order\PayType as OrderPayTypeEnum;
- use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
- use app\common\exception\BaseException;
- use app\api\service\order\Order as OrderService;
- use app\api\model\OrderGoods;
- use app\common\library\helper;
- use app\common\model\card\UserRiceDeliveryExpress as UserRiceDeliveryExpressModel;
- use app\common\model\card\UserRiceDelivery as UserRiceDeliveryModel;
- use app\api\service\User as UserService;
- use app\common\model\RefundCompensate;
- use app\common\model\OrderTj;
- use app\common\model\SmsRemind;
- use app\common\model\PushLog;
- use app\console\service\WxDelivery;
- use app\store\model\ChannelSaleVolumeTj;
- use app\store\model\user\CommerCountTj;
- use think\cache\driver\Redis;
- use app\common\model\member\MemberTj;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * 我的订单控制器
- * Class Order
- * @package app\api\controller
- */
- class Order extends Controller
- {
- public function tj(){
- $now = time();
- //推荐官销售数据统计
- $commerTj = new CommerCountTj();
- $commerTj->day($now);
- $commerTj->week($now);
- $commerTj->month($now);
- echo 'success commer'.PHP_EOL;
- //全渠道销售业绩构成
- $csvTj = new ChannelSaleVolumeTj();
- $csvTj->day($now);
- $csvTj->week($now);
- $csvTj->month($now);
- echo 'success order'.PHP_EOL;
- $otj = new OrderTj;
- $day = $otj->day(1);
- $month = $otj->month();
- $week = $otj->week();
- echo 'success order <br/>';
- $otjmember = new MemberTj;
- $otjmember->day(1);
- $otjmember->month();
- $otjmember->week();
- echo 'success member';
-
- }
- //给订单打新老用户tag
- public function ordertag(){
- $sql = 'select order_id,user_id from yoshop_order where pay_status=20 and tag=-1 limit 100';
- $result = Db::query($sql);
- foreach($result as $row){
- $user_id = $row['user_id'];
- $small = "select min(order_id) as min_order_id from yoshop_order where pay_status=20 and user_id=".$user_id;
- $s = Db::query($small);
- if($s[0]['min_order_id']==$row['order_id']){
- OrderModel::where('order_id',$row['order_id'])->update(['tag'=>1]);
- }else{
- OrderModel::where('order_id',$row['order_id'])->update(['tag'=>0]);
- }
- }
- echo 'success order tag';
- }
- public function smsremind(){
- $sms = SmsRemind::where('id','>',0)->find();
- if($sms->hours){
- $hours = $sms->hours;
- $hours_arr = explode(",",$hours);
- $now_hour = Date("H",time());
- if(in_array($now_hour,$hours_arr)){
- $filter['pay_status'] =20;
- $filter['delivery_status'] = 10;//未发货
- $filter['order_status'] = 10;//进行中
- $filter['delivery_type'] = 10;// 快递配送单
- $c = OrderModel::where($filter)->count();
- if($c>0){
-
- $mobiles = $sms->mobiles;
- $key = "sms remind".Date("YmdH",time());
- $rds = new Redis(config('cache.stores.redis'));
- $value = $rds->get($key);
- if($value){
- echo 'sms have send';
- }else{
- $rds->set($key,1,3600);
- $content = '您有'.$c.'个订单待发货,请及时处理!['.substr(Date("Y-m-d",time()),2)." ".$now_hour.":00".']';
- // var_dump($content);
- PushLog::addSmsMon($content,$mobiles,1);
- echo 'remind order success'.$content.',mobile:'.$sms->mobiles;
- }
- }else{
- echo 'no order remind';
- }
- }else{
- echo 'no the time now ';
- }
- }
- //每小时给订单打个新老用户的标签
- $this->ordertag();
- }
- /**
- * 获取当前用户待处理的订单数量
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function todoCounts()
- {
- $model = new OrderModel;
- $counts = $model->getTodoCounts();
- return $this->renderSuccess(compact('counts'));
- }
- /**
- * 订单商品详情
- * @param int $orderGoodsId 订单商品ID
- * @return \app\common\model\OrderGoods|null
- * @throws BaseException
- */
- public function refundGoods(int $order_goods_id)
- {
- $goods = OrderGoods::detail($order_goods_id);
- unset($goods['content']);
- return $this->renderSuccess(compact('goods'));
-
- }
- /**
- * 我的订单列表
- * @param string $dataType 订单类型 (all全部 payment待付款 received待发货 deliver待收货 completed已完成 comment待评价)
- * @return array|\think\response\Json
- * @throws BaseException
- * @throws \think\db\exception\DbException
- */
- public function list(string $dataType)
- {
- $isExchange = boolval($this->request->param('isExchange',0));
- $model = new OrderModel;
- $list = $model->getList($dataType,$isExchange);
- $orderService = new OrderService;
- //获取多个物流的最新信息
- $orderService->setUserOrder($list);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 订单详情信息
- * @param int $orderId 订单ID
- * @return array|\think\response\Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function detail(int $orderId)
- {
- // 订单详情
- $model = OrderModel::getUserOrderDetail($orderId);
- $ExpressModel = new ExpressModel;
- $express = [];
- $i = 0;
- if($model['delivery_status']==DeliveryStatusEnum::DELIVERED){//订单状态没有更改为已发货就不显示物流信息
- foreach ($model['goods'] as &$good){
- $refundCompensate = RefundCompensate::where('order_goods_id',$good['order_goods_id'])->where("finance_refund",10)->where("status",20)->find();
- if($refundCompensate){
- $good['compensate'] = ['refund_money'=>$refundCompensate->refund_money];
- }else{
- $good['compensate'] =null;
- }
- if($good['is_split_package']){
- foreach ($good['package'] as $pack){
- $express[$i] = $ExpressModel->deliverylist($pack['express_no'], $pack['delivery_time'], 2);
- $express[$i]['express_no'] = $pack['express_no'];
- $i++;
- }
- }else{
- if($good['express_no']){
- $express[$i] = $ExpressModel->deliverylist($good['express_no'], $good['delivery_time'], 2);
- $express[$i]['express_no'] = $good['express_no'];
- $i++;
- }
- }
- }
- }
- $model['express_list'] = $express;
- $express_no_list = array_values(array_filter(array_unique(helper::getArrayColumn($express, 'express_no'))));
- $model['express_no'] = implode(',',$express_no_list);
- OrderModel::restructureGoods($model);
- $model['has_receipt'] = ReceiptModel::ifApplyReceipt($orderId);
- $model['hx_qrcode_url'] = url('/api/order/getHxQrcode',['hxCode'=>$model['hx_code']], true, true)->build();
- if($model['delivery_type']==DeliveryType::SHOPS_DELIVERY){
- $model['shops'] = Shops::find($model['shop_id']);
- }
- return $this->renderSuccess([
- 'order' => $model, // 订单详情
- 'setting' => [
- // 积分名称
- 'points_name' => SettingModel::getPointsName(),
- ],
- ]);
- }
- /**
- * 检查订单核销状态
- */
- public function checkHxStatus(int $orderId)
- {
- // 订单详情
- $model = OrderModel::getDetail($orderId);
- if (empty($model)) {
- return $this->renderError("订单不存在");
- }
- $hx_status = $model['hx_status'];
- return $this->renderSuccess(compact('hx_status'));
- }
- /**
- * 核销码二维码地址
- */
- public function getHxQrcode(string $hxCode) {
- return OrderService::getHxQrcode($hxCode);
- }
- /**
- * 自制异业福利券核销码二维码地址
- */
- public function getHxWelQrcode($id) {
- return OrderService::getHxQrcode($id);
- }
- /**
- * 查看物流列表
- * @param int $orderId 订单ID
- * @return array|\think\response\Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function expressList(int $orderId)
- {
- // 订单信息
- $order = OrderModel::getDetail($orderId, ['goods' => ['image', 'package'], 'address']);
- if (!$order || $order['delivery_status']!=DeliveryStatusEnum::DELIVERED) {
- return $this->renderError('没有物流信息');
- }
- $orderService = new OrderService;
- //获取多个物流的最新信息
- $res = $orderService->getExpressList($order);
- return $this->renderSuccess($res);
- }
- /**
- * 查看物流详情
- * @param $orderId
- * @param $expressNo
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author: zjwhust
- * @Time: 2021/10/9 9:13
- */
- public function express($orderId, $expressNo)
- {
- // 订单信息
- $order = OrderModel::getDetail(intval($orderId), ['goods' => ['image', 'package'], 'address']);
- if (!$order || $order['delivery_status']!=DeliveryStatusEnum::DELIVERED) {
- return $this->renderError('没有物流信息');
- }
- $orderService = new OrderService;
- //获取多个物流的最新信息
- $res = $orderService->getExpressDetail($order, $expressNo);
- return $this->renderSuccess($res);
- }
- //获取物流的最新信息
- public function rcexpress($express_no){
- $orderService = new OrderService;
- $express = UserRiceDeliveryExpressModel::field('express_no,express_id,express_company,user_rice_delivery_id,delivery_time')->where("express_no",$express_no)->find();
- if(empty($express)){
- return $this->renderError("物流单号找不到");
- }
- $delivery_time = $express['delivery_time']??'';
- $delivery = UserRiceDeliveryModel::where('id',$express->user_rice_delivery_id)->find();
-
- $data = $orderService->getExpressInfo($express_no,$delivery_time);
- return $this->renderSuccess(['data'=>$data,'express'=>$express,'delivery'=>$delivery]);
- }
- /**
- * 取消订单
- * @param int $orderId
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function cancel(int $orderId)
- {
- $model = OrderModel::getDetail($orderId);
- if ($model->cancel()) {
- return $this->renderSuccess('订单取消成功');
- }
- return $this->renderError($model->getError() ?: '订单取消失败');
- }
- /**
- * 删除订单
- * @param int $orderId
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function delete(int $orderId)
- {
- $model = OrderModel::getDetail($orderId);
- if ($model->clear()) {
- return $this->renderSuccess('订单删除成功');
- }
- return $this->renderError($model->getError() ?: '订单删除失败');
- }
- /**
- * 确认收货
- * @param int $orderId
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function receipt(int $orderId)
- {
- $model = OrderModel::getDetail($orderId);
- if ($model->receipt()) {
- // //分佣结算逻辑
- // $cm = new GiveOutCommission();
- // $flag = $cm->confirmGoodsCommission($orderId);
- // if ($flag == false){
- // log_record('订单分佣结算失败:'.$orderId);
- // }
- // //order_goods的财务结算状态变更
- // OrderGoods::where('order_id',$orderId)->update(['finance_clearing_status'=>OrderGoods::FINANCE_CLEARING_WAIT]);
- return $this->renderSuccess('确认收货成功');
- }
- return $this->renderError($model->getError());
- }
- /**
- * 立即支付
- * @param int $orderId 订单ID
- * @param int $payType 支付方式
- * @return array|\think\response\Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT)
- {
- // 获取订单详情
- $model = OrderModel::getUserOrderDetail($orderId);
- // 订单支付事件
- if (!$model->onPay($payType)) {
- return $this->renderError($model->getError() ?: '订单支付失败');
- }
- // 构建微信支付请求
- $payment = $model->onOrderPayment($model, $payType);
- // 支付状态提醒
- return $this->renderSuccess([
- 'order_id' => $model['order_id'], // 订单id
- 'pay_type' => $payType, // 支付方式
- 'payment' => $payment // 微信支付参数
- ]);
- }
- //统计rf
- public function rf(){
- $r1f1 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING count(order_id)=1";
- $r1f2 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
- $r1f3 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING count(order_id)>=5";
- $r2f1 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-7*86400
- and pay_time>unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)=1";
- $r2f2 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-7*86400
- and pay_time>unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
- $r2f3 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-7*86400
- and pay_time>unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)>=5";
- $r3f1 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)=1";
- $r3f2 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
- $r3f3 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING count(order_id)>=5";
- $result11 = Db::query($r1f1);
- $result12 = Db::query($r1f2);
- $result13 = Db::query($r1f3);
- $result21 = Db::query($r2f1);
- $result22 = Db::query($r2f2);
- $result23 = Db::query($r2f3);
- $result31 = Db::query($r3f1);
- $result32 = Db::query($r3f2);
- $result33 = Db::query($r3f3);
- // var_dump(count($result11));
- // var_dump(count($result12));
- // var_dump(count($result13));
- $r11 = count($result11);
- $r12 = count($result12);
- $r13 = count($result13);
- $r21 = count($result21);
- $r22 = count($result22);
- $r23 = count($result23);
- // var_dump(count($result21));
- // var_dump(count($result22));
- // var_dump(count($result23));
- // var_dump(count($result31));
- // var_dump(count($result32));
- // var_dump(count($result33));
- $r31 = count($result31);
- $r32 = count($result32);
- $r33 = count($result33);
- $data[] = ['r11'=>$r11,"r12"=>$r12,"r13"=>$r13,'r1'=>$r11+$r12+$r13];
- $data[] = ['r21'=>$r21,"r22"=>$r22,"r23"=>$r23,'r2'=>$r21+$r22+$r23];
- $data[] = ['r31'=>$r31,"r32"=>$r32,"r33"=>$r33,'r3'=>$r31+$r32+$r33];
- return $this->renderSuccess(compact('data'));
- }
- //统计rm
- public function rm(){
- $r1f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
- $r1f2 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
- $r1f3 = "select user_id,count(order_id) as c from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
- $r2f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-60*86400
- and pay_time<unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
- $r2f2 ="select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-60*86400
- and pay_time<unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
- $r2f3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time>unix_timestamp(now())-60*86400
- and pay_time<unix_timestamp(now())-7*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
- $r3f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
-
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
-
- $r3f2 ="select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
- $r3f3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
- where pay_status=20
- and pay_time<unix_timestamp(now())-60*86400
- GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
-
- $result11 = Db::query($r1f1);
- $result12 = Db::query($r1f2);
- $result13 = Db::query($r1f3);
- $result21 = Db::query($r2f1);
- $result22 = Db::query($r2f2);
- $result23 = Db::query($r2f3);
- $result31 = Db::query($r3f1);
- $result32 = Db::query($r3f2);
- $result33 = Db::query($r3f3);
- // var_dump(count($result11));
- // var_dump(count($result12));
- // var_dump(count($result13));
- $r11 = count($result11);
- $r12 = count($result12);
- $r13 = count($result13);
- $r21 = count($result21);
- $r22 = count($result22);
- $r23 = count($result23);
- // var_dump(count($result21));
- // var_dump(count($result22));
- // var_dump(count($result23));
- // var_dump(count($result31));
- // var_dump(count($result32));
- // var_dump(count($result33));
- $r31 = count($result31);
- $r32 = count($result32);
- $r33 = count($result33);
- $data[] = ['r11'=>$r11,"r12"=>$r12,"r13"=>$r13,'r1'=>$r11+$r12+$r13];
- $data[] = ['r21'=>$r21,"r22"=>$r22,"r23"=>$r23,'r2'=>$r21+$r22+$r23];
- $data[] = ['r31'=>$r31,"r32"=>$r32,"r33"=>$r33,'r3'=>$r31+$r32+$r33];
- return $this->renderSuccess(compact('data'));
- }
- //用户将系统中已手动发货的订单录入到微信小程序发货系统
- //20 * * * * webuser curl https://wxapp.rwgmlc.cn/api/order/wx_delivery_cron
- public function wxDeliveryCron(){
- Log::info('wxDeliveryCroning');
- $wxDeliveryServ = new WxDelivery();
- $wxDeliveryServ->pullOrderStatus(10001);
- return $this->renderSuccess([]);
- }
- }
|