package com.goafanti.order.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.TOrderBack;
import com.goafanti.common.model.TOrderBillNew;
import com.goafanti.common.model.TOrderNew;
import com.goafanti.common.model.TOrderRefund;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.OrderListBo;
import com.goafanti.order.bo.OrderRefundBo;
import com.goafanti.order.service.FundManageOrderService;
import com.goafanti.order.service.OrderBillService;
import com.goafanti.order.service.OrderRefundService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping(value = "/api/admin/financial")
public class FundManagerOrderApiController extends CertifyApiController {
	@Resource
	private FundManageOrderService fundManageOrderServiceImpl;
	@Resource
	private OrderBillService orderBillServiceImpl;
	@Resource
	private OrderRefundService orderRefundServiceImpl;

	/**
	 * 财务管理员查看所有待分配的订单
	 * @param order	>> outsource 0否 1外包
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/allUnassignedOrder",method = RequestMethod.GET)
	public Result allUnassignedOrder(OrderListBo order, Integer pageNo, Integer pageSize){
		Result res = new Result();
		//order.setProcessStatus(2);//订单状态为2   改为前端传
		res.setData(fundManageOrderServiceImpl.allUnassignedOrder(order, pageNo, pageSize));
		return res;
	}

	/**
	 * 财务管理员分派订单
	 * @param financeId
	 * @return
	 */
	@RequestMapping(value="/distributionFinance", method = RequestMethod.POST)
	public Result distributionFinance(String orderNo, String financeId, Integer approval){
		Result res = new Result();
		Integer r = fundManageOrderServiceImpl.updateDistributionFinance(orderNo, financeId, approval);
		if(r == -1){
			res.getError().add(buildError("", "未通过特批不能分配"));
			return res;
		}else if( r == -2 ){
			res.getError().add(buildError("", "订单编号或者财务专员发生异常错误"));
			return res;
		}else{
			res.setData(r);
		}
		return res;
	}

	/**
	 * 财务专员查看分配给自己的订单
	 * @param order >> outsource 0否 1外包
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/financeList", method = RequestMethod.GET)
	public Result financeList(OrderListBo order, Integer pageNo, Integer pageSize ){
		Result res = new Result();
		order.setOrderStatus(2);//营销管理员审核通过
		res.setData(fundManageOrderServiceImpl.financeList(order, pageNo, pageSize));
		return res;
	}

	/**
	 * 财务专员驳回
	 * @param orderBack
	 * @return
	 */
	@RequestMapping(value="/reject", method = RequestMethod.POST)
	public Result reject(TOrderBack orderBack){
		Result res = new Result();
		if(null != orderBack.getReason() && orderBack.getReason().length() > 256){
			res.getError().add(buildError("", "驳回原因长度不能大于256"));
			return res;
		}
		Integer i = fundManageOrderServiceImpl.updateReject(orderBack);
		if(i>0){
			res.setData("驳回处理成功!");
		}else{
			res.getError().add(buildError("", "已经交款不能驳回"));
		}
		return res;
	}

