diff --git a/app/Dicts.php b/app/Dicts.php index 9b8edb00..8cb4648d 100644 --- a/app/Dicts.php +++ b/app/Dicts.php @@ -25,10 +25,12 @@ class Dicts extends Repository 'bloc_channel' => ['运营商', '中间商'], 'package_type' => ['基础套餐', '续费包', '加油包', '可选包', '附加包'], 'tables' => ['real' => 'RD', 'virtual' => 'VD'], + 'order_status' => ['已下单', '已取消', '已出库', '已签收'], + 'transaction_status' => ['未收款', '已收款'], ]; public function __construct() { parent::__construct(self::DICTS); } -} \ No newline at end of file +} diff --git a/app/Domains/Company/Http/Controllers/AccountController.php b/app/Domains/Company/Http/Controllers/AccountController.php index 91fa2758..f93414f1 100644 --- a/app/Domains/Company/Http/Controllers/AccountController.php +++ b/app/Domains/Company/Http/Controllers/AccountController.php @@ -3,6 +3,7 @@ namespace App\Domains\Company\Http\Controllers; use App\Core\Controller; use Illuminate\Http\Request; +use App\Exceptions\AuthException; use App\Domains\Sms\Services\SmsService; use App\Domains\Virtual\Services\CompanyAccountService; @@ -75,12 +76,12 @@ class AccountController extends Controller } if (empty($this->account->mobile)) { - return err('用户未绑定手机号'); + throw new AuthException('用户未绑定手机号', AuthException::NOT_BOUND_MOBILE); } $freqsecs = app(SmsService::class)->sendVcode($this->account->mobile, '密码找回'); - return res(['freg' => $freqsecs], '发送成功'); + return res(['freg' => $freqsecs, 'mobile' => $this->account->mobile], '发送成功'); } /** diff --git a/app/Domains/Company/Http/Controllers/OrderController.php b/app/Domains/Company/Http/Controllers/OrderController.php index 8a31948b..399abba3 100644 --- a/app/Domains/Company/Http/Controllers/OrderController.php +++ b/app/Domains/Company/Http/Controllers/OrderController.php @@ -25,13 +25,46 @@ class OrderController extends Controller /** * 订单列表 */ - public function paginate() + public function paginate(Dicts $dicts) { $conditions = $this->request->all(); + + $res = $this->orderService->paginate($conditions); - return res($res, '订单列表', 201); + $carrierOperators = $dicts->get('carrier_operator'); + $payChannels = array_keys($dicts->get('pay_channel')); + $orderStatues = $dicts->get('order_status'); + $transactionStatuses = $dicts->get('transaction_status'); + + + $list = $res->map(function ($item) use ($carrierOperators, $payChannels, $orderStatues, $transactionStatuses) { + return [ + 'id' => $item->id, + 'sn' => $item->sn, + 'package_name' => $item->package->name, + 'pay_channel' => $payChannels[$item->pay_channel], + 'carrier_operator' => $carrierOperators[$item->package->carrier_operator], + 'unit_price' => $item->unit_price, + 'counts' => $item->counts, + 'total_price' => $item->total_price, + 'custom_price' => $item->custom_price, + 'order_status' => $orderStatues[$item->order_status], + 'transaction_status' => $transactionStatuses[$item->transaction_status], + 'order_at' => $item->order_at, + ]; + }); + + if (empty($list)) { + return err('没有更多数据'); + } + + $order_at = $list->last()->order_at; + + $count = $this->orderService->count($conditions); + + return res($list, '订单列表', 201); } /** @@ -39,10 +72,11 @@ class OrderController extends Controller */ public function store() { - $conditions = $this->request->all(); + $attributes = $this->request->all(); + $attributes['company_id'] = $this->account->company_id; - $res = $this->orderService->paginate($conditions); + $res = $this->orderService->store($attributes); - return res($res, '订单列表', 201); + return res($res, '下单成功'); } } diff --git a/app/Domains/Company/Routes/api.php b/app/Domains/Company/Routes/api.php index fb7e854d..fcfbf3b7 100644 --- a/app/Domains/Company/Routes/api.php +++ b/app/Domains/Company/Routes/api.php @@ -26,5 +26,6 @@ $router->group(['prefix' => 'companies', 'as' => 'companies'], function ($router $router->get('/products', ['as' => 'products', 'uses' => 'BootstrapController@products']); $router->get('/orders/paginate', ['as' => 'orders/paginate', 'uses' => 'OrderController@paginate']); + $router->post('/orders/store', ['as' => 'orders/store', 'uses' => 'OrderController@store']); }); }); diff --git a/app/Domains/Virtual/Services/CommonService.php b/app/Domains/Virtual/Services/CommonService.php index cd468038..ae7fa6b1 100644 --- a/app/Domains/Virtual/Services/CommonService.php +++ b/app/Domains/Virtual/Services/CommonService.php @@ -47,4 +47,23 @@ class CommonService return ''; } + + /** + * 获取支付方式编号 + * + * @param string $payChannel + * @return int + */ + public static function intPayChannel($payChannel) + { + $payChannels = array_values(app(Dicts::class)->get('pay_channel')); + + foreach ($payChannels as $key => $value) { + if (in_array($payChannel, $value)) { + return $key; + } + } + + return 255; + } } diff --git a/app/Domains/Virtual/Services/OrderService.php b/app/Domains/Virtual/Services/OrderService.php index 54be0fda..a0bd9d5e 100644 --- a/app/Domains/Virtual/Services/OrderService.php +++ b/app/Domains/Virtual/Services/OrderService.php @@ -5,9 +5,13 @@ use App\Dicts; use App\Core\Service; use App\Models\Virtual\Order; use Illuminate\Validation\Rule; +use Illuminate\Support\Facades\DB; use App\Exceptions\NotExistException; +use App\Exceptions\NotAllowedException; use Illuminate\Support\Facades\Validator; +use App\Domains\Virtual\Services\CommonService; use App\Domains\Virtual\Repositories\OrderRepository; +use App\Domains\Virtual\Repositories\ProductRepository; class OrderService extends Service { @@ -39,7 +43,26 @@ class OrderService extends Service } /** - * 存储企业定价 + * 订单计数 + * + * @param array $conditions + * @return mixed + */ + public function count(array $conditions = []) + { + $select = [ + DB::raw('COUNT(*) as total_count'), + DB::raw('SUM(custom_price) as total_price'), + DB::raw('SUM(custom_price*transaction_status) as transacted_price'), + ]; + + $res = $this->orderRepository->select($select)->withConditions($conditions)->get(); + + return $res; + } + + /** + * 下单 * * @param array $attributes * @return Order @@ -50,6 +73,7 @@ class OrderService extends Service $rule = [ 'company_id' => ['required', 'exists:virtual_companies,id'], + 'product_id' => ['required'], 'counts' => ['required'], 'pay_channel' => ['required', Rule::in(array_collapse(app(Dicts::class)->get('pay_channel')))], 'contacts' => ['required', 'display_length:2,32'], @@ -61,7 +85,10 @@ class OrderService extends Service $message = [ 'company_id.required' => '请输入企业ID', 'company_id.exists' => '企业不存在或已删除', - 'counts' => '请输入订购数量', + 'product_id.required' => '请选择套餐', + 'counts.required' => '请输入订购数量', + 'pay_channel.required' => '请选择支付方式', + 'pay_channel.in' => '支付方式不合法', 'contacts.required' => '联系人不能为空', 'contacts.display_length' => '联系人名称长度不合法', 'mobile.required' => '手机号不能为空', @@ -72,16 +99,31 @@ class OrderService extends Service Validator::validate($attributes, $rule, $message); + if (!$product = app(ProductRepository::class)->withConditions(['id' => $attributes['product_id']])->first()) { + throw new NotExistException('套餐不存在或已删除'); + } + + if ($product->company_id != $attributes['company_id']) { + throw new NotAllowedException('非法操作'); + } + + $attributes['unit_price'] = $product->base_price; + $attributes['total_price'] = $attributes['unit_price'] * $attributes['counts']; + $attributes['custom_price'] = $attributes['unit_price'] * $attributes['counts']; + $attributes['order_at'] = date('Y-m-d H:i:s'); + $attributes['pay_channel'] = CommonService::intPayChannel($attributes['pay_channel']); + $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id; + if (!$attributes['id']) { - $node = $this->productRepository->create($attributes); + $node = $this->orderRepository->create($attributes); } if ($attributes['id']) { - if (!$node = $this->productRepository->find($attributes['id'])) { - throw new NotExistException('地址不存在或已删除'); + if (!$node = $this->orderRepository->find($attributes['id'])) { + throw new NotExistException('订单不存在或已删除'); } - $this->productRepository->setModel($node)->update($attributes); + $this->orderRepository->setModel($node)->update($attributes); } return $node; diff --git a/app/Models/Virtual/Order.php b/app/Models/Virtual/Order.php index 784bd931..79c2b920 100644 --- a/app/Models/Virtual/Order.php +++ b/app/Models/Virtual/Order.php @@ -3,6 +3,7 @@ namespace App\Models\Virtual; use App\Core\Model; +use App\Models\Real\OrderCard; class Order extends Model { @@ -10,6 +11,16 @@ class Order extends Model public function cards() { - return $this->hasMany(Card::class, 'order_id', 'id'); + return $this->hasMany(OrderCard::class, 'order_id', 'id'); + } + + public function company() + { + return $this->belongsTO(Company::class, 'company_id', 'id'); + } + + public function package() + { + return $this->belongsTo(Package::class, 'package_id', 'id'); } } diff --git a/database/migrations/2018_11_27_175137_create_base_tables.php b/database/migrations/2018_11_27_175137_create_base_tables.php index a4181071..f9981bcd 100644 --- a/database/migrations/2018_11_27_175137_create_base_tables.php +++ b/database/migrations/2018_11_27_175137_create_base_tables.php @@ -77,33 +77,6 @@ class CreateBaseTables extends Migration db_alter("{$prefix}_cards", "{$type}订单企业套餐卡关联表"); - Schema::create("{$prefix}_orders", function (Blueprint $table) { - $table->increments('id')->comment('订单ID'); - $table->string('sn', 32)->comment('订单编号'); - $table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型(0:基础套餐)'); - $table->integer('company_id')->unsigned()->default(0)->comment("{$type}企业ID"); - $table->string('transaction_no', 64)->comment('交易流水号'); - $table->tinyInteger('pay_channel')->unsigned()->default(0)->comment('支付方式(0:银行转账 1:账户余额 2:微信支付 3:支付宝 4:天猫续费)'); - $table->integer('unit_price')->unsigned()->default(0)->comment('单价'); - $table->integer('counts')->unsigned()->default(0)->comment('数量'); - $table->integer('total_price')->unsigned()->default(0)->comment('总价'); - $table->timestamp('order_at')->nullable()->comment('下单时间'); - $table->string('address')->default('')->comment('收货地址'); - $table->string('contact')->default('')->comment('联系人'); - $table->string('mobile')->default('')->comment('电话'); - $table->text('logistics_remark')->nullable()->comment('物流备注'); - $table->text('remark')->nullable()->comment('订单备注'); - $table->timestamps(); - $table->softDeletes(); - - $table->unique(['sn', 'deleted_at']); - $table->index('type'); - $table->index('company_id'); - $table->index('order_at'); - }); - - db_alter("{$prefix}_orders", "{$type}订单"); - Schema::create("{$prefix}_added_orders", function (Blueprint $table) { $table->increments('id')->comment('订单ID'); $table->string('sn', 32)->comment('订单编号'); @@ -128,6 +101,33 @@ class CreateBaseTables extends Migration db_alter("{$prefix}_orders", "{$type}增值包订单"); } + Schema::create("real_orders", function (Blueprint $table) { + $table->increments('id')->comment('订单ID'); + $table->string('sn', 32)->comment('订单编号'); + $table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型(0:基础套餐)'); + $table->integer('company_id')->unsigned()->default(0)->comment("企业ID"); + $table->string('transaction_no', 64)->comment('交易流水号'); + $table->tinyInteger('pay_channel')->unsigned()->default(0)->comment('支付方式(0:银行转账 1:账户余额 2:微信支付 3:支付宝 4:天猫续费)'); + $table->integer('unit_price')->unsigned()->default(0)->comment('单价'); + $table->integer('counts')->unsigned()->default(0)->comment('数量'); + $table->integer('total_price')->unsigned()->default(0)->comment('总价'); + $table->timestamp('order_at')->nullable()->comment('下单时间'); + $table->string('address')->default('')->comment('收货地址'); + $table->string('contacts')->default('')->comment('联系人'); + $table->string('mobile')->default('')->comment('电话'); + $table->text('logistics_remark')->nullable()->comment('物流备注'); + $table->text('remark')->nullable()->comment('订单备注'); + $table->timestamps(); + $table->softDeletes(); + + $table->unique(['sn', 'deleted_at']); + $table->index('type'); + $table->index('company_id'); + $table->index('order_at'); + }); + + db_alter("real_orders", "RD订单"); + Schema::create("blocs", function (Blueprint $table) { $table->increments('id')->comment('集团ID'); $table->string('sn', 32)->comment('集团编号'); diff --git a/database/migrations/2018_12_11_173842_add_package_id_product_id_to_virtual_products.php b/database/migrations/2018_12_11_173842_add_package_id_product_id_to_virtual_products.php deleted file mode 100644 index 2d32cb70..00000000 --- a/database/migrations/2018_12_11_173842_add_package_id_product_id_to_virtual_products.php +++ /dev/null @@ -1,34 +0,0 @@ -integer('package_id')->unsigned()->default(0)->after('company_id')->comment('套餐ID'); - $table->integer('product_id')->unsigned()->default(0)->after('package_id')->comment('定价ID'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::table("virtual_orders", function (Blueprint $table) { - $table->dropColumn('package_id'); - $table->dropColumn('product_id'); - }); - } -} diff --git a/database/migrations/2018_12_12_110035_add_custom_price_order_status_transaction_status_to_virtual_orders.php b/database/migrations/2018_12_12_110035_add_custom_price_order_status_transaction_status_to_virtual_orders.php deleted file mode 100644 index c11ad1c6..00000000 --- a/database/migrations/2018_12_12_110035_add_custom_price_order_status_transaction_status_to_virtual_orders.php +++ /dev/null @@ -1,34 +0,0 @@ -integer('custom_price')->unsigned()->default(0)->after('total_price')->comment('自定义总价'); - $table->tinyInteger('order_status')->unsigned()->default(0)->after('mobile')->comment('订单状态(0:已下单 1:已取消 2:已出库 3:已签收)'); - $table->tinyInteger('transaction_status')->unsigned()->default(0)->after('order_status')->comment('收款状态(0:未收款 1:已收款)'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - $table->dropColumn('custom_price'); - $table->dropColumn('order_status'); - $table->dropColumn('transaction_status'); - } -} diff --git a/database/migrations/2018_12_12_170419_create_virtual_orders_table.php b/database/migrations/2018_12_12_170419_create_virtual_orders_table.php new file mode 100644 index 00000000..35933a7f --- /dev/null +++ b/database/migrations/2018_12_12_170419_create_virtual_orders_table.php @@ -0,0 +1,59 @@ +increments('id')->comment('订单ID'); + $table->string('sn', 32)->comment('订单编号'); + $table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型(0:基础套餐)'); + $table->integer('company_id')->unsigned()->default(0)->comment("企业ID"); + $table->integer('package_id')->unsigned()->default(0)->after('company_id')->comment('套餐ID'); + $table->integer('product_id')->unsigned()->default(0)->after('package_id')->comment('定价ID'); + $table->string('transaction_no', 64)->default('')->comment('交易流水号'); + $table->tinyInteger('pay_channel')->unsigned()->default(0)->comment('支付方式(0:银行转账 1:账户余额 2:微信支付 3:支付宝 4:天猫续费)'); + $table->integer('unit_price')->unsigned()->default(0)->comment('单价'); + $table->integer('counts')->unsigned()->default(0)->comment('数量'); + $table->integer('total_price')->unsigned()->default(0)->comment('总价'); + $table->integer('custom_price')->unsigned()->default(0)->after('total_price')->comment('自定义总价'); + $table->timestamp('order_at')->nullable()->comment('下单时间'); + $table->string('area')->default('')->comment('区域'); + $table->string('address')->default('')->comment('收货地址'); + $table->string('contacts')->default('')->comment('联系人'); + $table->string('mobile')->default('')->comment('电话'); + $table->tinyInteger('order_status')->unsigned()->default(0)->after('mobile')->comment('订单状态(0:已下单 1:已取消 2:已出库 3:已签收)'); + $table->tinyInteger('transaction_status')->unsigned()->default(0)->after('order_status')->comment('收款状态(0:未收款 1:已收款)'); + $table->text('logistics_remark')->nullable()->comment('物流备注'); + $table->text('remark')->nullable()->comment('订单备注'); + $table->timestamps(); + $table->softDeletes(); + + $table->unique(['sn', 'deleted_at']); + $table->index('type'); + $table->index('company_id'); + $table->index('order_at'); + }); + + db_alter("virtual_orders", "VD订单"); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('virtual_orders'); + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 56d4d47b..b23a57b0 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -16,6 +16,7 @@ return array( 'CreateFailedJobsTable' => $baseDir . '/database/migrations/2018_11_16_190020_create_failed_jobs_table.php', 'CreateOrderTables' => $baseDir . '/database/migrations/2018_11_27_175146_create_order_tables.php', 'CreatePackageTables' => $baseDir . '/database/migrations/2018_11_27_175152_create_package_tables.php', + 'CreateVirtualOrdersTable' => $baseDir . '/database/migrations/2018_12_12_170419_create_virtual_orders_table.php', 'DatabaseSeeder' => $baseDir . '/database/seeds/DatabaseSeeder.php', 'DivisionByZeroError' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/DivisionByZeroError.php', 'Error' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/Error.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 7d962ac4..b2e0ca91 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -705,6 +705,7 @@ class ComposerStaticInite79258a3e34ad3e251999111d9f334d9 'CreateFailedJobsTable' => __DIR__ . '/../..' . '/database/migrations/2018_11_16_190020_create_failed_jobs_table.php', 'CreateOrderTables' => __DIR__ . '/../..' . '/database/migrations/2018_11_27_175146_create_order_tables.php', 'CreatePackageTables' => __DIR__ . '/../..' . '/database/migrations/2018_11_27_175152_create_package_tables.php', + 'CreateVirtualOrdersTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_12_170419_create_virtual_orders_table.php', 'DatabaseSeeder' => __DIR__ . '/../..' . '/database/seeds/DatabaseSeeder.php', 'DivisionByZeroError' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/DivisionByZeroError.php', 'Error' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/Error.php',