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.domain.CwBdVouchtempB;
import com.dhcc.finance.main.domain.CwBdVouchtempBExample;
import com.dhcc.finance.main.domain.CwBdVouchtempBExample.Criteria;
import com.dhcc.finance.main.model.CwBdVouchtempBUpd;
import com.dhcc.finance.main.model.VouchtempBForTable;
import com.dhcc.finance.main.service.CwBdVouchtempBService;
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日 下午2:34:46
 * @修改日志：
 */
@Service
public class CwBdVouchtempBServiceImpl implements CwBdVouchtempBService{
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	CwBdVouchtempBMapper cwBdVouchtempBMapper;
	
	@Autowired
	CwBdVouchtempFMapper cwBdVouchtempFMapper;//辅助信息

	/*
	 * (非 Javadoc)  
	 * Title: searchCwBdVouchtempBAllData 
	 * Description:查询全部凭证模板分录配置
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param pkVouchtemp
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#searchCwBdVouchtempBAllData(int, int, int, java.lang.String)    
	 * @author dml
	 * @date 2019年4月1日 下午6:47:10
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdVouchtempB>, CurrencyMeta> searchCwBdVouchtempBAllData(int pageNum,
			int pageSize, int navigatePages,String pkVouchtemp) {
		PageHelper.startPage(pageNum, pageSize);
		CwBdVouchtempBExample cwBdVouchtempBExample = new CwBdVouchtempBExample();
		Criteria createCriteria = cwBdVouchtempBExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andPkVouchtempEqualTo(pkVouchtemp);
		List<CwBdVouchtempB> listCwBdVouchtemp = cwBdVouchtempBMapper.selectByExample(cwBdVouchtempBExample);
		PageInfo<CwBdVouchtempB> pageInfo = new PageInfo<>(listCwBdVouchtemp, navigatePages);
		return new RestfulResponse<PageInfo<CwBdVouchtempB>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: addCwBdVouchtempB 
	 * Description:新增凭证模板分录配置及其辅助信息
	 * @param cwBdVouchtempB
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#addCwBdVouchtempB(com.dhcc.finance.main.domain.CwBdVouchtempB)    
	 * @author dml
	 * @date 2019年3月27日 下午2:44:06
	 * @修改日志：
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdVouchtempB(CwBdVouchtempB cwBdVouchtempB) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempB.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempB.setCreateTime(new Date());
		cwBdVouchtempB.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		cwBdVouchtempB.setPkVouchtempB(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdVouchtempBMapper.insert(cwBdVouchtempB);
		List<CwBdVouchtempB> listCwBdVouchtempB = new ArrayList<CwBdVouchtempB>();
		// 新增对应辅助配置
		listCwBdVouchtempB.add(cwBdVouchtempB);
		cwBdVouchtempFMapper.insertCwBdVouchtempF(listCwBdVouchtempB);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: updateCwBdVouchtempBByPk 
	 * Description:通过主键修改凭证模板分录配置
	 * @param cwBdVouchtempB
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#updateCwBdVouchtempBByPk(com.dhcc.finance.main.domain.CwBdVouchtempB)    
	 * @author dml
	 * @date 2019年3月27日 下午2:44:11
	 * @修改日志：
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdVouchtempBByPk(CwBdVouchtempBUpd cwBdVouchtempBUpd) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempBUpd.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempBUpd.setUpdateTime(new Date());
		
		CwBdVouchtempB cwBdVouchtempB = new CwBdVouchtempB();
		BeanUtils.copyProperties(cwBdVouchtempBUpd, cwBdVouchtempB);
		cwBdVouchtempBMapper.updateByPrimaryKeySelective(cwBdVouchtempB);
		if (StringUtils.isNotBlank(cwBdVouchtempB.getPkAccsubj()) && !cwBdVouchtempB.getPkAccsubj().equals(cwBdVouchtempBUpd.getOldPkAccsubj())) {
			//先删除之前辅助项
			cwBdVouchtempFMapper.deleteCwBdVouchtempF(cwBdVouchtempB);
			List<CwBdVouchtempB> listCwBdVouchtempB = new ArrayList<CwBdVouchtempB>();
			// 新增对应辅助配置
			listCwBdVouchtempB.add(cwBdVouchtempB);
			cwBdVouchtempFMapper.insertCwBdVouchtempF(listCwBdVouchtempB);
		}
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: deleteCwBdVouchtempBByPk 
	 * Description:逻辑删除凭证模板分录配置
	 * @param pkVouchtempB
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#deleteCwBdVouchtempBByPk(java.lang.String)    
	 * @author dml
	 * @date 2019年3月27日 下午2:44:15
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdVouchtempBByPk(String pkVouchtempB) {
		CwBdVouchtempB cwBdVouchtempB = new CwBdVouchtempB();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempB.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempB.setUpdateTime(new Date());
		cwBdVouchtempB.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwBdVouchtempB.setPkVouchtempB(pkVouchtempB);
		cwBdVouchtempBMapper.updateByPrimaryKeySelective(cwBdVouchtempB);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: searchCwBdVouchtempB 
	 * Description:查询某分录模板下所有分录信息不分页
	 * @param pkVouchtemp
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#searchCwBdVouchtempB(java.lang.String)    
	 * @author dml
	 * @date 2019年4月10日 下午2:27:55
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdVouchtempB>, CurrencyMeta> searchCwBdVouchtempB(String pkVouchtemp) {
		CwBdVouchtempBExample cwBdVouchtempBExample = new CwBdVouchtempBExample();
		Criteria createCriteria = cwBdVouchtempBExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andPkVouchtempEqualTo(pkVouchtemp);
		List<CwBdVouchtempB> listCwBdVouchtemp = cwBdVouchtempBMapper.selectByExample(cwBdVouchtempBExample);
		return new RestfulResponse<List<CwBdVouchtempB>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,listCwBdVouchtemp,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdVouchtempBForTableName</p>  
	 * <p>Description:查询计算编辑器表名 </p>
	 * @param tableName
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempBService#searchCwBdVouchtempBForTableName(java.lang.String)    
	 * @author huangpeng
	 * @date 2020年1月9日 上午11:56:01
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<VouchtempBForTable>, CurrencyMeta> searchCwBdVouchtempBForTableName(String tableName) {
		List<VouchtempBForTable> list = cwBdVouchtempBMapper.searchCwBdVouchtempBForTableName(tableName);
		// TODO Auto-generated method stub
		return new RestfulResponse<List<VouchtempBForTable>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
