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

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.act.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.projectrecord.service.ProjectRecordsService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workactivity.service.ActivityService;
import com.jeeplus.modules.workbidproject.entity.WorkBidProject;
import com.jeeplus.modules.workbidproject.entity.WorkBidProjectList;
import com.jeeplus.modules.workbidproject.service.WorkBidProjectService;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workclientinfo.service.WorkClientInfoService;
import com.jeeplus.modules.workproject.service.WorkProjectService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 投标项目Controller
 * @author mengxy
 * @version 2017-05-27
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "${adminPath}/workbidprojectlist/workBidProjectList")
public class WorkBidProjectListController extends BaseController {

	@Autowired
	private WorkBidProjectService workBidProjectService;
	@Autowired
	private HistoryService historyService;

	@Autowired
	private WorkProjectService workProjectService;

	@Autowired
	protected RuntimeService runtimeService;

	@Autowired
	protected TaskService taskService;

	@Autowired
	private ActTaskService actTaskService;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private WorkClientInfoService workClientInfoService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private ProjectRecordsService projectRecordsService;
	@Autowired
	private OfficeService officeService;

	@ModelAttribute
	public WorkBidProject get(@RequestParam(required=false) String id) {
		WorkBidProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workBidProjectService.get(id);
		}
		if (entity == null){
			entity = new WorkBidProject();
		}
		return entity;
	}
	
	/**
	 * 投标项目列表页面
	 */
	@RequiresPermissions("workbidproject:workBidProject:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkBidProjectList workBidProjectList, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkBidProjectList> page = workBidProjectService.findPageList(new Page<WorkBidProjectList>(request, response), workBidProjectList);
		model.addAttribute("page", page);
		return "modules/workbidproject/workBidProjectLists";
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workbidproject:workBidProject:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WorkBidProjectList workBidProjectList, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "投标项目"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WorkBidProjectList> page = workBidProjectService.findPageList(new Page<WorkBidProjectList>(request, response, -1), workBidProjectList);
    		new ExportExcel("投标项目", WorkBidProjectList.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出投标项目记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workbidprojectlist/workBidProjectList/?repage";
    }

}