package com.goafanti.order.controller;




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

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.OrderRefundInvoice;
import com.goafanti.common.model.TChangeDun;
import com.goafanti.common.model.TChangeTask;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.order.bo.InputNewOrderRefund;
import com.goafanti.order.bo.InputTChangeTask;
import com.goafanti.order.bo.NewOrderChangeBo;
import com.goafanti.order.service.OrderChangeService;

@RestController
@RequestMapping(value = "/api/admin/orderChange")
public class OrderChangeApiController extends CertifyApiController {
	
	@Autowired
	private OrderChangeService	orderChangeService;
	
	
	/**
	 *  新增变更
	 */
	@RequestMapping(value = "/addOrderChange", method = RequestMethod.POST)
	public Result addOrderChange(NewOrderChangeBo t,String startRemarks){
		Result res = new Result();
		if(null==t.getOrderNo()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		if(orderChangeService.checkOderNo(t.getOrderNo())){
			res.getError().add(buildError( "", "订单变更未完成"));
			return res;
		}
		res.setData(orderChangeService.addOrderChange(t,startRemarks));
		return res;
	}
	
	@RequestMapping(value = "/addChangeTask", method = RequestMethod.POST)
	public Result addChangeTask(TChangeTask t){
		Result res = new Result();
		if(StringUtils.isBlank(t.getCommodityId())){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目"));
			return res;
		}
		if(null==t.getCommodityPrice()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目金额"));
			return res;
		}
		if(null==t.getCommodityQuantity()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目数量"));
			return res;
		}
		if(StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if(null==t.getCid()){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更编号"));
			return res;
		}
		res.setData(orderChangeService.addChangeTask(t));
		return res;
	}
	
	/**
	 *  变更项目列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectChangeTask", method = RequestMethod.GET)
	public Result selectChangeTask(Integer id){
		Result res = new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.selectChangeTask(id));
		return res;
	}
	/**
	 *  变更催款列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectChangeDun", method = RequestMethod.GET)
	public Result selectChangeDun(Integer id){
		Result res = new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.selectChangeDun(id));
		return res;
	}
	/**
	 *  删除变更项目
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteChangeTask", method = RequestMethod.POST)
	public Result deleteChangeTask(TChangeTask t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.deleteChangeTask(t.getId()));
		return res;
	}
	
	/**
	 *  修改变更项目
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateChangeTask", method = RequestMethod.POST)
	public Result updateChangeTask(InputTChangeTask t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.updateChangeTask(t));
		return res;
	}
	/**
	  *     修改变更催款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateChangeDun", method = RequestMethod.POST)
	public Result updateChangeDun(TChangeDun t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更任务id"));
			return res;
		}
		res.setData(orderChangeService.updateChangeDun(t));
		return res;
	}
	/**
	 *  删除变更催款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteChangeDun", method = RequestMethod.POST)
	public Result deleteChangeDun(TChangeDun t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更任务id"));
			return res;
		}
		res.setData(orderChangeService.deleteChangeDun(t.getId()));
		return res;
	}
	@RequestMapping(value = "/addChangeDun", method = RequestMethod.POST)
	public Result addChangeDun(TChangeDun d){
		Result res = new Result();
		if(StringUtils.isBlank(d.getOrderNo())||null==d.getCtid()) {
			res.getError().add(buildError("", "订单编号与项目编号必须指定"));
			return res;
		}
		if(null==d.getProjectType()||null==d.getDunType()) {
			res.getError().add(buildError("", "项目分类和催款分类必须指定"));
			return res;
		}
		res.setData(orderChangeService.addChangeDun(d));
		return res;
	}
	/**
	 *    订单查看变更列表
	 */
	@RequestMapping(value = "/orderChangeDetails", method = RequestMethod.GET)
	public Result orderChangeDetails(String orderNo){
		Result res = new Result();
		if(null==orderNo){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeDetails( orderNo));
		return res;
	}
	/**
	 *    订单查看变更原订单
	 */
	@RequestMapping(value = "/orderChangeUsed", method = RequestMethod.GET)
	public Result orderChangeUsed(String orderNo){
		Result res = new Result();
		if(null==orderNo){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeUsed( orderNo));
		return res;
	}
	/**
	 *    id查看变更详情
	 */
	@RequestMapping(value = "/orderChangeDetailsById", method = RequestMethod.GET)
	public Result orderChangeDetailsById(Integer id){
		Result res = new Result();
		if(null==id){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单变更编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeDetailsById( id));
		return res;
	}
	/**
	 * 变更修改
	 */
	@RequestMapping(value = "/updateOrderChange", method = RequestMethod.POST)
	public Result orderChangeDetails(NewOrderChangeBo t ,Integer changeType,String startRemarks){
		Result res = new Result();
		if(null==t.getId()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单变更id"));
			return res;
		}
		if (changeType==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "修改变更类型"));
			return res;
		}
		res.setData(orderChangeService.updateOrderChange(t,changeType,startRemarks));
		return res;
	}
	/**
	 *   变更审核
	 */
	@RequestMapping(value = "/orderChangeAudit", method = RequestMethod.POST)
	public Result orderChangeAudit(String changeId,String remarks,Integer status,Integer processState,Integer rejectState){
		Result res = new Result();
		if(null==changeId){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "订单编号", "订单编号"));
			return res;
		}
		if(null==status){//2通过 3驳回
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "审核结果", "审核结果"));
			return res;
		}
		if(null==remarks){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "审核批示", "审核批示"));
			return res;
		}
		res.setData(orderChangeService.pushOrderChangeAudit( changeId, remarks, status, processState,rejectState));
		return res;
	}
	/**
	 *  变更日志
	 */
	@RequestMapping(value ="/orderChangeLogList",method = RequestMethod.GET)
	public Result orderChangeLogList(String changeId) {
		Result res =new Result();
		if (null==changeId) {
			res.getError().add(buildError("变更id错误", "变更id错误"));
			return res;
		}
		res.data(orderChangeService.selectOrderChangeLogList(changeId));
		return res;
	}
	/**
	 *	  变更文件上传 
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result uploadRefundOrderFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_change_file/", false, req, sign));
		return res;
	}
	/**
	 *  变更列表
	 */
	@RequestMapping(value ="/orderChangeList",method = RequestMethod.GET)
	public Result orderChangeList(String userName,Integer  processState,Integer timeType,String startTime,String endTime,
			String depId,String salesmanName,Integer complete,String orderNo, String contractNo,Integer type,Integer pageSize, Integer pageNo) {
		Result res =new Result();
		res.data(orderChangeService.selectOrderChangeList( userName,  processState, timeType, startTime, endTime,
				depId, salesmanName, complete, orderNo,  contractNo, type,pageSize,  pageNo));
		return res;
	}
	/**
	 *  新增回收发票
	 */
	@RequestMapping(value ="/addOrderRefundInvoice",method = RequestMethod.POST)
	public Result addOrderRefundInvoice(OrderRefundInvoice o) {
		Result res =new Result();
		if (o.getAmount()==null) {
			res.getError().add(buildError("金额不能为空", "金额不能为空"));
			return res;
		}
		res.data(orderChangeService.addOrderRefundInvoice(o));
		return res;
	}
	
	/**
	 *  新增回收发票
	 */
	@RequestMapping(value ="/getOrderChange",method = RequestMethod.GET)
	public Result getOrderChange(String  orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "订单编号", "订单编号"));
			return res;
		}
		res.data(orderChangeService.getOrderChange(orderNo));
		return res;
	}
	/**
	 *  删除回收发票
	 */
	@RequestMapping(value ="/deleteOrderRefundInvoice",method = RequestMethod.POST)
	public Result deleteOrderRefundInvoice(Integer id) {
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError("id不能为空", "id不能为空"));
			return res;
		}
		res.data(orderChangeService.deleteOrderRefundInvoice(id));
		return res;
	}
	/**
	 *  回收发票列表
	 */
	@RequestMapping(value ="/listOrderRefundInvoice",method = RequestMethod.GET)
	public Result listOrderRefundInvoice(String orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError("订单编号不能为空", "订单编号不能为空"));
			return res;
		}
		res.data(orderChangeService.listOrderRefundInvoice(orderNo));
		return res;
	}
	
	/**
	 *  回收发票列表
	 */
	@RequestMapping(value ="/listOrderInvoiceAndBill",method = RequestMethod.GET)
	public Result listOrderInvoiceAndBill(String orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError("订单编号不能为空", "订单编号不能为空"));
			return res;
		}
		res.data(orderChangeService.listOrderInvoiceAndBill(orderNo));
		return res;
	}
	/**
	 * 完成变更
	 */
	@RequestMapping(value ="/completeOrderChange",method = RequestMethod.GET)
	public Result completeOrderChange(NewOrderChangeBo nb) {
		Result res =new Result();
		if (StringUtils.isBlank(nb.getOrderNo())||nb.getType()==null) {
			res.getError().add(buildError("订单编号和类型不能为空", "订单编号和类型不能为空"));
			return res;
		}
		res.data(orderChangeService.pushCompleteOrderChange(nb));
		return res;
	}
	/**
	 * 取消变更
	 */
	@RequestMapping(value ="/cancelOrderChange",method = RequestMethod.POST)
	public Result cancelOrderChange(NewOrderChangeBo nb) {
		Result res =new Result();
		if (null==nb.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "变更编号", "变更编号"));
			return res;
		}
		if (nb.getProcessState()!=0&&(nb.getStatus()!=0 && nb.getStatus()!=3)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "变更流程或状态", "变更流程或状态"));
			return res;
		}
		res.data(orderChangeService.updateCancelOrderChange(nb));
		return res;
	}
	
	
	/**
	 * 	导出变更列表
     * 
     * @param response
     * @return
     */
    @RequestMapping(value = "/exportMyChange" , method = RequestMethod.GET)
    public Result exportMyBill(HttpServletResponse response,String userName,Integer  processState,Integer timeType,String startTime,String endTime,
			String depId,String salesmanName,Integer complete,String orderNo, String contractNo,Integer type,Integer pageSize, Integer pageNo)  {
    	Result res=new Result();
			try {
				orderChangeService.exportMyChange( response, userName,  processState, timeType, startTime, endTime,
						 depId, salesmanName, complete, orderNo,  contractNo, type, pageSize,  pageNo);
			} catch (Exception e) {
				res.getError().add(buildError("格式不正确"));
				e.printStackTrace();
				return res;
			}
         res.data(1); 
    	return res;
    }
	
    
    /**
	 *	  变更退款图片文件上传 
	 */
	@RequestMapping(value = "/uploadRefund", method = RequestMethod.POST)
	public Result uploadRefund(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_refund/", false, req, sign));
		return res;
	}
	
	/**
	 *	  新增退款信息 
	 */
	@RequestMapping(value = "/addRefund", method = RequestMethod.POST)
	public Result addRefund(InputNewOrderRefund or){
		Result res = new Result();
		if (or.getRefundAmount()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"金额", "金额"));
			return res;
		}
		if (or.getRefundDate()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"时间", "时间"));
			return res;
		}
		if (orderChangeService.checkrefund(or)) {
			res.getError().add(buildError("金额与实际收款不符", "金额与实际收款不符"));
			return res;
		}
		res.setData(orderChangeService.addOrderRefund(or));
		return res;
	}
	
	/**
	 * 删除退款信息
	 */
	@RequestMapping(value = "/deleteRefund", method = RequestMethod.POST)
	public Result deleteRefund(Integer id){
		Result res = new Result();
		res.setData(orderChangeService.deleteRefund(id));
		return res;
	}
	
	/**
	 * 退款信息列表
	 */
	@RequestMapping(value = "/listRefund", method = RequestMethod.GET)
	public Result listRefund(Integer id){
		Result res = new Result();
		res.setData(orderChangeService.listRefund(id));
		return res;
	}
	
	/**
	 * 确认退款
	 */
	@RequestMapping(value = "/pushRefund", method = RequestMethod.POST)
	public Result pushRefund(Integer id,String refundUrl){
		Result res = new Result();
		res.setData(orderChangeService.pushRefund(id,refundUrl));
		return res;
	}

	
}
