/**
 * 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.WorkContentCtrlprice;
import com.jeeplus.modules.workcontent.service.WorkContentCtrlpriceService;
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}/workcontent/workContentCtrlprice")
public class WorkContentCtrlpriceController extends BaseController {

	@Autowired
	private WorkContentCtrlpriceService workContentCtrlpriceService;
	
	@ModelAttribute
	public WorkContentCtrlprice get(@RequestParam(required=false) String id) {
		WorkContentCtrlprice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workContentCtrlpriceService.get(id);
		}
		if (entity == null){
			entity = new WorkContentCtrlprice();
		}
		return entity;
	}
	
	/**
	 *   清单及控制价编审列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(WorkContentCtrlprice workContentCtrlprice, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<WorkContentCtrlprice> list = workContentCtrlpriceService.findList(workContentCtrlprice);
        model.addAttribute("list", list);
		return "modules/workcontent/workContentCtrlpriceList";
	}

    /**
     * 查看，增加，编辑  清单及控制价编审表单页面
     */
    @RequestMapping(value = "form")
    public String form(WorkContentCtrlprice workContentCtrlprice, String view,Model model) {
        List<WorkContentCtrlprice> ctrlpricelist = new ArrayList<>();
        if(StringUtils.isNotBlank(workContentCtrlprice.getContentId())){
            ctrlpricelist = workContentCtrlpriceService.findList(workContentCtrlprice);
        }
        model.addAttribute("ctrlpricelist", ctrlpricelist);
        String viewPath = "modules/workcontent/workContentCtrlpriceList";
        if("view".equals(view)){
            viewPath+="View";
        }
		List<MainDictDetail> nodeList=DictUtils.getMainDictList("node_type");
		model.addAttribute("nodeList", nodeList);
        return viewPath;
    }

	/**
	 * 删除  清单及控制价编审
	 */
	@RequestMapping(value = "delete")
	public String delete(WorkContentCtrlprice workContentCtrlprice, RedirectAttributes redirectAttributes) {
		workContentCtrlpriceService.delete(workContentCtrlprice);
		addMessage(redirectAttributes, "删除  清单及控制价编审成功");
		return "redirect:"+Global.getAdminPath()+"/workcontentctrlprice/workContentCtrlprice/?repage";
	}
	
	/**
	 * 批量删除  清单及控制价编审
	 */
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			workContentCtrlpriceService.delete(workContentCtrlpriceService.get(id));
		}
		addMessage(redirectAttributes, "删除  清单及控制价编审成功");
		return "redirect:"+Global.getAdminPath()+"/workcontentctrlprice/workContentCtrlprice/?repage";
	}
	
	/**
	 * 导入Excel数据
	 */
	@ResponseBody
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public ResponseEntity importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        ResponseEntity<WorkContentCtrlprice> responseEntity = new ResponseEntity<>();
		try {
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<WorkContentCtrlprice> list = ei.getDataList(WorkContentCtrlprice.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<WorkContentCtrlprice> list = Lists.newArrayList(); 
    		new ExportExcel("  清单及控制价编审数据", WorkContentCtrlprice.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workcontentctrlprice/workContentCtrlprice/?repage";
    }
	
	
	

}