Goodscategory.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use fast\Tree;
  6. /**
  7. * 商品分类
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Goodscategory extends Backend {
  12. /**
  13. * StockGoodscategory模型对象
  14. * @var \app\admin\model\stock\Goodscategory
  15. */
  16. protected $noNeedRight = ['madevalue', 'getjsTree', 'category'];
  17. protected $model = null;
  18. protected $categoryList = [];
  19. public function _initialize() {
  20. parent::_initialize();
  21. $this->model = model('\app\admin\model\stock\Goodscategory');
  22. $goodsCategoryList = collection($this->model->select())->toArray();
  23. Tree::instance()->init($goodsCategoryList);
  24. $goodsCategory = [];
  25. $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  26. $primary = array(array('name' => '无', 'id' => '0'));
  27. $result = array_merge_recursive($primary, $this->categoryList);
  28. foreach ($result as $k => $v) {
  29. $goodsCategory[$v['id']] = $v['name'];
  30. }
  31. $this->view->assign("goodsCategory", $goodsCategory);
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index() {
  42. if ($this->request->isAjax()) {
  43. $list = $this->categoryList;
  44. $total = count($this->categoryList);
  45. $result = array("total" => $total, "rows" => $list);
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 添加
  52. */
  53. public function add() {
  54. if ($this->request->isPost()) {
  55. $params = $this->request->post("row/a");
  56. if ($params) {
  57. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  58. $params[$this->dataLimitField] = $this->auth->id;
  59. }
  60. try {
  61. //是否采用模型验证
  62. if ($this->modelValidate) {
  63. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  64. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  65. $this->model->validate($validate);
  66. }
  67. $result = $this->model->allowField(true)->save($params);
  68. if ($result !== false) {
  69. $this->success();
  70. } else {
  71. $this->error($this->model->getError());
  72. }
  73. } catch (\think\exception\PDOException $e) {
  74. $this->error($e->getMessage());
  75. }
  76. }
  77. $this->error(__('Parameter %s can not be empty', ''));
  78. }
  79. $result = $this->model->where('pid', 0)->order('value desc')->limit(1)->select();
  80. if ($result) {
  81. $suffix = (int) ($result[0]['value']);
  82. $suffix ++;
  83. } else {
  84. $suffix = 0;
  85. }
  86. $value = sprintf("%02d", $suffix);
  87. $this->view->assign('value', $value);
  88. return $this->view->fetch();
  89. }
  90. /**
  91. * 根据商品类别生成商品代码
  92. */
  93. public function madevalue() {
  94. if ($this->request->isPost()) {
  95. $id = $this->request->post('id');
  96. if ($id || $id == 0) {
  97. try {
  98. $result = $this->model->where('pid', $id)->field('value')->order('value desc')->limit(1)->select();
  99. $value = '';
  100. if (count($result) > 0) {
  101. $suffix = (int) (substr($result[0]['value'], -2));
  102. $suffix ++;
  103. $suffix = sprintf("%02d", $suffix); //数字自动补零
  104. $prefix = substr($result[0]['value'], 0, -2);
  105. $value = $prefix . $suffix;
  106. } else {
  107. $result = $this->model->where('id', $id)->field('value')->order('value desc')->limit(1)->select();
  108. if (count($result) > 0) {
  109. $value = $result[0]['value'] . '.01';
  110. } else {
  111. $value = '01';
  112. }
  113. }
  114. $this->success("获取成功!","", ["code" => $value]);
  115. } catch (\think\exception\PDOException $e) {
  116. $this->error($e->getMessage());
  117. }
  118. }
  119. $this->error(__('Parameter %s can not be empty', ''));
  120. }
  121. $this->error('只接受POST请求', '');
  122. }
  123. /**
  124. * 获取商品类别树
  125. */
  126. public function getjsTree() {
  127. $categoryList = collection($this->model->select())->toArray();
  128. $result = [];
  129. foreach ($categoryList as $k => $v) {
  130. $n = [];
  131. $n['id'] = $v['id'];
  132. $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
  133. $n['text'] = $v['name'];
  134. $n['type'] = $v['id'];
  135. $n['data'] = $v;
  136. $n['state'] = ["opend" => true, "disabled" => false, "selected" => true];
  137. $result[] = $n;
  138. }
  139. return json($result);
  140. }
  141. /**
  142. * 获取商品类别名称
  143. */
  144. public function category() {
  145. $result = collection($this->model->select())->toArray();
  146. return json($result);
  147. }
  148. /**
  149. * 编辑
  150. */
  151. public function edit($ids = null) {
  152. $row = $this->model->get($ids);
  153. if (!$row) {
  154. $this->error(__('No Results were found'));
  155. }
  156. $adminIds = $this->getDataLimitAdminIds();
  157. if (is_array($adminIds)) {
  158. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  159. $this->error(__('You have no permission'));
  160. }
  161. }
  162. if ($this->request->isPost()) {
  163. $params = $this->request->post("row/a");
  164. if ($params) {
  165. $params = $this->preExcludeFields($params);
  166. $result = false;
  167. Db::startTrans();
  168. try {
  169. //是否采用模型验证
  170. if ($this->modelValidate) {
  171. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  172. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  173. $row->validateFailException(true)->validate($validate);
  174. }
  175. $result = $row->allowField(true)->save($params);
  176. Db::commit();
  177. } catch (ValidateException $e) {
  178. Db::rollback();
  179. $this->error($e->getMessage());
  180. } catch (PDOException $e) {
  181. Db::rollback();
  182. $this->error($e->getMessage());
  183. } catch (Exception $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. }
  187. if ($result !== false) {
  188. $this->success();
  189. } else {
  190. $this->error(__('No rows were updated'));
  191. }
  192. }
  193. $this->error(__('Parameter %s can not be empty', ''));
  194. }
  195. $this->view->assign("row", $row);
  196. return $this->view->fetch();
  197. }
  198. /**
  199. * 删除
  200. */
  201. public function del($ids = "") {
  202. if ($ids) {
  203. $pk = $this->model->getPk();
  204. $adminIds = $this->getDataLimitAdminIds();
  205. if (is_array($adminIds)) {
  206. $this->model->where($this->dataLimitField, 'in', $adminIds);
  207. }
  208. $list = $this->model->where($pk, 'in', $ids)->select();
  209. $count = 0;
  210. Db::startTrans();
  211. try {
  212. foreach ($list as $k => $v) {
  213. $goodscount = model('\app\admin\model\stock\Goods')->where("goodscategoryid", $v->id)->count();
  214. if ($goodscount > 0) {
  215. $this->error("当前分类下面包含" . $goodscount . "种商品,禁止删除!");
  216. }
  217. $count += $v->delete();
  218. }
  219. Db::commit();
  220. } catch (PDOException $e) {
  221. Db::rollback();
  222. $this->error($e->getMessage());
  223. } catch (Exception $e) {
  224. Db::rollback();
  225. $this->error($e->getMessage());
  226. }
  227. if ($count) {
  228. $this->success();
  229. } else {
  230. $this->error(__('No rows were deleted'));
  231. }
  232. }
  233. $this->error(__('Parameter %s can not be empty', 'ids'));
  234. }
  235. }