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 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.main.dao.CwGlTransferdetailMapper;
import com.dhcc.finance.main.domain.CwGlTransferdetail;
import com.dhcc.finance.main.domain.CwGlTransferdetailExample;
import com.dhcc.finance.main.domain.CwGlTransferdetailExample.Criteria;
import com.dhcc.finance.main.model.CwGlTransferdetailAndSubName;
import com.dhcc.finance.main.service.CwGlTransferdetailService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
@Service
public class CwGlTransferdetailServiceImpl implements CwGlTransferdetailService{
	@Autowired
	CwGlTransferdetailMapper cwGlTransferdetailMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwGlTransferByNameLike</p>  
	 * <p>Description: 根据转账名称查询详情表</p>
	 * @param pkTransfer
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#searchCwGlTransferByNameLike(java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:09:22
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwGlTransferdetailAndSubName>, CurrencyMeta> searchCwGlTransferByNameLike(String pkTransfer,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwGlTransferdetailAndSubName> list = cwGlTransferdetailMapper.selectCwGlTransferdetail(pkTransfer);
		PageInfo<CwGlTransferdetailAndSubName> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlTransferdetailAndSubName>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwGlTransferdetail</p>  
	 * <p>Description: 逻辑删除转账定义详情表</p>
	 * @param pkTransferdetail
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#deleteCwGlTransferdetail(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:09:28
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> deleteCwGlTransferdetail(String pkTransferdetail) {
		 CwGlTransferdetail cwGlTransferdetail = new CwGlTransferdetail();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransferdetail.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlTransferdetail.setUpdateTime(new Date());
		cwGlTransferdetail.setUseState("0");
		cwGlTransferdetail.setPkTransferdetail(pkTransferdetail);
		int code = cwGlTransferdetailMapper.updateByPrimaryKeySelective(cwGlTransferdetail);
		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 cwGlTransferdetail
	 * @param pkTransfer
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#addCwGlTransfer(com.dhcc.finance.main.domain.CwGlTransferdetail, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:09:32
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> addCwGlTransfer(CwGlTransferdetail cwGlTransferdetail,
			String pkTransfer) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransferdetail.setCreateUser(sysUserInfo.getUserId());
			cwGlTransferdetail.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlTransferdetail.setCreateTime(new Date());
		cwGlTransferdetail.setUpdateTime(new Date());
		cwGlTransferdetail.setUseState("1");
		cwGlTransferdetail.setPkTransferdetail(SnowflakeIdWorker.getInstance().nextIdString());;
		cwGlTransferdetail.setPkTransfer(pkTransfer);
		int res = cwGlTransferdetailMapper.insert(cwGlTransferdetail);
		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: updateCwGlTransfer</p>  
	 * <p>Description:更新转账定义详情表数据 </p>
	 * @param cwGlTransferdetail
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#updateCwGlTransfer(com.dhcc.finance.main.domain.CwGlTransferdetail)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:09:35
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> updateCwGlTransfer(CwGlTransferdetail cwGlTransferdetail) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlTransferdetail.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlTransferdetail.setUpdateTime(new Date());
		int code = cwGlTransferdetailMapper.updateByPrimaryKeySelective(cwGlTransferdetail);
		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: searchCwGlTransferdetail</p>  
	 * <p>Description: 查询转账详情所有信息</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#searchCwGlTransferdetail()    
	 * @author baiyc
	 * @date 2019年4月12日 上午10:10:22
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwGlTransferdetail>, CurrencyMeta> searchCwGlTransferdetail(String pkTransfer) {
		CwGlTransferdetailExample cwGlTransferdetailExample = new CwGlTransferdetailExample();
        Criteria createCriteria = cwGlTransferdetailExample.createCriteria();
        createCriteria.andUseStateEqualTo("1");
        createCriteria.andPkTransferEqualTo(pkTransfer);
        List<CwGlTransferdetail> listCwGlTransferdetail = cwGlTransferdetailMapper.selectByExample(cwGlTransferdetailExample);
        return new RestfulResponse<List<CwGlTransferdetail>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,listCwGlTransferdetail,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwGlTransferByPkForBatch</p>  
	 * <p>Description: 批量删除结转明细 </p>
	 * @param pkDetailList
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlTransferdetailService#updateCwGlTransferByPkForBatch(java.util.List)    
	 * @author huangpeng
	 * @date 2019年8月24日 下午2:57:29
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwGlTransferByPkForBatch(List<String> pkDetailList) {
		int count = cwGlTransferdetailMapper.updateUseStateForBatch(pkDetailList);
		if (count > 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));
		}
	}

}
