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

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

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 org.springframework.web.client.RestTemplate;

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.PKDataUtil;
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.CwGlTransferMapper;
import com.dhcc.finance.main.dao.CwGlTransferdetailMapper;
import com.dhcc.finance.main.domain.CwGlTransfer;
import com.dhcc.finance.main.model.CwGlTransferAndSubjectName;
import com.dhcc.finance.main.model.CwGlTransferLike;
import com.dhcc.finance.main.service.CwGlTransferService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
@Service
public class CwGlTransferServiceImpl implements CwGlTransferService{
	
	@Autowired
	CwGlTransferMapper cwGlTransferMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	CwGlTransferdetailMapper cwGlTransferdetailMapper;
	@Autowired
	CwBdGlorgbookMapper cwBdGlorgbookMapper;
	@Autowired
    private RestTemplate loadBalancedRestTemplate;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwGlTranser</p>  
	 * <p>Description:查询转账定义表 </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#searchCwGlTranser(int, int, int)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:07:49
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<List<CwGlTransfer>, CurrencyMeta> searchCwGlTranser() {
		List<CwGlTransfer> list = cwGlTransferMapper.selectAll();
		return  new RestfulResponse<List<CwGlTransfer>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwGlTransfer</p>  
	 * <p>Description:逻辑删除转账定义 </p>
	 * @param pkTransfer
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#deleteCwGlTransfer(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:08:02
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<Object, CurrencyMeta> deleteCwGlTransfer(String pkTransfer) {
		Long count=cwGlTransferdetailMapper.searchCountDetail(pkTransfer);
		if(count>0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.TRANSFER_HAS_DETAIL.message(),
					MetaDataUtil.getMeta(tracer, FinanceStatusCode.TRANSFER_HAS_DETAIL));
		}
		Long count1 = cwBdGlorgbookMapper.searchCwBdGlorgbookofTransfer(pkTransfer);
		if(count1>0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,FinanceStatusCode.TRANSFER_IN_GLORGBOOK.message(),
					MetaDataUtil.getMeta(tracer, FinanceStatusCode.TRANSFER_IN_GLORGBOOK));
		}
		CwGlTransfer cwGlTransfer = new CwGlTransfer();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransfer.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlTransfer.setUpdateTime(new Date());
		cwGlTransfer.setUseState("0");
		cwGlTransfer.setPkTransfer(pkTransfer);
		int code = cwGlTransferMapper.updateByPrimaryKeySelective(cwGlTransfer);
		if (code > 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: addCwGlTransfer</p>  
	 * <p>Description: 增加转账定义</p>
	 * @param cwGlTransfer
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#addCwGlTransfer(com.dhcc.finance.main.domain.CwGlTransfer)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:08:31
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<Object, CurrencyMeta> addCwGlTransfer(CwGlTransfer cwGlTransfer) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransfer.setCreateUser(sysUserInfo.getUserId());
			cwGlTransfer.setUpdateUser(sysUserInfo.getUserId());
		}
		String code = PKDataUtil.getPK(FinanceDictConsts.PK.transfer, stringRedisTemplate, loadBalancedRestTemplate);
		cwGlTransfer.setUpdateTime(new Date());
		cwGlTransfer.setCreateTime(new Date());
		cwGlTransfer.setUseState("1");
		String pk= SnowflakeIdWorker.getInstance().nextIdString();
		cwGlTransfer.setPkTransfer(pk);
		cwGlTransfer.setCode(code);
		int res = cwGlTransferMapper.insert(cwGlTransfer);
		if (res > 0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pk,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: updateCwGlTransfer</p>  
	 * <p>Description: 更新转账定义表</p>
	 * @param cwGlTransfer
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#updateCwGlTransfer(com.dhcc.finance.main.domain.CwGlTransfer)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:08:46
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<Object, CurrencyMeta> updateCwGlTransfer(CwGlTransfer cwGlTransfer) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransfer.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlTransfer.setUpdateTime(new Date());
		int code = cwGlTransferMapper.updateByPrimaryKeySelective(cwGlTransfer);
		if (code > 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: searchCwGlTransferByNameLike</p>  
	 * <p>Description: 模糊查询转账定义表</p>
	 * @param cwGlTransferLike
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#searchCwGlTransferByNameLike(com.dhcc.finance.main.model.CwGlTransferLike, int, int, int)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:09:01
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<PageInfo<CwGlTransfer>, CurrencyMeta> searchCwGlTransferByNameLike(CwGlTransferLike cwGlTransferLike,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		String nameLike = cwGlTransferLike.getNameLike();
		List<CwGlTransfer> list=cwGlTransferMapper.selectByNameLike(nameLike);
		PageInfo<CwGlTransfer> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlTransfer>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (非 Javadoc)  
	 * <p>Title: searchTransferAndSubjscheme</p>  
	 * <p>Description: </p>
	 * @param cwGlTransferLike
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#searchTransferAndSubjscheme(com.dhcc.finance.main.model.CwGlTransferLike, int, int, int)    
	 * @author huangpeng
	 * @date 2019年3月29日 下午3:34:54
	 * @修改日志：
	 */
	
	@Override
	public RestfulResponse<PageInfo<CwGlTransferAndSubjectName>, CurrencyMeta> searchTransferAndSubjscheme(
			CwGlTransferLike cwGlTransferLike, int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		String nameLike = cwGlTransferLike.getNameLike();
		List<CwGlTransferAndSubjectName> list=cwGlTransferMapper.selectByName(nameLike);
		PageInfo<CwGlTransferAndSubjectName> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlTransferAndSubjectName>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: selectAllForEnablestate</p>  
	 * <p>Description:查询正在启用的结转方案 </p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferService#selectAllForEnablestate()    
	 * @author huangpeng
	 * @date 2019年7月11日 上午11:24:10
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwGlTransfer>, CurrencyMeta> selectAllForEnablestate() {
		List<CwGlTransfer> list = cwGlTransferMapper.selectAllForEnablestate();
		return  new RestfulResponse<List<CwGlTransfer>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	

}
