package com.jeeplus.modules.supply.projectInformation.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.projectInformation.entity.ProjectInformation;
import com.jeeplus.modules.supply.projectInformation.service.ProjectInformationService;
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/projectInformation")
public class ProjectInformationController extends BaseController {

    @Autowired
    private ProjectInformationService projectInformationService;

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

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

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

    /**
     * 删除项目
     */
    @ResponseBody
    @RequiresPermissions("supply:projectInformation:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(ProjectInformation projectInformation) {
        AjaxJson j = new AjaxJson();
        projectInformationService.delete(projectInformation);   //要自己写吗？ 前端传的是id，
        j.setMsg("删除项目成功");
        return j;
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequiresPermissions("supply:projectInformation:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids) {
        AjaxJson j = new AjaxJson();
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            //根据审定单表的id查找审定单信息表的项目定义号count
            ProjectInformation projectInformation = projectInformationService.get(id);
            int count = projectInformationService.getProjectDefinitionNumberCountBySubmissionId(projectInformation);
            if(count > 1){      //count>1，说明还有其它的审定单id关联着项目表，所以只要删除审定单信息表的信息即可
                projectInformationService.delete(projectInformation);
            }else {             //否则则关联删除
                projectInformationService.deleteAllById(projectInformation);
            }

        }
        j.setMsg("删除成功");
        return j;
    }

    /**
     * 导入年度物资上报Excel数据-根据条件自动发送流程
     */
    @ResponseBody
    @RequiresPermissions("supply:projectInformation:import")
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            LinkedHashMap verificationMap = projectInformationService.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<ProjectInformation> list = ei.getDataList(ProjectInformation.class);
                LinkedHashMap returnMap = projectInformationService.disposeImportProjectInformation(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:projectInformation: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 = projectInformationService.transformFile(file);
                ImportExcel ei = new ImportExcel(file, 1, 0);
                List<ProjectInformation> list = ei.getDataList(ProjectInformation.class);
                LinkedHashMap returnMap = projectInformationService.disposeImportProjectInformation(list,file1,repeat);
                //如果文件已经使用完毕，则删除
                projectInformationService.deleteFileContent(file);
                //放要放的值
                j.setBody(returnMap);
                j.setMsg(returnMap.get("msg").toString());
            }

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