|
@@ -63,14 +63,14 @@ class Goods extends Controller
|
|
|
->when(array_key_exists($status, $this->get_status_list), function ($query) use ($status) {
|
|
|
$query->where('status', $status);
|
|
|
})
|
|
|
- ->when($goods_name,function ($query) use($goods_name){
|
|
|
- $query->where('goods_name','like','%'.$goods_name.'%');
|
|
|
+ ->when($goods_name, function ($query) use ($goods_name) {
|
|
|
+ $query->where('goods_name', 'like', '%' . $goods_name . '%');
|
|
|
})
|
|
|
->with([
|
|
|
- 'goodsCategoryOne'=>function($query){
|
|
|
+ 'goodsCategoryOne' => function ($query) {
|
|
|
$query->field('id,name');
|
|
|
},
|
|
|
- 'goodsCategory'=>function($query){
|
|
|
+ 'goodsCategory' => function ($query) {
|
|
|
$query->field('id,name');
|
|
|
},
|
|
|
'goodsStock'
|
|
@@ -110,149 +110,139 @@ class Goods extends Controller
|
|
|
$this->_submit('form');
|
|
|
}
|
|
|
|
|
|
- protected function _submit($template){
|
|
|
- $id = input('id') ?:0;
|
|
|
+ protected function _submit($template)
|
|
|
+ {
|
|
|
+ $id = input('id') ?: 0;
|
|
|
if ($this->request->isGet()) {
|
|
|
list($data) = [[]];
|
|
|
- if($id > 0){
|
|
|
- $data = model::with(['goodsStock'])->find($id);
|
|
|
- if($data){
|
|
|
- $data['first_classify'] = $data['goods_category_first'];
|
|
|
- $data['second_classify'] = $data['goods_category_id'];
|
|
|
+ if ($id > 0) {
|
|
|
+ $info = model::field('is_deleted', true)
|
|
|
+ ->with([
|
|
|
+ 'goodsStock'
|
|
|
+ ])
|
|
|
+ ->find($id);
|
|
|
+ if (!$info){
|
|
|
+ $this->error('该商品不存在或已删除');
|
|
|
}
|
|
|
+ $info['first_classify'] = $info['goods_category_first'];
|
|
|
+ $info['second_classify'] = $info['goods_category_id'];
|
|
|
+ $data = $info;
|
|
|
}
|
|
|
- $this->category_list = GoodsCategoryService::get_list([],1);
|
|
|
- return $this->fetch($template,['vo' => $data]);
|
|
|
+ $this->category_list = GoodsCategoryService::get_list([], 1);
|
|
|
+ return $this->fetch($template, ['vo' => $data]);
|
|
|
}
|
|
|
if ($this->request->isPost()) {
|
|
|
list($data) = [$this->request->post()];
|
|
|
- if($data){
|
|
|
- $data['goods_category_first'] = $data['first_classify'];
|
|
|
- $data['goods_category_id'] = $data['second_classify'];
|
|
|
+ if (!isset_full($data, 'goods_stock')) {
|
|
|
+ $this->error('请添加商品规格');
|
|
|
+ }
|
|
|
+ if (!array_filter($data['goods_stock'])) {
|
|
|
+ $this->error('请添加商品规格!');
|
|
|
+ }
|
|
|
+ $data['goods_category_first'] = $data['first_classify'];
|
|
|
+ $data['goods_category_id'] = $data['second_classify'];
|
|
|
+
|
|
|
+ if($id > 0){
|
|
|
+ $info = model::field('is_deleted', true)
|
|
|
+ ->with([
|
|
|
+ 'goodsStock'
|
|
|
+ ])
|
|
|
+ ->find($id);
|
|
|
+ if (!$info){
|
|
|
+ $this->error('该商品不存在或已删除');
|
|
|
+ }
|
|
|
+ } else{
|
|
|
$goods_info = model::field('id')
|
|
|
->where('goods_category_first', $data['goods_category_first'])
|
|
|
->where('goods_category_id', $data['goods_category_id'])
|
|
|
->where('goods_name', $data['goods_name'])
|
|
|
->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
->find();
|
|
|
- if($goods_info){
|
|
|
+ if ($goods_info) {
|
|
|
$this->error('该商品已存在不能重复添加');
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- Db::startTrans();
|
|
|
- try{
|
|
|
- if($id > 0){
|
|
|
- model::where('id',$id)->update($data);
|
|
|
- $goods_id = $id;
|
|
|
- } else{
|
|
|
- $result = model::create($data);
|
|
|
- $goods_id = $result->id;
|
|
|
- }
|
|
|
- self::create_stock($goods_id,$data);
|
|
|
-
|
|
|
- Db::commit();
|
|
|
- } catch (Exception $e){
|
|
|
- Db::rollback();
|
|
|
- $this->error($e->getMessage());
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ if ($id > 0) {
|
|
|
+ self::create_stock($id, $data,$info,'update');
|
|
|
+ $info->save($data);
|
|
|
+ } else {
|
|
|
+ $result = model::create($data);
|
|
|
+ $id = $result->id;
|
|
|
+ self::create_stock($id, $data,[],'create');
|
|
|
}
|
|
|
- $this->success('商品编辑成功!', 'javascript:history.back()');
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ Db::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
+ $this->success('商品编辑成功!', 'javascript:history.back()');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected function create_stock($id, $data = [])
|
|
|
+ protected function create_stock($id, $data,$info,$type)
|
|
|
{
|
|
|
$goods_stock = $data['goods_stock'];
|
|
|
$goods_stock_data = [];
|
|
|
- if(isset($data['id']) && $data['id'] > 0){
|
|
|
+ $add_data = [];
|
|
|
+ $del_ids = [];
|
|
|
+ if ($type == 'update') {
|
|
|
+ // 编辑商品 编辑规格
|
|
|
+ foreach ($goods_stock as $key => $val) {
|
|
|
+ if (isset($val['id']) && $val['id'] > 0) {
|
|
|
+ // 修改规格
|
|
|
+ $goods_stock_data[$val['id']] = [
|
|
|
+ 'goods_id' => $id,
|
|
|
+ 'name' => $val['name'],
|
|
|
+ 'stock' => $val['stock'],
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ // 添加规格
|
|
|
+ $add_data[] = [
|
|
|
+ 'goods_id' => $id,
|
|
|
+ 'name' => $val['name'],
|
|
|
+ 'stock' => $val['stock'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($info['goods_stock'])){
|
|
|
+ foreach ($info['goods_stock'] as $index) {
|
|
|
+ if (array_key_exists($index['id'], $goods_stock_data)) {
|
|
|
+ // 更新规格
|
|
|
+ $save_data = $goods_stock_data[$index['id']];
|
|
|
+ $index->save($save_data);
|
|
|
+ } else {
|
|
|
+ // 删除规格
|
|
|
+ $del_ids[] = $index['id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- } else{
|
|
|
+ if ($add_data) {
|
|
|
+ GoodsStock::insertAll($add_data);
|
|
|
+ }
|
|
|
+ if ($del_ids) {
|
|
|
+ GoodsStock::where(['id' => ['IN', $del_ids]])->delete();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
// 添加商品 添加规格
|
|
|
- foreach ($goods_stock as $key=>$val){
|
|
|
+ foreach ($goods_stock as $key => $val) {
|
|
|
$goods_stock_data[] = [
|
|
|
- 'goods_id'=>$id,
|
|
|
- 'name'=>$val['name'],
|
|
|
- 'stock'=>$val['stock'],
|
|
|
+ 'goods_id' => $id,
|
|
|
+ 'name' => $val['name'],
|
|
|
+ 'stock' => $val['stock'],
|
|
|
];
|
|
|
}
|
|
|
- if($goods_stock_data){
|
|
|
+ if ($goods_stock_data) {
|
|
|
GoodsStock::insertAll($goods_stock_data);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 表单处理
|
|
|
- * @param array $data
|
|
|
- * @throws \think\db\exception\DataNotFoundException
|
|
|
- * @throws \think\db\exception\ModelNotFoundException
|
|
|
- * @throws \think\exception\DbException
|
|
|
- */
|
|
|
- protected function _form_filter(&$data)
|
|
|
- {
|
|
|
- if ($this->request->isGet()) {
|
|
|
- if($data){
|
|
|
- // 编辑获取数据时
|
|
|
- $data['first_classify'] = $data['goods_category_first'];
|
|
|
- $data['second_classify'] = $data['goods_category_id'];
|
|
|
- $data['goods_stock'] = GoodsStock::where('goods_id',$data['id'])->select();
|
|
|
- }
|
|
|
- $this->category_list = GoodsCategoryService::get_list([],1);
|
|
|
- }
|
|
|
- if ($this->request->isPost()) {
|
|
|
- if($data){
|
|
|
- // 添加编辑提交数据时
|
|
|
- $data['goods_category_first'] = $data['first_classify'];
|
|
|
- $data['goods_category_id'] = $data['second_classify'];
|
|
|
- if(isset($data['id']) && $data['id'] > 0){
|
|
|
- } else{
|
|
|
- $goods_info = model::field('id')
|
|
|
- ->where('goods_category_first', $data['goods_category_first'])
|
|
|
- ->where('goods_category_id', $data['goods_category_id'])
|
|
|
- ->where('goods_name', $data['goods_name'])
|
|
|
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
- ->find();
|
|
|
- if($goods_info){
|
|
|
- $this->error('该商品已存在不能重复添加');
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 表单结果处理
|
|
|
- * @param boolean $result
|
|
|
- */
|
|
|
- protected function _form_result($result)
|
|
|
- {
|
|
|
- if ($result && $this->request->isPost()) {
|
|
|
- list($data) = [$this->request->post()];
|
|
|
- $goods_stock = $data['goods_stock'];
|
|
|
- $goods_stock_data = [];
|
|
|
- if(isset($data['id']) && $data['id']){
|
|
|
-
|
|
|
- } else{
|
|
|
- // 添加商品 添加规格
|
|
|
- foreach ($goods_stock as $key=>$val){
|
|
|
- $goods_stock_data[] = [
|
|
|
- 'goods_id'=>$result,
|
|
|
- 'name'=>$val['name'],
|
|
|
- 'stock'=>$val['stock'],
|
|
|
- ];
|
|
|
- }
|
|
|
- if($goods_stock_data){
|
|
|
- GoodsStock::insertAll($goods_stock_data);
|
|
|
- }
|
|
|
- }
|
|
|
- $this->success('商品编辑成功!', 'javascript:history.back()');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 删除
|