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 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.CwBdParametricMapper;
import com.dhcc.finance.main.domain.CwBdParametric;
import com.dhcc.finance.main.domain.CwBdParametricExample;
import com.dhcc.finance.main.domain.CwBdParametricExample.Criteria;
import com.dhcc.finance.main.service.CwBdParametricService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

@Service
public class CwBdParametricServiceImpl implements CwBdParametricService{
	@Autowired
	CwBdParametricMapper cwBdParametricMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdParametricAllData</p>  
	 * <p>Description:获取全部参数信息配置 </p>
	 * @param code
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdParametricService#searchCwBdParametricAllData(java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年6月13日 下午2:41:54
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdParametric>, CurrencyMeta> searchCwBdParametricAllData(String code, int pageNum,
			int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize,"PARAMETRIC_CODE");
		CwBdParametricExample cwBdParametricExample = new CwBdParametricExample();
		Criteria createCriteria = cwBdParametricExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		if(code!=null) {
			createCriteria.andParametricCodeLike("%"+code+"%");
		}
		List<CwBdParametric> list = cwBdParametricMapper.selectByExample(cwBdParametricExample);
		PageInfo<CwBdParametric> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdParametric>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdParametricBypk</p>  
	 * <p>Description:逻辑删除参数配置 </p>
	 * @param pkParametric
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdParametricService#deleteCwBdParametricBypk(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年6月13日 下午2:53:27
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdParametricBypk(String pkParametric) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		CwBdParametric cwBdParametric = new CwBdParametric();
		cwBdParametric.setUpdateTime(new Date());
		if(sysUserInfo!=null) {
			cwBdParametric.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdParametric.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwBdParametric.setPkParametric(pkParametric);
		int res = cwBdParametricMapper.updateByPrimaryKeySelective(cwBdParametric);
		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: addCwBdParametric</p>  
	 * <p>Description: 新增财务参数化配置</p>
	 * @param cwBdParametric
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdParametricService#addCwBdParametric(com.dhcc.finance.main.domain.CwBdParametric)    
	 * @author huangpeng
	 * @date 2019年6月13日 下午2:57:43
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdParametric(CwBdParametric cwBdParametric) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if(sysUserInfo==null) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.USER_LOGOUT_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.USER_LOGOUT_ERROR));
		}
		cwBdParametric.setPkParametric(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdParametric.setCreateUser(sysUserInfo.getUserId());
		cwBdParametric.setCreateTime(new Date());
		cwBdParametric.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		int res = cwBdParametricMapper.insert(cwBdParametric);
		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: updateCwBdParametric</p>  
	 * <p>Description:修改财务参数配置 </p>
	 * @param cwBdParametric
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdParametricService#updateCwBdParametric(com.dhcc.finance.main.domain.CwBdParametric)    
	 * @author huangpeng
	 * @date 2019年6月13日 下午3:12:47
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdParametric(CwBdParametric cwBdParametric) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		cwBdParametric.setUpdateTime(new Date());
		if(sysUserInfo!=null) {
			cwBdParametric.setUpdateUser(sysUserInfo.getUserId());
		}
		int res = cwBdParametricMapper.updateByPrimaryKeySelective(cwBdParametric);
		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: searchCwBdParametricAll</p>  
	 * <p>Description: 查询所有信息</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdParametricService#searchCwBdParametricAll()    
	 * @author huangpeng
	 * @date 2019年6月13日 下午8:54:29
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdParametric>, CurrencyMeta> searchCwBdParametricAll() {
		CwBdParametricExample cwBdParametricExample = new CwBdParametricExample();
		List<CwBdParametric> list = cwBdParametricMapper.selectByExample(cwBdParametricExample);
		return new RestfulResponse<List<CwBdParametric>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
}
