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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.SysUserInfo;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.common.util.CacheDataUtil;
import com.dhcc.common.util.SnowflakeIdWorker;
import com.dhcc.finance.constant.FinanceDictConsts;
import com.dhcc.finance.main.dao.CwBdGlorgbookMapper;
import com.dhcc.finance.main.dao.CwBdScenarioMapper;
import com.dhcc.finance.main.dao.CwGlTransferExecMapper;
import com.dhcc.finance.main.dao.CwGlVoucherMapper;
import com.dhcc.finance.main.dao.CwGlVouchermaxnoMapper;
import com.dhcc.finance.main.domain.CwBdGlorgbook;
import com.dhcc.finance.main.domain.CwBdGlorgbookExample;
import com.dhcc.finance.main.domain.CwBdGlorgbookExample.Criteria;
import com.dhcc.finance.main.domain.CwGlVouchermaxno;
import com.dhcc.finance.main.model.CwBdGlorgbookAndOther;
import com.dhcc.finance.main.model.CwBdGlorgbookLike;
import com.dhcc.finance.main.model.CwGlTransferWeb;
import com.dhcc.finance.main.service.CwBdGlorgbookService;
import com.dhcc.finance.util.MetaDataUtil;
import com.esotericsoftware.minlog.Log;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
import io.netty.handler.codec.spdy.SpdySynReplyFrame;

/**
 * @author hp
 *
 */
