pull/1/head
parent
290b27d0e2
commit
dfc2c35374
@ -0,0 +1,236 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\api\controller; |
||||
|
||||
use app\api\model\RecoveryOrder; |
||||
use app\common\enum\RecoveryStatusEnum; |
||||
use app\common\enum\RecoveryTypeEnum; |
||||
use app\common\library\helper; |
||||
use cores\exception\BaseException; |
||||
use think\db\exception\DbException; |
||||
use think\response\Json; |
||||
|
||||
class Recovery extends Controller |
||||
{ |
||||
/** |
||||
* @notes:用户回收订单列表 |
||||
* @return Json |
||||
* @throws BaseException |
||||
* @throws DbException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function list(): Json |
||||
{ |
||||
$where = []; |
||||
$order_status = intval($this->request->post('order_status')); |
||||
if ($order_status) { |
||||
$where['order_status'] = $order_status; |
||||
} |
||||
$model = new RecoveryOrder(); |
||||
$list = $model->getUserList($where); |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:订单状态 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function orderStatus(): Json |
||||
{ |
||||
$list = array_values(RecoveryStatusEnum::data()); |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:获取详情 |
||||
* @return Json|void |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function detail() |
||||
{ |
||||
$recoveryId = intval($this->request->post('recovery_id')); |
||||
if (!$recoveryId) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$model = new RecoveryOrder(); |
||||
$model->getDetails($recoveryId); |
||||
} |
||||
|
||||
/** |
||||
* @notes:新增记录 |
||||
* @return Json |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function add(): Json |
||||
{ |
||||
$imageIds = $this->request->post('image_ids'); |
||||
if ($imageIds && !is_array($imageIds)) { |
||||
$imageIds = explode(',', $imageIds); |
||||
} |
||||
$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 || $recovery_type == RecoveryTypeEnum::DOOR) { |
||||
if (!$shipping_address) { |
||||
return $this->renderError('联系地址不能为空'); |
||||
} |
||||
if (!$house_number) { |
||||
return $this->renderError('门牌号不能为空'); |
||||
} |
||||
} |
||||
$shipping_address .= $house_number; |
||||
$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, |
||||
]; |
||||
$model = new RecoveryOrder(); |
||||
if ($model->add($data, $imageIds)) { |
||||
return $this->renderSuccess('提交成功'); |
||||
} |
||||
return $this->renderError('提交失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:更新 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function update(): Json |
||||
{ |
||||
$recovery_id = intval($this->request->post('recovery_id')); |
||||
if (!$recovery_id) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$imageIds = $this->request->post('image_ids'); |
||||
if ($imageIds && !is_array($imageIds)) { |
||||
$imageIds = explode(',', $imageIds); |
||||
} |
||||
$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('门牌号不能为空'); |
||||
} |
||||
} |
||||
$shipping_address .= $house_number; |
||||
$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, |
||||
]; |
||||
$model = new RecoveryOrder(); |
||||
if ($model->edit($data, $recovery_id, $imageIds)) { |
||||
return $this->renderSuccess('操作成功'); |
||||
} |
||||
return $this->renderError('操作失败'); |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\api\model; |
||||
|
||||
use app\common\model\RecoveryImage as BaseRecoveryImage; |
||||
|
||||
class RecoveryImage extends BaseRecoveryImage |
||||
{ |
||||
/** |
||||
* 隐藏字段 |
||||
* @var array |
||||
*/ |
||||
protected $hidden = [ |
||||
'store_id', |
||||
'create_time', |
||||
]; |
||||
|
||||
/** |
||||
* 更新关系记录 |
||||
* @param int $recoveryId |
||||
* @param array $imageIds 新的图片集 |
||||
* @return array|false |
||||
*/ |
||||
public static function updates(int $recoveryId, array $imageIds) |
||||
{ |
||||
// 删除所有的sku记录 |
||||
static::deleteAll(['goods_id' => $recoveryId]); |
||||
// 批量写入商品图片记录 |
||||
return static::increased($recoveryId, $imageIds); |
||||
} |
||||
} |
@ -0,0 +1,103 @@ |
||||
<?php |
||||
|
||||
namespace app\api\model; |
||||
|
||||
use app\api\service\User as UserService; |
||||
use app\common\model\RecoveryOrder as BaseRecoveryOrder; |
||||
use app\common\service\Order as OrderService; |
||||
use cores\exception\BaseException; |
||||
use think\db\exception\DbException; |
||||
use think\Paginator; |
||||
|
||||
class RecoveryOrder extends BaseRecoveryOrder |
||||
{ |
||||
/** |
||||
* 隐藏字段 |
||||
* @var array |
||||
*/ |
||||
protected $hidden = [ |
||||
'store_id', |
||||
'update_time', |
||||
]; |
||||
|
||||
/** |
||||
* @notes:用户回收订单列表 |
||||
* @param array $where |
||||
* @param int $listRows |
||||
* @return Paginator |
||||
* @throws BaseException |
||||
* @throws DbException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getUserList(array $where = [], int $listRows = 15): Paginator |
||||
{ |
||||
// 当前用户ID |
||||
$userId = UserService::getCurrentLoginUserId(); |
||||
if (!UserService::isStore()) { |
||||
$where = array_merge($where, ['user_id' => $userId]); |
||||
} |
||||
//分销商工程师 |
||||
return $this->where($where) |
||||
->where($where) |
||||
->order(['create_time' => 'desc']) |
||||
->paginate($listRows); |
||||
} |
||||
|
||||
/** |
||||
* @notes:订单详情 |
||||
* @param int $recoveryId |
||||
* @return BaseRecoveryOrder|array|null |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getDetails(int $recoveryId) |
||||
{ |
||||
$where['recovery_id'] = $recoveryId; |
||||
$where['user_id'] = UserService::getCurrentLoginUserId(); |
||||
return static::detail($where, ['images.file']); |
||||
} |
||||
|
||||
/** |
||||
* @notes:添加记录 |
||||
* @param array $data |
||||
* @param array $imageIds |
||||
* @return bool |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function add(array $data, array $imageIds = []): bool |
||||
{ |
||||
$data['order_no'] = OrderService::createOrderNo(); |
||||
$data['user_id'] = UserService::getCurrentLoginUserId(); |
||||
$data['store_id'] = self::$storeId; |
||||
$data['create_time'] = time(); |
||||
$data['update_time'] = $data['create_time']; |
||||
$insertId = $this->insertGetId($data); |
||||
if ($insertId) { |
||||
if ($imageIds) { |
||||
RecoveryImage::increased($insertId, $imageIds); |
||||
} |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* @notes:更新 |
||||
* @param array $data |
||||
* @param int $recoveryId |
||||
* @param array $imageIds |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function edit(array $data, int $recoveryId, array $imageIds = []): bool |
||||
{ |
||||
if ($this->where(['recovery_id' => $recoveryId])->save($data)) { |
||||
if ($imageIds) { |
||||
RecoveryImage::updates($recoveryId, $imageIds); |
||||
} |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\enum; |
||||
|
||||
class RecoveryStatusEnum extends EnumBasics |
||||
{ |
||||
// 待验收 |
||||
const ACCEPTED = 10; |
||||
|
||||
//已验收 |
||||
const ALREADY = 20; |
||||
|
||||
// 已取消 |
||||
const CANCEL = 30; |
||||
|
||||
/** |
||||
* 获取订单状态值 |
||||
* @return array |
||||
*/ |
||||
public static function data(): array |
||||
{ |
||||
return [ |
||||
self::ACCEPTED => [ |
||||
'name' => '待验收', |
||||
'value' => self::ACCEPTED, |
||||
], |
||||
self::ALREADY => [ |
||||
'name' => '已验收', |
||||
'value' => self::ALREADY, |
||||
], |
||||
self::CANCEL => [ |
||||
'name' => '已取消', |
||||
'value' => self::CANCEL, |
||||
], |
||||
]; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,38 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\enum; |
||||
|
||||
class RecoveryTypeEnum extends EnumBasics |
||||
{ |
||||
//上门 |
||||
const DOOR = 10; |
||||
|
||||
//门店 |
||||
const SHOP = 20; |
||||
|
||||
//邮寄 |
||||
const MAIL = 30; |
||||
|
||||
/** |
||||
* 获取回收方式 |
||||
* @return array |
||||
*/ |
||||
public static function data(): array |
||||
{ |
||||
return [ |
||||
self::DOOR => [ |
||||
'name' => '待验收', |
||||
'value' => self::DOOR, |
||||
], |
||||
self::SHOP => [ |
||||
'name' => '已验收', |
||||
'value' => self::SHOP, |
||||
], |
||||
self::MAIL => [ |
||||
'name' => '已取消', |
||||
'value' => self::MAIL, |
||||
], |
||||
]; |
||||
} |
||||
} |
@ -0,0 +1,48 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\model; |
||||
|
||||
use cores\BaseModel; |
||||
use think\model\relation\BelongsTo; |
||||
|
||||
class RecoveryImage extends BaseModel |
||||
{ |
||||
|
||||
// 定义表名 |
||||
protected $name = 'server_recovery_image'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'id'; |
||||
|
||||
protected $updateTime = false; |
||||
|
||||
/** |
||||
* 关联文件库 |
||||
* @return BelongsTo |
||||
*/ |
||||
public function file(): BelongsTo |
||||
{ |
||||
return $this->belongsTo('UploadFile', 'image_id', 'file_id'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:批量插入回收图片记录 |
||||
* @param int $recoveryId 回收ID |
||||
* @param array $imageIds 图片ID集 |
||||
* @return array|false |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function increased(int $recoveryId, array $imageIds) |
||||
{ |
||||
$dataset = []; |
||||
foreach ($imageIds as $imageId) { |
||||
$dataset[] = [ |
||||
'image_id' => $imageId, |
||||
'recovery_id' => $recoveryId, |
||||
'store_id' => self::$storeId |
||||
]; |
||||
} |
||||
return (new static)->addAll($dataset); |
||||
} |
||||
} |
@ -0,0 +1,67 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\model; |
||||
|
||||
use app\common\enum\RecoveryStatusEnum; |
||||
use app\common\enum\RecoveryTypeEnum; |
||||
use cores\BaseModel; |
||||
use think\model\relation\HasMany; |
||||
|
||||
class RecoveryOrder extends BaseModel |
||||
{ |
||||
// 定义表名 |
||||
protected $name = 'server_recovery'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'recovery_id'; |
||||
/** |
||||
* 追加字段 |
||||
* @var array |
||||
*/ |
||||
protected $append = [ |
||||
'order_status_text', //状态 |
||||
'recovery_type_text', //回收方式 |
||||
'image_list', |
||||
]; |
||||
|
||||
/** |
||||
* 关联图片表 |
||||
* @return HasMany |
||||
*/ |
||||
public function images(): HasMany |
||||
{ |
||||
return $this->hasMany('RecoveryImage')->order(['id']); |
||||
} |
||||
|
||||
public function getOrderStatusTextAttr($value, $data) |
||||
{ |
||||
$result = RecoveryStatusEnum::data(); |
||||
if (!empty($result[$data['order_status']]['name'])) { |
||||
return $result[$data['order_status']]['name']; |
||||
} |
||||
return '未知'; |
||||
} |
||||
|
||||
public function getRecoveryTypeTextAttr($value, $data) |
||||
{ |
||||
$result = RecoveryTypeEnum::data(); |
||||
if (!empty($result[$data['recovery_type']]['name'])) { |
||||
return $result[$data['recovery_type']]['name']; |
||||
} |
||||
return '未知'; |
||||
} |
||||
|
||||
/** |
||||
* @notes:详情 |
||||
* @param $where |
||||
* @param array $with |
||||
* @return RecoveryOrder|array|null |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function detail($where, array $with = []) |
||||
{ |
||||
return static::get($where, $with); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue