package com.goafanti.techproject.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

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

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
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.RequestParam;

import com.goafanti.admin.service.AdminService;
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.enums.AttachmentType;
import com.goafanti.common.enums.DeleteStatus;
import com.goafanti.common.enums.TechProjectFields;
import com.goafanti.common.enums.TechProjectStatus;
import com.goafanti.common.model.Admin;
import com.goafanti.common.model.ContractTask;
import com.goafanti.common.model.TechProject;
import com.goafanti.common.model.TechProjectLog;
import com.goafanti.common.model.TechWebsite;
import com.goafanti.common.model.User;
import com.goafanti.common.utils.AesUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.easemob.EasemobUtils;
import com.goafanti.easemob.enums.EasemonSysUsers;
import com.goafanti.order.service.OrderService;
import com.goafanti.techproject.bo.InputTechProject;
import com.goafanti.techproject.bo.InputTechWebsite;
import com.goafanti.techproject.bo.ProjectListBo;
import com.goafanti.techproject.bo.TechProjectManageListBo;
import com.goafanti.techproject.bo.TechWebsiteDetailBo;
import com.goafanti.techproject.bo.TechWebsiteListBo;
import com.goafanti.techproject.enums.TaskState;
import com.goafanti.techproject.service.TechProjectLogService;
import com.goafanti.techproject.service.TechProjectService;
import com.goafanti.techproject.service.TechWebsiteService;
import com.goafanti.user.bo.OrgUnitNames;
import com.goafanti.user.service.UserService;

@Controller
@RequestMapping(value = "/api/admin/techproject")
public class AdminTechProjectApiController extends CertifyApiController {
	@Value(value = "${aesSecretKey}")
	private String					aesSecretKey	= null;
	@Resource
	private TechProjectService		techProjectService;
	@Resource
	private TechWebsiteService		techWebsiteService;
	@Resource
	private TechProjectLogService	techProjectLogService;
	@Resource
	private UserService				userService;
	@Resource
	private AdminService			adminService;
	@Resource
	private OrderService orderServiceImpl;
	
	@Autowired
	EasemobUtils					easemobUtils;

	/**
	 * 科技网址申报公司下拉
	 * 
	 * @return
	 */
	@RequestMapping(value = "/websiteUnit", method = RequestMethod.GET)
	public Result getWebsiteUnit() {
		Result res = new Result();
		List<OrgUnitNames> list = techProjectService.selectWebsiteUnitNames();
		Map<String, String> map = new TreeMap<String, String>();
		for (OrgUnitNames o : list) {
			map.put(o.getUid(), o.getUnitName());
		}
		res.setData(map);
		return res;
	}

	/**
	 * 咨询师下拉列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getConsultant", method = RequestMethod.GET)
	public Result getConsultant() {
		Result res = new Result();
		List<Admin> list = adminService.selectTechprojectConsultant();
		Map<String, String> map = new TreeMap<String, String>();
		for (Admin o : list) {
			map.put(o.getId(), o.getName());
		}
		res.setData(map);
		return res;
	}

	/**
	 * 负责人下拉
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getPrincipal", method = RequestMethod.GET)
	public Result getPrincipal() {
		Result res = new Result();
		List<Admin> list = adminService.selectTechprojectPrincipal();
		Map<String, String> map = new TreeMap<String, String>();
		for (Admin o : list) {
			map.put(o.getId(), o.getName() + " " + (null == o.getPosition() ? "" : o.getPosition()));
		}
		res.setData(map);
		return res;
	}

	/**
	 * 科技项目申报列表
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/listTechProject", method = RequestMethod.POST)
	public Result listTechProject(String contractId, Integer province, String unitName, String uid, String pageNo,
			String pageSize) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		Pagination<TechProjectManageListBo> t = techProjectService.listManageTechProject(contractId, province, unitName,
				uid, pNo, pSize);
		if (null != t) {
			List<TechProjectManageListBo> l = (List<TechProjectManageListBo>) t.getList();
			for (TechProjectManageListBo w : l) {
				if (!StringUtils.isBlank(w.getPassword())) {
					try {
						w.setPassword(AesUtils.decrypt(w.getPassword(), aesSecretKey));
					} catch (Exception e) {
					}
				}
			}
			t.setList(l);
			res.setData(t);
		}
		return res;
	}

	/**
	 * 科技项目申报
	 * 
	 * @return
	 */
	@RequestMapping(value = "/applyTechProject", method = RequestMethod.POST)
	public Result applyTechProject(@Valid InputTechProject techProject, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					TechProjectFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(techProject.getUid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}

		if (StringUtils.isBlank(techProject.getSalesman())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到营销员", "营销员"));
			return res;
		}
		String salesman = techProject.getSalesman();
		User curUser = userService.selectByPrimaryKey(techProject.getUid());
		if (!checkCertify(res, curUser)) {
			return res;
		}
		TechProject tp = new TechProject();
		BeanUtils.copyProperties(techProject, tp);
		techProjectService.saveTechProject(tp, salesman);
		return res;

	}

