package com.controller.admin;


import com.controller.CommonController;
import com.entity.Bbs;
import com.service.BbsService;
import com.service.UsersService;
import com.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


/**
 *  留言控制类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class BbsController {
    @Resource
    private BbsService bbsService;
    @Resource
    private UsersService usersService;

    @Resource
    private CommonController commonController;
    /*设置访问路径*/
    private String path = "admin/views/bbs/";


    public static final String TABLE_NAME = "留言";


    /**
     * ===后台留言管理===
     *
     * @return
     */

    /*处理添加留言*/
    @RequestMapping("doAddBbs.do")
    public String doAddBbs(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "bbs_add";
    }

    // 添加留言
    @RequestMapping("addBbs.do")
    public String addBbs(HttpServletRequest request, Bbs bbs, HttpSession session) {
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            bbs.setUid(users.getId());
            bbs.setStatus("待回复");
            //bbs.setPubtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
            bbsService.add(bbs);
            return "redirect:bbsList.do";
        }
    }


    /*处理修改留言*/
    @RequestMapping("doUpdateBbs.do")
    public String doUpdateBbs(HttpServletRequest request, int id, ModelMap map) {
        map.put("bbs", bbsService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "bbs_update";
    }

    /*查看留言*/
    @RequestMapping("doLookBbs.do")
    public String doLookBbs(HttpServletRequest request, int id, ModelMap map) {
        map.put("bbs", bbsService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "bbs_look";
    }

    /*保存留言*/
    @RequestMapping("updateBbs.do")
    public String updateBbs(HttpServletRequest request, Bbs bbs) {
        bbs.setHtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
        bbs.setStatus("已回复");
        bbsService.update(bbs);
        return "redirect:bbsList.do";
    }


    // 分页查询留言信息的列表
    @RequestMapping("bbsList.do")
    public String bbsList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                          HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            if (users.getUtype().equals("普通用户")) {
                bMap.put("uid", users.getId());
            }

            List<Bbs> list = bbsService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "bbs_list";
        }
    }

    // 分页查询留言信息的列表--模糊查询
    @RequestMapping("bbsListSearch.do")
    public String bbsListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                HttpSession session, Bbs bbs) {
        Map<String, Object> bMap = new HashMap<>();
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            if (users.getUtype().equals("普通用户")) {
                bMap.put("uid", users.getId());
            }

            if (bbs.getName() != null && !bbs.getName().equals("")) {
                bMap.put("name", bbs.getName());
            }
            if (bbs.getUid() != null && !bbs.getUid().equals("")) {
                bMap.put("uid", bbs.getUid());
            }
            if (bbs.getNote2() != null && !bbs.getNote2().equals("")) {
                bMap.put("note2", bbs.getNote2());
            }
            if (bbs.getHtime() != null && !bbs.getHtime().equals("")) {
                bMap.put("htime", bbs.getHtime());
            }
            if (bbs.getStatus() != null && !bbs.getStatus().equals("")) {
                bMap.put("status", bbs.getStatus());
            }
            List<Bbs> list = bbsService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "bbs_list";
        }
    }

    /*删除留言*/
    @RequestMapping("deleteBbs.do")
    public String deleteBbs(int id) {
        bbsService.delete(id);
        return "redirect:bbsList.do";
    }

}