@Service
public class CwBdGlorgbookServiceImpl implements CwBdGlorgbookService{
	@Autowired
	CwBdGlorgbookMapper cwBdGlorgbookMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	CwGlVoucherMapper cwGlVoucherMapper;
	@Autowired
	CwBdScenarioMapper cwBdScenarioMapper;
	@Autowired
	CwGlVouchermaxnoMapper cwGlVouchermaxnoMapper;// 最大凭证号表
	@Autowired
	CwGlTransferExecMapper cwGlTransferExecMapper;// 结转信息表
	/* (非 Javadoc)  
	 * <p>Title: searchCwBdGlorgbookAndOther</p>  
	 * <p>Description: </p>
	 * @param cwBdGlorgbookLike
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookAndOther(com.dhcc.finance.main.model.CwBdGlorgbookLike, int, int, int)    
	 * @author huangpeng
	 * @date 2019年3月30日 上午11:05:33
	 * @修改日志：
	 */	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwBdGlorgbookAndOther>, CurrencyMeta> searchCwBdGlorgbookAndOther(
			CwBdGlorgbookLike cwBdGlorgbookLike, int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		String nameLike = cwBdGlorgbookLike.getNameLike();
		String pkAccperiodLike = cwBdGlorgbookLike.getPkAccperiodLike();
		List<CwBdGlorgbookAndOther> list=cwBdGlorgbookMapper.selectByOtherLike(nameLike,pkAccperiodLike);
		PageInfo<CwBdGlorgbookAndOther> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdGlorgbookAndOther>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (非 Javadoc)  
	 * <p>Title: deleteCwBdGlorgbookByPk</p>  
	 * <p>Description: 逻辑删除账套管理</p>
	 * @param pkGlorgbook
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#deleteCwBdGlorgbookByPk(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月30日 上午11:52:45
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdGlorgbookByPk(String pkGlorgbook) {
		Long count = cwGlVoucherMapper.selectIsExistsVoucherForGlorgbook(pkGlorgbook);
		if(count>0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.ACCOUNTING_HAS_PKVOUCHER.message(),
					MetaDataUtil.getMeta(tracer,FinanceStatusCode.ACCOUNTING_HAS_PKVOUCHER));

		}
		CwBdGlorgbook cwBdGlorgbook = new CwBdGlorgbook();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if(sysUserInfo!=null) {
			cwBdGlorgbook.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdGlorgbook.setUpdateTime(new Date());
		cwBdGlorgbook.setUseState("0");
		cwBdGlorgbook.setPkGlorgbook(pkGlorgbook);
		int res =cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
		if (res > 0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}else {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
		}
	}

	/* (非 Javadoc)  
	 * <p>Title: addCwBdGlorgbook</p>  
	 * <p>Description:新增账套管理 </p>
	 * @param cwBdGlorgbookAndOther
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#addCwBdGlorgbook(com.dhcc.finance.main.model.CwBdGlorgbookAndOther)    
	 * @author huangpeng
	 * @date 2019年3月30日 下午2:44:32
	 * @修改日志：
	 */
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> addCwBdGlorgbook(CwBdGlorgbook cwBdGlorgbook) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdGlorgbook.setCreateUser(sysUserInfo.getUserId());
			cwBdGlorgbook.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdGlorgbook.setCreateTime(new Date());
		cwBdGlorgbook.setUpdateTime(new Date());
		cwBdGlorgbook.setPkGlorgbook(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdGlorgbook.setUseState("1");
		int res = cwBdGlorgbookMapper.insert(cwBdGlorgbook);
		if (res > 0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}else {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
		}
	}

	/* (非 Javadoc)  
	 * <p>Title: updateCwBdGlorgbook</p>  
	 * <p>Description:修改账套管理 </p>
	 * @param cwBdGlorgbook
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#updateCwBdGlorgbook(com.dhcc.finance.main.domain.CwBdGlorgbook)    
	 * @author huangpeng
	 * @date 2019年3月30日 下午2:56:00
	 * @修改日志：
	 */
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> updateCwBdGlorgbook(CwBdGlorgbook cwBdGlorgbook, String pkCurrperiodBefore) throws ParseException {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdGlorgbook.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdGlorgbook.setUpdateTime(new Date());
		
		//比较当前会计期间和之前会计期间
		Integer periodNow = Integer.parseInt(cwBdGlorgbook.getPkCurrperiod().replaceAll("\\-",""));
		Integer periodBefore = Integer.parseInt(pkCurrperiodBefore.replaceAll("\\-",""));
		if(periodNow < periodBefore) { // 大改小 
			List<String> periodList = new ArrayList<String>();
			for(Integer time = periodBefore; time > periodNow; time--) {
				periodList.add(time.toString().substring(0,4)+"-"+time.toString().substring(4,6));
			}
			// 查询最大凭证号表
			String existVoucherOfPeriod = "";
			CwGlVouchermaxno CwGlVouchermaxnoNow = cwGlVouchermaxnoMapper.selectGetMaxNo(cwBdGlorgbook.getPkGlorgbook(), cwBdGlorgbook.getPkCurrperiod());
			List<CwGlVouchermaxno> cwGlVouchermaxnoList = cwGlVouchermaxnoMapper.selectGetMaxNoForBatch(cwBdGlorgbook.getPkGlorgbook(),periodList);
			for(CwGlVouchermaxno cwGlVouchermaxno:cwGlVouchermaxnoList) {
				// 之前会计期间存在数据且最大凭证号大于0
				if(cwGlVouchermaxno != null && Integer.parseInt(cwGlVouchermaxno.getMaxno()) > 0) {
					existVoucherOfPeriod = existVoucherOfPeriod + cwGlVouchermaxno.getPeriod()+",";
				}
				// 之前会计期间存在数据且最大凭证号等于0
				if(cwGlVouchermaxno != null && Integer.parseInt(cwGlVouchermaxno.getMaxno()) == 0) {
					cwGlVouchermaxnoMapper.deleteByPrimaryKey(cwGlVouchermaxno.getPkVouchermaxno());
				}
			}
			// 判断 之前会计期间存在数据且凭证号大于0，则不能修改会计期间
			if(existVoucherOfPeriod.length()>1) {
				existVoucherOfPeriod = existVoucherOfPeriod.substring(0, existVoucherOfPeriod.length()-1);
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,"本账套"+existVoucherOfPeriod+"会计期间内存在凭证，无法修改当前会计期间为"+cwBdGlorgbook.getPkCurrperiod(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
			}
			
			int res = 0;
			// 如果当前会计期间存在数据--更新账套表
			if(CwGlVouchermaxnoNow != null) {
				res = cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
			}else {// 当前会计期间不存在数据 --最大凭证号表添加一条数据，更新账套表
				CwGlVouchermaxno cwGlVouchermaxno = new CwGlVouchermaxno();
				if (sysUserInfo != null) {
		            cwGlVouchermaxno.setCreateUser(sysUserInfo.getUserId());
		        }
				cwGlVouchermaxno.setCreateTime(new Date());
		        cwGlVouchermaxno.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		        cwGlVouchermaxno.setPkVouchermaxno(SnowflakeIdWorker.getInstance().nextIdString());
				cwGlVouchermaxno.setPkGlorgbook(cwBdGlorgbook.getPkGlorgbook());
				cwGlVouchermaxno.setPeriod(cwBdGlorgbook.getPkCurrperiod());
				cwGlVouchermaxno.setMaxno("0");
		        cwGlVouchermaxnoMapper.insert(cwGlVouchermaxno);
		        res = cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
			}
			
			if (res > 0) {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
			}else {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
			}
		}
		
		if(periodNow > periodBefore) { // 小改大  
			//查询之前会计期间是否结转
			Map<String, Object> settleMap = new HashMap<>();
			settleMap.put("pkGlorgbook",cwBdGlorgbook.getPkGlorgbook());
			settleMap.put("period",pkCurrperiodBefore);
			Map<String, Long> selectIstransfer = cwGlTransferExecMapper.selectIstransfer(settleMap);
			if (selectIstransfer.get("allCount") != selectIstransfer.get("transferCount") || selectIstransfer.get("transferCount") != 1 ) {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,"该账套在会计期间"+pkCurrperiodBefore+"未结转，请结转该会计期间",MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
			}
			//查询当前会计期间是否有数据
			CwGlVouchermaxno CwGlVouchermaxnoNow = cwGlVouchermaxnoMapper.selectGetMaxNo(cwBdGlorgbook.getPkGlorgbook(), cwBdGlorgbook.getPkCurrperiod());
			
			int res = 0;
			// 如果当前会计期间存在数据--更新账套表
			if(CwGlVouchermaxnoNow != null) {
				res = cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
			}else {// 当前会计期间不存在数据 --最大凭证号表添加一条数据，更新账套表
				CwGlVouchermaxno cwGlVouchermaxno = new CwGlVouchermaxno();
				if (sysUserInfo != null) {
		            cwGlVouchermaxno.setCreateUser(sysUserInfo.getUserId());
		        }
				cwGlVouchermaxno.setCreateTime(new Date());
		        cwGlVouchermaxno.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		        cwGlVouchermaxno.setPkVouchermaxno(SnowflakeIdWorker.getInstance().nextIdString());
				cwGlVouchermaxno.setPkGlorgbook(cwBdGlorgbook.getPkGlorgbook());
				cwGlVouchermaxno.setPeriod(cwBdGlorgbook.getPkCurrperiod());
				cwGlVouchermaxno.setMaxno("0");
		        cwGlVouchermaxnoMapper.insert(cwGlVouchermaxno);
		        res = cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
			}
			
			if (res > 0) {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
			}else {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
			}
		}
		
		int res = cwBdGlorgbookMapper.updateByPrimaryKeySelective(cwBdGlorgbook);
		if (res > 0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}else {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
		}
	}
	

	/* (非 Javadoc)  
	 * <p>Title: searchCwBdGlorgbookAllData</p>  
	 * <p>Description: 获取所有的账套信息</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookAllData()    
	 * @author huangpeng
	 * @date 2019年4月8日 上午11:53:07
	 * @修改日志：
	 */
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<List<CwBdGlorgbook>, CurrencyMeta> searchCwBdGlorgbookAllData() {
		CwBdGlorgbookExample example = new CwBdGlorgbookExample();
		Criteria criteria = example.createCriteria();
		criteria.andUseStateEqualTo("1");
		List<CwBdGlorgbook> list = cwBdGlorgbookMapper.selectByExample(example);
		return new RestfulResponse<List<CwBdGlorgbook>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (non-Javadoc) <br/> 
	 * Title: searchCwBdGlorgbookNowDayNuANDSum <br/>  
	 * Description: TODO 查询帐套今日新增数目和总数<br/>  
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookNowDayNuANDSum()
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> searchCwBdGlorgbookNowDayNuANDSum() {
		// TODO Auto-generated method stub
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM");
		String format = dateFormat.format(calendar.getTime());
		String NowDate = format+"%";
		Map<String, Long> searchCwBdGlorgbookNowDayNuANDSum = cwBdGlorgbookMapper.searchCwBdGlorgbookNowDayNuANDSum(NowDate);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,searchCwBdGlorgbookNowDayNuANDSum,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: selectCwBdGlorgbookIsCreate</p>  
	 * <p>Description:校验:当前日期是否允许生成凭证 (对外)</p>
	 * @param xtzxmbh
	 * @param makeDate
	 * @param tradeCode
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#selectCwBdGlorgbookIsCreate(java.lang.String, java.lang.String, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年8月30日 上午9:40:01
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> selectCwBdGlorgbookIsCreate(String xtzxmbh, String makeDate,String tradeCode) {
		if(StringUtils.isNotBlank(tradeCode)) {
			Long count = cwBdScenarioMapper.selectIsExistsOfTradeCode(tradeCode);
			if(count==0) {				
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.TRADECODE_ISNOT_EXIST.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.TRADECODE_ISNOT_EXIST));
			}
		}
		Long res = cwBdGlorgbookMapper.selectCwBdGlorgbookIsCreate(xtzxmbh,makeDate);
		if(res==0) {
			CwBdGlorgbookExample example = new CwBdGlorgbookExample();
			Criteria criteria = example.createCriteria();
			criteria.andUseStateEqualTo("1");
			criteria.andXtzxmbhEqualTo(xtzxmbh);
			List<CwBdGlorgbook> list = cwBdGlorgbookMapper.selectByExample(example);
			if(list.size()==0) {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.GLORGBOOK_MISMATCH.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.GLORGBOOK_MISMATCH));
			}
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,"账套当前会计期间为："+list.get(0).getPkCurrperiod()+","+FinanceStatusCode.FAIL_CHECK_GLORGBOOK.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.FAIL_CHECK_GLORGBOOK));
		}
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/**
	 * 功能描述:批量校验多个子项目的制单日期是否在当前期间
	 * @param xtzxmbh
	 * @param makeDate
	 * @param tradeCode
	 * @return RestfulResponse<Object,CurrencyMeta>
	 *@author likai
	 *@data 2020年4月27日上午11:54:34
	 *修改日志
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> batchCheckCwBdGlorgbookIsCreate(List<String> xtzxmbhs, String makeDate,String tradeCode) {
		if(StringUtils.isNotBlank(tradeCode)) {
			//判断当前交易码是否需要记账
			Long count = cwBdScenarioMapper.selectIsRecorded(tradeCode);
			if(count==0) {				
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.TRADECODE_ISNOT_EXIST.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.TRADECODE_ISNOT_EXIST));
			}
		}
		Map<String, Object> param = new HashMap<>();
		param.put("list", xtzxmbhs);
		//判断传入的子项目中存在没有建账的项目
		String glorgbooks = cwBdGlorgbookMapper.selectIfExistGlorgbook(param);
		if(StringUtils.isNotBlank(glorgbooks) || glorgbooks!= null ) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.IS_NOT_GLORGBOOK_MISMATCH.message()+","+glorgbooks,
					MetaDataUtil.getMeta(tracer, FinanceStatusCode.IS_NOT_GLORGBOOK_MISMATCH));
		}
		String pkCurrperiod = null;
		// 2020-02-02
		 boolean status = makeDate.contains("-");
		 if (status) {
			  pkCurrperiod = makeDate.substring(0,7 );
		 } else {
			 pkCurrperiod= makeDate.substring(0,4 )+"-"+makeDate.substring(4,6);
		 }
		 Map<String, Object> map = new HashMap<>();
			map.put("list", xtzxmbhs);
			map.put("makeDate", pkCurrperiod);
			//查询项目中当前会计期间不在制单日期所在的会计期间内的项目
		 String cwBdGlorgbooks = cwBdGlorgbookMapper.selectIfExistPkCurrperiod(map);
		 if(StringUtils.isNotBlank(glorgbooks)||cwBdGlorgbooks!= null ) {
			 return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.IS_NOT_PKCURRPERIOD.message()+","+cwBdGlorgbooks,cwBdGlorgbooks,
						MetaDataUtil.getMeta(tracer, FinanceStatusCode.IS_NOT_PKCURRPERIOD));
		}
		
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (non-Javadoc) <br/> 
	 * Title: searchBdGlorgbooksbyXTzxmbh <br/>  
	 * Description: TODO <br/>  
	 * @param xtzxmbh
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchBdGlorgbooksbyXTzxmbh(java.lang.String)  
	 */
	@Override
	public RestfulResponse<CwBdGlorgbook, CurrencyMeta> searchBdGlorgbooksbyXTzxmbh(String xtzxmbh) {
		// TODO Auto-generated method stub
		CwBdGlorgbook searchBdGlorgbooksbyXTzxmbh = cwBdGlorgbookMapper.searchBdGlorgbooksbyXTzxmbh(xtzxmbh);
		return new RestfulResponse<CwBdGlorgbook, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,searchBdGlorgbooksbyXTzxmbh,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdGlorgbookForSelect</p>  
	 * <p>Description:获取全部版本信息列表下拉框 </p>
	 * @param nameLike
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookForSelect(java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年8月28日 上午9:21:54
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdGlorgbook>, CurrencyMeta> searchCwBdGlorgbookForSelect(String nameLike,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize,"XTZXMBH DESC");
		CwBdGlorgbookExample cwBdGlorgbookExample = new CwBdGlorgbookExample();
		Criteria createCriteria = cwBdGlorgbookExample.createCriteria();
		if(StringUtils.isNotEmpty(nameLike)) {
			createCriteria.andNameLike("%"+nameLike+"%");
		}
		List<CwBdGlorgbook> selectByExample = cwBdGlorgbookMapper.selectByExample(cwBdGlorgbookExample);
		PageInfo<CwBdGlorgbook> pageInfo = new PageInfo<>(selectByExample, navigatePages);
		return new RestfulResponse<PageInfo<CwBdGlorgbook>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: selectCwBdGlorgbookForLocal</p>  
	 * <p>Description:校验:当前日期是否允许生成凭证(本地) </p>
	 * @param xtzxmbh
	 * @param makeDate
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#selectCwBdGlorgbookForLocal(java.lang.String, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年8月30日 上午9:32:01
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> selectCwBdGlorgbookForLocal(String xtzxmbh, String makeDate) {
		Long res = cwBdGlorgbookMapper.selectCwBdGlorgbookIsCreate(xtzxmbh,makeDate);
		if(res==0) {
			CwBdGlorgbookExample example = new CwBdGlorgbookExample();
			Criteria criteria = example.createCriteria();
			criteria.andUseStateEqualTo("1");
			criteria.andXtzxmbhEqualTo(xtzxmbh);
			List<CwBdGlorgbook> list = cwBdGlorgbookMapper.selectByExample(example);
			if(list.size()==0) {
				return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.GLORGBOOK_MISMATCH.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.GLORGBOOK_MISMATCH));
			}
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,"账套当前会计期间为："+list.get(0).getPkCurrperiod()+","+FinanceStatusCode.FAIL_CHECK_GLORGBOOK.message(),MetaDataUtil.getMeta(tracer,FinanceStatusCode.FAIL_CHECK_GLORGBOOK));
		}
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdGlorgbookofTransferDetail</p>  
	 * <p>Description:账套下凭证数量查询 </p>
	 * @param nameLike
	 * @param period
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookofTransferDetail(java.lang.String, java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年10月5日 上午9:35:00
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwGlTransferWeb>, CurrencyMeta> searchCwBdGlorgbookofTransferDetail(
			String nameLike, String period, String userId,String istransfer, int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwGlTransferWeb> list=cwBdGlorgbookMapper.searchCwBdGlorgbookofTransferDetail(nameLike,period,userId,istransfer);
		PageInfo<CwGlTransferWeb> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlTransferWeb>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdGlorgbookofSettle</p>  
	 * <p>Description: </p>
	 * @param nameLike
	 * @param period
	 * @param userId
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdGlorgbookService#searchCwBdGlorgbookofSettle(java.lang.String, java.lang.String, java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年12月2日 下午9:46:52
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwGlTransferWeb>, CurrencyMeta> searchCwBdGlorgbookofSettle(String nameLike,
			String period, String userId, int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwGlTransferWeb> list=cwBdGlorgbookMapper.searchCwBdGlorgbookofSettle(nameLike,period,userId);
		PageInfo<CwGlTransferWeb> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlTransferWeb>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	

}