	/**
	 * 科技项目申报详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/techProjectDetial", method = RequestMethod.POST)
	public Result techProjectDetial(String pid) {
		Result res = new Result();
		if (StringUtils.isBlank(pid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "公司"));
		} else {
			res.setData(techProjectService.selectProjectDetail(pid));
		}
		return res;
	}

	/**
	 * 科技项目状态流转
	 * 
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = "/techProjectLog", method = RequestMethod.POST)
	public Result techProjectLog(String pid) {
		Result res = new Result();
		if (StringUtils.isBlank(pid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "科技项目申报ID"));
		} else {
			res.setData(techProjectLogService.selectTechProjectLogByPid(pid));
		}
		return res;
	}

	/**
	 * update科技项目
	 * 
	 * @param t
	 * @param l
	 * @param recordTimeFormattedDate
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/updateTechProject", method = RequestMethod.POST)
	public Result updateTechProject(@Valid InputTechProject techProject, BindingResult bindingResult,
			String recordTimeFormattedDate) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					TechProjectFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(techProject.getId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到科技申报项目", "科技项目申报ID"));
			return res;
		}

		TechProject t = techProjectService.selectByPrimaryKey(techProject.getId());
		if (null == t) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "找不到申报项目", "科技项目申报"));
			return res;
		}

		if (TechProjectStatus.CALLBACK.getCode() == t.getState()) {
			res.getError().add(buildError(ErrorConstants.RECORD_CALLBACK, "当前记录已退单，无法修改！"));
			return res;
		}

		if (TechProjectStatus.SETTLEMENT.getCode() == t.getState()) {
			res.getError().add(buildError(ErrorConstants.RECORD_SETTLEMENT, "当前记录已结款，无法修改！"));
			return res;
		}

		Date recordTime = null;
		if (!StringUtils.isBlank(recordTimeFormattedDate)) {
			try {
				recordTime = DateUtils.parseDate(recordTimeFormattedDate, AFTConstants.YYYYMMDDHHMMSS);
			} catch (ParseException e) {
			}
		}

		TechProject tp = new TechProject();
		TechProjectLog tpl = new TechProjectLog();
		BeanUtils.copyProperties(techProject, tp);
		BeanUtils.copyProperties(techProject, tpl);
		res.setData(techProjectService.updateTechProject(tp, tpl, recordTime));
		if (techProject.getState() != null && !techProject.getState().equals(t.getState())
				&& techProject.getState() <= TechProjectStatus.CALLBACK.getCode()
				&& techProject.getState() >= TechProjectStatus.SUBMIT.getCode()) {
			easemobUtils.sendMessage(EasemonSysUsers.ADMIN_DDDT.getUsername(),
					userService.selectNumberByPrimaryKey(t.getUid()), "您的科技项目申报订单：#%s%s，状态变更为%s", t.getSerialNumber(),
					t.getProjectName() == null ? "" : t.getProjectName(),
					TechProjectStatus.getStatus(techProject.getState()).getDesc());
		}
		return res;
	}

	/**
	 * 流转状态下拉
	 * 
	 * @return
	 */
	@RequestMapping(value = "/status", method = RequestMethod.GET)
	public Result Status() {
		Result res = new Result();
		res.setData(disposeStatus());
		return res;
	}

