package com.goafanti.order.controller;


import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

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

import org.apache.poi.hssf.util.HSSFColor.TURQUOISE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.TTaskHours;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.TOrderTaskDetailBo;
import com.goafanti.order.bo.TOrderTaskListBo;
import com.goafanti.order.bo.TaskProgressBo;
import com.goafanti.order.bo.UpdateProjectStatusBo;
import com.goafanti.order.bo.inuptTaskHoursListBo;
import com.goafanti.order.bo.inuptTaskListBo;
import com.goafanti.order.bo.managerListBo;
import com.goafanti.order.service.OrderProjectService;

@RestController
@RequestMapping(value = "/api/admin/orderProject")
public class OrderProjectApiController extends CertifyApiController {
	
	@Autowired
	private OrderProjectService	orderProjectService;
	@Value(value = "${upload.path}")
	private String	uploadPath			= null;
	
	/**
	 * 项目分配
	 * taskId 项目ID
	 * taskReceiverId 受理人ID
	 * type 0管理员派单 1经理转交 2经理派单 3咨询师转交 4回退
	 */
	@RequestMapping(value = "/projectDistribution", method = RequestMethod.POST)
	public Result projectDistribution(Integer taskId,String taskReceiverId ,String remarks,Integer type){
		Result res = new Result();
		if(null==taskId || StringUtils.isBlank(taskReceiverId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号和受理人"));
			return res;
		}
		if(type==null) type=0;
		boolean flag=true;
		if (type==0&&(TokenManager.hasRole(AFTConstants.TECH_ADMIN)||TokenManager.hasRole(AFTConstants.SUPERADMIN)))flag=false;
		else if ((type==1||type==2)&&TokenManager.hasRole(AFTConstants.TECH_MANAGER))flag=false;
		else if ((type==3||type==4)&&TokenManager.hasRole(AFTConstants.TECH))flag=false;
		if (flag) {
			res.getError().add(buildError("角色不正确，请勿在浏览器登录多个账号！"));
			return res;
		}
		
		res.setData(orderProjectService.updateProjectDistribution( taskId, taskReceiverId,remarks,type));
		return res;
	}
	
	/**
	 * 任务派单列表   我的任务，我的外包任务
	 * specially  0我的任务  1任务派单
	 */
	@RequestMapping(value="/orderTaskList" ,method = RequestMethod.GET)
	public Result orderTaskList(inuptTaskListBo ib){
		Result res=new Result();
		res.setData(orderProjectService.orderTaskList(ib));
		return res;
	}
	
	
	/**
	 * 任务派单查询
	 */
	@RequestMapping(value="/selectTaskList" ,method = RequestMethod.GET)
	public Result selectTaskList(String name,String orderNo,String taskId,Integer taskStatus,String adminName,Integer specially  ,String depId  ,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.setData(orderProjectService.selectTaskList( name, orderNo, taskId, taskStatus, adminName,specially ,  depId  ,pageNo, pageSize));
		return res;
	}
	
	
	
	/**
	 * 经理查询订单
	 */
	@RequestMapping(value="/managerSelect" ,method = RequestMethod.GET)
	public Result managerSelect(String name,String orderNo,String contractNo,String cid,String projectType,
			String depId,String techDepId  ,Integer declarationBatch  ,String commodityPrice,String startTime,
			String endTime,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.setData(orderProjectService.managerSelect( name, orderNo, contractNo, cid, projectType,
				 depId, techDepId  , declarationBatch  ,commodityPrice,startTime, endTime , pageNo, pageSize));
		return res;
	}
	
	
	/**
	 * 经理查询订单
	 */
	@RequestMapping(value="/exportManagerSelect" ,method = RequestMethod.GET)
	public Result exportManagerSelect(String name,String orderNo,String contractNo,String cid,String projectType,
			String depId,String techDepId  ,Integer declarationBatch  ,String commodityPrice,String startTime,
			String endTime,Integer pageNo,Integer pageSize){
		@SuppressWarnings("unchecked")
		List<managerListBo> list=(List<managerListBo>) orderProjectService.managerSelect( name, orderNo, contractNo, 
				cid, projectType,depId, techDepId  , declarationBatch  ,commodityPrice,startTime, endTime , pageNo, pageSize).getList();
		NewExcelUtil<managerListBo> excel=new NewExcelUtil<>(managerListBo.class);
		return excel.exportExcel(list, "企业项目查询列表", uploadPath);
	}
	
	
	/**
	 * 任务详情
	 */
	@RequestMapping(value="/orderTaskDetail" ,method = RequestMethod.GET)
	public Result orderTaskDetail(String id){
		Result res=new Result();
		res.setData(orderProjectService.orderTaskDetail(id));
		return res;
	}
	/**
	 * 任务修改(咨询师)
	 */
	@RequestMapping(value="/updateOrderTask" ,method = RequestMethod.POST)
	public Result updateOrderTask(TOrderTaskDetailBo t){
		Result res=new Result();
		res.setData(orderProjectService.updateOrderTask(t));
		return res;
	}
	/**
	 * 修改状态(咨询师)
	 */
	@RequestMapping(value="/updateProjectStatus" ,method = RequestMethod.POST)
	public Result updateProjectStatus(@Valid UpdateProjectStatusBo a,BindingResult bindingResult){
		Result res=new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage()));
            return res;
		}
		res.setData(orderProjectService.updateProjectStatus( a));
		return res;
	}
	/**
	 * 添加工时
	 */
	@RequestMapping(value="/addTaskHours" ,method = RequestMethod.POST)
	public Result addTaskHours(TTaskHours t,String taskDate){
		Result res=new Result();
		if (t.getHours()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务时间"));
			return res;
		}
		if (StringUtils.isBlank(taskDate)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "工作日期"));
			return res;
		}
		if (StringUtils.isBlank(t.getRemarks())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "工作内容"));
			return res;
		}
		if (StringUtils.isNotBlank(taskDate)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				t.setTaskDay(sdf.parse(taskDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		res.setData(orderProjectService.addTaskHours(t));
		return res;
	}
	/**
	 * 任务工时列表
	 */
	@RequestMapping(value="/taskHoursList" ,method = RequestMethod.GET)
	public Result taskHoursList(inuptTaskHoursListBo ib,
			Integer pageNo, Integer pageSize){
		Result res=new Result();
		res.setData(orderProjectService.taskHoursList(ib,pageNo, pageSize));
		return res;
	}
	/**
	 * 任务工时详情列表
	 */
	@RequestMapping(value="/taskHoursDetailsList" ,method = RequestMethod.GET)
	public Result taskHoursDetailsList( String taskId ){
		Result res=new Result();
		if (StringUtils.isBlank(taskId)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目编号"));
			return res;
		}
		res.setData(orderProjectService.taskHoursDetailsList(taskId));
		return res;
	}
	
	
	/**
	 * 任务操作日志
	 */
	@RequestMapping(value="/TaskLogList" ,method = RequestMethod.GET)
	public Result TaskLogList(Integer id){
		Result res=new Result();
		res.setData(orderProjectService.TaskLogList(id));
		return res;
	}
	/** 任务文件上传 **/
	@RequestMapping(value = "/uploadOrderTaskFile", method = RequestMethod.POST)
	public Result uploadOrderTaskFile(HttpServletRequest req,String sign){
		Result res = new Result();
		res.setData(handleFile(res, "/order_task_file/", false, req, sign));
		return res;
	}
	/**
	 * 	新增项目申报进度
	 */
	@RequestMapping(value="/createTaskProgress" ,method = RequestMethod.POST)
	public Result createTaskProgress(TaskProgressBo t){
		Result res=new Result();
		if ( null==t.getTaskId()|| null==t.getAlreadyNumber()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"参数","参数"));
		}
		res.setData(orderProjectService.createTaskProgress(t));
		return res;
	}
	
	/**
	 * 	修改项目申报进度
	 */
	@RequestMapping(value="/updateTaskProgress" ,method = RequestMethod.POST)
	public Result updateTaskProgress(TaskProgressBo t){
		Result res=new Result();
		if (null==t.getTaskId()|| null==t.getAlreadyNumber()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"参数","参数"));
		}
		res.setData(orderProjectService.updateTaskProgress(t));
		return res;
	}
	
	/**
	 * 	删除项目申报进度
	 */
	@RequestMapping(value="/delectTaskProgress" ,method = RequestMethod.POST)
	public Result delectTaskProgress(Integer id){
		Result res=new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"参数","参数"));
		}
		res.setData(orderProjectService.delectTaskProgress(id));
		return res;
	}
	/**
	 * 	查看项目申报进度
	 */
	@RequestMapping(value="/selectTaskProgress" ,method = RequestMethod.GET)
	public Result selectTaskProgress(Integer tid){
		Result res=new Result();
		if (null==tid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"参数","参数"));
		}
		res.setData(orderProjectService.selectTaskProgress(tid));
		return res;
	}
	
	/**
	 * 	项目申报进度日志
	 */
	@RequestMapping(value="/selectTaskProgressLog" ,method = RequestMethod.GET)
	public Result selectTaskProgressLog(Integer tid){
		Result res=new Result();
		if (null==tid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"参数","参数"));
		}
		res.setData(orderProjectService.selectTaskProgressLog(tid));
		return res;
	}
	/**
	 * 	拆分项目
	 */
	@RequestMapping(value="/addSplitProject" ,method = RequestMethod.POST)
	public Result addSplitProject(Integer tid,String splitList){
		Result res=new Result();
		if (null==tid||StringUtils.isBlank(splitList)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"项目和拆分数","项目和拆分数"));
			return res;
		}
		int i=orderProjectService.pushSplitProject(tid,splitList);
		if (i==-1) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"拆分数","拆分数"));
			return res;
		}
		res.setData(i);
		return res;
	}
	
	/**
	 * 	拆分项目列表
	 */
	@RequestMapping(value="/splitProjectList" ,method = RequestMethod.GET)
	public Result splitProjectList(Integer tid){
		Result res=new Result();
		if (null==tid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"项目和拆分数","项目和拆分数"));
			return res;
		}
		res.setData(orderProjectService.splitProjectList(tid));
		return res;
	}
	
	
	
	/**
	 * 我的工时列表导出xls
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exportMyTaskList", method = RequestMethod.GET)
	public Result exportMyTaskList(HttpServletResponse response,inuptTaskHoursListBo ib, Integer pageNo, Integer pageSize) {
		if(pageSize==null)pageSize=999999;
		@SuppressWarnings("unchecked")
		List<TOrderTaskListBo> list=(List<TOrderTaskListBo>) orderProjectService.taskHoursList(ib,  pageNo, pageSize).getList();
		NewExcelUtil<TOrderTaskListBo> excel=new NewExcelUtil<>(TOrderTaskListBo.class);
		return excel.exportExcel(list, "工时信息记录表", response);
	}
	
	/**
	 * 我的任务列表导出xls
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exporProjectList", method = RequestMethod.GET)
	public Result exporProjectList(HttpServletResponse response,inuptTaskListBo ib) {
		if(ib.getPageSize()==null)ib.setPageSize(99999);
		String str="项目任务列表";
		@SuppressWarnings("unchecked")
		List<TOrderTaskListBo> list=(List<TOrderTaskListBo>) orderProjectService.orderTaskList(ib).getList();
		NewExcelUtil<TOrderTaskListBo>excel=new NewExcelUtil<>(TOrderTaskListBo.class);
		return excel.exportExcel(list, str, response);
		}
	
	/**
	 * 	项目日志 （转交日志）
	 * @param tid
	 * @return
	 */
	@RequestMapping(value = "/taskAttributionLog",method = RequestMethod.GET)
	public Result taskAttributionLog(Integer tid) {
		Result res =new Result();
		if (tid==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"项目编号","项目编号"));
			return res;
		}
		res.data(orderProjectService.taskAttributionLog(tid));
		return res;
	}
	/**
	 * 	项目日志 （转交日志）
	 * @param tid
	 * @return
	 */
	@RequestMapping(value = "/selectUidByproject",method = RequestMethod.GET)
	public Result selectUidByproject(Integer uid) {
		Result res =new Result();
		if (uid==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"客户编号","客户编号"));
			return res;
		}
//		res.data(/* orderProjectService.selectUidByproject(uid) */);
		return res;
	}
}
