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

import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractData;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractDataInfo;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractDataOffice;
import com.jeeplus.modules.workcontractinfo.service.WorkContractDataService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Date;

/**
 * 合同评价表Controller
 * @author 杨帆
 * @version 2018-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/workcontractData/workcontractData")
public class WorkContractDataController extends BaseController {

	@Autowired
	private WorkContractDataService workContractDataService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private OfficeDao officeDao;

	@ModelAttribute
	public WorkContractData get(@RequestParam(required=false) String id) {
		WorkContractData entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workContractDataService.get(id);
		}
		if (entity == null){
			entity = new WorkContractData();
		}
		return entity;
	}
	
	/**
	 * 合同评价信息列表页面
	 */
	@RequiresPermissions("workcontractData:workcontractData:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkContractData workContractData, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		int count = UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax",count);
		if(workContractData.getBeginDate()==null){
			workContractData.setBeginDate(getThisMonth());
		}
		if (workContractData.getEndDate()==null){
			workContractData.setEndDate(new Date());
		}
		if (count == 4){
			if(StringUtils.isNotBlank(workContractData.getCompanyId())){
				request.setAttribute("backState",1);
			}
			List<WorkContractData> list = workContractDataService.findCompanyList(workContractData);
			model.addAttribute("list",list);
		}else if (count == 3){
			if(isOffice()){//如果所在分公司下没有子公司
				return "redirect:"+Global.getAdminPath()+"/workcontractData/workcontractData/officeList?repage";
			}
			if(StringUtils.isNotBlank(workContractData.getCompanyId())){
				request.setAttribute("backState",1);
			}
			List<WorkContractData> list = workContractDataService.findBranchList(workContractData);
			model.addAttribute("list",list);
			return "modules/workcontractdata/workContractDataList";
		}else if (count == 2){
			return "redirect:"+Global.getAdminPath()+"/workcontractData/workcontractData/infoList?repage";
		}else {
			model.addAttribute("list", new ArrayList<>());
		}

		return "modules/workcontractdata/workContractDataList";
	}

	/**
	 * 合同评价信息列表页面
	 */
	@RequiresPermissions("workcontractData:workcontractData:list")
	@RequestMapping(value = "officeList")
	public String officeList(WorkContractDataOffice workContractDataOffice, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		if(workContractDataOffice.getBeginDate()==null){
			workContractDataOffice.setBeginDate(getThisMonth());
		}
		if (workContractDataOffice.getEndDate()==null){
			workContractDataOffice.setEndDate(new Date());
		}
		int count = UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax",count);
		if(StringUtils.isNotBlank(workContractDataOffice.getCompanyId())){
			request.setAttribute("backState",1);
		}
		List<WorkContractDataOffice> list = workContractDataService.findListByOffice(workContractDataOffice);
		model.addAttribute("list",list);

		return "modules/workcontractdata/workContractDataOfficeList";
	}

	/**
	 * 合同评价信息列表页面
	 */
	@RequiresPermissions("workcontractData:workcontractData:list")
	@RequestMapping(value = "infoList")
	public String infoList(WorkContractDataInfo workContractDataInfo, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		int count = UserUtils.getSelectRoleMax();
		if(workContractDataInfo.getBeginDate()==null){
			workContractDataInfo.setBeginDate(getThisMonth());
		}
		if (workContractDataInfo.getEndDate()==null){
			workContractDataInfo.setEndDate(new Date());
		}
		if(StringUtils.isNotBlank(workContractDataInfo.getOfficeId())){
			request.setAttribute("backState",1);
		}
		Page<WorkContractDataInfo> page = workContractDataService.findPageByInfo(new Page<WorkContractDataInfo>(request, response),workContractDataInfo);
		model.addAttribute("page",page);
		return "modules/workcontractdata/workContractDataInfoList";
	}

	/**
	 * 查看，增加，编辑合同评价信息表单页面
	 */
	@RequiresPermissions(value={"workcontractData:workcontractData:view","workcontractData:workcontractData:add","workcontractData:workcontractData:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkContractData workContractData, Model model) {
		model.addAttribute("workcontractData", workContractData);
		return "modules/workcontractdata/workcontractDataForm";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workcontractData:workcontractData:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WorkContractData workContractData, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "合同统计表-分公司"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<WorkContractData> list = workContractDataService.findCompanyList(workContractData);
			String agent = request.getHeader("USER-AGENT");
            new ExportExcel("合同统计表-分公司", WorkContractData.class).setDataList(list).write(response, fileName,agent).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出合同统计表-分公司记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workcontractData/workcontractData/?repage";
    }

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workcontractData:workcontractData:export")
	@RequestMapping(value = "exportOffice", method=RequestMethod.POST)
	public String exportFileOffice(WorkContractDataOffice workContractDataOffice, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "合同统计表-部门"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			List<WorkContractDataOffice> list = workContractDataService.findListByOffice(workContractDataOffice);
			String agent = request.getHeader("USER-AGENT");
			new ExportExcel("合同统计表-部门", WorkContractDataOffice.class).setDataList(list).write(response, fileName,agent).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出合同统计表-部门记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workcontractData/workcontractData/?repage";
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workcontractData:workcontractData:export")
	@RequestMapping(value = "exportInfo", method=RequestMethod.POST)
	public String exportFileInfo(WorkContractDataInfo workContractDataInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "合同统计表-明细"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<WorkContractDataInfo> page = workContractDataService.findPageByInfo(new Page<WorkContractDataInfo>(request, response),workContractDataInfo);
			String agent = request.getHeader("USER-AGENT");
			new ExportExcel("合同统计表-明细", WorkContractDataInfo.class).setDataList(page.getList()).write(response, fileName,agent).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出合同统计表-明细记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workcontractData/workcontractData/?repage";
	}

	public Date getThisMonth() throws ParseException {
		// 获取前月的第一天
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		calendar.add(Calendar.YEAR, 0);
		calendar.set(Calendar.DAY_OF_YEAR, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = format.format(calendar.getTime());
		return format.parse(firstDay);
	}
	//分公司下是否存在子公司
	public Boolean isOffice(){
		String companyId= UserUtils.getSelectBranchOffice();
		List<Office>office=	officeDao.getAllBranch(companyId);
		if(office!=null&&office.size()>0){
			return false;
		}else{
			return true;
		}
	}
}