// +----------------------------------------------------------------------
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;
/**
* 我的订单控制器
* 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
';
$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_timeunix_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_timeunix_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_timeunix_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=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=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_timeunix_timestamp(now())-60*86400
and pay_time=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=1000";
$r3f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
where pay_status=20
and pay_time=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=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/order/wx_delivery_cron
public function wxDeliveryCron(){
$wxDeliveryServ = new WxDelivery();
$wxDeliveryServ->pullOrderStatus(10001);
return $this->renderSuccess([]);
}
}