CategoryController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace app\backend\modules\goods\controllers;
  3. use app\backend\modules\goods\models\Category;
  4. use app\backend\modules\goods\models\Categorys;
  5. use app\backend\modules\goods\models\Goods;
  6. use app\backend\modules\uploadVerificate\UploadVerificationBaseController;
  7. use app\common\components\BaseController;
  8. use app\common\helpers\PaginationHelper;
  9. use app\common\models\goods\GoodsService;
  10. use app\common\models\GoodsCategory;
  11. use app\common\models\Member;
  12. use app\common\models\Order;
  13. use app\common\helpers\Url;
  14. use Illuminate\Support\Facades\DB;
  15. use phpDocumentor\Reflection\DocBlock\Description;
  16. use Setting;
  17. use app\backend\modules\filtering\models\Filtering;
  18. /**
  19. * Created by PhpStorm.
  20. * Author: 芸众商城 www.yunzshop.com
  21. * Date: 2017/2/22
  22. * Time: 下午1:51
  23. */
  24. class CategoryController extends UploadVerificationBaseController
  25. {
  26. /**
  27. * 商品分类列表172
  28. */
  29. public function index()
  30. {
  31. $thirdShow = \Setting::get('shop.category.cat_level') == 3 ? true : false; //根据后台设置显示两级还是三级分类
  32. if(request()->ajax()){
  33. $keyword = request()->keyword;
  34. if($keyword){
  35. $category_data = Categorys::searchCategory($keyword);
  36. // return $this->successJson('', $category_data);
  37. return $this->successJson('', array('data' => $category_data ,'thirdShow' => $thirdShow));
  38. }
  39. $category_data = Categorys::getCategoryData();
  40. return $this->successJson('', array('data' => $category_data ,'thirdShow' => $thirdShow));
  41. }
  42. return view('goods.category.list');
  43. }
  44. /**
  45. * 获取所有商城商品分类(批量修改用)
  46. */
  47. public function getAllShopCategory(){
  48. $setlevel = \Setting::get('shop.category.cat_level') ? : 2; //根据后台设置显示两级还是三级分类
  49. $category_data = Categorys::uniacid()
  50. ->select('id', 'name', 'parent_id')->with(['hasManyChildren' => function ($query) use ($setlevel){
  51. $query->select('id', 'name', 'parent_id')
  52. ->with(['hasManyChildren' => function ($query) use ($setlevel) {
  53. $query->select('id', 'name', 'parent_id')->where('level', $setlevel);
  54. }]);
  55. }])->where('level', 1)->where('plugin_id',0)
  56. ->orderBy('display_order' ,'asc')->orderBy('id' ,'asc')->get();
  57. return $this->successJson('获取成功',['list'=>$category_data,'setlevel'=>$setlevel]);
  58. }
  59. /**
  60. * 批量修改商城商品分类
  61. */
  62. public function changeManyGoodsCategory(){
  63. $request = request();
  64. if (!$request->goods_id_arr || !is_array($request->goods_id_arr)){
  65. return $this->errorJson('请选择要修改分类的商品');
  66. }
  67. $setlevel = \Setting::get('shop.category.cat_level') ? : 2;
  68. $category_id = 0;
  69. $category_ids = '';
  70. for ($i=1;$i<=$setlevel;$i++){
  71. $key = 'level_'.$i;
  72. if (empty($request->$key)){
  73. return $this->errorJson('请选择'.$i.'级分类');
  74. }
  75. $category_id = $request->$key;
  76. $category_ids .= empty($category_ids) ? $request->$key : ','.$request->$key;
  77. }
  78. //校验分类关系链是否正确
  79. if (!$check_res = \app\common\models\Category::checkCategory(explode(',',$category_ids))){
  80. return $this->errorJson('分类异常');
  81. }
  82. if(!$goods_ids = \app\common\models\Goods::uniacid()->whereIn('plugin_id',[0,44,120])->whereIn('id',$request->goods_id_arr)->pluck('id')){
  83. return $this->errorJson('选择商品不存在或非商城商品');
  84. }
  85. $category_goods_ids = GoodsCategory::whereIn('goods_id',$goods_ids)->pluck('goods_id');
  86. $insert_ids = $goods_ids->diff($category_goods_ids);
  87. $update_ids = $goods_ids->intersect($category_goods_ids);
  88. // dd($goods_ids->toArray(),$update_ids->toArray(),$insert_ids,$update_ids);
  89. if ($insert_ids){
  90. $time = time();
  91. $insert_data = $insert_ids->map(function ($v) use ($category_id,$category_ids,$time){
  92. return [
  93. 'goods_id'=>$v,
  94. 'category_id'=>$category_id,
  95. 'category_ids'=>$category_ids,
  96. 'created_at'=>$time,
  97. 'updated_at'=>$time
  98. ];
  99. })->all();
  100. GoodsCategory::insert($insert_data);
  101. }
  102. if ($update_ids){
  103. GoodsCategory::whereIn('goods_id',$update_ids)->update([
  104. 'category_id'=>$category_id,
  105. 'category_ids'=>$category_ids
  106. ]);
  107. }
  108. return $this->successJson('修改成功');
  109. }
  110. public function categoryInfo()
  111. {
  112. return view('goods.category.info', [
  113. 'id'=>request()->id,
  114. 'level' => request()->level,
  115. 'parent_id'=> request()->parent_id,
  116. ])->render();
  117. }
  118. /**
  119. * 添加商品分类
  120. */
  121. public function addCategory()
  122. {
  123. // sleep(5);
  124. //判断分类等级
  125. $level = request()->level ? request()->level : '1';
  126. //判断是否有父类id没有默认0
  127. $parent_id = request()->parent_id ? request()->parent_id : '0';
  128. $categoryModel = new Category();
  129. //分类等级
  130. $categoryModel->level = $level;
  131. //父类id
  132. $categoryModel->parent_id = $parent_id;
  133. $parent = [];
  134. //url地址
  135. $url = Url::absoluteWeb('goods.category.index');
  136. if ($parent_id > 0) {
  137. //查出父分类
  138. $parent = Category::getCategory($parent_id);
  139. //地址栏显示父分类
  140. $url = Url::absoluteWeb('goods.category.index', ['parent_id' => $parent_id]);
  141. }
  142. //获取分类发送过来的值
  143. $requestCategory = request()->category;
  144. if ($requestCategory) {
  145. if (isset($requestCategory['filter_ids']) && is_array($requestCategory['filter_ids'])) {
  146. $requestCategory['filter_ids'] = implode(',', $requestCategory['filter_ids']);
  147. }
  148. //将数据赋值到model
  149. $categoryModel->fill($requestCategory);
  150. //其他字段赋值
  151. $categoryModel->uniacid = \YunShop::app()->uniacid;
  152. //字段检测
  153. $validator = $categoryModel->validator();
  154. if ($validator->fails()) {
  155. //检测失败
  156. $this->errorJson($validator->messages());
  157. } else {
  158. //数据保存
  159. if ($categoryModel->save()) {
  160. //显示信息并跳转
  161. return $this->successJson('分类创建成功');
  162. } else {
  163. $this->errorJson('分类创建失败');
  164. }
  165. }
  166. }
  167. return $this->successJson('ok',[
  168. 'item' => $categoryModel,
  169. 'parent' => $parent,
  170. 'level' => $level,
  171. 'label_group' => [],
  172. ]);
  173. // return view('goods.category.info', [
  174. // 'item' => $categoryModel,
  175. // 'parent' => $parent,
  176. // 'level' => $level,
  177. // 'label_group' => [],
  178. // ])->render();
  179. }
  180. /**
  181. * 修改分类
  182. */
  183. public function editCategory()
  184. { //查询这个分类是否存在
  185. $categoryModel = Category::getCategory(request()->id);
  186. //判断是否有父类id没有默认0
  187. $parent_id = request()->parent_id ? request()->parent_id : '0';
  188. if (!$categoryModel) {
  189. return $this->errorJson('无此记录或已被删除');
  190. }
  191. //URL地址
  192. $url = Url::absoluteWeb('goods.category.index', ['parent_id' => $categoryModel->parent_id]);
  193. if (!empty($categoryModel->parent_id)) {
  194. //查出父分类
  195. $parent = Category::getCategory($categoryModel->parent_id);
  196. }
  197. if (isset($categoryModel->filter_ids)) {
  198. $filter_ids = explode(',', $categoryModel->filter_ids);
  199. $label_group = Filtering::categoryLabel($filter_ids)->get();
  200. }
  201. //获取分类发送过来的值
  202. $requestCategory = request()->category;
  203. if ($requestCategory) {
  204. if (isset($requestCategory['filter_ids']) && is_array($requestCategory['filter_ids'])) {
  205. $requestCategory['filter_ids'] = implode(',', $requestCategory['filter_ids']);
  206. }
  207. //将数据赋值到model
  208. $categoryModel->fill($requestCategory);
  209. //字段检测
  210. $validator = $categoryModel->validator();
  211. if ($validator->fails()) {
  212. //检测失败
  213. $this->errorJson($validator->messages());
  214. } else {
  215. //数据保存
  216. if ($categoryModel->save()) {
  217. //显示信息并跳转
  218. return $this->successJson('分类保存成功', $url);
  219. } else {
  220. $this->errorJson('分类保存失败');
  221. }
  222. }
  223. }
  224. $categoryModel['thumb_url'] = yz_tomedia($categoryModel['thumb']);
  225. $categoryModel['adv_img_url'] = yz_tomedia($categoryModel['adv_img']);
  226. $parent['thumb_url'] = yz_tomedia($parent['thumb']);
  227. $cat_level = \Setting::get('shop.category')['cat_level'] ?: 0;
  228. $link = yzAppFullUrl('/catelist/'.request()->id);
  229. return $this->successJson('ok',[
  230. 'item' => $categoryModel,
  231. 'level' => $categoryModel->level,
  232. 'label_group' => $label_group,
  233. 'parent' => $parent,
  234. 'cat_level' => $cat_level,
  235. 'link' => $link
  236. ]);
  237. // return view('goods.category.info', [
  238. // 'item' => $categoryModel,
  239. // 'level' => $categoryModel->level,
  240. // 'label_group' => $label_group,
  241. // 'parent' => $parent,
  242. // ])->render();
  243. }
  244. /**
  245. * 删除商品分类
  246. */
  247. public function deletedCategory()
  248. {
  249. $category = Category::find(request()->id);
  250. if (!$category) {
  251. return $this->errorJson('无此分类或已经删除');
  252. }
  253. //查询是否有商品分类关联表 find_in_set
  254. $good_ids = GoodsCategory::whereRaw('FIND_IN_SET(?,category_ids)', $category->id)
  255. ->pluck('goods_id')
  256. ->toArray();
  257. //查询是否有商品
  258. $goods = Goods::whereIn('id', $good_ids)->first();
  259. if (!empty($goods)) {
  260. return $this->errorJson('分类下存在商品,不允许删除');
  261. }
  262. $result = Category::deletedAllCategory($category->id);
  263. if ($result) {
  264. return $this->successJson('删除分类成功');
  265. } else {
  266. return $this->errorJson('删除分类失败');
  267. }
  268. }
  269. /**
  270. * 批量删除分类
  271. * @return \Illuminate\Http\JsonResponse
  272. */
  273. public function batchDeleteCategory()
  274. {
  275. $ids = request()->ids;
  276. if(empty($ids)){
  277. return $this->errorJson('请选择要删除的分类');
  278. }
  279. $category = Category::uniacid()->select('id')->whereIn('id', $ids)->get();
  280. if (!$category) {
  281. return $this->errorJson('无此分类或已经删除');
  282. }
  283. foreach($category as $categoryID){
  284. //查询是否有商品分类关联表 find_in_set
  285. $good_ids = GoodsCategory::whereRaw('FIND_IN_SET(?,category_ids)', $categoryID->id)
  286. ->pluck('goods_id')
  287. ->toArray();
  288. //查询是否有商品
  289. $goods = Goods::whereIn('id', $good_ids)->first();
  290. if (!empty($goods)) {
  291. return $this->errorJson('分类下存在商品,不允许删除');
  292. }
  293. }
  294. foreach ($ids as $id) {
  295. $goods = Category::destroy($id);
  296. }
  297. return $this->successJson('删除分类成功');
  298. }
  299. /**
  300. * 获取搜索分类
  301. * @return html
  302. */
  303. public function getSearchCategorys()
  304. {
  305. $keyword = \YunShop::request()->keyword;
  306. $categorys = Category::getCategorysByName($keyword);
  307. return view('goods.category.query', [
  308. 'categorys' => $categorys
  309. ])->render();
  310. }
  311. /**
  312. * 获取搜索分类,返回数组
  313. * @return string
  314. * @throws \Throwable
  315. */
  316. public function getSearchCategorysJson()
  317. {
  318. $keyword = request()->keyword;
  319. $categorys = Category::getCategorysByName($keyword);
  320. foreach ($categorys as &$item){
  321. $item['thumb'] = yz_tomedia($item['thumb']);
  322. }
  323. return $this->successJson('ok',$categorys);
  324. }
  325. public function test()
  326. {
  327. //$order = Order::uniacid()->where("id",1265)->first();
  328. //$refund = RefundApply::uniacid()->where("order_id",1265)->first();
  329. //$createNotice = new OrderReceivedNotice($order);
  330. //$createNotice->sendMessage();
  331. }
  332. public function getCategoryData()
  333. {
  334. $data = Category::getCategoryData();
  335. return $this->successJson('', $data);
  336. }
  337. public function getCategorySmallData()
  338. {
  339. $data = Category::getCategorySmallData();
  340. return $this->successJson('', $data);
  341. }
  342. /**
  343. * 获取搜索分类,返回数组
  344. * @return string
  345. * @throws \Throwable
  346. */
  347. public function getCategorysJson()
  348. {
  349. $level = request()->level;
  350. $parent_id = request()->parent_id;
  351. $plugin_id = request()->input('plugin_id', 0);
  352. $categorys = Category::parentIdGetCategorys($level,$parent_id,$plugin_id)->get()->toArray();
  353. return $this->successJson('ok',$categorys);
  354. }
  355. /**
  356. * 批量更新商品分类显示
  357. */
  358. public function batchDisplay()
  359. {
  360. return $this->batchOption(request()->ids, 'enabled', request()->enabled);
  361. }
  362. /**
  363. * 批量更新商品分类推荐
  364. */
  365. public function batchRecommend()
  366. {
  367. return $this->batchOption(request()->ids, 'is_home', request()->is_home);
  368. }
  369. protected function batchOption($ids, $field, $value)
  370. {
  371. if (is_array($ids) && isset($value)){
  372. Category::uniacid()->whereIn('id', $ids)->update([$field=> $value]);
  373. return $this->successJson("批量更新成功");
  374. }
  375. return $this->errorJson('修改失败, 请检查参数');
  376. }
  377. public function batchService()
  378. {
  379. $ids = request()->ids;
  380. if (empty($ids)) {
  381. return $this->errorJson('请选取分类');
  382. }
  383. $service_form = request()->service_form;
  384. $good_ids = GoodsCategory::whereIn('category_id', $ids)->pluck('goods_id')->unique()->all();
  385. if (empty($good_ids)) {
  386. return $this->errorJson('分类下商品为空');
  387. }
  388. DB::transaction(function () use ($service_form, $good_ids) {
  389. foreach ($good_ids as $gid) {
  390. $goods_service = GoodsService::uniacid()->where('goods_id', $gid)->first();
  391. if (!$goods_service) {
  392. $goods_service = new GoodsService();
  393. $goods_service->uniacid = \YunShop::app()->uniacid;
  394. $goods_service->goods_id = $gid;
  395. }
  396. $goods_service->is_automatic = $service_form['is_automatic'];
  397. $goods_service->time_type = $service_form['time_type'];
  398. $goods_service->on_shelf_time = $service_form['on_shelf_time'];
  399. $goods_service->lower_shelf_time = $service_form['lower_shelf_time'];
  400. $goods_service->loop_date_start = $service_form['loop_date_start'];
  401. $goods_service->loop_date_end = $service_form['loop_date_end'];
  402. $goods_service->loop_time_up = $service_form['loop_date_end'];
  403. $goods_service->loop_time_down = $service_form['loop_time_down'];
  404. $goods_service->auth_refresh_stock = $service_form['auth_refresh_stock'];
  405. $goods_service->original_stock = $service_form['original_stock'];
  406. $goods_service->save();
  407. }
  408. });
  409. return $this->successJson("批量设置成功");
  410. }
  411. }