package com.goafanti.order.controller;





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

import org.apache.commons.lang3.StringUtils;
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.OutsourceOrganization;
import com.goafanti.common.model.TOrderOutsource;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.OrderOutsourceDtails;
import com.goafanti.order.enums.OrderNewState;
import com.goafanti.order.service.OrderNewService;
import com.goafanti.order.service.OutsourceOrgService;

@RestController
@RequestMapping(value = "/api/admin/outsourceOrg")
public class OutsourceOrgApiController extends CertifyApiController {

	@Resource
	private OutsourceOrgService	outsourceOrgService;
	@Resource
	private OrderNewService	orderNewService;

	@RequestMapping(value = "/addOutsourceOrg", method=RequestMethod.POST)
	public Result addOutsourceOrg(OutsourceOrganization o) {
		Result res=new Result();
		if (StringUtils.isBlank(o.getName())||
				StringUtils.isBlank(o.getContacts())||
					StringUtils.isBlank(o.getContactsMobile())) {
			res.getError().add(buildError("", "名称、联系人、电话不能为空"));
			return res;
		}
		res.data(outsourceOrgService.addOutsourceOrg(o));
		return res;
	}
	@RequestMapping(value = "/updateOutsourceOrg", method=RequestMethod.POST)
	public Result updateOutsourceOrg(OutsourceOrganization o) {
		Result res=new Result();
		if (StringUtils.isBlank(o.getName())||
				StringUtils.isBlank(o.getContacts())||
					StringUtils.isBlank(o.getContactsMobile())) {
			res.getError().add(buildError("", "名称、联系人、电话不能为空"));
			return res;
		}
		res.data(outsourceOrgService.updateOutsourceOrg(o));

		return res;
	}
	@RequestMapping(value = "/deleteOutsourceOrg", method=RequestMethod.POST)
	public Result deleteOutsourceOrg(Integer id) {
		Result res=new Result();
		if (id==null) {
			res.getError().add(buildError("", "必须选择外包公司"));
			return res;
		}
		res.data(outsourceOrgService.deleteOutsourceOrg(id));
		return res;
	}
	@RequestMapping(value = "/selectOutsourceOrg", method=RequestMethod.GET)
	public Result selectOutsourceOrg(String  orderNo,String tid) {
		Result res=new Result();
		if (orderNo==null) {
			res.getError().add(buildError("", "必须选择订单"));
			return res;
		}
		res.data(outsourceOrgService.selectOutsourceOrg(orderNo,tid));
		return res;
	}
	/** 外包文件上传 **/
	@RequestMapping(value = "/uploadOutsourceFile", method = RequestMethod.POST)
	public Result uploadOutsourceFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_outsource/", false, req, sign));
		return res;
	}
	/**
	 * 订单外包列表
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/selectOrderOutsourceOrg", method=RequestMethod.GET)
	public Result selectOrderOutsourceOrg(String  orderNo) {
		Result res=new Result();
		if (orderNo==null) {
			res.getError().add(buildError("", "必须选择订单"));
			return res;
		}
		res.data(outsourceOrgService.selectOrderOutsourceOrg(orderNo));
		return res;
	}

	/**
	 * 订单发起外包
	 */
	@RequestMapping(value="/auditOutsource", method = RequestMethod.POST)
	public Result auditOutsource(TOrderOutsource o,Integer orderStatus){
		Result res=new Result();
		if(StringUtils.isBlank(o.getOrderNo())||StringUtils.isBlank(o.getCompanyName())||
				o.getAmount()==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "外包信息","外包信息"));
			return res;
		}
		if(orderStatus==null||(orderStatus!=OrderNewState.QDSHTG.getCode()&&orderStatus!=OrderNewState.QDSHJJ.getCode())||
				orderNewService.chekeOrderStatus(o.getOrderNo())){
			res.getError().add(buildError( "审核状态错误","审核状态错误"));
			return res;
		}
		return res.data(orderNewService.updateOrderNew(o.getOrderNo(),orderStatus,null,1,o));
	}

	/**
	 * 项目发起外包（发起外包项目）
	 */
	@RequestMapping(value="/outsourceProjectAudit", method = RequestMethod.POST)
	public Result outsourceProjectAudit(TOrderOutsource o){
		Result res=new Result();
		if(StringUtils.isBlank(o.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "外包信息","外包信息"));
			return res;
		}
		if(o.getType()!=1||o.getTid()==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "项目外包信息","项目外包信息"));
			return res;
		}
		//项目发起并且不是外包。
		if(o.getType()==1&&o.getStartType()==1&&
				orderNewService.checkOfficialType(o)){
			res.getError().add(buildError(ErrorConstants.DATA_EMPTY_ERROR, "后台官费分类","后台官费分类"));
			return res;
		}
		return res.data(orderNewService.pushOutsourceProjectAudit(o));
	}

	/**
	 * 外包审核列表
	 */
	@RequestMapping(value="/orderOutsourceList", method = RequestMethod.GET)
	public Result orderOutsourceList(String name,String contractNo,String starTime,String endTime,Integer tid,
			Integer refundStatus,Integer type,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.data(outsourceOrgService.orderOutsourceList(name, contractNo, starTime, endTime,tid,refundStatus,type, pageNo, pageSize));
		return res;
	}

	/**
	 * 外包审核列表(营销管理员列表)
	 */
	@RequestMapping(value="/salesmanOrderOutsourceList", method = RequestMethod.GET)
	public Result salesmanOrderOutsourceList(String name,String contractNo,String starTime,String endTime,Integer refundStatus,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.data(outsourceOrgService.salesmanOrderOutsourceList(name, contractNo, starTime, endTime,refundStatus, pageNo, pageSize));
		return res;
	}
	/**
	 * 外包审核详情
	 */
	@RequestMapping(value="/orderOutsourceDtails", method = RequestMethod.GET)
	public Result orderOutsourceDtails(String orderNo,String tid){
		Result res=new Result();
		res.data(outsourceOrgService.orderOutsourceDtails(orderNo,tid));
		return res;
	}
	/**
	 * 外包审核
	 */
	@RequestMapping(value="/auditOutsourceNew", method = RequestMethod.POST)
	public Result auditOutsourceNew(OrderOutsourceDtails o){
		Result res=new Result();
		if (o.getId()==null||null==o.getRefundStatus()||StringUtils.isBlank(o.getRemarks())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "外包信息","外包信息"));
			return res;
		}
		res.data(outsourceOrgService.updateAuditOutsource(o));
		return res;
	}

	/**
	 * 外包审核日志
	 */
	@RequestMapping(value="/selectOutsourceLog", method = RequestMethod.GET)
	public Result selectOutsourceLog(Integer tid){
		Result res=new Result();
		if (tid==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "项目id","项目id"));
			return res;
		}
		res.data(outsourceOrgService.selectOutsourceLog(tid));
		return res;
	}
	 /**
     *	 下载文档
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public Result downloadTemplateFile(HttpServletResponse response, String path) {
        Result res = new Result();
            if (null == path) {
                res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件！"));
            } else {
               String suffix = path.substring(path.lastIndexOf("."));
               String name=path.substring(path.lastIndexOf("_")+1, path.lastIndexOf("."));
                String fileName = name + suffix;
                downloadFile(response, fileName, path);
        }
        return res;
    }


    /**
	 * 	超级管理员驳回
	 */
	@RequestMapping(value="/superAdminReject", method = RequestMethod.POST)
	public Result superAdminReject(Integer id){
		Result res=new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "外包编号","外包编号"));
			return res;
		}
		if(!TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
			res.getError().add(buildError("","本驳回只允许超级管理员执行！"));
			return res;
		}
		res.data(outsourceOrgService.updateSuperAdminReject(id));
		return res;
	}


}
