package com.controller.admin;


import com.controller.CommonController;
import com.entity.EmergencyResources;
import com.service.EmergencyResourcesService;
import com.util.Info;
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 org.springframework.web.multipart.MultipartFile;

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 ReserveController {
    @Resource
    private CommonController commonController;
    /*设置访问路径*/
    private String path="admin/views/reserve_materials/";


    public static final String TABLE_NAME = "储备物资";

    @Resource
    private EmergencyResourcesService reserveService;

    /**
     * ===储备物资管理===
     *
     * @return
     */

    /*处理添加储备物资*/
    @RequestMapping("doAddReserve.do")
    public String doAddReserve(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "reserve_add";
    }

    // 添加储备物资
    @RequestMapping("addReserve.do")
    public String addReserve(@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));
        reserveService.add(emergencyResources);
        return "redirect:reserveList.do";
    }


    /*处理修改储备物资*/
    @RequestMapping("doUpdateReserve.do")
    public String doUpdateReserve(HttpServletRequest request, int id, ModelMap map) {
        map.put("emergencyResources", reserveService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "reserve_update";
    }

    /*查看储备物资*/
    @RequestMapping("doLookReserve.do")
    public String doLookReserve(HttpServletRequest request, int id, ModelMap map) {
        map.put("emergencyResources", reserveService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "reserve_look";
    }

    /*保存储备物资*/
    @RequestMapping("updateReserve.do")
    public String updateReserve(@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);
        }
        reserveService.update(emergencyResources);
        return "redirect:reserveList.do";
    }


    // 分页查询储备物资信息的列表
    @RequestMapping("reserveList.do")
    public String reserveList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                         HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();
        bMap.put("fid","3");
        List<EmergencyResources> list = reserveService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "reserve_list";
    }

    // 分页查询储备物资信息的列表--模糊查询
    @RequestMapping("reserveListSearch.do")
    public String reserveListSearch(@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 = reserveService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "reserve_list";
    }

    /*删除储备物资*/
    @RequestMapping("deleteReserve.do")
    public String deleteReserve(int id) {
        reserveService.delete(id);
        return "redirect:reserveList.do";
    }


}

