123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\approve\controller;
- use app\common\constant\CommonConstant;
- use app\common\model\Goods as model;
- use app\common\model\GoodsStock;
- use app\common\service\GoodsCategoryService;
- use library\Controller;
- use think\Db;
- use think\Exception;
- /**
- * 商品
- */
- class Goods extends Controller
- {
- /**
- * 绑定数据表
- * @var string
- */
- protected $table = 'Goods';
- /**
- * 控制器初始化
- */
- protected function initialize()
- {
- $this->get_status_list = CommonConstant::get_status_list();
- }
- /**
- * 列表
- * @auth true
- * @menu true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function index()
- {
- $status = input('status');
- $goods_name = input('goods_name');
- $this->title = '商品列表';
- $data = model::field('is_deleted', true)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->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 . '%');
- })
- ->with([
- 'goodsCategoryOne' => function ($query) {
- $query->field('id,name');
- },
- 'goodsCategory' => function ($query) {
- $query->field('id,name');
- },
- 'goodsStock'
- ])
- ->order('sort desc,id desc');
- self::_init($data);
- }
- /**
- * 添加
- * @auth true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function add()
- {
- $this->title = '添加';
- $this->_submit('form');
- }
- /**
- * 编辑
- * @auth true
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function edit()
- {
- $this->title = '编辑';
- $this->_submit('form');
- }
- protected function _submit($template)
- {
- $id = input('id') ?: 0;
- if ($this->request->isGet()) {
- list($data) = [[]];
- 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]);
- }
- if ($this->request->isPost()) {
- list($data) = [$this->request->post()];
- 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('该商品不存在或已删除');
- }
- $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)
- ->where('id', 'neq',$id)
- ->find();
- if ($goods_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) {
- $this->error('该商品已存在不能重复添加');
- }
- }
- Db::startTrans();
- try {
- if ($id > 0) {
- self::create_stock($id, $data,$info,'update');
- $info->force(true)->save($data);
- } else {
- $result = model::create($data);
- $id = $result->id;
- self::create_stock($id, $data,[],'create');
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('商品编辑成功!', 'javascript:history.back()');
- }
- }
- protected function create_stock($id, $data,$info,$type)
- {
- $goods_stock = $data['goods_stock'];
- $goods_stock_data = [];
- $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'];
- }
- }
- }
- if ($add_data) {
- GoodsStock::insertAll($add_data);
- }
- if ($del_ids) {
- GoodsStock::where(['id' => ['IN', $del_ids]])->delete();
- }
- } else {
- // 添加商品 添加规格
- foreach ($goods_stock as $key => $val) {
- $goods_stock_data[] = [
- 'goods_id' => $id,
- 'name' => $val['name'],
- 'stock' => $val['stock'],
- ];
- }
- if ($goods_stock_data) {
- GoodsStock::insertAll($goods_stock_data);
- }
- }
- return true;
- }
- /**
- * 删除
- * @auth true
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function remove()
- {
- $this->applyCsrfToken();
- // TODO 删除商品,检验申请数据
- $this->_delete($this->table);
- }
- /**
- * 启用
- * @auth true
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function resume()
- {
- $this->applyCsrfToken();
- $this->_save($this->table, ['status' => CommonConstant::STATUS_NORMAL]);
- }
- /**
- * 禁用
- * @auth true
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function forbid()
- {
- $this->applyCsrfToken();
- $this->_save($this->table, ['status' => CommonConstant::STATUS_FROZEN]);
- }
- }
|