package com.goafanti.demand.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.goafanti.admin.service.AftFileService;
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.DemandAuditStatus;
import com.goafanti.common.model.AftFile;
import com.goafanti.common.model.Demand;
import com.goafanti.common.model.DemandPublish;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.demand.service.DemandOrderService;
import com.goafanti.demand.service.DemandPublishPageService;
import com.goafanti.demand.service.DemandPublishService;
import com.goafanti.demand.service.DemandService;
import com.goafanti.user.service.UserService;

@RestController
@RequestMapping(value = "/api/admin/demand")
public class AdminDemandApiController extends CertifyApiController {
	@Resource
	private DemandService		demandService;
	@Resource
	private UserService			userService;
	@Resource
	private AftFileService		aftFileService;
	@Resource
	private DemandOrderService	demandOrderService;
	@Resource
	DemandPublishService	demandPublishService;
	@RequestMapping(value = "/matchAchievement", method = RequestMethod.POST)
	public Result matchAchievement(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "成果ID"));
			return res;
		}
		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d|| !DeleteStatus.UNDELETE.getCode().equals(d.getDeletedSign())
				|| !DemandAuditStatus.AUDITED.getCode().equals(d.getAuditStatus())) {
			res.getError().add(buildError("", "当前状态无法匹配！"));
			return res;
		}
		res.setData(demandService.updateMatchAchievement(d));
		return res;
	}

	
	/**
	 * 下载技术需求批量导入Excel模板
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
	public Result downloadTemplateFile(HttpServletResponse response, String sign) {
		Result res = new Result();
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.DEMAND_TEMPLATE) {
			String fileName = "";
			AftFile af = aftFileService.selectAftFileBySign(sign);
			if (null == af) {
				res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件！"));
			} else {
				String path = af.getFilePath();
				String suffix = path.substring(path.lastIndexOf("."));
				fileName = AttachmentType.DEMAND_TEMPLATE.getDesc() + suffix;
				downloadFile(response, fileName, path);
			}
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 个人用户--需求列表
	 */
	@RequestMapping(value = "/userList", method = RequestMethod.GET)
	public Result userList(String pageNo, String pageSize) {
		Result res = new Result();
		//res.setData(null); TODO
		return res;
	}

	/**
	 * 组织用户--需求列表(个人组织合并)
	 */
	@RequestMapping(value = "/orgList", method = RequestMethod.GET)
	public Result orgList(String pageNo, String pageSize) {
		Result res = new Result();
		//res.setData(null); TODO
		return res;
	}

	/**
	 * 需求列表
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result list(String name,String employerName,Integer demandType,
			Integer auditStatus,Integer status,String startDate, String endDate,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(demandService.listDemand(name, employerName, demandType, auditStatus, status,startDate,endDate, pageNo, pageSize));
		return res;
	}
	
	/**
	 * 个人需求详情
	 */
	@RequestMapping(value = "/userDemandDetail", method = RequestMethod.GET)
	public Result userDemandDetail(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		res.setData(demandService.selectUserDemandDetail(id));
		return res;
	}

	/**
	 * 组织用户详情(个人组织合并)
	 */
	@RequestMapping(value = "/orgDemandDetail", method = RequestMethod.GET)
	public Result orgDemandDetail(String id,Integer dataCategory) {
		Result res = new Result();

		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		if(dataCategory==0){
			res.setData(demandService.selectUserDemandDetail(id));
			return res;
		}else if(dataCategory==1){
			res.setData(demandService.selectOrgDemandDetail(id));
			return res;
		}
		return res;
	}
	
	/**
	 * 需求详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/demandDetail", method = RequestMethod.GET)
	public Result demandDetail(String id){
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		res.setData(demandService.selectDemandDetail(id));
		return res;
	}
	
	/**
	 * 审核需求
	 * @param id
	 * @param auditResult
	 * @return
	 */
	
	@RequestMapping(value = "/auditDemand", method = RequestMethod.GET)
	public Result auditDemand(String id, Integer auditResult,String auditInfo){
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}
		if(d.getAuditStatus() != DemandAuditStatus.INAUDIT.getCode()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求未达审核条件"));
			return res;
		}
		if(AFTConstants.NO == auditResult && StringUtils.isBlank(auditInfo)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需填写审核意见"));
			return res;
		}
		if(AFTConstants.YES == auditResult){
			d.setAuditStatus(DemandAuditStatus.AUDITED.getCode());
		}else if(AFTConstants.NO == auditResult){
			d.setAuditStatus(DemandAuditStatus.UNAUDITED.getCode());
		}
		if(StringUtils.isNotBlank(auditInfo)) d.setAuditInfo(auditInfo);
		demandService.updateByPrimaryKeySelective(d);
		return res;
	}
	
	/**
	 * 需求管理--获取个人用户下拉
	 */
	@RequestMapping(value = "/userNames", method = RequestMethod.GET)
	public Result getUserNames() {
		Result res = new Result();
		res.setData(userService.selectDemandUserNames());
		return res;
	}

	/**
	 * 需求管理--获取组织用户下拉
	 */
	@RequestMapping(value = "/unitNames", method = RequestMethod.GET)
	public Result getUnitNames() {
		Result res = new Result();
		res.setData(userService.selectDemandUnitNames());
		return res;
	}

	/**
	 * 需求资料--图片上传
	 */
	@RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
	public Result uploadPicture(HttpServletRequest req, String sign, String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户ID", "用户ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);

		if (attachmentType == AttachmentType.DEMAND_PICTURE|| attachmentType == AttachmentType.DEMAND_COVER_PICTURE) {
			res.setData(handleFiles(res, "/demand/", false, req, sign, uid));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}

		return res;
	}

	/**
	 * 需求资料--文本文件上传
	 */
	@RequestMapping(value = "/uploadTextFile", method = RequestMethod.POST)
	public Result uploadTextFile(HttpServletRequest req, String sign, String uid) {
		Result res = new Result();

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

		AttachmentType attachmentType = AttachmentType.getField(sign);

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

	/**
	 * 需求撤消发布(下架)
	 */
	@RequestMapping(value = "/offShelf", method = RequestMethod.POST)
	public Result offShelf(String id,Integer auditStatus) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}

		Demand d = demandService.selectByPrimaryKey(id);

		if (null == d) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}
		if(DemandAuditStatus.AUDITED.getCode() != auditStatus){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法撤销发布"));
			return res;
		}
		d.setReleaseStatus(auditStatus);
		res.setData(demandService.updateReleaseStatus(d));
		return res;
	}

	/**
	 * 下载需求文件--文本文件
	 */
	@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;
		}

		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}

		downloadUnPrivateFile(response, d.getTextFileDownloadFileName(), d.getTextFileUrl());
		return res;
	}

	/**
	 * 我的需求列表
	 */
	@RequestMapping(value = "/myList", method = RequestMethod.GET)
	public Result myList(String pageNo, String pageSize) {
		Result res = new Result();
		// res.setData(null) todo;
		return res;
	}
	
	/**
     * 需求发布
     */
    @RequestMapping(value = "/addDemandPublish", method = RequestMethod.POST)
    private Result addDemandPublish(DemandPublish d) {
    	Result res = new Result();
    	if (StringUtils.isBlank(d.getDemandId())) {
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求", "需求"));
            return res;
        }
        res.setData(demandPublishService.insertDemandPublish(d));     
        return res;
    }
    /**
     * 撤销发布
     */
    @RequestMapping(value = "/deletePublish", method = RequestMethod.GET)
    private Result deletePublish(String id) {
        Result res = new Result();
        res.setData(demandPublishService.deletePublish(id));     
        return res;
    }
    /**
     * 修改发布
     */
    @RequestMapping(value = "/updatePublish", method = RequestMethod.GET)
    private Result updatePublish(DemandPublish d) {
        Result res = new Result();
        res.setData(demandPublishService.updatePublish(d));     
        return res;
    }
    
    /**
     * 发布列表
     */
    @RequestMapping(value = "/listPublish", method = RequestMethod.GET)
    private Result listPublish(String name,String publishPlatform,Integer publishClient,String publishPage,
    		Integer ifTop, Integer pageNo, Integer pageSize,String employerName) {
        Result res = new Result();
        if (null==pageNo) {
			pageNo=1;
		}
        if (null==pageSize) {
			pageSize=10;
		}
        res.setData(demandPublishService.listPublish( name, publishPlatform, publishClient, publishPage,
        		 ifTop,  pageNo,  pageSize,employerName));     
        return res;
    }
    /**
     * 获取需求页面位置
     */
    @RequestMapping(value="/getPublishPage",method = RequestMethod.GET)
    private Result getPublishPage(){
    	Result res=new Result();
    	return res.data(DemandPublishPageService.getBranchInformation());
    }
   
}
