package com.jeeplus.modules.sg.managementcenter.reserveManagement.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
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.filing.basicInformation.entity.BasicInformation;
import com.jeeplus.modules.filing.summary.entity.Summary;
import com.jeeplus.modules.sg.managementcenter.materialproject.util.ImportUtil;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ExportScheme;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveManagement;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.service.ReserveManagementService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.utils.ReserveExportUtil;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.utils.ReserveManagementProUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
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.List;
import java.util.Map;

/**
 * 项目储备信息 Controller
 * @author 徐滕
 * @version 2020-09-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sg/managementCenter/reserveManagement")
public class ReserveManagementController extends BaseController {

    @Autowired
    private ReserveManagementService service;

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

    /**
     * 项目储备信息列表页面
     * @param reserveManagement
     * @param model
     * @return
     */
    @RequiresPermissions("sg:managementcenter:reserveManagement:list")
    @RequestMapping(value = "skipList")
    public String skipList(ReserveManagement reserveManagement, Model model) {
        model.addAttribute("reserveManagement", reserveManagement);
        return "modules/sg/managementcenter/reserveManagement/reserveManagementInformation";
    }


    /**
     * 方案交底结论列表页面
     * @param reserveManagement
     * @param model
     * @return
     */
    @RequiresPermissions("sg:managementcenter:disClosureReserveModule:list")
    @RequestMapping(value = "moduleList")
    public String moduleList(ReserveManagement reserveManagement, Model model) {
        model.addAttribute("reserveManagement", reserveManagement);
        return "modules/sg/managementcenter/reserveManagement/disClosureReserveModuleList";
    }

    /**
     * 项目储备信息数据列表
     * @param reserveManagement
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:list")
    @RequestMapping(value = "list")
    public Map<String, Object> basicData(ReserveManagement reserveManagement, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<ReserveManagement> page = service.findPage(new Page<ReserveManagement>(request, response), reserveManagement);
        return getBootstrapData(page);
    }

    /**
     * 方案交底结论数据列表
     * @param reserveManagement
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:disClosureReserveModule:list")
    @RequestMapping(value = "moduleForm")
    public Map<String, Object> moduleData(ReserveManagement reserveManagement, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ReserveManagement> page = service.findModulePage(new Page<ReserveManagement>(request, response), reserveManagement);
        return getBootstrapData(page);
    }


    /**
     * 专职人员增加，编辑表单页面
     * @param reserveManagement
     * @param model
     * @return
     */
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:add","sg:managementcenter:reserveManagement:edit"},logical= Logical.OR)
    @RequestMapping(value = "form")
    public String form(ReserveManagement reserveManagement, Model model) {
        model.addAttribute("reserveManagement", reserveManagement);
        //判断id是否为空 如果未空则为新增 否则为修改
        if(StringUtils.isBlank(reserveManagement.getId())){
            return "modules/sg/managementcenter/reserveManagement/reserveManagementForm";
        }
        return "modules/sg/managementcenter/reserveManagement/reserveManagementModifyByzz";
    }

    /**
     * 项目经理编辑表单页面
     * @param reserveManagement
     * @param model
     * @return
     */
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:jlEdit"},logical= Logical.OR)
    @RequestMapping(value = "jlEdit")
    public String jlEdit(ReserveManagement reserveManagement, Model model) {
        model.addAttribute("reserveManagement", reserveManagement);
        return "modules/sg/managementcenter/reserveManagement/reserveManagementModifyByjl";
    }

    /**
     * 查看表单页面
     */
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:view"},logical= Logical.OR)
    @RequestMapping(value = "view")
    public String view(ReserveManagement reserveManagement, Model model) {
        model.addAttribute("reserveManagement", reserveManagement);
        return "modules/sg/managementcenter/reserveManagement/reserveManagementView";
    }

    /**
     * 专职人员保存表单
     * @param reserveManagement
     * @return
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:add"},logical=Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(ReserveManagement reserveManagement){
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(reserveManagement);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        service.save(reserveManagement);//保存
        j.setSuccess(true);
        j.setMsg("保存储备项目基本信息成功");
        return j;
    }

    /**
     * 专职人员修改表单
     * @param reserveManagement
     * @return
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:edit"},logical=Logical.OR)
    @RequestMapping(value = "update")
    public AjaxJson update(ReserveManagement reserveManagement){
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(reserveManagement);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        service.update(reserveManagement);
        j.setSuccess(true);
        j.setMsg("修改储备项目基本信息成功");
        return j;
    }

    /**
     * 项目经理修改表单
     * @param reserveManagement
     * @return
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:jlEdit"},logical=Logical.OR)
    @RequestMapping(value = "jlUpdate")
    public AjaxJson jlUpdate(ReserveManagement reserveManagement){
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(reserveManagement);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        service.updatejl(reserveManagement);
        j.setSuccess(true);
        j.setMsg("修改储备项目基本信息成功");
        return j;
    }

    /**
     * 删除表单
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:managementcenter:reserveManagement:del"},logical=Logical.OR)
    @RequestMapping(value = "deleteAll")
    public AjaxJson delete(String ids){
        AjaxJson j = new AjaxJson();
        String idArray[] =ids.split(",");
        for(String id : idArray){
            service.delete(service.get(id));
        }
        j.setMsg("删除储备项目基本信息成功");
        return j;
    }

    /**
     * 专职人员导入Excel数据
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:import")
    @RequestMapping(value = "import")
    public AjaxJson importBasic(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            ImportUtil importUtil = new ImportUtil(file, 0, 0);
            //获取导入表格所有数据
            List<ReserveManagement> reserveManagementList = ReserveManagementProUtil.getProjectUtil(importUtil);
            //查询已有的参数数据 并分类返回已存在数据和未存在数据
            Map map = service.getExistProjectList(reserveManagementList);
            //未存在数据
            List<ReserveManagement> withoutList = (List<ReserveManagement>) map.get("without");
            //已存在数据
            List<ReserveManagement> existList = (List<ReserveManagement>) map.get("exist");

            //对未存在的数据进行批量导入
            if(withoutList.size()>0){
                service.insertList(withoutList);
            }

            //对已存在的数据进行修改
            if(existList.size()>0){
                for (ReserveManagement reserveManagement : existList) {
                    service.update(reserveManagement);
                }
            }
            int i = withoutList.size() + existList.size();
            j.setMsg("已成功导入" + i + "条项目储备信息记录");

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

    /**
     * 经理导入Excel数据
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:jlImport")
    @RequestMapping(value = "jlImport")
    public AjaxJson jlImportBasic(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 3, 0);
            List<ExportScheme> list = ei.getDataList(ExportScheme.class);
            for (ExportScheme s :
                    list) {
                service.jlSave(s);
            }
            j.setMsg("已成功导入");

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

    //导出excel
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:export")
    @RequestMapping(value = "export")
    public void exportExcel(ReserveManagement reserveManagement, HttpServletRequest request, HttpServletResponse response) {
        List<ReserveManagement> list = service.exportFindList(reserveManagement);
        //生成序列号
        int i = 1;
        for (ReserveManagement s : list) {
            s.setId(String.valueOf(i));
            i++;
        }

        ReserveExportUtil util = ReserveExportUtil.getOne();
        XSSFWorkbook workbook = util.getWorkbook("reserveManagement");
        util.exportBasic(response,workbook,list);
    }


    //导出excel
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:jlExport")
    @RequestMapping(value = "jlExport")
    public void jlExportExcel(ExportScheme exportScheme, HttpServletRequest request, HttpServletResponse response) {
        List<ExportScheme> list = service.exportModuleList(exportScheme);
        //生成序列号
        int i = 1;
        for (ExportScheme s : list) {
            s.setId(String.valueOf(i));
            i++;
        }
        ReserveExportUtil util = ReserveExportUtil.getOne();
        XSSFWorkbook workbook = util.getWorkbook("exportScheme");
        util.exportJlModule(response,workbook,list);
    }

    /**
     * 下载导入基础信息数据模板
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:import")
    @RequestMapping(value = "template")
    public AjaxJson importTemplate(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "储备项目基础信息导入模板.xlsx";
            List<BasicInformation> list = Lists.newArrayList();
            new ExportExcel("储备项目基础信息", BasicInformation.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
        }
        return j;
    }


    /**
     * 下载导入基础信息数据模板
     */
    @ResponseBody
    @RequiresPermissions("sg:managementcenter:reserveManagement:import")
    @RequestMapping(value = "schemeTemplate")
    public AjaxJson importSchemeTemplate(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "方案交底结论导入模板.xlsx";
            List<BasicInformation> list = Lists.newArrayList();
            new ExportExcel("方案交底结论", ExportScheme.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
        }
        return j;
    }

}
