package com.controller.admin;


import com.controller.CommonController;
import com.entity.EarlyWarning;
import com.service.EarlyWarningService;
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.util.List;
import java.util.Map;


/**
 *  预警信息类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class EarlyWarningController {
    @Resource
    private EarlyWarningService earlyWarningService;
    @Resource
    private UsersService usersService;

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


    public static final String TABLE_NAME = "预警";


    /**
     * ===后台预警管理===
     *
     * @return
     */

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

    // 添加预警
    @RequestMapping("addEarlyWarning.do")
    public String addEarlyWarning(HttpServletRequest request, EarlyWarning earlyWarning, HttpSession session) {
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            earlyWarning.setUid(users.getId());
            //earlyWarning.setPubtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
            earlyWarningService.add(earlyWarning);
            return "redirect:earlyWarningList.do";
        }
    }


    /*处理修改预警*/
    @RequestMapping("doUpdateEarlyWarning.do")
    public String doUpdateEarlyWarning(HttpServletRequest request, int id, ModelMap map) {
        map.put("earlyWarning", earlyWarningService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "earlyWarning_update";
    }

    /*查看预警*/
    @RequestMapping("doLookEarlyWarning.do")
    public String doLookEarlyWarning(HttpServletRequest request, int id, ModelMap map) {
        map.put("earlyWarning", earlyWarningService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "earlyWarning_look";
    }

    /*保存预警*/
    @RequestMapping("updateEarlyWarning.do")
    public String updateEarlyWarning(HttpServletRequest request, EarlyWarning earlyWarning) {
        earlyWarningService.update(earlyWarning);
        return "redirect:earlyWarningList.do";
    }


    // 分页查询预警信息的列表
    @RequestMapping("earlyWarningList.do")
    public String earlyWarningList(@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<EarlyWarning> list = earlyWarningService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "earlyWarning_list";
        }
    }

    // 分页查询预警信息的列表--模糊查询
    @RequestMapping("earlyWarningListSearch.do")
    public String earlyWarningListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                         HttpSession session, EarlyWarning earlyWarning) {
        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 (earlyWarning.getName() != null && !earlyWarning.getName().equals("")) {
                bMap.put("name", earlyWarning.getName());
            }
            if (earlyWarning.getBtype() != null && !earlyWarning.getBtype().equals("")) {
                bMap.put("btype", earlyWarning.getBtype());
            }
            if (earlyWarning.getUid() != null && !earlyWarning.getUid().equals("")) {
                bMap.put("uid", earlyWarning.getUid());
            }
            if (earlyWarning.getStatus() != null && !earlyWarning.getStatus().equals("")) {
                bMap.put("status", earlyWarning.getStatus());
            }
            List<EarlyWarning> list = earlyWarningService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "earlyWarning_list";
        }
    }

    /*删除预警*/
    @RequestMapping("deleteEarlyWarning.do")
    public String deleteEarlyWarning(int id) {
        earlyWarningService.delete(id);
        return "redirect:earlyWarningList.do";
    }

}

