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(); // 删除商品时,检验申请数据 $id = input('id') ?: 0; $apply = ApproveApplyGoods::where('goods_id',$id)->column('info_id'); $stock = ApproveStockGoods::where('goods_id',$id)->column('info_id'); $use = ApproveUseGoods::where('goods_id',$id)->column('info_id'); $data = array_merge($apply,$stock); $info_id = array_merge($data,$use); if($info_id){ $approve = \app\common\model\ApproveInfo::field('id') ->where('id','in',$info_id) ->where('status',CommonConstant::STATUS_2) ->where('is_deleted',CommonConstant::IS_DELETED_0) ->find(); if($approve){ $this->error('存在有审批中的申请单,不能删除'); } } $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]); } }