package com.dhcc.finance.main.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhcc.common.constant.FinanceStatusCode;
import com.dhcc.common.constant.SysConstants;
import com.dhcc.common.constant.SysStatusCode;
import com.dhcc.common.model.CurrencyMeta;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.finance.constant.FinanceDictConsts;
import com.dhcc.finance.main.dao.CwBdRecordBatchMapper;
import com.dhcc.finance.main.domain.CwBdRecordBatch;
import com.dhcc.finance.main.model.VoucherRequestInfo;
import com.dhcc.finance.main.service.CwBdRecordBatchService;
import com.dhcc.finance.main.service.FinanceForThirdService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

@Service
public class CwBdRecordBatchServiceImpl implements CwBdRecordBatchService{

	@Autowired
	CwBdRecordBatchMapper  cwBdRecordBatchMapper;
	@Autowired
	FinanceForThirdService financeForThirdService;
	@Autowired
	private Tracer tracer;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdRecordBatchAll</p>  
	 * <p>Description:查询所有批次信息 </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdRecordBatchService#searchCwBdRecordBatchAll(int, int, int)    
	 * @author huangpeng
	 * @date 2019年7月16日 上午9:23:26
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdRecordBatch>, CurrencyMeta> searchCwBdRecordBatchAll(String signType, String batchNo, String failReason, String tradeCode, String xtzxmbh, int pageNum, int pageSize,
																							 int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
//		if(code!=null) {
//			createCriteria.andParametricCodeLike("%"+code+"%");
//		}
		List<CwBdRecordBatch> list = cwBdRecordBatchMapper.selectCwBdRecordBatchList(signType,batchNo,failReason,tradeCode,xtzxmbh);
		PageInfo<CwBdRecordBatch> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdRecordBatch>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: createVoucherByInterface</p>  
	 * <p>Description: 生成记账失败的凭证</p>
	 * @param voucherRequestInfo
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdRecordBatchService#createVoucherByInterface(com.dhcc.finance.main.model.VoucherRequestInfo)    
	 * @author huangpeng
	 * @date 2019年8月23日 下午9:36:29
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> createVoucherByInterface(VoucherRequestInfo voucherRequestInfo) {
		String jym=voucherRequestInfo.getTradeCode();
		RestfulResponse<Object, CurrencyMeta> res = financeForThirdService.createVoucherInfo(voucherRequestInfo);
       	CwBdRecordBatch cwBdRecordBatch1 = new CwBdRecordBatch();//批次号记录实体
       	cwBdRecordBatch1.setBatchNo(voucherRequestInfo.getBatchNo());
 	    cwBdRecordBatch1.setTradecode(jym);
 	    cwBdRecordBatch1.setUpdateTime(new Date());
 		if(SysConstants.CODE.SUCCESS != res.getCode().intValue()) {
 		    cwBdRecordBatch1.setFailReason(res.getMsg());
 			cwBdRecordBatch1.setSignType(FinanceDictConsts.SF.no);  
 		}else {
 			if(StringUtils.isNotBlank((String)res.getData())&&FinanceStatusCode.SCENARIO_NOT_CREATE_VOUCHER.message().equals((String)res.getData())) {
 				cwBdRecordBatch1.setFailReason((String)res.getData());
 			}else {
 	 		    cwBdRecordBatch1.setFailReason("");
 			}
 		    cwBdRecordBatch1.setSignType(FinanceDictConsts.SF.yes);
 		}
 			cwBdRecordBatchMapper.updateRecordBybatchNo(cwBdRecordBatch1);//更新批次号记录表
		return res;
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdRecordBatchByBatchNo</p>  
	 * <p>Description: 根据批次号查询批次信息</p>
	 * @param batchNo
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdRecordBatchService#searchCwBdRecordBatchByBatchNo(java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年10月24日 下午2:35:13
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdRecordBatch>, CurrencyMeta> searchCwBdRecordBatchByBatchNo(String batchNo,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwBdRecordBatch> list = cwBdRecordBatchMapper.searchCwBdRecordBatchByBatchNo(batchNo);
		PageInfo<CwBdRecordBatch> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdRecordBatch>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
