package com.goafanti.order.controller;



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

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.TOrderNewWithBLOBs;
import com.goafanti.common.model.TOrderRefundWithBLOBs;
import com.goafanti.common.model.TOrderTask;
import com.goafanti.order.bo.InputArrearsDunListBo;
import com.goafanti.order.enums.ApprovalNewState;
import com.goafanti.order.enums.OrderNewState;
import com.goafanti.order.service.OrderNewService;

@RestController
@RequestMapping(value = "/api/admin/newOrder")
public class AdminNewOrderApiController extends CertifyApiController {
	
	@Autowired
	private OrderNewService orderNewService;
	/**
	 * 创建订单
	 * @param uid
	 * @param orderType
	 * @return
	 */
	@RequestMapping(value = "/createOrder",method = RequestMethod.POST)
	public Result createOrder(String uid,Integer orderType){
		Result res = new Result();
		if(StringUtils.isBlank(uid)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","客户编号"));
			return res;
		}
		if(orderType == null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单类型"));
			return res;
		}
		res.data(orderNewService.createServiceOrder(uid,orderType));
		return res;
	}
	/**
	 * 添加项目
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addOrderTask",method = RequestMethod.POST)
	public Result addOrderTask(TOrderTask t){
		Result res = new Result();
		if(StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if (t.getOrderNo()!=null&&orderNewService.checkOrderMain(t)) {
			res.getError().add(buildError("订单主要任务已存在","订单主要任务已存在"));
			return res;
		}
		return res.data(orderNewService.addOrderTask(t));
	}
	/**
	 * 修改项目
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateOrderTask",method = RequestMethod.POST)
	public Result updateOrderTask(TOrderTask t){
		Result res=new Result();
		if(null==t.getId()){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","项目编号"));
			return res;
		}
		if (t.getOrderNo()!=null&&orderNewService.checkOrderMain(t)) {
			res.getError().add(buildError("订单主要任务已存在","订单主要任务已存在"));
			return res;
		}
		return res.data(orderNewService.updateOrderTask(t));
	}
	/**
	 * 删除订单项目
	 * @param detailId
	 * @return
	 */
	@RequestMapping(value = "/deleteOrderTask",method = RequestMethod.POST)
	public Result deleteOrderTask(Integer id){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","项目编号"));
			return res;
		}
		return res.data(orderNewService.delectOrderTask(id));
	}
	/**
	 * 删除订单
	 * @param detailId
	 * @return
	 */
	@RequestMapping(value = "/deleteOrderNew",method = RequestMethod.POST)
	public Result deleteOrderNew(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		int i=orderNewService.deleteOrderNew(orderNo);
		if (i==-1) {
			res.getError().add(buildError("订单未达删除条件","订单未达删除条件"));
			return res;
		}
		return res.data(i);
	}
	
	/**
	 * 获取订单项目信息
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/getOrderTask",method = RequestMethod.GET)
	public Result getOrderTask(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		res.setData(orderNewService.selectOrderTask(orderNo));
		
		return res;
	}
	/**
	 * 修改订单
	 * @param orderNo
	 * @param firstPayment
	 * @param orderAmount
	 * @param signFirstPayment
	 * @param signTotalAmount
	 * @param approval
	 * @param orderRemarks
	 * @return
	 */
	@RequestMapping(value = "/updateServiceOrderNew",method = RequestMethod.POST)
	public Result updateServiceOrder(TOrderNewWithBLOBs t,String isSubmit,String signDate){
		Result res = new Result();
		if(StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "订单编号"));
			return res;
		}
		if(null==t.getTotalAmount() || null==t.getFirstAmount()){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "订单金额 和 首付金额"));
			return res;
		}
		if(StringUtils.isBlank(t.getContractPictureUrl())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "合同图片"));
			return res;
		}
		if(null==isSubmit){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "提交状态"));
			return res;
		}
		int i=Integer.valueOf(isSubmit);
		if (i==1) {
			if (orderNewService.checkORderTask(t.getOrderNo())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "主项目"));
				return res;
			}
			if (orderNewService.checkORderDun(t.getOrderNo())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "催款信息"));
				return res;
			}
		}
		
		if (StringUtils.isNotBlank(signDate)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				t.setSignTime(sdf.parse(signDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		res.setData(orderNewService.updateServiceOrder(t,i));
		return res;
	}
	
	/** 
	 * 订单详情
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/getOrderNewDetail", method = RequestMethod.GET)
	public Result getOrderNewDetail(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		res.setData(orderNewService.getOrderNewDetail(orderNo));
		return res;
	}
	/**
	 * 收款截点编辑
	 */
	@RequestMapping(value = "/createOrderDun", method = RequestMethod.POST)
	public Result createOrderDun(String orderDun,String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		res.setData(orderNewService.createOrderDun(orderDun,orderNo));
		return res;
	}
	/**
	 * 查看订单收款截点
	 */
	@RequestMapping(value = "/selectOrderDun", method = RequestMethod.GET)
	public Result selectOrderDun(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		res.setData(orderNewService.selectOrderDun(orderNo));
		return res;
	}
	/**
	 * 订单列表
	 * 
	 *specially  0 个人 1 营销员管理 2 驳回 3 特批 4 咨询师管理 5部门订单查询
	 *distribution	（咨询师管理列表 0 未分配 1部分 2全部
	 *amountStatus 0 10万元以下 1 10万~20万 2 20万~30万 3 30万~40万  4 40万以上
	 */
	@RequestMapping(value="/orderNewList", method = RequestMethod.GET)
	public Result orderNewList(String name,String orderNo,String starTime,String endTime ,Integer specially ,Integer approval,
			Integer distribution,String depId,String contractNo,Integer outsource,Integer liquidationStatus,Integer amountStatus,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.data(orderNewService.orderNewList(name, orderNo, starTime, endTime, specially ,approval, distribution,depId, 
				contractNo,outsource, liquidationStatus,amountStatus,pageNo, pageSize));
		return res;
	}
	
	
	/**
	 * 订单审核
	 */
	@RequestMapping(value="/auditOrderNew", method = RequestMethod.POST)
	public Result auditOrderNew(String orderNo,Integer orderStatus,String reason){
		Result res=new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		if(orderStatus==null||(orderStatus!=OrderNewState.QDSHTG.getCode()&&orderStatus!=OrderNewState.QDSHJJ.getCode())||
				orderNewService.chekeOrderStatus(orderNo)){
			res.getError().add(buildError( "审核状态错误","审核状态错误"));
			return res;
		}
		if (orderStatus==OrderNewState.QDSHJJ.getCode()&&StringUtils.isBlank(reason)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"驳回原因", "驳回原因"));
			return res;
		}
		return res.data(orderNewService.updateOrderNew(orderNo,orderStatus,reason,0,null));
	}
	
	
	/**
	 * 催款统计列表
	 */
	@RequestMapping(value="/dunOrderNewList", method = RequestMethod.GET)
	public Result dunOrderNewList(String name,String orderNo,String starTime,String endTime,Integer specially,
			String adminName,String depId,Integer newStatus,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.data(orderNewService.dunOrderNewList(name, orderNo, starTime, endTime,specially,  adminName, depId,newStatus,pageNo, pageSize));
		return res;
	}
	
	/**
	 * 催款统计列表
	 */
	@RequestMapping(value="/arrearsDunList", method = RequestMethod.GET)
	public Result arrearsDunList(InputArrearsDunListBo in){
		Result res=new Result();
		res.data(orderNewService.arrearsDunList(in));
		return res;
	}
	/**
	 * 新增催款记录
	 */
	@RequestMapping(value="/createDunLog", method = RequestMethod.POST)
	public Result createDunLog(String dunId,String dumTime,String remarks){
		Result res=new  Result();
		if (StringUtils.isBlank(dunId)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "催款ID","催款ID"));
			return res;
		}
		if (StringUtils.isBlank(dumTime)||StringUtils.isBlank(remarks)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "时间与描述","时间与描述"));
			return res;
		}
		res.data(orderNewService.createDunLog(dunId, dumTime, remarks));
		return res;
	}
	/**
	 * 催款记录查询
	 */
	@RequestMapping(value="/selectDunLogList", method = RequestMethod.GET)
	public Result selectDunLogList(String dunId){
			Result res=new Result();
			if (StringUtils.isBlank(dunId)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "催款ID","催款ID"));
				return res;
			}
			res.data(orderNewService.selectDunLogList(dunId));
			return res;
	}
	/**
	 * 订单退单
	 */
	@RequestMapping(value="/addOrderRefund", method = RequestMethod.POST)
	public Result addOrderRefund(TOrderRefundWithBLOBs t){
			Result res=new Result();
			if (StringUtils.isBlank(t.getOrderNo())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
				return res;
			}
			if(StringUtils.isBlank(t.getContractUrl())||StringUtils.isBlank(t.getApplicationUrl())){
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "合同与申请表","合同与申请表"));
				return res;
			}
			if (StringUtils.isBlank(t.getReason())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "退单原因","退单原因"));
				return res;
			}
			if(orderNewService.checkOderSettlementAmount(t.getOrderNo())){
				res.getError().add(buildError( "未达退单条件","未达退单条件"));
				return res;
			}
			res.data(orderNewService.addOrderRefund(t));
			return res;
	}
	/**
	 * 退单详情
	 */
	@RequestMapping(value="/orderRefundDetail", method = RequestMethod.GET)
	public Result OrderRefund(String id){
			Result res=new Result();
			if (StringUtils.isBlank(id)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "退单编号","退单编号"));
				return res;
			}
			res.data(orderNewService.orderRefundDetail(id));
			return res;
	}
	/**
	 * 订单退单
	 */
	@RequestMapping(value="/updateOrderRefund", method = RequestMethod.POST)
	public Result updateOrderRefund(TOrderRefundWithBLOBs t){
			Result res=new Result();
			if (null==t.getId()) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "退单编号","退单编号"));
				return res;
			}
			if(StringUtils.isBlank(t.getContractUrl())||StringUtils.isBlank(t.getApplicationUrl())){
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "合同与申请表","合同与申请表"));
				return res;
			}
			if (StringUtils.isBlank(t.getReason())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "退单原因","退单原因"));
				return res;
			}
			res.data(orderNewService.updateOrderRefund(t));
			return res;
	}
	/**
	 * 审批特批订单
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/approvalOrder",method = RequestMethod.POST)
	public Result approvalOrder(String orderNo,Integer confirm,String reason){
		Result res = new Result();
		if (StringUtils.isBlank(orderNo)||confirm==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号和审批","订单编号和审批"));
			return res;
		}
		if (confirm==ApprovalNewState.BH.getCode()&&StringUtils.isBlank(reason)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"驳回原因", "驳回原因"));
			return res;
		}
		if (orderNewService.chekeApprovalOrderStatus(orderNo)) {
			res.getError().add(buildError( "审核状态错误","审核状态错误"));
			return res;
		}
		return res.data(orderNewService.updateApprovalOrder(orderNo,confirm,reason));
	}
	/**
	 * 退单列表
	 */
	@RequestMapping(value="/orderRefundList", method = RequestMethod.GET)
	public Result orderRefundList(String name,String orderNo,String starTime,String endTime,Integer specially,Integer pageNo,Integer pageSize){
		Result res=new Result();
		res.data(orderNewService.orderRefundList(name, orderNo, starTime, endTime,specially, pageNo, pageSize));
		return res;
	}
	
	
	
	
	
	/**
	 * 订单结单
	 */
	@RequestMapping(value="/OrderOver", method = RequestMethod.GET)
	public Result updateOrderOver(String orderNo){
		Result res=new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		int i=orderNewService.updateOrderOver(orderNo);
		if (i==-1) {
			res.getError().add(buildError("不符合结单条件","不符合结单条件"));
			return res;
		}
		res.setData(i);
		return res;
	}
	/**
	 * 咨询师驳回
	 */
	@RequestMapping(value = "/technicianBackOrder", method = RequestMethod.POST)
	public Result technicianBackOrder(String orderNo,String reason){
		Result res = new Result();
		int i=orderNewService.updatetechnicianBackOrder(orderNo, reason);
		if (i==-1) {
			res.getError().add(buildError("不符合驳回条件","不符合驳回条件"));
			return res;
		}
		return res;
	}
	/**
	 * 财务订单转交
	 */
	@RequestMapping(value = "/updateFinance", method= RequestMethod.GET)
	public Result updateFinance(String orderNo,String newFinance) {
		Result res = new Result();
		orderNewService.updateFinance( orderNo, newFinance);
		return res;
	}
	
	/**
	 * 查看订单日志
	 */
	@RequestMapping(value ="/selectOrderLog", method=RequestMethod.GET)
	public Result selectOrderLog (String orderNo) {
		Result res=new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "订单编号","订单编号"));
			return res;
		}
		res.data(orderNewService.selectOrderLog(orderNo));
		return res;
	}
	
	/** 退单文件上传 **/
	@RequestMapping(value = "/uploadRefundOrderFile", method = RequestMethod.POST)
	public Result uploadRefundOrderFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_refund_file/", false, req, sign));
		return res;
	}
	
	
	
	

	
	
	/**
	 * 催款列表下载 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exportOrderDunData", method = RequestMethod.GET)
	public Result exportOrderDunData(String name,String orderNo,String starTime,String endTime,Integer specially,String adminName,
			String depId,Integer newStatus,Integer pageNo,Integer pageSize,HttpServletResponse response){
		 Result res = new Result();
 		try {
 			orderNewService.exportOrderDunData( name, orderNo, starTime, endTime, specially, adminName, depId,newStatus, pageNo, pageSize, response);
		} catch (Exception e) {
			res.getError().add(buildError("格式不正确"));
			return res;
		}
 		return res;
	}
	/**
	 * 	项目列表下载 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exportOrderTaskData", method = RequestMethod.GET)
	public Result exportOrderTaskData(String orderNo,HttpServletResponse response){
		 Result res = new Result();
 		try {
 			orderNewService.exportOrderTaskData(  orderNo,response);
		} catch (Exception e) {
			res.getError().add(buildError("格式不正确"));
			return res;
		}
 		return res;
	}
	
	
	/**
	 * 	项目列表下载 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exportArrearsDunListData", method = RequestMethod.GET)
	public Result exportArrearsDunListData(InputArrearsDunListBo in,HttpServletResponse response){
		 Result res = new Result();
 		try {
 			orderNewService.exportArrearsDunListData(  in,response);
		} catch (Exception e) {
			res.getError().add(buildError("格式不正确"));
			return res;
		}
 		return res;
	}
	
}
