Update Order.php

This commit is contained in:
Anyon 2020-09-11 14:06:37 +08:00
parent 2860c33cd0
commit 7b3290c233

View File

@ -5,7 +5,6 @@ namespace app\data\controller\api\auth;
use app\data\controller\api\Auth;
use app\data\service\GoodsService;
use app\data\service\OrderService;
use app\data\service\TruckService;
use app\wechat\service\WechatService;
use think\admin\extend\CodeExtend;
use think\exception\HttpResponseException;
@ -28,6 +27,36 @@ class Order extends Auth
}
}
/**
* 获取订单列表
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function get()
{
$map = [['mid', '=', $this->mid]];
if (!$this->request->has('order_no', 'param', true)) {
$map[] = ['status', 'in', [0, 2, 3, 4, 5]];
}
$query = $this->_query('ShopOrder')->equal('status,order_no');
$result = $query->where($map)->order('id desc')->page(true, false, false, 20);
if (count($result['list']) > 0) {
$codes = array_unique(array_column($result['list'], 'order_no'));
$items = $this->app->db->name('ShopOrderItem')->whereIn('order_no', $codes)->select()->toArray();
foreach ($result['list'] as &$vo) {
[$vo['count'], $vo['items']] = [0, []];
foreach ($items as $item) {
if ($vo['order_no'] === $item['order_no']) {
$vo['items'][] = $item;
$vo['count'] += $item['stock_sales'];
}
}
}
}
$this->success('获取订单数据成功!', $result);
}
/**
* 会员创建订单
* @throws \think\db\exception\DataNotFoundException
@ -114,32 +143,35 @@ class Order extends Auth
public function perfect()
{
$data = $this->_vali([
'order_no.require' => '订单单号不能为空!',
'address_id.require' => '收货地址不能为空!',
'order_no.require' => '订单单号不能为空!',
'address_code.require' => '收货地址不能为空!',
]);
// 收货地址
$map = ['mid' => $this->mid, 'id' => $data['address_id']];
$address = $this->app->db->name('StoreMemberAddress')->where($map)->where(['deleted' => 0])->find();
$map = ['mid' => $this->mid, 'code' => $data['address_code'], 'deleted' => 0];
$address = $this->app->db->name('DataMemberAddress')->where($map)->find();
if (empty($address)) $this->error('会员收货地址异常!');
// 订单检查
$map = ['order_no' => $data['order_no'], 'mid' => $this->mid];
$map = ['mid' => $this->mid, 'order_no' => $data['order_no']];
$order = $this->app->db->name('ShopOrder')->where($map)->whereIn('status', [1, 2])->find();
if (empty($order)) $this->error('订单状态异常,请重新下单!');
// 组装数据
$update = ['status' => 2];
$update['truck_address_id'] = $data['address_id'];
$update['order_no'] = $data['order_no'];
$update['truck_code'] = $data['address_code'];
$update['truck_name'] = $address['name'];
$update['truck_phone'] = $address['phone'];
$update['truck_province'] = $address['province'];
$update['truck_city'] = $address['city'];
$update['truck_area'] = $address['area'];
$update['truck_address'] = $address['address'];
// 运费计算
$result = TruckService::instance()->amount($address['province'], $order['express_rule_number'], $order['price_discount']);
$update['price_express'] = $result['amount'];
$update['price_total'] = $order['price_discount'] + $result['amount'];
$update['express_rule_content'] = $result['content'];
$update['truck_datetime'] = date('Y-m-d H:i:s');
// 运费计算 @todo 计算快递费用
// $result = TruckService::instance()->amount($address['province'], $order['express_rule_number'], $order['price_discount']);
// $update['price_express'] = $result['amount'];
// $update['price_total'] = $order['price_discount'] + $result['amount'];
// $update['express_rule_content'] = $result['content'];
if ($this->app->db->name('ShopOrder')->where($map)->update($update) !== false) {
$this->success('订单确认成功!', $this->_getPaymentParams($order['order_no'], $order['price_total'] - $order['price_reduction']));
$this->success('订单确认成功!', $this->_getPaymentParams($order['order_no'], $order['amount_total']));
} else {
$this->error('订单确认失败,请稍候再试!');
}
@ -191,33 +223,6 @@ class Order extends Auth
}
}
/**
* 获取订单列表
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function get()
{
$map = [['mid', '=', $this->mid]];
if (!$this->request->has('order_no', 'param', true)) {
$map[] = ['status', 'in', [0, 2, 3, 4, 5]];
}
$query = $this->_query('ShopOrder')->equal('status,order_no');
$result = $query->where($map)->order('id desc')->page(true, false, false, 20);
if (count($result['list']) > 0) {
$codes = array_unique(array_column($result['list'], 'order_no'));
$items = $this->app->db->name('ShopOrderItem')->whereIn('order_no', $codes)->select()->toArray();
foreach ($result['list'] as &$vo) {
[$vo['count'], $vo['items']] = [0, []];
foreach ($items as $item) if ($vo['order_no'] === $item['order_no']) {
$vo['items'][] = $item;
$vo['count'] += $item['stock_sales'];
}
}
}
$this->success('获取订单数据成功!', $result);
}
/**
* 主动取消未支付的订单
@ -227,14 +232,17 @@ class Order extends Auth
*/
public function cancel()
{
$map = $this->_vali(['order_no.require' => '订单号不能为空!']);
$order = $this->app->db->name('ShopOrder')->where(['mid' => $this->mid])->where($map)->find();
$map = $this->_vali([
'mid.value' => $this->mid,
'order_no.require' => '订单号不能为空!',
]);
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) $this->error('订单查询失败,请稍候再试!');
if (in_array($order['status'], [1, 2])) {
$result = $this->app->db->name('ShopOrder')->where($map)->update([
'status' => 0,
'cancel_status' => 1,
'cancel_remark' => '用户主动取消订单!',
'cancel_remark' => '会员主动取消订单!',
'cancel_datetime' => date('Y-m-d H:i:s'),
]);
if ($result !== false && OrderService::instance()->syncStock($order['order_no'])) {
@ -255,98 +263,24 @@ class Order extends Auth
*/
public function confirm()
{
$where = $this->_vali(['mid' => $this->mid, 'order_no.require' => '订单号不能为空!']);
$order = $this->app->db->name('ShopOrder')->where($where)->find();
$map = $this->_vali([
'mid.value' => $this->mid,
'order_no.require' => '订单号不能为空!',
]);
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) $this->error('订单查询失败,请稍候再试!');
if (in_array($order['status'], [4])) {
if ($this->app->db->name('ShopOrder')->where($where)->update(['status' => '5']) !== false) {
if ($this->app->db->name('ShopOrder')->where($map)->update(['status' => 5]) !== false) {
// OrderService::instance()->syncConfrimOrderAmount($order['order_no']);
$this->success('订单确认成功!');
} else {
$this->error('订单确认失败,请稍候再试!');
}
} else {
$this->error('该订单状态不允许确认哦~');
$this->error('订单不能确认收货哦~');
}
}
/**
* 提交退换货订单
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function createRefund()
{
$input = $this->_vali([
'order_no.require' => '订单单号不能为空!',
'goodsinfo.require' => '待处理商品不能为空!',
'refund_content.require' => '提交申请退货描述!',
'type.require' => '操作类型不能为空!',
'refund_images' => input('refund_images', ''),
]);
$input['goodsinfo'] = json_decode($input['goodsinfo'], true);
if (!is_array($input['goodsinfo'])) $this->error('待处理的商品不能为空!');
$map = [['order_no', '=', $input['order_no']], ['status', 'in', [3, 4, 5, 6]]];
$order = $this->app->db->name('ShopOrder')->where(['pay_state' => 1])->where($map)->find();
// $order = $this->app->db->name('ShopOrder')->where(['order_no' => $input['order_no']])->find();
if (empty($order)) $this->error('订单状态不允许退换货!');
try {
if (OrderService::instance()->refund($input['order_no'], $input, $input['goodsinfo'])) {
$this->success('提交退换货成功!');
} else {
$this->error("提交退换货失败!");
}
} catch (HttpResponseException $exception) {
throw $exception;
} catch (\Exception $exception) {
$this->success("提交退换货失败,请稍候再试!");
}
}
/**
* 获取退换货申请列表
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function queryRefund()
{
$query = $this->_query('ShopOrderRefund')->in('refund_status#status,order_no')->equal('group_no,refund_no');
$query->where(['mid' => $this->mid])->fieldRaw('group_no,sum(refund_number) refund_number,sum(refund_amount) refund_amount,refund_status,type');
$result = $query->group('group_no')->page(true, false, false, 15);
if (count($result['list']) > 0) {
$unis = array_unique(array_column($result['list'], 'group_no'));
$list = $this->app->db->name('ShopOrderRefund')->whereIn('group_no', $unis)->select()->toArray();
foreach ($result['list'] as &$vo) {
$vo['list'] = [];
foreach ($list as $item) if ($item['group_no'] === $vo['group_no']) $vo['list'][] = $item;
}
}
$this->success('获取退换货申请列表', $result);
}
/**
* 获取可退货订单
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getRefundOrder()
{
$map = $this->_vali(['order_no.require' => '订单编号不能为空!']);
$order = $this->app->db->name('ShopOrder')->where(['mid' => $this->mid])->where($map)->find();
if (empty($order)) $this->error('订单查询失败!');
$order['list'] = $this->app->db->name('ShopOrderItem')->where($map)->select()->toArray();
$rlist = $this->app->db->name('ShopOrderRefund')->where($map)->whereIn('refund_status', [1, 2, 3])->select()->toArray();
if (count($order['list']) > 0) foreach ($order['list'] as &$vo) if (count($rlist) > 0) foreach ($rlist as $rule) {
if ($vo['goods_id'] === $rule['goods_id'] && $vo['goods_spec'] === $rule['goods_spec']) {
$vo['number_goods'] -= $rule['refund_number'];
}
}
$this->success('获取订单成功', $order);
}
/**
* 订单状态统计
* @throws \think\db\exception\DataNotFoundException
@ -355,9 +289,13 @@ class Order extends Auth
*/
public function total()
{
$query = $this->app->db->name('ShopOrder');
$query->fieldRaw('mid,status,count(1) count')->where(['mid' => $this->mid]);
$this->success('获取订单统计成功!', $query->group('status')->select()->toArray());
$map = ['mid' => $this->mid, 'deleted' => 0];
$data = ['t0' => 0, 't1' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0];
$query = $this->app->db->name('ShopOrder')->fieldRaw('status,count(1) count');
$query->where($map)->group('status')->select()->each(function ($item) use (&$data) {
$data["t{$item['status']}"] = $item['count'];
});
$this->success('获取状态统计成功!', $data);
}
}