package com.jeeplus.modules.supply.materialsProvidedByAForReview.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.supply.materialsProvidedByAForReview.entity.MaterialsProvidedByAForReview;
import com.jeeplus.modules.supply.materialsProvidedByAForReview.service.MaterialsProvidedByAForReviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 甲供材报审Controller
 * @author: 黄俊杰
 * @create: 2022-02-28 16:00
 */
@Controller
@RequestMapping(value = "${adminPath}/supply/MaterialsProvidedByAForReview")
public class MaterialsProvidedByAForReviewController extends BaseController {
    @Autowired
    private MaterialsProvidedByAForReviewService materialsProvidedByAForReviewService;

    @ModelAttribute
    public MaterialsProvidedByAForReview get(@RequestParam(required = false) String id) {
        MaterialsProvidedByAForReview entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = materialsProvidedByAForReviewService.get(id);
        }
        if (entity == null) {
            entity = new MaterialsProvidedByAForReview();
        }
        return entity;
    }

    /**
     * 项目列表页面
     */
    @RequiresPermissions("supply:MaterialsProvidedByAForReview:list")
    @RequestMapping(value = {"list", ""})
    public String list(MaterialsProvidedByAForReview materialsProvidedByAForReview, Model model) {
        model.addAttribute("materialsProvidedByAForReview", materialsProvidedByAForReview);
        return "modules/supply/materialsProvidedByAForReview/supplyMaterialsProvidedByAForReviewList";
    }

    /**
     * 列表数据
     */
    @ResponseBody
    @RequiresPermissions("supply:MaterialsProvidedByAForReview:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(MaterialsProvidedByAForReview materialsProvidedByAForReview, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<MaterialsProvidedByAForReview> page = materialsProvidedByAForReviewService.findPage(new Page<MaterialsProvidedByAForReview>(request, response), materialsProvidedByAForReview);
        return getBootstrapData(page);
    }

    /**
     * 删除项目
     */
    @ResponseBody
    @RequiresPermissions("supply:MaterialsProvidedByAForReview:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(MaterialsProvidedByAForReview materialsProvidedByAForReview) {
        AjaxJson j = new AjaxJson();
        materialsProvidedByAForReviewService.delete(materialsProvidedByAForReview);
        j.setMsg("删除成功");
        return j;
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequiresPermissions("supply:MaterialsProvidedByAForReview:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids) {
        AjaxJson j = new AjaxJson();
        String[] idArray = ids.split(",");
        List idList = Arrays.asList(idArray);
        materialsProvidedByAForReviewService.deleteByIdList(idList);
        j.setMsg("删除成功");
        return j;
    }


    /**
     * 导入年度物资上报Excel数据-根据条件自动发送流程
     */
    @ResponseBody
    @RequiresPermissions("supply:MaterialsProvidedByAForReview:import")
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 0, 0);
            List<MaterialsProvidedByAForReview> list = ei.getDataList(MaterialsProvidedByAForReview.class);
            Map<String,Object> map = materialsProvidedByAForReviewService.disposeMaterialsProvidedByAForReview(list);
            Boolean bool = (Boolean) map.get("success");
            if(bool){
                j.setMsg("已成功导入 " + map.get("size").toString() +" 条甲供材报审记录");
            }else{
                j.setSuccess(false);
                j.setMsg(map.get("message").toString());
            }
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入甲供材报审失败！失败信息：" + e.getMessage());
        }
        return j;
    }


}
