package com.yc.education.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.*;
import com.yc.education.service.*;
import com.yc.education.util.SendEmailUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * huangpan
 * 2019/9/27
 * energy
 */
@Controller
public class XingtaiController {
    @Autowired
    TeamService teamService;
    @Autowired
    MienService mienService;
    @Autowired
    HistoryService historyService;
    @Autowired
    HonorService honorService;
    @Autowired
    CultureService cultureService;
    @Autowired
    JournalismService journalismService;
    @Autowired
    ClassificationService classificationService;
    @Autowired
    AboutusService aboutusService;
    @Autowired
    ProductService productService;
    @Autowired
    ProductclassifyService productclassifyService;
    @Autowired
    TechnologyService technologyService;
    @Autowired
    TechnologyclassifyService technologyclassifyService;
    @Autowired
    IdeaService ideaService;
    @Autowired
    LifeService lifeService;
    @Autowired
    JobService jobService;
    @Autowired
    ContacusService contacusService;
    @Autowired
    ContactclassifyService contactclassifyService;
    @Autowired
    CorporateService corporateService;
    @Autowired
    BannerService bannerService;
    @Autowired
    ProductOneService productOneService;
    @Autowired
    FootService footService;

    /*首页信息*/
    @RequestMapping(value = {" ","/index.html"})
    public ModelAndView journalismList(String meageests) {
        List<Journalism> list = journalismService.findJOurnalism();
        List<Technology> technologies = technologyService.iconTechnology();
        List<Productclassify> productclassifyList = productOneService.getProductclassify();
        List<Banner> banner = bannerService.getBanner();
        ModelAndView modelAndView = new ModelAndView("/xingtai/index");
        modelAndView.addObject("technologies", technologies);
        modelAndView.addObject("productclassifyList", productclassifyList);
        modelAndView.addObject("journalist", list);
        modelAndView.addObject("ideaxUrl", "index");
        modelAndView.addObject("banner", banner);
        modelAndView.addObject("meageests", meageests);
        return modelAndView;
    }

    /*查询新闻分类内容*/
    @RequestMapping("/news.html")
    public ModelAndView getNews(@Param("id") Integer id, HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                @RequestParam(value = "pageSize",defaultValue = "9") Integer pageSize) {
        ModelAndView modelAndVie = new ModelAndView("xingtai/news");
        List<Classification> classification = classificationService.getClassificationSort();
        if (id==null){
            for (Classification classification1:classification){
                id=classification1.getTid();
                break;
            }
        }
        List<Journalism> jouranList = journalismService.getJournalism(pageNum, pageSize, null, id);
        for (Journalism j:jouranList){
            System.err.println(j.getId());
        }
        /*List<Classification> classfiactionList = classificationService.getClassificationSort();*/
        session.setAttribute("newsId", id);
        modelAndVie.addObject("classfiactionList", classification);
        modelAndVie.addObject("pageInfo", new PageInfo<>(jouranList));
        modelAndVie.addObject("pageId", id);
        modelAndVie.addObject("pageNums", pageNum);
    modelAndVie.addObject("ideaxUrl", "news");
        return modelAndVie;
    }

    /*
     *查看新闻详情
     */
    @RequestMapping("news_detail.html")
    public ModelAndView newsDetail(@Param("id") int id) {
        Journalism journalismId = journalismService.getJournalismId(id);
        List<Classification> classification = classificationService.getClassificationSort();
       /* List<Classification> classification = classificationService.getClassification();*/
        ModelAndView modelAndView = new ModelAndView("/xingtai/news_detail");
        modelAndView.addObject("pfid", journalismId.getTypes());
        modelAndView.addObject("journalismId", journalismId);
        modelAndView.addObject("classification", classification);
        modelAndView.addObject("ideaxUrl", "news");
        return modelAndView;
    }

