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

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.supply.judgementBills.entity.SupplyJudgementBills;
import com.jeeplus.modules.supply.judgementBills.service.SupplyJudgementBillsService;
import org.apache.commons.lang3.StringEscapeUtils;
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.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 审定单 controller
 * @author: 徐滕
 * @create: 2022-02-28 15:55
 **/
@Controller
@RequestMapping(value = "${adminPath}/supply/judgementBills")
public class SupplyJudgementBillsController extends BaseController {
    @Autowired
    private SupplyJudgementBillsService service;

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

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

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

    /**
     * 项目列表页面
     */
    @RequiresPermissions("supply:judgementBills:view")
    @RequestMapping(value = "view")
    public String view(SupplyJudgementBills supplyJudgementBills, Model model) {
        model.addAttribute("supplyJudgementBills", supplyJudgementBills);
        return "modules/supply/judgementBills/supplyJudgementBillsView";
    }



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

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

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