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

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

import org.apache.commons.lang.StringUtils;
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.CwBdScenarioMapper;
import com.dhcc.finance.main.domain.CwBdScenario;
import com.dhcc.finance.main.domain.CwBdScenarioExample;
import com.dhcc.finance.main.domain.CwBdScenarioExample.Criteria;
import com.dhcc.finance.main.service.CwBdScenarioService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
@Service
public class CwBdScenarioServiceImpl implements CwBdScenarioService{
	@Autowired
	CwBdScenarioMapper cwBdScenarioMapper;
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdScenarioOfList</p>  
	 * <p>Description: </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenarioService#searchCwBdScenarioOfList(int, int, int)    
	 * @author huangpeng
	 * @date 2020年2月4日 上午9:35:09
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdScenario>, CurrencyMeta> searchCwBdScenarioOfList(int pageNum, int pageSize,
			int navigatePages,String jym, String jycjmc) {
		PageHelper.startPage(pageNum, pageSize,"JYM");
		CwBdScenarioExample cwBdScenarioExample = new CwBdScenarioExample();
		Criteria createCriteria = cwBdScenarioExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		if (StringUtils.isNotBlank(jym)) {
			createCriteria.andJymLike("%"+jym+"%");
		}
		if (StringUtils.isNotBlank(jycjmc)) {
			createCriteria.andJycjmcLike("%"+jycjmc+"%");
		}
		List<CwBdScenario> list = cwBdScenarioMapper.selectByExample(cwBdScenarioExample);
		PageInfo<CwBdScenario> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdScenario>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdScenarioById</p>  
	 * <p>Description: </p>
	 * @param idList
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenarioService#deleteCwBdScenarioById(java.util.List)    
	 * @author huangpeng
	 * @date 2020年2月4日 上午9:36:02
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdScenarioById(List<String> idList) {
		int res = cwBdScenarioMapper.deleteCwBdScenarioById(idList);
		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: addCwBdScenario</p>  
	 * <p>Description: </p>
	 * @param cwBdScenario
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenarioService#addCwBdScenario(com.dhcc.finance.main.domain.CwBdScenario)    
	 * @author huangpeng
	 * @date 2020年2月4日 上午9:44:33
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdScenario(CwBdScenario cwBdScenario) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		String id = SnowflakeIdWorker.getInstance().nextIdString();
		cwBdScenario.setId(id);
		cwBdScenario.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		cwBdScenario.setCreateTime(new Date());
		cwBdScenario.setRecordAccount(FinanceDictConsts.SF.yes);
		cwBdScenario.setRecordBenefit(FinanceDictConsts.SF.no);
		cwBdScenario.setRecordSeq(FinanceDictConsts.SF.no);
		if(sysUserInfo.getUserId()!=null) {
		cwBdScenario.setCreateUser(sysUserInfo.getUserId());
		}
		int res = cwBdScenarioMapper.insert(cwBdScenario);
		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: updateCwBdScenario</p>  
	 * <p>Description: </p>
	 * @param cwBdScenario
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenarioService#updateCwBdScenario(com.dhcc.finance.main.domain.CwBdScenario)    
	 * @author huangpeng
	 * @date 2020年2月4日 上午10:09:05
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdScenario(CwBdScenario cwBdScenario) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		cwBdScenario.setUpdateTime(new Date());
		if(sysUserInfo.getUserId()!=null) {
		cwBdScenario.setUpdateUser(sysUserInfo.getUserId());
		}
		int res = cwBdScenarioMapper.updateByPrimaryKeySelective(cwBdScenario);
		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: searchCwBdScenarioAllData</p>  
	 * <p>Description:查询全部交易场景参数配置数据 </p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenarioService#searchCwBdScenarioAllData()    
	 * @author huangpeng
	 * @date 2020年2月4日 下午2:11:51
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdScenario>, CurrencyMeta> searchCwBdScenarioAllData() {
		CwBdScenarioExample cwBdScenarioExample = new CwBdScenarioExample();
		Criteria createCriteria = cwBdScenarioExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		List<CwBdScenario> res = cwBdScenarioMapper.selectByExample(cwBdScenarioExample);
		return new RestfulResponse<List<CwBdScenario>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,res,
				MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