	/**
	 * 	财务收款
	 * 	财务专员新增流水记录并流转订单状态
	 * @param billNew
	 * @return
	 */
	@RequestMapping(value="/addReceiptsFlow", method = RequestMethod.POST)
	public Result addReceiptsFlow(TOrderBillNew billNew,String contractNo){
		Result res = new Result();
		if(null != billNew.getRemarks() && billNew.getRemarks().length() > 255){
			res.getError().add(buildError("", "备注长度不能大于255"));
			return res;
		}

		if (StringUtils.isBlank(contractNo)) {
			res.getError().add(buildError("", "合同编号不能为空!"));
			return res;
		} else if (contractNo.length() > 32) {
			res.getError().add(buildError("", "合同编号长度不能大于32!"));
			return res;
		}
		//流水时间处理
		//if(StringUtils.isNotEmpty(billNew.getFinancialPayTimes())){
			try {
				billNew.setFinancialPayTime(new SimpleDateFormat("yyyy-MM-dd").parse(billNew.getFinancialPayTimes()));
			} catch (ParseException e) {
				e.printStackTrace();
				res.getError().add(buildError("", "财务流水时间有误!"));
				return res;
			}
		//}
		Integer i = fundManageOrderServiceImpl.addReceiptsFlow(billNew,contractNo);
		if(i>0){
			res.setData("新增收款记录成功!");
		}else if(i == -3){
			res.getError().add(buildError("", "订单变更锁定中"));
		}else if(i == -2){
			res.getError().add(buildError("", "订单编号错误!"));
		}else{
			res.getError().add(buildError("", "本次收款金额大于应收金额!"));
		}
		return res;
	}
	/**
	 *  订单审核 财务审核
	 */
	@RequestMapping(value="/financeApproval", method = RequestMethod.POST)
	public Result financeApproval(String orderNo,String remarks,String aid) {
		Result res=new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError("","订单必须指定。"));
		}
		res.data(fundManageOrderServiceImpl.updateFinanceApproval(orderNo,remarks,aid));
		return res;
	}

	/**
	 * 财务专员查看自己要处理的退单数据
	 * @param refund
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/getRefundList", method = RequestMethod.GET)
	public Result getRefundList(OrderRefundBo refund, Integer pageNo, Integer pageSize){
		Result res = new Result();
		refund.setAccept(TokenManager.getAdminId());
		if(null != refund.getRefundStatus()){
			refund.setRefundStatus(refund.getRefundStatus() + 1);
		}
		res.setData(orderRefundServiceImpl.getRefundList(refund, pageNo, pageSize));
		return res;
	}

	/**
	 * 财务专员审核退款
	 * @param refund
	 * @return
	 */
	@RequestMapping(value="/changeRefund", method = RequestMethod.POST)
	public Result changeRefund(TOrderRefund refund){
		Result res = new Result();
		if(null != refund.getResult() && refund.getResult().length() > 256){
			res.getError().add(buildError("", "审批意见长度不能大于256"));
			return res;
		}
		int r = fundManageOrderServiceImpl.updateChangeRefund(refund);
		if(r>0){
			res.setData("审核退款成功!");
		}else{
			res.getError().add(buildError("", "状态异常不能审批!"));
		}
		return res;
	}

	/**
	 * 财务专员新增退款流水
	 * @param orderRefund
	 * @return
	 */
	@RequestMapping(value="/addRefundFlow", method = RequestMethod.POST)
	public Result addRefundFlow(TOrderBillNew orderRefund){
		Result res = new Result();
		if(null != orderRefund.getRemarks() && orderRefund.getRemarks().length() > 255){
			res.getError().add(buildError("", "备注长度不能大于255"));
			return res;
		}
		//if(StringUtils.isNotEmpty(orderRefund.getFinancialPayTimes())){
			try {
			orderRefund.setFinancialPayTime(new SimpleDateFormat("yyyy-MM-dd").parse(orderRefund.getFinancialPayTimes()));
			} catch (ParseException e) {
				e.printStackTrace();
				res.getError().add(buildError("", "财务流水时间有误!"));
				return res;
			}
		//}
		int r = fundManageOrderServiceImpl.addRefundFlow(orderRefund);
		if(r>0){
			res.setData("退款记录新增成功!");
		}else{
			res.getError().add(buildError("", "退款金额不能大于已交款金额"));
		}
		return res;
	}

	/**
	 * 根据id查看订单流水数据
	 * @return
	 */
	@RequestMapping(value="/selectOneRefundFlow", method = RequestMethod.GET)
	public Result selectOneRefundFlow(String billNo){
		Result res = new Result();
		res.setData(fundManageOrderServiceImpl.selectOneRefundFlow(billNo));
		return res;
	}

	/**
	 * 根据订单编号查看该订单相关的所有流水
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value="/selectOneOrderAllRefundFlow", method = RequestMethod.GET)
	public Result selectOneOrderAllRefundFlow(String orderNo){
		Result res = new Result();
		res.setData(fundManageOrderServiceImpl.selectOneOrderAllRefundFlow(orderNo));
		return res;
	}

	/**
	 * 财务专员查看自己的流水
	 * @param billNew
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/myBillList", method = RequestMethod.GET)
	public Result myBillList(TOrderBillNew billNew ,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderBillServiceImpl.myBillList(billNew, pageNo, pageSize));
		return res;
	}

	/**
	 * 删除流水
	 *
	 */
	@RequestMapping(value="/deleteMyBill", method = RequestMethod.GET)
	public Result deleteMyBill(String billNo){
		Result res = new Result();
		res.setData(orderBillServiceImpl.deleteMyBill(billNo));
		return res;
	}
	/**
	 * 查看部门的流水(并入查看自己)
	 * @param billNew
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/deptBillList", method = RequestMethod.GET)
	public Result deptBillList(TOrderBillNew billNew, Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderBillServiceImpl.myBillList(billNew,pageNo, pageSize));
		return res;
	}



	 /**
	  * 修改合同编号
	  * @param orderNew
	  * @return
	  */
	 @RequestMapping(value="/updateContractNo", method = RequestMethod.POST)
	 public Result updateContractNo(TOrderNew orderNew){
		 Result res = new Result();
		 if(StringUtils.isBlank(orderNew.getContractNo())){
			 res.getError().add(buildError("", "合同编号不能为空!"));
			 return res;
		 } else if(orderNew.getContractNo().length()>32){
			 res.getError().add(buildError("", "合同编号长度不能大于32!"));
			 return res;
		 }
		 res.setData(fundManageOrderServiceImpl.updateContractNo(orderNew));
		 return res;
	 }
}
