feat: #20220802 数据库修改

This commit is contained in:
赵世界 2022-08-09 16:56:52 +08:00
parent ec7704bbb2
commit 3d40201c17
29 changed files with 157 additions and 67 deletions

View File

@ -15,7 +15,7 @@
1. `composer install`
2. `cp .env.example .env`
3. 修改 .env 配置项为本地配置
4. 创建数据库 `CREATE DATABASE IF NOT EXISTS `erp` DEFAULT CHARACTER SET utf8;`
4. 创建数据库 `CREATE DATABASE IF NOT EXISTS `erp` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;`
5. `php artisan migrate` 如果数据填充没有执行成功,则需要再次执行 `php artisan migrate:fresh --seed`
6. `php artisan key:generate`
7. `php artisan update:super_admin_permissions` 更新超级管理员角色权限

View File

@ -40,7 +40,7 @@ class Test extends Command
*/
public function handle()
{
$shop = Shop::query()->find(2);
$shop = Shop::query()->find(1);
$business = BusinessFactory::init()->make($shop->plat_id);
$business->setShop($shop);
// 下载商品列表
@ -55,7 +55,7 @@ class Test extends Command
// 订单下载
$beginTime = DateTimeUtils::getMicroTime('2022-08-08');
$endTime = DateTimeUtils::getMicroTime('2022-08-09');
$business->downloadOrders($beginTime, $endTime);
$business->downloadOrdersAndSave($beginTime, $endTime);
$this->info('执行测试成功');
}

View File

@ -52,8 +52,8 @@ class RegisterController extends Controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'unique:users', 'max:255'],
'email' => ['string', 'email', 'max:255', 'unique:users'],
'name' => ['required', 'string', 'unique:users', 'max:191'],
'email' => ['string', 'email', 'max:191', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'role_id' => ['required', 'numeric', 'exists:roles,id'],
]);

View File

@ -32,7 +32,7 @@ class GoodsBrandsController extends Controller
{
$validator = Validator::make($request->all(), [
'names' => 'required|array',
'names.*' => 'required|string|max:255|unique:goods_brands,name',
'names.*' => 'required|string|max:191|unique:goods_brands,name',
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
@ -68,7 +68,7 @@ class GoodsBrandsController extends Controller
'name' => [
'required',
'string',
'max:255',
'max:191',
Rule::unique('goods_brands')->ignore($id),
]
]);

View File

@ -336,7 +336,6 @@ class GoodsSkusController extends Controller
$import = new GoodsSkusImport();
$path = $request->file('goodsSkus');
Excel::import($import, $path);
$this->setAfterUpdate('');
$this->addLog(0, 'import');
} catch (ValidationException $exception) {
$this->setValidatorFailResponse($exception->validator->getMessageBag()->getMessages());

View File

@ -32,7 +32,7 @@ class GoodsTypesController extends Controller
{
$validator = Validator::make($request->all(), [
'names' => 'required|array',
'names.*' => 'required|string|max:255|unique:goods_types,name',
'names.*' => 'required|string|max:191|unique:goods_types,name',
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
@ -68,7 +68,7 @@ class GoodsTypesController extends Controller
'name' => [
'required',
'string',
'max:255',
'max:191',
Rule::unique('goods_types')->ignore($id),
]
]);

View File

@ -39,7 +39,7 @@ class PermissionsController extends Controller
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255|unique:permissions,name',
'name' => 'required|string|max:191|unique:permissions,name',
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
@ -63,7 +63,7 @@ class PermissionsController extends Controller
public function update($id, Request $request)
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255', Rule::unique('permissions')->ignore($id),]
'name' => ['required', 'string', 'max:191', Rule::unique('permissions')->ignore($id),]
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

View File

@ -40,7 +40,7 @@ class RolesController extends Controller
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255|unique:roles,name',
'name' => 'required|string|max:191|unique:roles,name',
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
@ -75,7 +75,7 @@ class RolesController extends Controller
public function update($id, Request $request)
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255', Rule::unique('roles')->ignore($id),]
'name' => ['required', 'string', 'max:191', Rule::unique('roles')->ignore($id),]
]);
if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

View File

