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

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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.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.CwBdVouchtempBMapper;
import com.dhcc.finance.main.dao.CwBdVouchtempFMapper;
import com.dhcc.finance.main.dao.CwBdVouchtempMapper;
import com.dhcc.finance.main.domain.CwBdVouchtemp;
import com.dhcc.finance.main.domain.CwBdVouchtempB;
import com.dhcc.finance.main.domain.CwBdVouchtempExample;
import com.dhcc.finance.main.domain.CwBdVouchtempExample.Criteria;
import com.dhcc.finance.main.model.CwBdVouchtempAdd;
import com.dhcc.finance.main.service.CwBdVouchtempService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**
 * 功能描述：凭证模板配置接口实现
 * @author dml
 * @date 2019年3月27日 下午1:48:27
 * @修改日志：
 */
@Service
public class CwBdVouchtempServiceImpl implements CwBdVouchtempService{
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	CwBdVouchtempMapper cwBdVouchtempMapper;//凭证模板配置
	
	@Autowired
	CwBdVouchtempBMapper cwBdVouchtempBMapper;//凭证模板分录信息
	
	@Autowired
	CwBdVouchtempFMapper cwBdVouchtempFMapper;//辅助信息
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdVouchtempAllData</p>  
	 * <p>Description: 查询全部凭证模板配置
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#searchCwBdVouchtempAllData(int, int, int)    
	 * @author dml
	 * @date 2019年3月27日 下午1:55:15
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdVouchtemp>, CurrencyMeta> searchCwBdVouchtempAllData(int pageNum, int pageSize,
			int navigatePages,String nameLike,String Ismanual,String CODE) {
		PageHelper.startPage(pageNum, pageSize, "CODE");
		CwBdVouchtempExample cwBdVouchtempExample = new CwBdVouchtempExample();
		Criteria createCriteria = cwBdVouchtempExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		if (StringUtils.isNotBlank(nameLike)) {
			createCriteria.andNameLike("%"+nameLike+"%");
		}
		if (StringUtils.isNotBlank(Ismanual)) {
			createCriteria.andIsmanualEqualTo(Ismanual);
		}
		if (StringUtils.isNotBlank(CODE)) {
			createCriteria.andCodeLike("%"+CODE+"%");
		}
		List<CwBdVouchtemp> listCwBdVouchtemp = cwBdVouchtempMapper.selectByExample(cwBdVouchtempExample);
		PageInfo<CwBdVouchtemp> pageInfo = new PageInfo<>(listCwBdVouchtemp, navigatePages);
		return new RestfulResponse<PageInfo<CwBdVouchtemp>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdVouchtempAllData</p>  
	 * <p>Description: 查询全部凭证模板配置(手工凭证为一)
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#searchCwBdVouchtempAllDataForISMANUAL(int, int, int)    
	 * @author lipeng
	 * @date 2019年7月25日 下午1:55:15
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdVouchtemp>, CurrencyMeta> searchCwBdVouchtempAllDataForISMANUAL(int pageNum, int pageSize,
			int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwBdVouchtemp> listCwBdVouchtemp = cwBdVouchtempMapper.selectforISMANUAL();
		PageInfo<CwBdVouchtemp> pageInfo = new PageInfo<>(listCwBdVouchtemp, navigatePages);
		return new RestfulResponse<PageInfo<CwBdVouchtemp>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: addCwBdSubjscheme</p>  
	 * <p>Description: 新增凭证模板配置及相关联的分录信息辅助信息
	 * @param cwBdVouchtemp
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#addCwBdSubjscheme(com.dhcc.finance.main.domain.CwBdVouchtemp)    
	 * @author dml
	 * @date 2019年3月27日 下午2:17:00
	 * @修改日志：
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdVouchtemp(CwBdVouchtempAdd cwBdVouchtempAdd) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempAdd.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempAdd.setCreateTime(new Date());
		cwBdVouchtempAdd.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		String rowKey = SnowflakeIdWorker.getInstance().nextIdString();
		cwBdVouchtempAdd.setPkVouchtemp(rowKey);
		//新增凭证模板
		CwBdVouchtemp cwBdVouchtemp = new CwBdVouchtemp();
		BeanUtils.copyProperties(cwBdVouchtempAdd, cwBdVouchtemp);
		cwBdVouchtempMapper.insert(cwBdVouchtemp);
		//新增分录配置
		List<CwBdVouchtempB> listCwBdVouchtempB = new ArrayList<CwBdVouchtempB>();
		listCwBdVouchtempB = cwBdVouchtempAdd.getListCwBdVouchtempB();
		if (listCwBdVouchtempB != null && listCwBdVouchtempB.size()>0) {
			listCwBdVouchtempB.forEach(item -> {
				if (sysUserInfo != null) {
					item.setCreateUser(sysUserInfo.getUserId());
				}
				item.setPkVouchtempB(SnowflakeIdWorker.getInstance().nextIdString());
				item.setPkVouchtemp(rowKey);
				item.setCreateTime(new Date());
				item.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			});
			//新增对应分录信息
			cwBdVouchtempBMapper.insertCwBdVouchtempBBatch(listCwBdVouchtempB);
			//新增对应辅助信息
			cwBdVouchtempFMapper.insertCwBdVouchtempF(listCwBdVouchtempB);
		}
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwBdVouchtempByPk</p>  
	 * <p>Description: 修改凭证模板配置
	 * @param cwBdVouchtemp
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#updateCwBdVouchtempByPk(com.dhcc.finance.main.domain.CwBdVouchtemp)    
	 * @author dml
	 * @date 2019年3月27日 下午2:19:07
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdVouchtempByPk(CwBdVouchtemp cwBdVouchtemp) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtemp.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtemp.setUpdateTime(new Date());
		cwBdVouchtempMapper.updateByPrimaryKeySelective(cwBdVouchtemp);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdVouchtempByPk</p>  
	 * <p>Description: 删除凭证模板配置
	 * @param pkVouchtemp
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#deleteCwBdVouchtempByPk(java.lang.String)    
	 * @author dml
	 * @date 2019年3月27日 下午2:19:24
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdVouchtempByPk(String pkVouchtemp) {
		CwBdVouchtemp cwBdVouchtemp = new CwBdVouchtemp();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtemp.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtemp.setUpdateTime(new Date());
		cwBdVouchtemp.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwBdVouchtemp.setPkVouchtemp(pkVouchtemp);
		cwBdVouchtempMapper.updateByPrimaryKeySelective(cwBdVouchtemp);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdVouchtemp</p>  
	 * <p>Description:查询凭证模板所有数据 </p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempService#searchCwBdVouchtemp()    
	 * @author baiyc
	 * @date 2019年4月8日 下午7:44:26
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdVouchtemp>, CurrencyMeta> searchCwBdVouchtemp() {
		CwBdVouchtempExample cwBdVouchtempExample = new CwBdVouchtempExample();
		Criteria createCriteria = cwBdVouchtempExample.createCriteria();
		createCriteria.andUseStateEqualTo("1");
		cwBdVouchtempExample.setOrderByClause("CODE");
		List<CwBdVouchtemp> listCwBdVouchtemp = cwBdVouchtempMapper.selectByExample(cwBdVouchtempExample);
		return new RestfulResponse<List<CwBdVouchtemp>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,listCwBdVouchtemp,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}	

}
