/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workcontent.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.workcontent.entity.WorkContentInvestmentCost;
import com.jeeplus.modules.workcontent.service.WorkContentInvestmentCostService;
import com.jeeplus.modules.workreceiptsregister.entity.ResponseEntity;
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.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;

/**
 * 工程费用Controller
 * @author ssrh
 * @version 2018-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/workcontentinvestmentcost/workContentInvestmentCost")
public class WorkContentInvestmentCostController extends BaseController {

	@Autowired
	private WorkContentInvestmentCostService workContentInvestmentCostService;
	
	@ModelAttribute
	public WorkContentInvestmentCost get(@RequestParam(required=false) String id) {
		WorkContentInvestmentCost entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workContentInvestmentCostService.get(id);
		}
		if (entity == null){
			entity = new WorkContentInvestmentCost();
		}
		return entity;
	}
	
	/**
	 * 工程费用列表页面
	 */
	/*@RequiresPermissions("workcontentinvestmentcost:workContentInvestmentCost:list")*/
	@RequestMapping(value = {"list", ""})
	public String list(WorkContentInvestmentCost workContentInvestmentCost,String view, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<WorkContentInvestmentCost> list = new ArrayList<>();
        if(StringUtils.isNotBlank(workContentInvestmentCost.getContentId())){
            list = workContentInvestmentCostService.findList(workContentInvestmentCost);
        }
        model.addAttribute("investmentCostList", list);
        String viewPath = "modules/workcontent/workContentInvestmentCostList";
        if("view".equals(view)){
            viewPath+="View";
        }
		List<MainDictDetail> unitList=DictUtils.getMainDictList("unit_type");
		model.addAttribute("unitList", unitList);
		List<MainDictDetail> nodeList=DictUtils.getMainDictList("node_type");
		model.addAttribute("nodeList", nodeList);
		List<MainDictDetail> costList=DictUtils.getMainDictList("cost_type");
		model.addAttribute("costList", costList);
        return viewPath;
	}

	/**
	 * 导入Excel数据
	 */
	@ResponseBody
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public Object importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        ResponseEntity<WorkContentInvestmentCost> responseEntity = new ResponseEntity<>();
		try {
			ImportExcel ei = new ImportExcel(file, 1, 0);
            List<WorkContentInvestmentCost> list = ei.getDataList(WorkContentInvestmentCost.class);
            responseEntity.setData(list);
		} catch (Exception e) {
		    logger.error("导入工程费用失败！",e);
		    responseEntity.setCode(400);
		    responseEntity.setMessage("导入工程费用失败！"+e.getMessage());
		}
		return responseEntity;
    }
	
	/**
	 * 下载导入工程费用数据模板
	 */
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "工程费用数据导入模板.xlsx";
    		List<WorkContentInvestmentCost> list = Lists.newArrayList(); 
    		new ExportExcel("工程费用数据", WorkContentInvestmentCost.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workcontentinvestmentcost/workContentInvestmentCost/?repage";
    }
	
}