package com.controller.admin;


import com.controller.CommonController;
import com.entity.EmergencyResources;
import com.service.EmergencyResourcesService;
import com.service.ResourcesTypeService;
import com.service.UsersService;
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;


import com.util.Info;
import org.springframework.web.multipart.MultipartFile;

/**
 *  应急资源信息类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class EmergencyResourcesController {
    @Resource
    private EmergencyResourcesService emergencyResourcesService;
    @Resource
    private UsersService usersService;

    @Resource
    private ResourcesTypeService resourcesTypeService;

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


    public static final String TABLE_NAME = "应急资源";


    /**
     * ===后台应急资源管理===
     *
     * @return
     */

    /*处理添加应急资源*/
    @RequestMapping("doAddEmergencyResources.do")
    public String doAddEmergencyResources(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("id",3);
        map.put("tlist",resourcesTypeService.getAll(tMap));
        return path + "emergencyResources_add";
    }

    // 添加应急资源
    @RequestMapping("addEmergencyResources.do")
    public String addEmergencyResources(@RequestParam(value = "file", required = false) MultipartFile file, String img,
                                        HttpServletRequest request, EmergencyResources emergencyResources, HttpSession session) {
        img = Info.fileUpload(file, request, img);
        emergencyResources.setImg(img);
        //emergencyResources.setPubtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
        emergencyResourcesService.add(emergencyResources);
        return "redirect:emergencyResourcesList.do";
    }


    /*处理修改应急资源*/
    @RequestMapping("doUpdateEmergencyResources.do")
    public String doUpdateEmergencyResources(HttpServletRequest request, int id, ModelMap map) {
        map.put("emergencyResources", emergencyResourcesService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("id",3);
        map.put("tlist",resourcesTypeService.getAll(tMap));
        return path + "emergencyResources_update";
    }

    /*查看应急资源*/
    @RequestMapping("doLookEmergencyResources.do")
    public String doLookEmergencyResources(HttpServletRequest request, int id, ModelMap map) {
        map.put("emergencyResources", emergencyResourcesService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "emergencyResources_look";
    }

    /*保存应急资源*/
    @RequestMapping("updateEmergencyResources.do")
    public String updateEmergencyResources(@RequestParam(value = "file", required = false) MultipartFile file, String img,
                                           HttpServletRequest request, EmergencyResources emergencyResources) {
        img = Info.fileUpload(file, request, img);
        if (img != null && !img.equals("")) {
            emergencyResources.setImg(img);
        }
        emergencyResourcesService.update(emergencyResources);
        return "redirect:emergencyResourcesList.do";
    }


    // 分页查询应急资源信息的列表
    @RequestMapping("emergencyResourcesList.do")
    public String emergencyResourcesList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                         HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();
        List<EmergencyResources> list = emergencyResourcesService.getAllNotFid(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "emergencyResources_list";
    }

    // 分页查询应急资源信息的列表--模糊查询
    @RequestMapping("emergencyResourcesListSearch.do")
    public String emergencyResourcesListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                               HttpSession session, EmergencyResources emergencyResources) {
        Map<String, Object> bMap = new HashMap<>();

        if (emergencyResources.getName() != null && !emergencyResources.getName().equals("")) {
            bMap.put("name", emergencyResources.getName());
        }
        if (emergencyResources.getBtype() != null && !emergencyResources.getBtype().equals("")) {
            bMap.put("btype", emergencyResources.getBtype());
        }
        if (emergencyResources.getFid() != null && !emergencyResources.getFid().equals("")) {
            bMap.put("fid", emergencyResources.getFid());
        }
        List<EmergencyResources> list = emergencyResourcesService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "emergencyResources_list";
    }

    /*删除应急资源*/
    @RequestMapping("deleteEmergencyResources.do")
    public String deleteEmergencyResources(int id) {
        emergencyResourcesService.delete(id);
        return "redirect:emergencyResourcesList.do";
    }

}