	/**
	 * 获取科技部门
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getDepartment", method = RequestMethod.GET)
	public Result getDepartment(String uid) {
		Result res = new Result();
		res.setData(techWebsiteService.getDepartment(uid));
		return res;
	}

	/**
	 * 删除科技项目记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteTechProject", method = RequestMethod.POST)
	public Result deleteTechProject(@RequestParam(name = "ids[]", required = false) String[] ids) {
		Result res = new Result();
		if (ids == null || ids.length < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
		} else {
			res.setData(techProjectService.deleteByPrimaryKey(Arrays.asList(ids)));
		}
		return res;
	}

	/**
	 * 删除科技网址记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteTechWebsite", method = RequestMethod.POST)
	public Result deleteTechWebsite(@RequestParam(name = "ids[]", required = false) String[] ids) {
		Result res = new Result();
		if (ids == null || ids.length < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
		} else {
			res.setData(techWebsiteService.deleteByPrimaryKey(Arrays.asList(ids)));
		}
		return res;
	}

	/**
	 * 科技单位网址管理list
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/listTechWebsite", method = RequestMethod.POST)
	public Result listTechWebsite(Integer province, String unitName, String pageNo, String pageSize) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}

		Pagination<TechWebsiteListBo> t = techWebsiteService.listTechWebsite(province, unitName, pNo, pSize);
		if (null != t) {
			List<TechWebsiteListBo> l = (List<TechWebsiteListBo>) t.getList();
			for (TechWebsiteListBo w : l) {
				if (!StringUtils.isBlank(w.getPassword())) {
					try {
						w.setPassword(AesUtils.decrypt(w.getPassword(), aesSecretKey));
					} catch (Exception e) {
					}
				}
			}
			t.setList(l);
			res.setData(t);
		}
		return res;
	}

	/**
	 * 网址管理详情
	 * 
	 * @param wid
	 * @return
	 */
	@RequestMapping(value = "/techWebsiteDetail", method = RequestMethod.POST)
	public Result techWebsiteDetail(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "公司"));
		} else {
			TechWebsiteDetailBo w = techWebsiteService.selectTechWebsiteDetail(id);
			if (null != w && !StringUtils.isBlank(w.getPassword())) {
				try {
					w.setPassword(AesUtils.decrypt(w.getPassword(), aesSecretKey));
				} catch (Exception e) {
				}
			}

			/*
			 * if (null != w && !StringUtils.isBlank(w.getPassword())) {
			 * w.setPassword(Base64Utils.decodeData(w.getPassword())); }
			 */
			res.setData(w);
		}
		return res;
	}

	/**
	 * 科技单位网址add+update
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/disposeTechWebsite", method = RequestMethod.POST)
	public Result disposeTechWebsite(@Valid InputTechWebsite w, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					TechProjectFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(w.getUid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}

		User curUser = userService.selectByPrimaryKey(w.getUid());
		if (!checkCertify(res, curUser)) {
			return res;
		}

		if (null != w && null != (w.getPassword())) {
			try {
				w.setPassword(AesUtils.encrypt(w.getPassword().trim(), aesSecretKey));
			} catch (Exception e) {
			}
		}

		TechWebsite tw = new TechWebsite();
		BeanUtils.copyProperties(w, tw);

		res.setData(techWebsiteService.saveWebsite(tw));
		return res;
	}

	/**
	 * 科技项目申报材料上传
	 * 
	 * @param req
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result cognizanceFile(HttpServletRequest req, String uid, String id, String sign) {
		Result res = new Result();
		User curUser = userService.selectByPrimaryKey(uid);
		if (!checkCertify(res, curUser)) {
			return res;
		}

		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到科技申报项目", "科技项目申报ID"));
			return res;
		}

		TechProject t = techProjectService.selectByPrimaryKey(id);
		if (null == t) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "找不到申报项目", "科技项目申报"));
			return res;
		}

		if (TechProjectStatus.CALLBACK.getCode() == t.getState()) {
			res.getError().add(buildError(ErrorConstants.RECORD_CALLBACK, "当前记录已退单，无法修改！"));
			return res;
		}

		if (TechProjectStatus.SETTLEMENT.getCode() == t.getState()) {
			res.getError().add(buildError(ErrorConstants.RECORD_SETTLEMENT, "当前记录已结款，无法修改！"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);

		if (attachmentType == AttachmentType.TECH_PROJECT) {
			res.setData(handleFiles(res, "/techProject/", true, req, sign, uid));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}

		return res;
	}

	/**
	 * 下载科技项目材料
	 * 
	 * @param response
	 * @param fileName
	 * @param path
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public Result download(HttpServletResponse response, String id) {
		Result res = new Result();

		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技项目ID"));
			return res;
		}

		TechProject t = techProjectService.selectByPrimaryKey(id);
		if (null == t) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技项目ID"));
			return res;
		}

		downloadFile(response, t.getApprovalDownloadFileName(), t.getApprovalUrl());
		return res;
	}

	private Object disposeStatus() {
		Map<String, String> status = new TreeMap<String, String>();
		if (TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
			for (TechProjectStatus p : TechProjectStatus.values()) {
				status.put(p.getCode().toString(), p.getDesc());
				status.remove(TechProjectStatus.OTHER.getCode().toString());
			}
		} else {
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.CREATE.getCode())) {
				status.put(TechProjectStatus.CREATE.getCode().toString(), TechProjectStatus.CREATE.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.SIGN.getCode())) {
				status.put(TechProjectStatus.SIGN.getCode().toString(), TechProjectStatus.SIGN.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.DELIVERD.getCode())) {
				status.put(TechProjectStatus.DELIVERD.getCode().toString(), TechProjectStatus.DELIVERD.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.CIRCULATION.getCode())) {
				status.put(TechProjectStatus.CIRCULATION.getCode().toString(), TechProjectStatus.CIRCULATION.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.SUBMIT.getCode())) {
				status.put(TechProjectStatus.SUBMIT.getCode().toString(), TechProjectStatus.SUBMIT.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.ACCEPT.getCode())) {
				status.put(TechProjectStatus.ACCEPT.getCode().toString(), TechProjectStatus.ACCEPT.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.APPRVOVED.getCode())) {
				status.put(TechProjectStatus.APPRVOVED.getCode().toString(), TechProjectStatus.APPRVOVED.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.REJECT.getCode())) {
				status.put(TechProjectStatus.REJECT.getCode().toString(), TechProjectStatus.REJECT.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.SETTLEMENT.getCode())) {
				status.put(TechProjectStatus.SETTLEMENT.getCode().toString(), TechProjectStatus.SETTLEMENT.getDesc());
			}
			if (TokenManager.hasPermission("TechProjectStatus" + TechProjectStatus.CALLBACK.getCode())) {
				status.put(TechProjectStatus.CALLBACK.getCode().toString(), TechProjectStatus.CALLBACK.getDesc());
			}
		}
		return status;
	}
	
	/** 查询立项订单 **/
	@RequestMapping(value = "/listSetUpServiceOrder", method = RequestMethod.POST)
	public Result listSetUpServiceOrder(String buyerName,String salesmanName, String financeName,
			String depId,Integer approval, Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.listSetUpServiceOrder(buyerName, salesmanName, financeName, depId, approval, pageNo, pageSize));
		return res;
	}
	
	
	/**
	 * 项目列表
	 * @param bo
	 * @param depId
	 * @param orderStartTime
	 * @param orderEndTime
	 * @param setUpStartDate
	 * @param setUpEndDate
	 * @return
	 */
	@RequestMapping(value = "/listProject",method = RequestMethod.POST)
	public Result listProject(ProjectListBo bo,String depId,
			String orderStartDate,String orderEndDate,String setUpStartDate,String setUpEndDate,Integer pageNo,Integer pageSize){
		Result res = new Result();
		res.setData(techProjectService.listProject(bo, depId, orderStartDate, orderEndDate, setUpStartDate, setUpEndDate,pageNo,pageSize));
		return res;
	}
	
	
	/**
	 * 查询项目详情
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/getProjectDetail",method = RequestMethod.GET)
	public Result getProjectDetail(String contractId,Integer buyerType){
		Result res = new Result();
		if(StringUtils.isBlank(contractId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目ID"));
			return res;
		}
		if(buyerType == null){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "客户类型"));
			return res;
		}
		res.setData(techProjectService.selectTechProjectDetail(contractId,buyerType));
		return res;
	}
	
	/**
	 * 查看项目任务
	 * @param contractId
	 * @return
	 */
	@RequestMapping(value = "/getProjectTask",method = RequestMethod.GET)
	public Result getProjectTask(String contractId){
		Result res = new Result();
		if(StringUtils.isBlank(contractId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目ID"));
			return res;
		}
		res.setData(techProjectService.selectProjectTask(contractId));
		return res;
	}
	

	

	
	/**
	 * 查询任务详情
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/getProjectTaskDetail",method = RequestMethod.GET)
	public Result getProjectTaskDetail(String taskId){
		Result res = new Result();
		if(StringUtils.isBlank(taskId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号"));
			return res;
		}
		res.setData(techProjectService.selectProjectTaskDetail(taskId));
		return res;
	}
	
	/**
	 * 修改项目任务
	 * @param taskId
	 * @param commodityId
	 * @param commodityMode
	 * @param taskComment
	 * @return
	 */
	@RequestMapping(value = "/updateProjectTask",method = RequestMethod.POST)
	public Result updateProjectTask(String taskId,String commodityId,String commodityMode,String taskComment){
		Result res = new Result();
		if(StringUtils.isBlank(taskId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号"));
			return res;
		}
		ContractTask task = techProjectService.selectTaskById(taskId);
		if(task != null){
			if(task.getTaskStatus() != TaskState.UN_DISTRIBUTION.getCode())
				res.getError().add(buildError(ErrorConstants.NOT_ACCORD_WITH_NEXT, ""));
		}
		task.setCommodityId(commodityId);
		task.setTaskComment(taskComment);
		task.setCommodityMode(commodityMode);
		techProjectService.updateProjectTask(task);
		return res;
	}
	
	
	/**
	 * 
	 * @param projectNo 项目编号
	 * @param buyerName 客户名称
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @param taskStatus 任务状态
	 * @param categoryId 业务分类
	 * @return
	 */
	@RequestMapping(value = "/listMyTask",method = RequestMethod.POST)
	public Result listMyTask(String projectNo,String buyerName,String startDate,String endDate,String publishStatus, Integer taskStatus,String categoryId,
			Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(techProjectService.selectMyProjectTask(projectNo, buyerName, TokenManager.getAdminId(), "", 
				startDate, endDate, taskStatus, categoryId,null,publishStatus, pageNo, pageSize));
		return res;
	}
	
	/**
	 * 	项目任务查询
	 * @param projectNo 项目编号
	 * @param buyerName 客户名称 
	 * @param taskReceiverName 任务负责人
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @param taskStatus 任务状态
	 * @param categoryId 任务分类
	 * @param pageNo 
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/listTask",method = RequestMethod.POST)
	public Result listTask(String projectNo,String buyerName,String taskReceiverName, String startDate,String endDate, 
			Integer taskStatus,String categoryId,String deleteSign,String publishStatus, Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(techProjectService.selectProjectTask(projectNo, buyerName, "", taskReceiverName, startDate, endDate, 
				taskStatus, categoryId,deleteSign,publishStatus, pageNo, pageSize));
		return res;
	}
	
	
	/**
	 * 启动资料
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/startTask",method = RequestMethod.GET)
	public Result startTask(String taskId){
		Result res = new Result();
		if(StringUtils.isBlank(taskId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号"));
			return res;
		}
		ContractTask task = techProjectService.selectTaskById(taskId);
		if(task != null){
			if(task.getTaskStatus() != TaskState.DISTRIBUTION.getCode()){
				res.getError().add(buildError(ErrorConstants.NOT_ACCORD_WITH_NEXT, ""));
				return res;
			}
			if(task.getDeletedSign() != DeleteStatus.UNDELETE.getCode()){
				res.getError().add(buildError(ErrorConstants.NOT_ACCORD_WITH_NEXT, ""));
				return res;
			}
			task.setTaskStatus(TaskState.STARTED.getCode());
			techProjectService.updateProjectTask(task);
		}
		return res;
	}
	
	/**
	 * 完成任务
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/completeTask",method = RequestMethod.GET)
	public Result completeTask(String taskId){
		Result res = new Result();
		if(StringUtils.isBlank(taskId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号"));
			return res;
		}
		ContractTask task = techProjectService.selectTaskById(taskId);
		if(task != null){
			if(task.getTaskStatus() != TaskState.STARTED.getCode()){
				res.getError().add(buildError(ErrorConstants.NOT_ACCORD_WITH_NEXT, ""));
				return res;
			}
			if(task.getDeletedSign() != DeleteStatus.UNDELETE.getCode()){
				res.getError().add(buildError(ErrorConstants.NOT_ACCORD_WITH_NEXT, ""));
				return res;
			}
			task.setTaskStatus(TaskState.COMPLETE.getCode());
			task.setFinishTime(new Date());
			techProjectService.updateProjectTask(task);
		}
		return res;
	}
	
	/**
	 * 转交任务
	 * @param taskId
	 * @param receiverId
	 * @return
	 */
	@RequestMapping(value = "/transferTask", method = RequestMethod.GET)
	public Result transferTask(String taskId,String taskReceiverId){
		Result res = new Result();
		if(StringUtils.isBlank(taskId) || StringUtils.isBlank(taskReceiverId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务和转交人"));
			return res;
		}
		ContractTask task = new ContractTask();
		task.setId(taskId);
		task.setTaskReceiver(taskReceiverId);
		techProjectService.updateProjectTask(task);
		return res;
	}
	

	
	/**
	 * 锁定、解锁、废除任务
	 * @param taskId
	 * @param operatorType
	 * @return
	 */
	@RequestMapping(value = "/lockOrRevokeOrder", method = RequestMethod.GET)
	public Result lockOrRevokeOrder(String taskId,Integer operatorType){
		Result res = new Result();
		if(StringUtils.isBlank(taskId) || operatorType == null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号和操作类型"));
			return res;
		}
		ContractTask task = techProjectService.selectTaskById(taskId);
		if(task.getDeletedSign() == DeleteStatus.UNDELETE.getCode() && operatorType == 2){ //锁定
			task.setDeletedSign(DeleteStatus.LOCKING.getCode());
		}else if(task.getDeletedSign() == DeleteStatus.LOCKING.getCode() && operatorType == 0){ //解锁
			task.setDeletedSign(DeleteStatus.UNDELETE.getCode());
		}else if(task.getDeletedSign() == DeleteStatus.UNDELETE.getCode() && operatorType == 1){ //废除
			task.setDeletedSign(DeleteStatus.DELETED.getCode());
		}
		techProjectService.updateProjectTask(task);
		return res;
	}
	

	

	
	/**
	 * 项目任务公示修改
	 * @param taskId
	 * @param commodityId
	 * @param commodityMode
	 * @param taskComment
	 * @return
	 */
	@RequestMapping(value = "/updatePublishStatus",method = RequestMethod.POST)
	public Result updateProjectTask(String taskId,String publishStatus){
		Result res = new Result();
		if(StringUtils.isBlank(taskId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "任务编号"));
			return res;
		}
		ContractTask task = techProjectService.selectTaskById(taskId);
		if(task != null){
			if(task.getTaskStatus() != TaskState.COMPLETE.getCode()){
				res.getError().add(buildError( "任务状态","操作材料未完成"));
			return res;}
		}
		task.setPublishStatus(Integer.valueOf(publishStatus));
		if (publishStatus.equals("0")) {
		task.setPublishTime(new Date());
		}
		techProjectService.updateProjectTask(task);
		return res;
	}
	
	/**
	 * 营销员查询项目列表
	 * @param bo
	 * @param depId
	 * @param orderStartTime
	 * @param orderEndTime
	 * @param setUpStartDate
	 * @param setUpEndDate
	 * @return
	 */
	@RequestMapping(value = "/salesmanListProject",method = RequestMethod.POST)
	public Result salesmanListProject(ProjectListBo bo,String depId,
			String orderStartDate,String orderEndDate,String setUpStartDate,String setUpEndDate,Integer pageNo,Integer pageSize){
		Result res = new Result();
		res.setData(techProjectService.salesmanListProject(bo, depId, orderStartDate, orderEndDate, setUpStartDate, setUpEndDate,pageNo,pageSize));
		return res;
	}
}
