package com.jeeplus.modules.sg.budgetSpectaculars.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.sg.budgetSpectaculars.entity.*;
import com.jeeplus.modules.sg.budgetSpectaculars.service.ProjectBasicsService;
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;

/**
 * 基础底表展示处理
 * 预算执行管理-综合查询表导入数据处理
 * @author: 徐滕
 * @create: 2021-09-14 15:04
 **/
@Controller
@RequestMapping(value = "${adminPath}/budgetSpectaculars/budgetSpectaculars")
public class ProjectBasicsController extends BaseController {
    @Autowired
    private ProjectBasicsService service;

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

    /**
     * 基础底表展示信息 列表页面
     * @param projectBasicsInfo
     * @param model
     * @return
     */
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:list")
    @RequestMapping(value = "list")
    public String list(ProjectBasicsInfo projectBasicsInfo, Model model) {
        model.addAttribute("projectBasicsInfo", projectBasicsInfo);
        return "modules/sg/project/projectBasics/projectBasicsList";
    }

    /**
     * 基础底表展示信息 列表数据
     * @param projectBasicsInfo
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(ProjectBasicsInfo projectBasicsInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ProjectBasicsInfo> page = service.findPage(new Page<ProjectBasicsInfo>(request, response), projectBasicsInfo);
        return getBootstrapData(page);
    }


    /**
     * 文件上传
     */
    @RequestMapping(value = "importExcel")
    public String importExcel() {
        return "modules/sg/project/projectBasics/importExcel";
    }

    /**
     * 导入综合查询Excel数据

     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:import")
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file,@RequestParam("year") String year) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 0, 0);
            List<BudgetExecuteInfo> budgetExecuteList = ei.getDataListNew(BudgetExecuteInfo.class);
            //数据处理
            service.disposeBudgetExecuteInfo(budgetExecuteList,year);
            j.setSuccess(true);
            j.setMsg( "导入项目信息成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项目信息失败！失败信息："+e.getMessage());
        }
        return j;
    }

    /**
     * 导入项目明细帐Excel数据
     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:importProjectDetail")
    @RequestMapping(value = "importProjectDetailFile")
    public AjaxJson importProjectDetailFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 0, 0);
            List<ProjectDetailInfo> projectDetailList = ei.getDataListNew(ProjectDetailInfo.class);
            //数据处理
            service.disposeProjectDetailInfo(projectDetailList);
            j.setSuccess(true);
            j.setMsg( "导入项目明细信息成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项目明细信息失败！失败信息："+e.getMessage());
        }
        return j;
    }

    /**
     * 导入工程结算Excel数据

     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:importProjectSettlement")
    @RequestMapping(value = "importProjectSettlementFile")
    public AjaxJson importProjectSettlementFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<ProjectSettlementInfo> budgetExecuteList = ei.getDataListNew(ProjectSettlementInfo.class);
            //数据处理
            service.disposeProjectSettlementInfo(budgetExecuteList);
            j.setSuccess(true);
            j.setMsg( "导入工程结算成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入工程结算失败！失败信息："+e.getMessage());
        }
        return j;
    }

    /**
     * 导入甲供材超欠供物资明细Excel数据
     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:importWhichMaterial")
    @RequestMapping(value = "importWhichMaterialFile")
    public AjaxJson importWhichMaterialFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 0, 0);
            List<WhichMaterialInfo> projectDetailList = ei.getDataListNew(WhichMaterialInfo.class);
            //数据处理
            service.disposeWhichMaterialInfo(projectDetailList);
            j.setSuccess(true);
            j.setMsg( "导入甲供材超欠供物资明细成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入甲供材超欠供物资明细失败！失败信息："+e.getMessage());
        }
        return j;
    }


    /**
     * 导入人工维护Excel数据
     */
    @ResponseBody
    @RequiresPermissions("budgetSpectaculars:budgetSpectaculars:importHandmadeVindicate")
    @RequestMapping(value = "importHandmadeVindicateFile")
    public AjaxJson importHandmadeVindicateFile(@RequestParam("file") MultipartFile file) {
        AjaxJson j = new AjaxJson();
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<HandmadeVindicateInfo> projectDetailList = ei.getDataList(HandmadeVindicateInfo.class);
            //数据处理
            service.disposeHandmadeVindicateInfo(projectDetailList);
            j.setSuccess(true);
            j.setMsg( "导入手工维护表成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入手工维护表失败！失败信息："+e.getMessage());
        }
        return j;
    }

}
