ParameterTemplate.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\parameter;
  12. use app\common\repositories\store\parameter\ParameterValueRepository;
  13. use app\common\repositories\store\parameter\ParameterTemplateRepository;
  14. use app\validate\admin\ParameterTemplateValidate;
  15. use think\App;
  16. use crmeb\basic\BaseController;
  17. use think\exception\ValidateException;
  18. use app\common\repositories\store\StoreCategoryRepository;
  19. /**
  20. * 商品参数模板
  21. */
  22. class ParameterTemplate extends BaseController
  23. {
  24. protected $repository;
  25. /**
  26. * City constructor.
  27. * @param App $app
  28. * @param ParameterTemplateRepository $repository
  29. */
  30. public function __construct(App $app, ParameterTemplateRepository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * 列表
  37. * @Author:Qinii
  38. * @Date: 2020/5/8
  39. * @Time: 14:40
  40. * @return mixed
  41. */
  42. public function lst()
  43. {
  44. [$page, $limit] = $this->getPage();
  45. $where = $this->request->params(['template_name', 'cate_ids', 'mer_name', 'mer_id']);
  46. $where['is_mer'] = $this->request->param('is_mer', 1);
  47. if ($merId = $this->request->merId()) {
  48. $where['mer_id'] = $merId;
  49. unset($where['is_mer']);
  50. }
  51. // 处理分类id
  52. if (!empty($where['cate_ids'])) {
  53. $where['cate_id'] = end($where['cate_ids']);
  54. if(count($where['cate_ids']) !== 3) {
  55. $cartWhere = [
  56. ['path', 'like', '%/'.$where['cate_id'].'/%'],
  57. ['is_show', '=', 1]
  58. ];
  59. $categories = app()->make(StoreCategoryRepository::class)->selectWhere($cartWhere, 'store_category_id')->toArray();
  60. $storeCategoryIds = array_column($categories, 'store_category_id');
  61. $where['cate_id'] = $storeCategoryIds;
  62. }
  63. unset($where['cate_ids']);
  64. }
  65. $data = $this->repository->getList($where, $page, $limit);
  66. return app('json')->success($data);
  67. }
  68. /**
  69. * 详情
  70. * @param $id
  71. * @return \think\response\Json
  72. * @author Qinii
  73. */
  74. public function detail($id)
  75. {
  76. $data = $this->repository->detail($id, $this->request->merId());
  77. return app('json')->success($data);
  78. }
  79. /**
  80. * 添加
  81. * @return \think\response\Json
  82. * @author Qinii
  83. */
  84. public function create()
  85. {
  86. $data = $this->checkParams(1);
  87. $this->repository->create($this->request->merId(), $data);
  88. return app('json')->success('添加成功');
  89. }
  90. /**
  91. * 编辑
  92. * @param $id
  93. * @return \think\response\Json
  94. * @author Qinii
  95. */
  96. public function update($id)
  97. {
  98. $data = $this->checkParams();
  99. $where['template_id'] = $id;
  100. if ($merId = $this->request->merId()) {
  101. $where['mer_id'] = $merId;
  102. }
  103. if (!$res = $this->repository->getWhere($where)) {
  104. return app('json')->fail('数据不存在');
  105. }
  106. $this->repository->update($id, $data, $merId);
  107. return app('json')->success('编辑成功');
  108. }
  109. public function deleteValue($id, ParameterValueRepository $repository)
  110. {
  111. $repository->dostory($id, $this->request->merId());
  112. return app('json')->success('删除成功');
  113. }
  114. /**
  115. * 删除
  116. * @param $id
  117. * @return \think\response\Json
  118. * @author Qinii
  119. */
  120. public function delete($id)
  121. {
  122. $where['template_id'] = $id;
  123. if ($merId = $this->request->merId()) {
  124. $where['mer_id'] = $merId;
  125. }
  126. if (!$this->repository->getWhere($where)) {
  127. return app('json')->fail('数据不存在');
  128. }
  129. $this->repository->delete($id);
  130. return app('json')->success('操作成功');
  131. }
  132. /**
  133. * 根据cate_id获取参数模板列表
  134. * @return \think\response\Json
  135. * @author Qinii
  136. * @day 2022/11/22
  137. */
  138. public function select()
  139. {
  140. $where = $this->request->params(['cate_id', 0]);
  141. $where['mer_id'] = 0;
  142. $data['sys'] = $this->repository->getSelect($where);
  143. $where = ['mer_id' => $this->request->merId()];
  144. $data['mer'] = $this->repository->getSelect($where);
  145. return app('json')->success($data);
  146. }
  147. /**
  148. * 根据模板id 获取参数
  149. * @param $id
  150. * @return \think\response\Json
  151. * @author Qinii
  152. * @day 2022/11/22
  153. */
  154. public function show()
  155. {
  156. $template_ids = $this->request->param('template_ids');
  157. if (!$template_ids) return app('json')->success([]);
  158. $where['template_ids'] = $template_ids;
  159. $data = $this->repository->show($where);
  160. return app('json')->success($data);
  161. }
  162. /**
  163. * 验证参数
  164. * @param $isCreate
  165. * @return array
  166. * @author Qinii
  167. */
  168. public function checkParams($isCreate = 0)
  169. {
  170. $mer_id = $this->request->merId();
  171. $data = $this->request->params(['template_name', ['cate_ids', []], 'sort', 'params','delete_params']);
  172. app()->make(ParameterTemplateValidate::class)->check($data);
  173. if ($mer_id == 0 && empty($data['cate_ids'])) {
  174. throw new ValidateException('请选择商品分类');
  175. }
  176. if ($isCreate) {
  177. foreach ($data['params'] as $item) {
  178. foreach ($item['values'] as &$subArray) {
  179. if (isset($subArray['parameter_value_id'])) {
  180. unset($subArray['parameter_value_id']);
  181. }
  182. }
  183. if (isset($item['parameter_id'])) unset($item['parameter_id']);
  184. if (isset($item['template_id'])) unset($item['template_id']);
  185. $params[] = $item;
  186. }
  187. $data['params'] = $params;
  188. }
  189. return $data;
  190. }
  191. }