You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/controller/Recovery.php

429 lines
15 KiB

10 months ago
<?php
declare (strict_types=1);
namespace app\api\controller;
use app\api\model\RecoveryOrder;
10 months ago
use app\api\model\Server\RecoveryCategory;
use app\api\model\Server\ServerRecovery;
10 months ago
use app\common\enum\RecoveryStatusEnum;
use app\common\enum\RecoveryTypeEnum;
use app\common\library\helper;
use cores\exception\BaseException;
10 months ago
use think\db\exception\DataNotFoundException;
10 months ago
use think\db\exception\DbException;
10 months ago
use think\db\exception\ModelNotFoundException;
10 months ago
use think\response\Json;
class Recovery extends Controller
{
10 months ago
/**
* @notes:分类列表
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function categoryList(): Json
{
$model = new RecoveryCategory();
$list = $model->list();
if (!empty($list)) {
foreach ($list as $key => $value) {
unset($list[$key]['image']);
}
}
return $this->renderSuccess(compact('list'));
}
/**
* @notes:服务列表页
* @throws DbException
* @author: wanghousheng
*/
public function recoveryList(): Json
{
$recovery_name = $this->request->post('recovery_name');
$category_id = intval($this->request->post('category_id'));
10 months ago
$order_field = (string)$this->request->post('order_field');
$order_sort = (string)$this->request->post('order_sort', 'desc');
10 months ago
$where = [];
if ($recovery_name) {
10 months ago
$where[] = ['server_recovery.recovery_name', 'like', "%$recovery_name%"];
10 months ago
}
if ($category_id) {
10 months ago
$where[] = ['server_recovery.category_id', '=', $category_id];
10 months ago
}
10 months ago
$where[] = ['server_recovery.status', '=', 1];
10 months ago
$model = new ServerRecovery();
10 months ago
$list = $model->getList($where, 15, $order_field, $order_sort);
10 months ago
$data['list'] = $list->items();
$data['total'] = $list->total();
if (!$list->isEmpty()) {
foreach ($data['list'] as $key => $value) {
unset($data['list'][$key]['image']);
unset($data['list'][$key]['category']);
}
}
return $this->renderSuccess($data);
}
10 months ago
/**
* @notes:用户回收订单列表
* @return Json
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
10 months ago
public function orderList(): Json
10 months ago
{
$where = [];
$order_status = intval($this->request->post('order_status'));
if ($order_status) {
$where['order_status'] = $order_status;
}
$model = new RecoveryOrder();
10 months ago
$list = $model->getUserList($where, 15, ['shop']);
10 months ago
$data['list'] = $list->items();
$data['total'] = $list->total();
if ($data['total']) {
10 months ago
$recovery_id = array_column($data['list'], 'recovery_id');
$model = new ServerRecovery();
$recovery_list = $model->whereIn('recovery_id', $recovery_id)
->with(['image'])
->select()
->toArray();
$image_list = [];
if ($recovery_list) {
$image_list = array_column($recovery_list, 'recovery_image', 'recovery_id');
}
10 months ago
foreach ($data['list'] as $key => $value) {
$data['list'][$key]['is_cancel'] = 0;
10 months ago
$data['list'][$key]['is_success'] = 0;
10 months ago
$data['list'][$key]['is_edit'] = 0;
10 months ago
$data['list'][$key]['recovery_image'] = '';
if (!empty($image_list[$value['recovery_id']])) {
$data['list'][$key]['recovery_image'] = $image_list[$value['recovery_id']];
}
10 months ago
if ($value['order_status'] == RecoveryStatusEnum::ACCEPTED) {
$data['list'][$key]['is_cancel'] = 1;
10 months ago
$data['list'][$key]['is_success'] = 1;
10 months ago
$data['list'][$key]['is_edit'] = 1;
10 months ago
}
}
}
10 months ago
return $this->renderSuccess($data);
10 months ago
}
/**
* @notes:订单状态
* @return Json
* @author: wanghousheng
*/
public function orderStatus(): Json
{
$list = array_values(RecoveryStatusEnum::data());
return $this->renderSuccess(compact('list'));
}
public function updateOrderStatus(): Json
{
$params = $this->request->param();
if (empty($params['order_id']) || empty($params['recovery_id']) || empty($params['status'])) {
return $this->renderError('缺少必要参数');
}
$model = new RecoveryOrder();
$res = $model->save(['order_status' => $params['status']]);
if ($res) {
return $this->renderSuccess('更新成功');
}
return $this->renderError('更新失败');
}
10 months ago
/**
* @notes:获取详情
10 months ago
* @return Json
10 months ago
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public function orderDetail(): Json
10 months ago
{
10 months ago
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
10 months ago
return $this->renderError('缺少必要参数');
}
$model = new RecoveryOrder();
10 months ago
$info = $model->getDetails($orderId);
10 months ago
if ($info && !empty($info['images'])) {
10 months ago
$info['is_cancel'] = 0;
10 months ago
$info['is_success'] = 0;
$info['is_edit'] = 0;
10 months ago
if ($info['order_status'] == RecoveryStatusEnum::ACCEPTED) {
$info['is_cancel'] = 1;
10 months ago
$info['is_success'] = 1;
$info['is_edit'] = 1;
10 months ago
}
10 months ago
$images_list = helper::getArrayColumn($info['images'], 'file');
// $arr = [];
// foreach ($images_list as $image) {
// $arr[] = $image['preview_url'];
// }
// $info['images_list'] = $arr;
$info['images_list'] = $images_list;
10 months ago
unset($info['images']);
10 months ago
//回收信息
$info['recovery_image'] = '';
$recoveryInfo = ServerRecovery::detail(['recovery_id' => $info['recovery_id']], ['image']);
if (!empty($recoveryInfo['recovery_image'])) {
$info['recovery_image'] = $recoveryInfo['recovery_image'];
}
//门店信息
$info['shop_info'] = \app\api\model\store\Shop::detail($info['shop_id'], ['logoImage']);
10 months ago
}
10 months ago
return $this->renderSuccess(['detail' => $info]);
10 months ago
}
10 months ago
/**
* @notes:取消订单
* @return Json
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public function cancelOrder(): Json
10 months ago
{
10 months ago
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
10 months ago
return $this->renderError('缺少必要参数');
}
$model = new RecoveryOrder();
10 months ago
if ($model->cancel($orderId)) {
10 months ago
return $this->renderSuccess('操作成功');
}
return $this->renderError('操作失败');
}
10 months ago
/**
* @notes:新增记录
* @return Json
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public function addOrder(): Json
10 months ago
{
$imageIds = $this->request->post('image_ids');
10 months ago
if ($imageIds) {
if (!is_array($imageIds)) {
$imageIds = explode(',', $imageIds);
}
10 months ago
} else {
$imageIds = [];
10 months ago
}
$shop_id = intval($this->request->post('shop_id'));
if (!$shop_id) {
return $this->renderError('门店不能为空');
}
10 months ago
$recovery_id = intval($this->request->post('recovery_id'));
if (!$recovery_id) {
return $this->renderError('缺少必要参数');
}
10 months ago
$model = new ServerRecovery();
$recovery_name = $model->where(['recovery_id' => $recovery_id, 'status' => 1])->value('recovery_name');
if (!$recovery_name) {
return $this->renderError('非法请求');
}
10 months ago
$recovery_type_arr = array_values(RecoveryStatusEnum::data());
$recovery_type_arr = array_column($recovery_type_arr, 'value');
$recovery_type = intval($this->request->post('recovery_type'));
if (!in_array($recovery_type, $recovery_type_arr)) {
return $this->renderError('回收方式错误');
}
$username = $this->request->post('username');
if (!$username) {
return $this->renderError('姓名不能为空');
}
$server_time = $this->request->post('server_time');
if (!$server_time || !strtotime($server_time)) {
return $this->renderError('服务时间不合法');
}
$mobile = $this->request->post('mobile');
if (!$mobile || !preg_match("/1[3457896]\d{9}$/", $mobile)) {
return $this->renderError('手机号不正确');
}
$expect_price = $this->request->post('expect_price');
if (!$expect_price || helper::number2($expect_price) <= 0) {
return $this->renderError('期待价格不能为空');
}
$sex = intval($this->request->post('sex', 1));
$remake = $this->request->post('remake');
$brand = $this->request->post('brand');
if (!$brand) {
return $this->renderError('品牌不能为空');
}
$model = $this->request->post('model');
if (!$model) {
return $this->renderError('型号不能为空');
}
$wx_account = $this->request->post('wx_account');
$house_number = $this->request->post('house_number');
$shipping_address = $this->request->post('shipping_address');
//邮寄回收
if ($recovery_type == RecoveryTypeEnum::MAIL || $recovery_type == RecoveryTypeEnum::DOOR) {
if (!$shipping_address) {
return $this->renderError('联系地址不能为空');
}
if (!$house_number) {
return $this->renderError('门牌号不能为空');
}
}
$express_id = intval($this->request->post('express_id'));
$express_no = $this->request->post('express_no');
$data = [
10 months ago
'recovery_id' => $recovery_id,
10 months ago
'express_id' => $express_id,
'express_no' => $express_no,
'shipping_address' => $shipping_address,
'wx_account' => $wx_account,
'model' => $model,
'brand' => $brand,
'remake' => $remake,
'sex' => $sex,
'expect_price' => $expect_price,
'mobile' => $mobile,
'server_time' => $server_time,
'username' => $username,
'recovery_type' => $recovery_type,
'shop_id' => $shop_id,
10 months ago
'recovery_name' => $recovery_name,
10 months ago
'house_number' => $house_number,
10 months ago
];
$model = new RecoveryOrder();
if ($model->add($data, $imageIds)) {
return $this->renderSuccess('提交成功');
}
return $this->renderError('提交失败');
}
/**
* @notes:更新
* @return Json
* @author: wanghousheng
*/
10 months ago
public function updateOrder(): Json
10 months ago
{
10 months ago
$order_id = intval($this->request->post('order_id'));
if (!$order_id) {
10 months ago
return $this->renderError('缺少必要参数');
}
10 months ago
$image_str = $this->request->post('image_ids');
$imageIds = [];
if ($image_str) {
$imageIds = explode(',', $image_str);
}
// if ($image_str) {
// if (!is_array($image_str)) {
// $imageIds = explode(',', $image_str);
// } else {
// $imageIds = $image_str;
// }
// }
// if ($imageIds && !is_array($imageIds)) {
// $imageIds = explode(',', $imageIds);
// } else {
// $imageIds = [];
// }
10 months ago
$shop_id = intval($this->request->post('shop_id'));
if (!$shop_id) {
return $this->renderError('门店不能为空');
}
$recovery_type_arr = array_values(RecoveryStatusEnum::data());
$recovery_type_arr = array_column($recovery_type_arr, 'value');
$recovery_type = intval($this->request->post('recovery_type'));
if (!in_array($recovery_type, $recovery_type_arr)) {
return $this->renderError('回收方式错误');
}
$username = $this->request->post('username');
if (!$username) {
return $this->renderError('姓名不能为空');
}
$server_time = $this->request->post('server_time');
if (!$server_time || !strtotime($server_time)) {
return $this->renderError('服务时间不合法');
}
$mobile = $this->request->post('mobile');
if (!$mobile || !preg_match("/1[3457896]\d{9}$/", $mobile)) {
return $this->renderError('手机号不正确');
}
$expect_price = $this->request->post('expect_price');
if (!$expect_price || helper::number2($expect_price) <= 0) {
return $this->renderError('期待价格不能为空');
}
$sex = intval($this->request->post('sex', 1));
$remake = $this->request->post('remake');
$brand = $this->request->post('brand');
if (!$brand) {
return $this->renderError('品牌不能为空');
}
$model = $this->request->post('model');
if (!$model) {
return $this->renderError('型号不能为空');
}
$wx_account = $this->request->post('wx_account');
$house_number = $this->request->post('house_number');
$shipping_address = $this->request->post('shipping_address');
//邮寄回收
if ($recovery_type == RecoveryTypeEnum::MAIL) {
if (!$shipping_address) {
return $this->renderError('发货地址不能为空');
}
if (!$house_number) {
return $this->renderError('门牌号不能为空');
}
}
$express_id = intval($this->request->post('express_id'));
$express_no = $this->request->post('express_no');
$data = [
'express_id' => $express_id,
'express_no' => $express_no,
'shipping_address' => $shipping_address,
'wx_account' => $wx_account,
'model' => $model,
'brand' => $brand,
'remake' => $remake,
'sex' => $sex,
'expect_price' => $expect_price,
'mobile' => $mobile,
'server_time' => $server_time,
'username' => $username,
'recovery_type' => $recovery_type,
'shop_id' => $shop_id,
10 months ago
'house_number' => $house_number,
10 months ago
];
$model = new RecoveryOrder();
$model->edit($data, $order_id, $imageIds);
return $this->renderSuccess('操作成功');
10 months ago
}
10 months ago
/**
* @notes:订单验收
* @return Json
* @author: wanghousheng
*/
public function completeOrder(): Json
{
$order_id = intval($this->request->post('order_id'));
if (!$order_id) {
return $this->renderError('缺少必要参数');
}
10 months ago
if (!RecoveryOrder::detail(['order_id' => $order_id, 'order_status' => RecoveryStatusEnum::ACCEPTED])) {
10 months ago
return $this->renderError('订单信息不存在');
}
$model = new RecoveryOrder();
if ($model->where(['order_id' => $order_id])->save(['order_status' => RecoveryStatusEnum::ALREADY])) {
return $this->renderSuccess('操作成功');
}
return $this->renderError('操作失败');
}
10 months ago
}