package com.yc.education.controller.admin;

import com.yc.education.model.Classify01;
import com.yc.education.model.Classify01en;
import com.yc.education.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: hsa
 * @description:
 * @author: hgueng
 * @create: 2019-11-12 18:15
 **/

@Controller
public class Classify01Contrller {

    @Autowired
    private Classify01Service classify01Service;

    @Autowired
    private Classify02Service classify02Service;

    @Autowired
    private ProductService productService;

    @Autowired
    private Classify01enService classify01enService;

    @Autowired
    private Classify02enService classify02enService;

    @Autowired
    private ProductenService productenService;

    //查找所有的一级分类,根据排序值和时间倒序
    @RequestMapping("admin/class01_list.html")
    public String class01List(Model model, HttpServletRequest request){
        List<Classify01> list = classify01Service.findAllClass01();
        model.addAttribute("list", list);
        return "/admin/class01_list";
    }

    //查找所有的一级分类,根据排序值和时间倒序
    @RequestMapping("admin/class01_list_en.html")
    public String class01ListEn(Model model){
        List<Classify01en> lists = classify01enService.findAllClass01();
        model.addAttribute("list", lists);
        return "/adminEn/class01_list";
    }

    //新增一级分类
    @RequestMapping("/admin/add_class01.html")
    public String addClass01(){
        return "admin/add_class01";
    }

    //新增一级分类
    @RequestMapping("/admin/add_class01_en.html")
    public String addClass01En(){
        return "adminEn/add_class01";
    }

    //提交新增的一级分类信息
    @RequestMapping("admin/submit_addClass01.html")
    public String submitAddClass01(Classify01 classify01){
        classify01Service.save(classify01);
        return "redirect: class01_list.html";
    }

    //提交新增的一级分类信息
    @RequestMapping("admin/submit_addClass01_en.html")
    public String submitAddClass01En(Classify01en classify01en ){
        classify01enService.save(classify01en);
        return "redirect: class01_list_en.html";
    }

    //根据id更改一级分类
    @RequestMapping("admin/update_class01.html")
    public String updateClass01(Model model,Integer pid){
        Classify01 classify01 = classify01Service.selectByKey(pid);
        model.addAttribute("class01", classify01);
        return "admin/update_class01";
    }

    //根据id更改一级分类
    @RequestMapping("admin/update_class01_en.html")
    public String updateClass01En(Model model,Integer pid){
        Classify01en classify01en = classify01enService.selectByKey(pid);
        model.addAttribute("class01", classify01en);
        return "adminEn/update_class01";
    }

    //提交更新的一级分类
    @RequestMapping("admin/submit_upClass01.html")
    public String submit_upClass01(Classify01 classify01 ){
        classify01Service.updateNotNull(classify01);
        return "redirect: class01_list.html";
    }

    //提交更新的一级分类
    @RequestMapping("admin/submit_upClass01_en.html")
    public String submit_upClass01En(Classify01en classify01en ){
        classify01enService.updateNotNull(classify01en);
        return "redirect: class01_list_en.html";
    }

    //根据id删除一级分类,先删除商品中对应的所有一级商品,在删除对应的二级分类
    @RequestMapping(value = "/admin/delete_class01.html",method = RequestMethod.POST)
    @ResponseBody
    public String deleteClass01(Integer pid){
        productService.deleteByCid(pid);
        classify02Service.deleteByCid(pid);
        int num = classify01Service.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }

    //根据id删除一级分类,先删除商品中对应的所有一级商品,在删除对应的二级分类
    @RequestMapping(value = "/admin/delete_class01_en.html",method = RequestMethod.POST)
    @ResponseBody
    public String deleteClass01En(Integer pid){
        productenService.deleteByCid(pid);
        classify02enService.deleteByCid(pid);
        int num = classify01enService.delete(pid);
        if (num > 0) {
            return "ok";
        } else {
            return "error";
        }
    }

}
