package com.controller.admin;


import com.controller.CommonController;
import com.entity.Donation;
import com.service.*;
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 DonationController {
    @Resource
    private DonationService donationService;

    @Resource
    private DonationTypeService donationTypeService;

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


    public static final String TABLE_NAME = "捐赠信息";


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

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

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


    /*处理修改应急资源*/
    @RequestMapping("doUpdateDonation.do")
    public String doUpdateDonation(HttpServletRequest request, int id, ModelMap map) {
        map.put("donation", donationService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        map.put("tlist",donationTypeService.getAll(null));
        return path + "donation_update";
    }

    /*查看应急资源*/
    @RequestMapping("doLookDonation.do")
    public String doLookDonation(HttpServletRequest request, int id, ModelMap map) {
        map.put("donation", donationService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        map.put("classList",donationTypeService.getAll(null));
        return path + "donation_look";
    }

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


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

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

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

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

}

