Commit 797d2748 authored by luhongguang's avatar luhongguang

update:商品编辑增加判断

parent 1f4ff8d9
......@@ -27,5 +27,7 @@ class GoodsException extends BaseException
13 => '无权限操作',
14 => '当前商品不是审核通过状态,无法做上下架操作',
15 => '当前商品不存在',
16 => '到期时间不得小于当前时间',
17 => '商品名称重复,请重新修复',
];
}
\ No newline at end of file
......@@ -12,16 +12,28 @@ namespace Validate;
class GoodsAddValidate extends BaseValidate
{
protected $rule = [
// 'goods_spu_id' => 'require',
// 'online_status' => 'require',
// 'uid' => 'require',
// 'username' => 'require',
'category_1_id' => 'require',
'category_2_id' => 'require',
'name' => 'require',
'url' => 'require',
'expiration_time' => 'require',
'rule_desc' => 'require',
'price' => 'require',
'inventory_total' => 'require',
'life_account_id' => 'require',
'merchant_id' => 'require',
];
protected $message = [
"goods_spu_id" => "商品id不能为空",
"online_status" => "商品上下架状态不能为空",
"uid" => "用户id不能为空",
"username" => "用户username不能为空",
'category_1_id' => '请选择所属类目',
'category_2_id' => '请选择所属类目',
'name' => '商品名称不能为空',
'url' => '请上传商品图片',
'expiration_time' => '请填写到期时间',
'rule_desc' => '请填写使用规则',
'price' => '请填写售价',
'inventory_total' => '请填写库存',
'life_account_id' => 'life_account_id 不能为空',
'merchant_id' => 'merchant_id 不能为空',
];
}
\ No newline at end of file
<?php
namespace Validate;
/**
* Class GoodsEditValidate
*
* @package Validate
*/
class GoodsEditValidate extends BaseValidate
{
protected $rule = [
'category_1_id' => 'require',
'category_2_id' => 'require',
'name' => 'require',
'url' => 'require',
'expiration_time' => 'require',
'rule_desc' => 'require',
'price' => 'require',
'inventory_total' => 'require',
];
protected $message = [
'category_1_id' => '请选择所属类目',
'category_2_id' => '请选择所属类目',
'name' => '商品名称不能为空',
'url' => '请上传商品图片',
'expiration_time' => '请填写到期时间',
'rule_desc' => '请填写使用规则',
'price' => '请填写售价',
'inventory_total' => '请填写库存',
];
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ use App\Services\goods\GoodsService;
use App\Services\goods\CategoryService;
use \Validate\CheckGoodsNameValidate;
use \Validate\GoodsAuditValidate;
use \Validate\GoodsAddValidate;
use \Validate\GoodsEditValidate;
class GoodsController extends Base
......@@ -23,6 +25,8 @@ class GoodsController extends Base
*/
public function addAction()
{
(new GoodsAddValidate())->validate();
$params = $this->params;
GoodsService::addGoods($params);
$this->success();
......@@ -33,6 +37,8 @@ class GoodsController extends Base
*/
public function editAction()
{
(new GoodsEditValidate())->validate();
$params = $this->params;
GoodsService::editGoods($params);
$this->success();
......@@ -81,7 +87,7 @@ class GoodsController extends Base
$params = $this->params;
$res = GoodsService::checkGoodsName($params["name"], $params["life_account_id"]);
$this->success(["data"=>$res]);
$this->success(["data" => $res]);
}
/**
......
......@@ -30,6 +30,12 @@ class GoodsService
return $res['id_datetime'][$type] ?? [];
}
/**
* 雪花id
* @param $type
* @param int $count
* @return array|mixed
*/
public static function getSnowIdgenId($type, $count = 1)
{
$res = Idgen::get(appConfig('idgen.partner'), appConfig('idgen.key'), [[
......@@ -77,7 +83,10 @@ class GoodsService
throw new GoodsException(['cus' => 2]);
}
$spuId = $res[0];
$checkName = self::checkGoodsName($spuData["name"], $spuData["life_account_id"]);
if (!$checkName["can_use"]) {
throw new GoodsException(['cus' => 17]);
}
GoodsSpu::save([
"goods_spu_id" => $spuId,
"shop_id" => $shopId,
......@@ -89,9 +98,6 @@ class GoodsService
"sub_type" => GoodsSpu::SUB_TYPE_TO_SHOP,
"sale_type" => empty($spuData["setmeal"]) ? GoodsSpu::SALE_TYPE_NO_COMBINATION : GoodsSpu::SALE_TYPE_COMBINATION,
"rule_limit" => $spuData["rule_limit"],
"rule_date_type" => $spuData["rule_date_type"],
"rule_start_time" => $spuData["rule_start_time"],
"rule_end_time" => $spuData["rule_end_time"],
"rule_desc" => $spuData["rule_desc"],
"rule_refund" => $spuData["rule_refund"],
]);
......@@ -102,7 +108,6 @@ class GoodsService
* 添加sku
* @param $spuId
* @param $shopId
* @param $lifeAccountId
* @param array $skuData
* @return mixed
* @throws GoodsException
......@@ -115,6 +120,10 @@ class GoodsService
}
$skuId = $res[0];
if ($skuData["expiration_time"] < date("Y-m-d H:i:s")) {
throw new GoodsException(['cus' => 16]);
}
$skuParams = [
"goods_spu_id" => $spuId,
"goods_sku_id" => $skuId,
......@@ -127,9 +136,6 @@ class GoodsService
"desc" => $skuData["desc"],
"expiration_time" => $skuData["expiration_time"],
"rule_limit" => $skuData["rule_limit"],
"rule_date_type" => $skuData["rule_date_type"],
"rule_start_time" => $skuData["rule_start_time"],
"rule_end_time" => $skuData["rule_end_time"],
"rule_desc" => $skuData["rule_desc"],
"rule_refund" => $skuData["rule_refund"],
"inventory_total" => $skuData["inventory_total"],
......@@ -307,11 +313,16 @@ class GoodsService
* 商品名是否可用
* @param $goodsName
* @param $lifeAccountId
* @return bool
* @param int $goodsSpuId
* @return bool[]
*/
public static function checkGoodsName($goodsName, $lifeAccountId)
public static function checkGoodsName($goodsName, $lifeAccountId, $goodsSpuId = 0)
{
$sku = GoodsSpu::get("*", ["name" => $goodsName, "life_account_id" => $lifeAccountId]);
if ($goodsSpuId) {
$sku = GoodsSpu::get("*", ["name" => $goodsName, "life_account_id" => $lifeAccountId, "goods_spu_id[!]" => $goodsSpuId]);
} else {
$sku = GoodsSpu::get("*", ["name" => $goodsName, "life_account_id" => $lifeAccountId]);
}
$nameCanUse = false;
if (empty($sku)) {
$nameCanUse = true;
......@@ -328,14 +339,14 @@ class GoodsService
public static function editGoods($params = [])
{
$goodsSpuId = $params["goods_spu_id"];
$goodsSpu = GoodsSpu::get("*", ["goods_spu_id"=>$goodsSpuId]);
$goodsSpu = GoodsSpu::get("*", ["goods_spu_id" => $goodsSpuId]);
if (empty($goodsSpu)) {
throw new GoodsException(["cus" => 15]);
}
GoodsSpu::beginTransaction();
self::editGoodsSpu($goodsSpuId, $params);
self::editGoodsSpu($goodsSpu, $params);
$goodsSkuList = GoodsSku::select("*", ["goods_spu_id" => $goodsSpuId]);
if (!empty($goodsSkuList)) {
foreach ($goodsSkuList as $sku) {
......@@ -350,26 +361,45 @@ class GoodsService
return true;
}
private static function editGoodsSpu($goodsSpuId, $spuData = [])
/**
* @param $goodsSpu
* @param array $params
* @return \Api\PhpUtils\Mysql\MysqlBase
* @throws GoodsException
*/
private static function editGoodsSpu($goodsSpu, $params = [])
{
$checkName = self::checkGoodsName($params["name"], $goodsSpu["life_account_id"], $goodsSpu["goods_spu_id"]);
if (!$checkName["can_use"]) {
throw new GoodsException(['cus' => 17]);
}
return GoodsSpu::save([
"name" => $spuData["name"],
"url" => $spuData["url"],
"desc" => $spuData["desc"],
"name" => $params["name"],
"url" => $params["url"],
"desc" => $params["desc"],
"type" => GoodsSpu::TYPE_VIRTUAL,
"sub_type" => GoodsSpu::SUB_TYPE_TO_SHOP,
"sale_type" => empty($spuData["setmeal"]) ? GoodsSpu::SALE_TYPE_NO_COMBINATION : GoodsSpu::SALE_TYPE_COMBINATION,
"rule_limit" => $spuData["rule_limit"],
"rule_date_type" => $spuData["rule_date_type"],
"rule_start_time" => $spuData["rule_start_time"],
"rule_end_time" => $spuData["rule_end_time"],
"rule_desc" => $spuData["rule_desc"],
"rule_refund" => $spuData["rule_refund"],
], ["goods_spu_id"=>$goodsSpuId]);
"sale_type" => empty($params["setmeal"]) ? GoodsSpu::SALE_TYPE_NO_COMBINATION : GoodsSpu::SALE_TYPE_COMBINATION,
"rule_limit" => $params["rule_limit"],
"rule_desc" => $params["rule_desc"],
"rule_refund" => $params["rule_refund"],
], ["goods_spu_id" => $goodsSpu["goods_spu_id"]]);
}
/**
* @param $goodsSkuId
* @param $skuData
* @param array $params
* @return \Api\PhpUtils\Mysql\MysqlBase
* @throws GoodsException
*/
private static function editGoodsSku($goodsSkuId, $skuData, $params = [])
{
if ($params["expiration_time"] < date("Y-m-d H:i:s")) {
throw new GoodsException(['cus' => 16]);
}
$skuParams = [
"category_1_id" => $params["category_1_id"],
"category_2_id" => $params["category_2_id"],
......@@ -378,9 +408,6 @@ class GoodsService
"desc" => $params["desc"],
"expiration_time" => $params["expiration_time"],
"rule_limit" => $params["rule_limit"],
"rule_date_type" => $params["rule_date_type"],
"rule_start_time" => $params["rule_start_time"],
"rule_end_time" => $params["rule_end_time"],
"rule_desc" => $params["rule_desc"],
"rule_refund" => $params["rule_refund"],
"inventory_total" => $params["inventory_total"],
......@@ -392,6 +419,6 @@ class GoodsService
if (!empty($params["setmeal"])) {
$skuParams["setmeal"] = $params["setmeal"];
}
return GoodsSku::save($skuParams, ["goods_sku_id"=>$goodsSkuId]);
return GoodsSku::save($skuParams, ["goods_sku_id" => $goodsSkuId]);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment