Goods.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 商品管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Goods extends Backend
  11. {
  12. /**
  13. * Goods模型对象
  14. * @var \app\admin\model\Goods
  15. */
  16. protected $model = null;
  17. protected $category_model = null;
  18. protected $dynamic_model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Goods;
  23. $this->category_model = new \app\admin\model\GoodsCategory();
  24. $this->dynamic_model = new \app\admin\model\GoodsDynamic();
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['category'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 查看
  60. */
  61. public function category()
  62. {
  63. $list = $this->category_model->where(['status_switch'=>1])->select();
  64. return json($list);
  65. }
  66. /**
  67. * 添加
  68. */
  69. public function add()
  70. {
  71. if ($this->request->isAjax()) {
  72. $param = $this->request->request();
  73. $data = $param['row'];
  74. $data['category_name'] = $this->category_model->where(['id'=>$data['category_id']])->value('title');
  75. $data['createtime'] = time();
  76. $data['updatetime'] = time();
  77. //验证动态属性
  78. if (!empty($data['households'])){
  79. $dynamic = json_decode($data['households'],true);
  80. foreach ($dynamic as$k=>$v){
  81. if (empty($k)||empty($v)){
  82. $this->error('动态类型-类型或者价格不能为空');
  83. }
  84. }
  85. }
  86. // 开启事务
  87. $this->model->startTrans();
  88. $this->dynamic_model->startTrans();
  89. try {
  90. $id =$this->model->insertGetId($data);
  91. if (!empty($data['households'])){
  92. foreach ($dynamic as$k=>$v){
  93. $temp['goods_id'] = $id;
  94. $temp['title'] = $k;
  95. $temp['type_price'] = $v;
  96. $temp['createtime'] = time();
  97. $temp['updatetime'] = time();
  98. $this->dynamic_model->insert($temp);
  99. $this->dynamic_model->commit();
  100. }
  101. }
  102. $this->model->commit();
  103. } catch (\Exception $e) {
  104. $this->model->rollback();
  105. $this->dynamic_model->rollback();
  106. $this->error();
  107. }
  108. $this->success();
  109. }
  110. // 必须将结果集转换为数组
  111. // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
  112. $dataList = collection($this->category_model->where(['status_switch'=>1])->select())->toArray();
  113. foreach ($dataList as $k => &$v) {
  114. $v['pid'] = __($v['pid']);
  115. }
  116. unset($v);
  117. Tree::instance()->init($dataList);
  118. $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  119. $data = [0 => __('None')];
  120. foreach ($this->dataList as $k => &$v) {
  121. $data[$v['id']] = $v['title'];
  122. }
  123. $this->view->assign("ruledata", $data);
  124. return $this->view->fetch();
  125. }
  126. /**
  127. * 修改
  128. */
  129. public function edit($ids = null)
  130. {
  131. if ($this->request->isAjax()) {
  132. $param = $this->request->request();
  133. $data = $param['row'];
  134. $data['category_name'] = $this->category_model->where(['id'=>$data['category_id']])->value('title');
  135. $data['updatetime'] = time();
  136. //验证动态属性
  137. if (!empty($data['households'])){
  138. $dynamic = json_decode($data['households'],true);
  139. foreach ($dynamic as$k=>$v){
  140. if (empty($k)||empty($v)){
  141. $this->error('动态类型-类型或者价格不能为空');
  142. }
  143. }
  144. }
  145. // 开启事务
  146. $this->model->startTrans();
  147. $this->dynamic_model->startTrans();
  148. try {
  149. $this->model->where(['id'=>$ids])->update($data);
  150. if (!empty($data['households'])){
  151. $this->dynamic_model->where(['goods_id'=>$ids])->delete();
  152. foreach ($dynamic as$k=>$v){
  153. $temp['goods_id'] = $ids;
  154. $temp['title'] = $k;
  155. $temp['type_price'] = $v;
  156. $temp['createtime'] = time();
  157. $temp['updatetime'] = time();
  158. $this->dynamic_model->insert($temp);
  159. }
  160. }
  161. $this->model->commit();
  162. $this->dynamic_model->commit();
  163. } catch (\Exception $e) {
  164. $this->model->rollback();
  165. $this->dynamic_model->rollback();
  166. $this->error();
  167. }
  168. $this->success();
  169. }
  170. // 必须将结果集转换为数组
  171. // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
  172. $dataList = collection($this->category_model->where(['status_switch'=>1])->select())->toArray();
  173. foreach ($dataList as $k => &$v) {
  174. $v['pid'] = __($v['pid']);
  175. }
  176. unset($v);
  177. Tree::instance()->init($dataList);
  178. $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  179. $data = [0 => __('None')];
  180. foreach ($this->dataList as $k => &$v) {
  181. $data[$v['id']] = $v['title'];
  182. }
  183. $row = $this->model->get(['id' => $ids]);
  184. $row = $this->model->get(['id' => $ids]);
  185. $this->view->assign("row", $row);
  186. $this->view->assign("ruledata", $data);
  187. return $this->view->fetch();
  188. }
  189. }