|
@@ -12,6 +12,7 @@ use App\Models\Product\Type as ProductType;
|
|
|
use App\Models\Product\Attr as ProductAttr;
|
|
|
use App\Models\Product\Skus as ProductSkus;
|
|
|
use App\Models\Product\City as ProductCity;
|
|
|
+use App\Models\ProductPhoto;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
@@ -97,7 +98,7 @@ class Product extends Auth{
|
|
|
* 添加
|
|
|
*
|
|
|
* */
|
|
|
- public function add( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
+ public function add( Request $request, Model $Model,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
if( request()->isMethod('post') ){
|
|
|
// 验证参数
|
|
|
$request->scene('add')->validate();
|
|
@@ -122,6 +123,12 @@ class Product extends Auth{
|
|
|
$attr = request('attr',[]);
|
|
|
$skuList = request('sku',[]);
|
|
|
$cityIds = request('city_ids',[]);
|
|
|
+ $photoList = request('photo_list',[]);
|
|
|
+ // 循环
|
|
|
+ foreach ($photoList as $key => $value) {
|
|
|
+ if( !$value ) unset($photoList[$key]);
|
|
|
+ }
|
|
|
+ $photoList = array_values($photoList);
|
|
|
// 如果没有选择,则意味着全部
|
|
|
$cityIds = $cityIds ? $cityIds : [1];
|
|
|
$data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
|
|
@@ -131,6 +138,8 @@ class Product extends Auth{
|
|
|
// 限购提示
|
|
|
if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
|
|
|
// 限购提示
|
|
|
+ if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
|
|
|
+ // 限购提示
|
|
|
if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
|
|
|
// 上下架
|
|
|
if( $data['puton_time'] ) {
|
|
@@ -171,6 +180,25 @@ class Product extends Auth{
|
|
|
// 重组
|
|
|
$specAttr[$key] = $value;
|
|
|
}
|
|
|
+ // 图册
|
|
|
+ foreach ($photoList as $key => $value) {
|
|
|
+ // 整理数据
|
|
|
+ $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
|
|
|
+ // 重新整理
|
|
|
+ $photoList[$key] = $value;
|
|
|
+ }
|
|
|
+ // 存在图册
|
|
|
+ if( $photoList ) {
|
|
|
+ // 写入失败
|
|
|
+ $result = $ProductPhoto->query()->insert($photoList);
|
|
|
+ // 提示新增失败
|
|
|
+ if( !$result ) {
|
|
|
+ // 回滚
|
|
|
+ DB::rollBack();
|
|
|
+ // 提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 循环SKU
|
|
|
foreach ($skuList as $attrNames => $value) {
|
|
|
// 属性ID值
|
|
@@ -257,7 +285,7 @@ class Product extends Auth{
|
|
|
* 编辑
|
|
|
*
|
|
|
* */
|
|
|
- public function edit( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
+ public function edit( Request $request, Model $Model,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
if(request()->isMethod('post')){
|
|
|
// 验证参数
|
|
|
$request->scene('edit')->validate();
|
|
@@ -282,6 +310,12 @@ class Product extends Auth{
|
|
|
$attr = request('attr',[]);
|
|
|
$skuList = request('sku',[]);
|
|
|
$cityIds = request('city_ids',[]);
|
|
|
+ $photoList = request('photo_list',[]);
|
|
|
+ // 循环
|
|
|
+ foreach ($photoList as $key => $value) {
|
|
|
+ if( !$value ) unset($photoList[$key]);
|
|
|
+ }
|
|
|
+ $photoList = array_values($photoList);
|
|
|
// 如果没有选择,则意味着全部
|
|
|
$cityIds = $cityIds ? $cityIds : [1];
|
|
|
$data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
|
|
@@ -291,6 +325,8 @@ class Product extends Auth{
|
|
|
// 限购提示
|
|
|
if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
|
|
|
// 限购提示
|
|
|
+ if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
|
|
|
+ // 限购提示
|
|
|
if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
|
|
|
// 上下架
|
|
|
if( $data['puton_time'] ) {
|
|
@@ -317,6 +353,17 @@ class Product extends Auth{
|
|
|
// 提示
|
|
|
return json_send(['code'=>'error','msg'=>'修改失败']);
|
|
|
}
|
|
|
+ // 获取旧图册
|
|
|
+ $oldPhoto = $ProductPhoto->getListByProductId($id);
|
|
|
+ // 循环个数
|
|
|
+ for ($i=0; $i < 4; $i++) {
|
|
|
+ // 如果存在旧数据与新数据,修改
|
|
|
+ if( isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->update(['thumb'=>(string)$photoList[$i],'update_time'=>time()]);
|
|
|
+ // 如果存在旧数据,不存在新数据,删除
|
|
|
+ if( isset($oldPhoto[$i]) && !isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->delete();
|
|
|
+ // 如果不存在旧数据,存在新数据,新增
|
|
|
+ if( !isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->add(['sort'=>$i,'product_id'=>$id,'thumb'=>(string)$photoList[$i],'insert_time'=>time(),'update_time'=>time()]);
|
|
|
+ }
|
|
|
// 组合数据
|
|
|
foreach ($specAttr as $key => $value) {
|
|
|
// 查询结果
|
|
@@ -405,6 +452,7 @@ class Product extends Auth{
|
|
|
$oldData = $oldData->toArray();
|
|
|
$oldData['description'] = $Model->getDesc($id);
|
|
|
$oldData['city_ids'] = $ProductCity->getListByProductId($id);
|
|
|
+ $photoList = $ProductPhoto->getListByProductId($id);
|
|
|
// 获取产品属性
|
|
|
$attrList = $ProductAttr->getListByProductId($id);
|
|
|
// 规格属性
|
|
@@ -442,6 +490,7 @@ class Product extends Auth{
|
|
|
$this->assign('businessList',$businessList);
|
|
|
$this->assign('producerList',$producerList);
|
|
|
$this->assign('oldData',$oldData);
|
|
|
+ $this->assign('photoList',$photoList);
|
|
|
$this->assign('skuList',$skuList);
|
|
|
$this->assign('specList',$specList);
|
|
|
$this->assign('crumbs','修改');
|
|
@@ -454,7 +503,7 @@ class Product extends Auth{
|
|
|
* 编辑
|
|
|
*
|
|
|
* */
|
|
|
- public function copy( Request $request, Model $Model,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
+ public function copy( Request $request, Model $Model,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
|
|
|
if( request()->isMethod('post') ){
|
|
|
// 验证参数
|
|
|
$request->scene('add')->validate();
|
|
@@ -477,6 +526,12 @@ class Product extends Auth{
|
|
|
$attr = request('attr',[]);
|
|
|
$skuList = request('sku',[]);
|
|
|
$cityIds = request('city_ids',[]);
|
|
|
+ $photoList = request('photo_list',[]);
|
|
|
+ // 循环
|
|
|
+ foreach ($photoList as $key => $value) {
|
|
|
+ if( !$value ) unset($photoList[$key]);
|
|
|
+ }
|
|
|
+ $photoList = array_values($photoList);
|
|
|
// 如果没有选择,则意味着全部
|
|
|
$cityIds = $cityIds ? $cityIds : [1];
|
|
|
$data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
|
|
@@ -484,9 +539,13 @@ class Product extends Auth{
|
|
|
// 限购提示
|
|
|
if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
|
|
|
// 限购提示
|
|
|
+ if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
|
|
|
+ // 限购提示
|
|
|
if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
|
|
|
// 总库存
|
|
|
if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
|
|
|
+ // 总库存
|
|
|
+ $data['stock_total'] = $data['stock'];
|
|
|
// 获取规格属性
|
|
|
$specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
|
|
|
// 开启事务
|
|
@@ -501,6 +560,25 @@ class Product extends Auth{
|
|
|
// 提示
|
|
|
return json_send(['code'=>'error','msg'=>'新增失败']);
|
|
|
}
|
|
|
+ // 图册
|
|
|
+ foreach ($photoList as $key => $value) {
|
|
|
+ // 整理数据
|
|
|
+ $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
|
|
|
+ // 重新整理
|
|
|
+ $photoList[$key] = $value;
|
|
|
+ }
|
|
|
+ // 存在图册
|
|
|
+ if( $photoList ) {
|
|
|
+ // 写入失败
|
|
|
+ $result = $ProductPhoto->query()->insert($photoList);
|
|
|
+ // 提示新增失败
|
|
|
+ if( !$result ) {
|
|
|
+ // 回滚
|
|
|
+ DB::rollBack();
|
|
|
+ // 提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 组合数据
|
|
|
foreach ($specAttr as $key => $value) {
|
|
|
// 查询结果
|
|
@@ -543,6 +621,8 @@ class Product extends Auth{
|
|
|
// 转成好存储的数据
|
|
|
$value['product_id']= $id;
|
|
|
// 转成好存储的数据
|
|
|
+ $value['stock_total']= $value['stock'];
|
|
|
+ // 转成好存储的数据
|
|
|
$value['insert_time']= time();
|
|
|
// 转成好存储的数据
|
|
|
$value['update_time']= time();
|
|
@@ -590,6 +670,7 @@ class Product extends Auth{
|
|
|
$oldData = $oldData->toArray();
|
|
|
$oldData['description'] = $Model->getDesc($id);
|
|
|
$oldData['city_ids'] = $ProductCity->getListByProductId($id);
|
|
|
+ $photoList = $ProductPhoto->getListByProductId($id);
|
|
|
// 获取产品属性
|
|
|
$attrList = $ProductAttr->getListByProductId($id);
|
|
|
// 规格属性
|
|
@@ -627,6 +708,7 @@ class Product extends Auth{
|
|
|
$this->assign('businessList',$businessList);
|
|
|
$this->assign('producerList',$producerList);
|
|
|
$this->assign('oldData',$oldData);
|
|
|
+ $this->assign('photoList',$photoList);
|
|
|
$this->assign('skuList',$skuList);
|
|
|
$this->assign('specList',$specList);
|
|
|
$this->assign('crumbs','复制');
|