@ -37,7 +37,7 @@ class ShopsController extends Controller
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255|unique:shops,name',
'name' => 'required|string|max:191|unique:shops,name',
'plat_id' => 'required|integer',
]);
if ($validator->fails()) {

View File

@ -33,7 +33,7 @@ class UsersController extends Controller
public function store(Request $request, Faker $faker)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255|unique:users,name',
'name' => 'required|string|max:191|unique:users,name',
'password' => 'required|string|min:8|confirmed',
'email' => 'email',
'role_name' => 'required|string|exists:roles,name'
@ -67,7 +67,7 @@ class UsersController extends Controller
'name' => [
'required',
'string',
'max:255',
'max:191',
Rule::unique('users')->ignore($id),
],
// 'old_password' => 'sometimes|required|string|min:8',

View File

@ -26,8 +26,8 @@ class GoodsRequest extends FormRequest
{
return [
'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
'title' => ['required', 'string', 'max:255'],
'img_url' => ['required', 'string', 'max:255'],
'title' => ['required', 'string', 'max:191'],
'img_url' => ['required', 'string', 'max:191'],
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
'brand_id' => ['integer', 'exists:goods_brands,id'],
'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))],

View File

@ -27,7 +27,7 @@ class GoodsSkuRequest extends FormRequest
return [
'id' => ['sometimes', 'required', 'integer', 'exists:goods_skus,id'],
'goods_id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
'title' => ['sometimes', 'required', 'string', 'max:255'],
'title' => ['sometimes', 'required', 'string', 'max:191'],
'sku_code' => ['sometimes', 'required', 'distinct', 'alpha_dash', 'max:32'],
'status' => ['sometimes', 'required', 'integer', Rule::in([0, 1, 2])],
'num' => ['sometimes', 'required', 'integer'],

View File

@ -2,11 +2,14 @@
namespace App\Imports;
use App\Models\DailyStockRecord;
use App\Models\Goods;
use App\Models\GoodsBrand;
use App\Models\GoodsSku;
use App\Models\GoodsType;
use App\Utils\DateTimeUtils;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
@ -30,11 +33,11 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
unset($collection[0], $collection[1]);
$arr = $collection->toArray();
$validator = Validator::make($arr, [
'*.0' => ['required', 'string', 'max:255'],
'*.1' => ['required', 'string', 'max:255', 'exists:goods_types,name'],
'*.2' => ['string', 'max:255', 'exists:goods_brands,name'],
'*.0' => ['required', 'string', 'max:191'],
'*.1' => ['required', 'string', 'max:191', 'exists:goods_types,name'],
'*.2' => ['string', 'max:191', 'exists:goods_brands,name'],
'*.3' => ['required', 'alpha_dash', 'max:32'],
'*.4' => ['required', 'string', 'max:255'],
'*.4' => ['required', 'string', 'max:191'],
'*.5' => ['required', 'distinct', 'alpha_dash', 'max:32'],
'*.6' => ['required', 'string', Rule::in(['下架', '在售', '预警'])],
'*.7' => ['required', 'max:10'],
@ -76,16 +79,33 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows
}
$skus[] = $sku;
}
DB::beginTransaction();
try {
if ($newGoods) {
$goods = new Goods();
$goods->batchInsert(array_values($newGoods));
$hasGoods = Goods::query()->whereIn('goods_code', array_column($newGoods, 'goods_code'))->get(['id', 'goods_code'])->toArray();
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
foreach ($skus as &$sku) {
$sku['goods_id'] = isset($hasGoods[$sku['goods_id']]) ? $hasGoods[$sku['goods_id']]['id'] : $sku['goods_id'];
foreach ($skus as &$newGoodsSku) {
$newGoodsSku['goods_id'] = isset($hasGoods[$newGoodsSku['goods_id']]) ? $hasGoods[$newGoodsSku['goods_id']]['id'] : $newGoodsSku['goods_id'];
}
unset($newGoodsSku);
}
$goodsSku = new GoodsSku();
$goodsSku->batchInsert(array_values($skus));
$collection = GoodsSku::query()->whereIn('sku_code', array_column($skus, 'sku_code'))->get(['id', 'sku_code'])->toArray();
$newRecords = [];
foreach ($collection as $sku) {
$newRecords[] = [
'sku_id' => $sku['id'],
'day' => DateTimeUtils::getToday(),
];
}
$record = new DailyStockRecord();
$record->batchInsert($newRecords);
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
}
}
}

View File

@ -9,5 +9,29 @@ class BusinessGoodsSku extends Model
*
* @var array
*/
protected $guarded = [];
protected $fillable = [
'shop_id',
'business_order_id',
'already_cancel_number',
'cancel_status',
'category_name',
'external_sku_id',
'goods_amount',
'goods_cost_price',
'goods_id',
'goods_name',
'goods_number',
'goods_price',
'goods_purchase_price',
'goods_specification',
'help_sell_amount',
'is_supplier',
'need_verification_number',
'shipping_status',
'sku_id',
'sub_order_sn',
'theoretically_refund_amount',
'thumb_url',
'verification_number',
];
}

View File

@ -9,5 +9,40 @@ class BusinessOrder extends Model
*
* @var array
*/
protected $guarded = [];
protected $fillable = [
'shop_id',
'receiver_address_detail',
'receiver_address_province',
'self_pick_site_no',
'discount_amount',
'theoretical_refund_amount',
'receiver_address_district',
'verification_status',
'inner_transaction_id',
'is_supplier',
'service_amount',
'supply_participate_no',
'updated_at',
'order_amount',
'receiver_address_city',
'receiver_name',
'business_note',
'buyer_memo',
'logistics_type',
'help_sell_nickname',
'activity_title',
'after_sales_status',
'mall_activity_type',
'transaction_id',
'activity_no',
'confirm_at',
'platform_discount_amount',
'participate_no',
'receiver_mobile',
'shipping_status',
'shipping_amount',
'cancel_status',
'nick_name',
'order_sn',
];
}

View File

@ -15,7 +15,7 @@ abstract class BusinessClient
abstract public function auth();
abstract public function downloadGoodsList();
abstract public function downloadGoodsListAndBind();
abstract public function downloadGoods($skuId);
@ -23,7 +23,7 @@ abstract class BusinessClient
abstract public function incrQuantity($skuId);
abstract public function downloadOrders($beginTime, $endTime, $activityNo = '', $downloadType = 'default');
abstract public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default');
abstract public function saveOrders($orders);
@ -86,12 +86,8 @@ abstract class BusinessClient
'form_params' => $params
];
$res = (new Client())->request('POST', $url, $headers);
$size = $res->getBody()->getSize();
$res = json_decode($res->getBody()->getContents(), true);
$disableLogType = [
'pdd.ktt.goods.query.list',
'pdd.ktt.order.list',
'pdd.ktt.increment.order.query'
];
$log = new Log();
$log->module = 'plat';
$log->action = 'POST';
@ -99,7 +95,7 @@ abstract class BusinessClient
$log->target_id = $this->skuId;
$log->target_field = $params['type'];
$log->user_id = 1;
if (!in_array($params['type'], $disableLogType, true)) {
if ($size < 64000) {
$log->message = json_encode($res, 256);
}
$log->save();

View File

@ -38,7 +38,7 @@ class KuaiTuanTuan extends BusinessClient
return $this->shop;
}
public function downloadGoodsList($page = 1)
public function downloadGoodsListAndBind($page = 1)
{
[$type, $appendParams] = Goods::downloadGoods($this->shop->owner_id, $page);
$res = $this->doRequest($type, $appendParams);
@ -67,25 +67,29 @@ class KuaiTuanTuan extends BusinessClient
}
/**
* 没有发起售后,未取消的订单
* 下载没有发起售后,未取消的订单
*
* @param $beginTime
* @param $endTime
* @param $activityNo
* @param $downloadType
* @param int $page
* @param string $activityNo
* @param string $downloadType
* @return mixed
*/
public function downloadOrders($beginTime, $endTime, $activityNo = '', $downloadType = 'default')
public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default')
{
if ('increment' === $downloadType) {
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $activityNo);
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page);
} else {
[$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $activityNo);
[$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $page);
}
$res = $this->doRequest($type, $appendParams);
return $res['ktt_order_list_response']['order_list'];
$this->saveOrders($res['ktt_order_list_response']['order_list']);
exit();
$pageNum = ceil($res['ktt_order_list_response']['total_count'] / $appendParams['size']);
if ($pageNum > $page && 10 >= $page) {
$this->downloadOrdersAndSave($beginTime, $endTime, $page + 1);
}
}
public function saveOrders($orders)

View File

@ -3,20 +3,21 @@
namespace App\Services\Business\KuaiTuanTuan;
use App\Models\BusinessOrder;
use App\Models\BusinessOrderItem;
class Order
{
/**
* 根据成交时间拉取订单列表
*/
public static function downloadOrders($beginTime, $endTime, $activityNo)
public static function downloadOrders($beginTime, $endTime, $page = 1)
{
$type = 'pdd.ktt.order.list';
$appendParams = [
'confirm_at_begin' => $beginTime, // 成交启始时间, 必填,毫秒时间戳
'confirm_at_end' => $endTime, // 成交结束时间,必填, 毫秒时间戳,成交结束时间 - 成交启始时间 <= 24h
'page_number' => 1, // 页码, 必填
'page_size' => 100, // 数量, 必填, 1100
'page_number' => $page, // 页码, 必填
'page_size' => 1, // 数量, 必填, 1100
// 非必填
// 'activity_no' => $activityNo, // 团号
'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
@ -31,13 +32,13 @@ class Order
/**
* 快团团增量查订单
*/
public static function downloadIncrementOrders($beginTime, $endTime, $activityNo)
public static function downloadIncrementOrders($beginTime, $endTime, $page = 1)
{
$type = 'pdd.ktt.increment.order.query';
$appendParams = [
'start_updated_at' => $beginTime, // 更新起始时间
'end_updated_at' => $endTime, // 更新结束时间
'page_number' => 1, // 页码
'page_number' => $page, // 页码
'page_size' => 100, // 数量
// 非必填
// 'activity_no' => $activityNo, // 团号
@ -54,12 +55,13 @@ class Order
public static function saveOrders(array $orders, $shopId)
{
foreach ($orders as $order) {
unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']);
$orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
$order
);
foreach ($order['sub_order_list'] as $item) {
$orderRecord = BusinessOrder::updateOrCreate(
BusinessOrderItem::updateOrCreate(
['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']],
$item
);

View File

@ -15,7 +15,7 @@ class MiaoXuan extends BusinessClient
// TODO: Implement auth() method.
}
public function downloadGoodsList()
public function downloadGoodsListAndBind()
{
}
@ -33,7 +33,7 @@ class MiaoXuan extends BusinessClient
$this->formDataPostRequest($url, $appendParams);
}
public function downloadOrders($beginTime, $endTime, $activityNo = '', $downloadType = 'default')
public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default')
{
}

View File

@ -67,7 +67,7 @@ return [
|
*/
'timezone' => 'UTC',
'timezone' => 'PRC',
/*
|--------------------------------------------------------------------------

View File

@ -52,8 +52,8 @@ return [
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
@ -71,7 +71,7 @@ return [
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'charset' => 'utf8mb4',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
@ -86,7 +86,7 @@ return [
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'charset' => 'utf8mb4',
'prefix' => '',
'prefix_indexes' => true,
],

View File

@ -13,6 +13,7 @@ class CreateUsersTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
@ -23,6 +24,8 @@ class CreateUsersTable extends Migration
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
// 索引
$table->unique('name');
});
}

View File

@ -13,6 +13,7 @@ class CreateGoodsTypesTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('goods_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable(false);

View File

@ -13,6 +13,7 @@ class CreateGoodsBrandsTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('goods_brands', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable(false);

View File

@ -13,6 +13,7 @@ class CreateGoodsTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('goods', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title')->nullable(false);

View File

@ -13,6 +13,7 @@ class CreateGoodsSkusTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('goods_skus', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('goods_id')->nullable(false)->comment('商品id');
@ -28,6 +29,7 @@ class CreateGoodsSkusTable extends Migration
$table->unsignedInteger('reserve')->default(0)->comment('预留量');
$table->timestamps();
// 索引
$table->unique(['goods_id', 'sku_code']);
});
}

View File

@ -13,6 +13,7 @@ class CreateMenusTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('menus', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('code', 32)->nullable(false)->comment('菜单编码');

View File

@ -13,6 +13,7 @@ class CreateShopsTable extends Migration
*/
public function up()
{
Schema::defaultStringLength(191);
Schema::create('shops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();

View File

@ -16,7 +16,7 @@ class CreateBusinessOrdersTable extends Migration
Schema::create('business_orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('shop_id');
$table->bigInteger('activity_no')->nullable();
$table->string('activity_no')->nullable();
$table->string('activity_title')->nullable();
$table->bigInteger('after_sales_status')->nullable();
$table->string('business_note')->nullable();