| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use fast\Tree;
- /**
- * 商品管理
- *
- * @icon fa fa-circle-o
- */
- class Goods extends Backend
- {
- /**
- * Goods模型对象
- * @var \app\admin\model\Goods
- */
- protected $model = null;
- protected $category_model = null;
- protected $dynamic_model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Goods;
- $this->category_model = new \app\admin\model\GoodsCategory();
- $this->dynamic_model = new \app\admin\model\GoodsDynamic();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['category'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
-
-
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 查看
- */
- public function category()
- {
- $list = $this->category_model->where(['status_switch'=>1])->select();
- return json($list);
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isAjax()) {
- $param = $this->request->request();
- $data = $param['row'];
- $data['category_name'] = $this->category_model->where(['id'=>$data['category_id']])->value('title');
- $data['createtime'] = time();
- $data['updatetime'] = time();
- //验证动态属性
- if (!empty($data['households'])){
- $dynamic = json_decode($data['households'],true);
- foreach ($dynamic as$k=>$v){
- if (empty($k)||empty($v)){
- $this->error('动态类型-类型或者价格不能为空');
- }
- }
- }
- // 开启事务
- $this->model->startTrans();
- $this->dynamic_model->startTrans();
- try {
- $id =$this->model->insertGetId($data);
- if (!empty($data['households'])){
- foreach ($dynamic as$k=>$v){
- $temp['goods_id'] = $id;
- $temp['title'] = $k;
- $temp['type_price'] = $v;
- $temp['createtime'] = time();
- $temp['updatetime'] = time();
- $this->dynamic_model->insert($temp);
- $this->dynamic_model->commit();
- }
- }
- $this->model->commit();
- } catch (\Exception $e) {
- $this->model->rollback();
- $this->dynamic_model->rollback();
- $this->error();
- }
- $this->success();
- }
- // 必须将结果集转换为数组
- // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
- $dataList = collection($this->category_model->where(['status_switch'=>1])->select())->toArray();
- foreach ($dataList as $k => &$v) {
- $v['pid'] = __($v['pid']);
- }
- unset($v);
- Tree::instance()->init($dataList);
- $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- $data = [0 => __('None')];
- foreach ($this->dataList as $k => &$v) {
- $data[$v['id']] = $v['title'];
- }
- $this->view->assign("ruledata", $data);
- return $this->view->fetch();
- }
- /**
- * 修改
- */
- public function edit($ids = null)
- {
- if ($this->request->isAjax()) {
- $param = $this->request->request();
- $data = $param['row'];
- $data['category_name'] = $this->category_model->where(['id'=>$data['category_id']])->value('title');
- $data['updatetime'] = time();
- //验证动态属性
- if (!empty($data['households'])){
- $dynamic = json_decode($data['households'],true);
- foreach ($dynamic as$k=>$v){
- if (empty($k)||empty($v)){
- $this->error('动态类型-类型或者价格不能为空');
- }
- }
- }
- // 开启事务
- $this->model->startTrans();
- $this->dynamic_model->startTrans();
- try {
- $this->model->where(['id'=>$ids])->update($data);
- if (!empty($data['households'])){
- $this->dynamic_model->where(['goods_id'=>$ids])->delete();
- foreach ($dynamic as$k=>$v){
- $temp['goods_id'] = $ids;
- $temp['title'] = $k;
- $temp['type_price'] = $v;
- $temp['createtime'] = time();
- $temp['updatetime'] = time();
- $this->dynamic_model->insert($temp);
- }
- }
- $this->model->commit();
- $this->dynamic_model->commit();
- } catch (\Exception $e) {
- $this->model->rollback();
- $this->dynamic_model->rollback();
- $this->error();
- }
- $this->success();
- }
- // 必须将结果集转换为数组
- // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
- $dataList = collection($this->category_model->where(['status_switch'=>1])->select())->toArray();
- foreach ($dataList as $k => &$v) {
- $v['pid'] = __($v['pid']);
- }
- unset($v);
- Tree::instance()->init($dataList);
- $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- $data = [0 => __('None')];
- foreach ($this->dataList as $k => &$v) {
- $data[$v['id']] = $v['title'];
- }
- $row = $this->model->get(['id' => $ids]);
- $row = $this->model->get(['id' => $ids]);
- $this->view->assign("row", $row);
- $this->view->assign("ruledata", $data);
- return $this->view->fetch();
- }
- }
|