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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
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.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.supply.materialLibrary.entity.MaterialLibrary;
import com.jeeplus.modules.supply.particulars.entity.SupplyParticulars;
import com.jeeplus.modules.supply.particulars.service.SupplyParticularsService;
import com.jeeplus.modules.supply.stockOut.entity.SupplyStockOut;
import com.jeeplus.modules.supply.stockOut.service.SupplyStockOutService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringEscapeUtils;

/**
 * 超欠供明细controller
 * @author: 徐滕
 * @create: 2022-03-02 10:31
 **/
@Controller
@RequestMapping(value = "${adminPath}/supply/particulars")
public class SupplyParticularsController extends BaseController {

    @Autowired
    private SupplyParticularsService service;
    @Autowired
    private SupplyStockOutService stockOutService;

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

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

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


    /**
     * 根据物料编码查询出库单历史物料信息
     * @param supplyStockOut
     * @param model
     * @return
     */
    @RequestMapping(value = "viewMaterials")
    public String viewMaterials(SupplyStockOut supplyStockOut, Model model) {
        model.addAttribute("supplyStockOut", supplyStockOut);
        return "modules/supply/particulars/stockOut/supplyStockOutList";
    }

    /**
     * 列表数据
     */
    @ResponseBody
    @RequestMapping(value = "dataMaterials")
    public Map<String, Object> dataMaterials(SupplyStockOut supplyStockOut, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SupplyStockOut> page = stockOutService.findPage(new Page<SupplyStockOut>(request, response), supplyStockOut);
        return getBootstrapData(page);
    }


    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequestMapping(value = "export")
    public AjaxJson exportFile(String json, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            json = StringEscapeUtils.unescapeHtml(json);
            String fileName = "超欠供核对明细表"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            ArrayList<SupplyParticulars> list  = JSON.parseObject(json, new TypeReference<ArrayList<SupplyParticulars>>(){});
            new ExportExcel("超欠供核对明细表", SupplyParticulars.class).setDataList(list).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出超欠供核对明细表记录失败！失败信息："+e.getMessage());
        }
        return j;
    }
}
