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.DateUtils;
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.sg.managementcenter.reserveManagement.entity.ScaleDetails;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.service.ScaleDetailsService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 建设规模 Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sg/managementCenter/scaleDetails")
public class ScaleDetailsController extends BaseController {

    @Autowired
    private ScaleDetailsService scaleDetailsService;

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

    /**
     * 建设规模信息列表页面
     * @param scaleDetails
     * @param model
     * @return
     */
    @RequiresPermissions("sg:reserve:scaleDetails:list")
    @RequestMapping(value = "list")
    public String list(ScaleDetails scaleDetails, Model model) {
        model.addAttribute("scaleDetails", scaleDetails);
        return "modules/sg/managementcenter/reserveManagement/scaleDetailsList";
    }

    @RequiresPermissions("sg:reserve:scaleDetails:list")
    @RequestMapping(value = "afterList")
    public String afterList(ScaleDetails scaleDetails, Model model) {
        model.addAttribute("scaleDetails", scaleDetails);
        return "modules/sg/managementcenter/reserveManagement/afterList";
    }

    /**
     * 建设规模信息数据列表
     * @param scaleDetails
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:list")
    @RequestMapping(value = "data")
    public Map<String, Object> basicData(ScaleDetails scaleDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ScaleDetails> page = scaleDetailsService.findPage(new Page<ScaleDetails>(request, response), scaleDetails);
        return getBootstrapData(page);
    }

    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:list")
    @RequestMapping(value = "afterData")
    public Map<String, Object> afterData(ScaleDetails scaleDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ScaleDetails> page = scaleDetailsService.findExaminePage(new Page<ScaleDetails>(request, response), scaleDetails);
        return getBootstrapData(page);
    }
     

    /**
     * 专职人员增加，编辑表单页面
     * @param scaleDetails
     * @param model
     * @return
     */
    @RequiresPermissions(value={"sg:reserve:scaleDetails:add","sg:reserve:scaleDetails:edit"},logical= Logical.OR)
    @RequestMapping(value = "form")
    public String form(ScaleDetails scaleDetails, Model model) {
        model.addAttribute("scaleDetails", scaleDetails);
        return "modules/sg/managementcenter/reserveManagement/scaleDetailsForm";
    }

    /**
     * 保存表单
     * @param scaleDetails
     * @return
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:reserve:scaleDetails:add","sg:reserve:scaleDetails:edit"},logical=Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(ScaleDetails scaleDetails){
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(scaleDetails);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        scaleDetailsService.save(scaleDetails);//保存
        j.setSuccess(true);
        j.setMsg("保存建设规模信息成功");
        return j;
    }


    /**
     *
     */
    @ResponseBody
    @RequiresPermissions(value={"sg:reserve:scaleDetails:del"},logical=Logical.OR)
    @RequestMapping(value = "deleteAll")
    public AjaxJson delete(String ids){
        AjaxJson j = new AjaxJson();
        String idArray[] =ids.split(",");
        for(String id : idArray){
            scaleDetailsService.delete(scaleDetailsService.get(id));
        }
        j.setMsg("删除建设规模信息成功");
        return j;
    }

   

    /**
     * 经理导入Excel数据
     */
    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:import")
    @RequestMapping(value = "import")
    public AjaxJson jlImportBasic(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            StringBuffer stringBuffer = new StringBuffer();
            List<ScaleDetails> list = ei.getDataList(ScaleDetails.class);
            for (ScaleDetails s :
                    list) {
                scaleDetailsService.save(s);
            }
            j.setSuccess(true);
            j.setMsg("已成功导入" + stringBuffer);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入建设规模信息失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    //导出excel
    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:export")
    @RequestMapping(value = "export")
    public AjaxJson export(String ids,ScaleDetails scaleDetails, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String idArray[] = null;
            if (null!=ids && !"".equals(ids)) {
                idArray = ids.split(",");
            }
            String fileName = "建设规模信息"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<ScaleDetails> list = scaleDetailsService.exportList(idArray);
            new ExportExcel("建设规模信息", ScaleDetails.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;
    }

    //导出excel
    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:export")
    @RequestMapping(value = "exportAfterList")
    public AjaxJson exportAfterList(String ids,ScaleDetails scaleDetails, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String idArray[] = null;
            if (null!=ids && !"".equals(ids)) {
                idArray = ids.split(",");
            }
            String fileName = "建设规模信息"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<ScaleDetails> list = scaleDetailsService.exportAfterList(idArray);
            new ExportExcel("建设规模信息", ScaleDetails.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;
    }


    /**
     * 下载导入模板
     */
    @ResponseBody
    @RequiresPermissions("sg:reserve:scaleDetails:import")
    @RequestMapping(value = "template")
    public AjaxJson importTemplate(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "建设规模信息详细导入模板.xlsx";
            List<BasicInformation> list = Lists.newArrayList();
            new ExportExcel("建设规模详细信息表", ScaleDetails.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
        }
        return j;
    }

    

}
