/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.supply.stockOut.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.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 org.springframework.web.multipart.MultipartFile;

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

/**
 * 出库单controller
 * @author: 徐滕
 * @create: 2022-03-02 10:31
 **/
@Controller
@RequestMapping(value = "${adminPath}/supply/stockOut")
public class SupplyStockOutController extends BaseController {

    @Autowired
    private SupplyStockOutService supplyStockOutService;

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

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

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

    /**
     * 项目列表页面
     */
    @RequiresPermissions("supply:stockOut:view")
    @RequestMapping(value = "view")
    public String view(SupplyStockOut supplyStockOut, Model model) {
        model.addAttribute("supplyStockOut", supplyStockOut);
        return "modules/supply/stockOut/supplyStockOutView";
    }



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

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

    /**
     * 导入年度物资上报Excel数据-根据条件自动发送流程
     */
    @ResponseBody
    @RequiresPermissions("supply:stockOut: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<SupplyStockOut> list = ei.getDataList(SupplyStockOut.class);
            int size = list.size();
            Map<String,Object> map = supplyStockOutService.disposeStockOut(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;
    }
}