    /*
     *星泰简介
     */
    @RequestMapping("about.html")
    public ModelAndView getAbout() {
        Culture cultureId = cultureService.getCultureId(1);
        ModelAndView modelAndView = new ModelAndView("/xingtai/about");
        modelAndView.addObject("cultureId", cultureId);
        modelAndView.addObject("ideaxUrl", "about");
        return modelAndView;
    }

    /*公司历程*/
    @RequestMapping("history.html")
    public ModelAndView getHistory() {
        Culture cultureId = cultureService.getCulture(2);
        ModelAndView modelAndView = new ModelAndView("/xingtai/history");
        List<History> history = historyService.getHistoryTime();
        History historyd = historyService.getHistoryd();
        modelAndView.addObject("cultureId", cultureId);
        modelAndView.addObject("history", history);
        modelAndView.addObject("historyd", historyd);
        modelAndView.addObject("ideaxUrl", "about");
        return modelAndView;
    }

    /*公司团队*/
    @RequestMapping("team.html")
    public ModelAndView getTeam(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "12") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView("/xingtai/team");
        List<Team> team = teamService.getTeam(pageNum, pageSize, null);
        modelAndView.addObject("team", new PageInfo<>(team));
        modelAndView.addObject("ideaxUrl", "about");
        modelAndView.addObject("pageNums", pageNum);
        return modelAndView;
    }

    /*公司团队详情*/
    @RequestMapping("team_detail.html")
    public ModelAndView teamDetail(@Param("id") int id) {
        Team teamId = teamService.getTeamId(id);
        ModelAndView modelAndView = new ModelAndView("/xingtai/team_detail");
        modelAndView.addObject("teamId", teamId);
        modelAndView.addObject("ideaxUrl", "about");
        return modelAndView;
    }

    /*公司文化*/
    @RequestMapping("culture.html")
    public ModelAndView getCulture() {
        ModelAndView modelAndView = new ModelAndView("/xingtai/culture");
        Culture culture = cultureService.getCulture(4);
        modelAndView.addObject("culture", culture);
        modelAndView.addObject("ideaxUrl", "about");
        return modelAndView;
    }

    /*荣誉资质*/
    @RequestMapping("honor.html")
    public ModelAndView getHonor(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "12") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView("/xingtai/honor");
        List<Honor> honor = honorService.getHonor(pageNum, pageSize);
        modelAndView.addObject("honor",new PageInfo<>(honor) );
        modelAndView.addObject("ideaxUrl", "about");
        modelAndView.addObject("pageNums", pageNum);
        return modelAndView;
    }

    /*员工风采*/
    @RequestMapping("staff.html")
    public ModelAndView getMien() {
        List<Mien> miens = mienService.selectByExamplex();
        ModelAndView modelAndView = new ModelAndView("/xingtai/staff");
        modelAndView.addObject("miens", miens);
        modelAndView.addObject("ideaxUrl", "about");
        return modelAndView;
    }

    /*头部*/
    @RequestMapping("newshead.html")
    public ModelAndView getClassif(HttpSession session) {
       ModelAndView modelAndView = new ModelAndView("/xingtai/newshead");
        List<Classification> classfiactionList = classificationService.getClassificationSort();
        List<Aboutus> aboutusList = aboutusService.getAboutus();
        List<Technology> technologies = technologyService.tTechnology();
        List<Productclassify> productclassifyList = productOneService.getProductclassify();
       List<Idea> ideaList=  ideaService.getIdeas();
        Foot foots = footService.getFoots();
        modelAndView.addObject("classfiactionList", classfiactionList);
        modelAndView.addObject("aboutusList", aboutusList);
        modelAndView.addObject("technologies", technologies);
        modelAndView.addObject("productclassifyList", productclassifyList);
        modelAndView.addObject("ideaList", ideaList);
        modelAndView.addObject("foots", foots);
       String personnel=(String) session.getAttribute("userName");
       System.err.println(personnel);
       if (personnel==null){
           session.setAttribute("userName", "");
       }else {
           session.setAttribute("userName", personnel);
       }

        return modelAndView;
    }
    /*尾部*/
    @RequestMapping("footer.html")
    public ModelAndView getFooter(){
        ModelAndView modelAndView = new ModelAndView("/xingtai/footer");
        List<Technology> technologies = technologyService.tTechnology();
        List<Productclassify> productclassifyList = productOneService.getProductclassify();
        List<Aboutus> aboutusList = aboutusService.getAboutus();
        Corporate corporate = corporateService.selCorprate();
        Foot foots = footService.getFoots();
        modelAndView.addObject("corporate", corporate);
        modelAndView.addObject("technologies", technologies);
        modelAndView.addObject("productclassifyList", productclassifyList);
        modelAndView.addObject("aboutusList", aboutusList);
        modelAndView.addObject("foots", foots);

        return modelAndView;
    }

    /*技术平台*/
    /*查询分类信息*/
    @RequestMapping("technology.html")
    public ModelAndView getTechnology(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "9") Integer pageSize ) {
        ModelAndView modelAndView = new ModelAndView("xingtai/technology");
     /*   List<Technology> technologies1 = technologyService.tTechnology();
        modelAndView.addObject("technologies1", technologies1);*/
        List<Technology> technologies = technologyService.selectAllTechnology(pageNum, pageSize, null);

        modelAndView.addObject("pageInfo", new PageInfo<>(technologies));
        modelAndView.addObject("ideaxUrl", "technology");
        modelAndView.addObject("pageNums", pageNum);
        return modelAndView;
    }
    /*查询详情*/
    @RequestMapping("technology_detail.html")
   public  ModelAndView getTechById(Integer id,Integer parentId){
        ModelAndView modelAndView = new ModelAndView("xingtai/technology_detail");
        Technology technology = technologyService.selectTechnologyById(id);
        modelAndView.addObject("technology", technology);
        modelAndView.addObject("ideaxUrl", "technology");
        return modelAndView;
    }

    /*产品及服务*/
    /*查询信息*/
    @RequestMapping("product.html")
    public ModelAndView getProduct(Integer id) {
        ModelAndView modelAndView = new ModelAndView("xingtai/product");
        List<Productclassify> productclassify = productOneService.getProductclassify();
        List<Productclassify> productById = productOneService.getProductById(id);
        /*一级*/
        int ppdId=0;
        for (Productclassify pr:productById
             ) {
             ppdId  =  pr.getId();
          break;
        }
        System.out.println(ppdId+"ppdId+lwh");
        List<Productclassify> product = productOneService.getProduct();
        /*二级*/
        int ppdIds=0;
        for (Productclassify prs:product){
            if(prs.getPfid()==ppdId) {
                ppdIds = prs.getId();
                break;
            }
        }
        System.out.println(ppdIds+"ppdIds+lwh");
        List<Product> productAll = productService.getProductAll(ppdIds);
        modelAndView.addObject("productclassify", productclassify);
        modelAndView.addObject("productById", productById);
        modelAndView.addObject("product", product);
        modelAndView.addObject("productAll", productAll);
        modelAndView.addObject("ppdId", ppdId);
        modelAndView.addObject("ppdIds", ppdIds);
        modelAndView.addObject("ideaxUrl", "product");
        return modelAndView;
    }

    /*通过分类查询*/
    @RequestMapping("productst.html")
    public ModelAndView getProducts(Integer id) {
        ModelAndView modelAndView = new ModelAndView("xingtai/product");
        List<Productclassify> productclassify = productOneService.getProductclassify();
        List<Productclassify> productById = productOneService.getProductById(0);
        List<Productclassify> product = productOneService.getProduct();
        List<Productclassify> productByIds = productOneService.getProductById(id);
        /*二级*/
        int ppdIds=0;
        for (Productclassify prs:productByIds){
            if(prs.getPfid()==id) {
                ppdIds = prs.getId();
                break;
            }
        }
        List<Product> productAll = productService.getProductAll(ppdIds);
        modelAndView.addObject("productById", productById);
        modelAndView.addObject("productclassify", productclassify);
        modelAndView.addObject("product", product);
        modelAndView.addObject("productAll", productAll);
        modelAndView.addObject("ppdId", id);
        modelAndView.addObject("ppdIds", ppdIds);
        modelAndView.addObject("ideaxUrl", "product");
        return modelAndView;
    }
    /*二级分类查询*/
    @RequestMapping("products.html")
    @ResponseBody
    public List<Product> getProductsId(Integer id){
        List<Product> productAll = productService.getProductAll(id);
        return productAll;
    }

    /*人力资源*/
        /*通过id查询不同路径*/
        @RequestMapping("idea.html")
        public ModelAndView getIdea(Integer id){
            ModelAndView modelAndView=new ModelAndView();
             List<Idea> ideaList= ideaService.getIdeas();
            Idea idea = ideaService.getIdeaById(id);
            System.out.println(idea.getName()+"name");
                 if(idea.getName().equals("人才理念")){
                     modelAndView.setViewName("xingtai/idea");
                 } else if(idea.getName().equals("招聘岗位")){
                     modelAndView.setViewName("xingtai/job");
                 }else{
                     modelAndView.setViewName("xingtai/life");
             }
             if(id==2){
                 List<Job> jobs = jobService.getJobs();
                 modelAndView.addObject("jobs", jobs);
             }else if(id==1){
                 Life lifeById = lifeService.getLifeFpId(id);
                 modelAndView.addObject("lifeById", lifeById);
             }else {
                 Mien miens = mienService.getShengh();
               modelAndView.addObject("mien", miens);
             }
             modelAndView.addObject("ideaList", ideaList);
             modelAndView.addObject("ideaId", id);
            modelAndView.addObject("ideaxUrl", "idea");
             return modelAndView;
        }

        /*联系我们*/
    @RequestMapping("contact.html")
    public ModelAndView contcact(){
        ModelAndView modelAndView=new ModelAndView("xingtai/contact");
        List<Contactclassify> cclassify = contactclassifyService.getCclassify();
        modelAndView.addObject("cclassify", cclassify);
        Foot foots = footService.getFoots();
        modelAndView.addObject("foots", foots);
        modelAndView.addObject("ideaxUrl", "contact");
        Corporate corporate = corporateService.selCorprate();
        modelAndView.addObject("corporate", corporate);
        return modelAndView;
    }
    /*提交*/
    @RequestMapping("contact-submit.html")
    public String contcactSt(Contactus contactus){
        contactus.setState(0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        contactus.setDatechanged(commentdatemin);
        contacusService.save(contactus);
      /*  response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(" <script type='text/javascript'> alert('提交成功')</script>");
        *//*out.println("</HTML>");
        out.flush();*//*
        out.close();*/
        return "redirect:contact.html";
        /*response.sendRedirect("contact.html");*/
       /* request.getRequestDispatcher("contact.html");*/
    }
    /*测试邮箱发送*/
    @RequestMapping("eamil.html")
    @ResponseBody
    public boolean ttc(Integer id,String codephone,String com,String cood ){
        /**
         * 发送邮件处理
         *
         * @param longin    邮箱账号
         * @param password  邮箱授权码
         * @param sender    邮件发送者 一般与邮箱账号相同
         * @param recipient 邮件接收人
         * @param theme     邮件主题
         * @param text      邮件内容
         */
        Job jobById = jobService.getJobById(id);
        String longin=codephone+com;
        String password=cood;
        String sender=longin;
        String recipient="lwhstart@163.com";
        String theme="[求职"+jobById.getTitle()+"--"+jobById.getJposition()+"]";
        String text=jobById.getJdescription();
        SendEmailUtil.sendMail(longin ,password, longin, recipient, theme, text);
        return true;
    }


}
