package com.jeeplus.modules.supply.materialLibrary.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.materialLibrary.entity.MaterialLibrary;
import com.jeeplus.modules.supply.materialLibrary.service.MaterialLibraryService;
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.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料库Controller
 * @author: 黄俊杰
 * @create: 2022-02-28 16:00
 */
@Controller
@RequestMapping(value = "${adminPath}/supply/materialLibrary")
public class MaterialLibraryController extends BaseController {

    @Autowired
    private MaterialLibraryService materialLibraryService;

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

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

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

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

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

    /**
     * 导入Excel数据-
     */
    @ResponseBody
    @RequiresPermissions("supply:materialLibrary:import")
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            LinkedHashMap verificationMap = materialLibraryService.dataVerification(file);
            //获取数据库数据状态
            Boolean verificationMapBool = (Boolean) verificationMap.get("success");
            if(verificationMapBool){
                j.setBody(verificationMap);
                j.setMsg(verificationMap.get("msg").toString());
            }else{
                String repeat = "0";  //repeat=0:第一次交互，1：覆盖，2：不覆盖，3：重复
                ImportExcel ei = new ImportExcel(file, 1, 0);
                List<MaterialLibrary> list = ei.getDataList(MaterialLibrary.class);
                LinkedHashMap returnMap = materialLibraryService.disposeImportMaterialLibrary(list,file,repeat);
                //放要放的值
                Boolean boole = (Boolean) returnMap.get("success");
                if(!boole){
                    j.setSuccess(false);
                }
                j.setBody(returnMap);
                j.setMsg(returnMap.get("msg").toString());
            }

        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入物料库失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    /**
     * 导入Excel数据-覆盖
     */
    @ResponseBody
//    @RequiresPermissions("supply:stockOut:import")
    @RequestMapping(value = "importTwo")
    public AjaxJson importFileTwo(String filePath,String repeat) {
        AjaxJson j = new AjaxJson();
        try {
            File file = new File(filePath);
            if("2".equals(repeat)){
                file.delete();
            }else {
                MultipartFile file1 = materialLibraryService.transformFile(file);
                ImportExcel ei = new ImportExcel(file, 1, 0);
                List<MaterialLibrary> list = ei.getDataList(MaterialLibrary.class);
                LinkedHashMap returnMap = materialLibraryService.disposeImportMaterialLibrary(list, file1, repeat);
                //如果文件已经使用完毕，则删除
                materialLibraryService.deleteFileContent(file);
                Boolean boole = (Boolean) returnMap.get("success");
                if(!boole){
                    j.setSuccess(false);
                }
                //放要放的值
                j.setBody(returnMap);
                j.setMsg(returnMap.get("msg").toString());
            }
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入物料库失败！失败信息：" + e.getMessage());
        }
        return j;
    }







}
