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

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.dao.CwBdScenariorelationMapper;
import com.dhcc.finance.main.domain.CwBdScenariorelation;
import com.dhcc.finance.main.domain.CwBdScenariorelationExample;
import com.dhcc.finance.main.domain.CwBdScenariorelationExample.Criteria;
import com.dhcc.finance.main.service.CwBdScenariorelationService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**    
 * @author lipeng
 * Description:交易场景凭证模板对应关系实现类   
 * @date 2019年6月27日 上午11:32:31
 * @path com.dhcc.finance.main.service.impl.CwBdScenariorelationServiceImpl
 */
@Service
public class CwBdScenariorelationServiceImpl implements CwBdScenariorelationService {

	/* (non-Javadoc)
	 * @see com.dhcc.finance.main.service.CwBdScenariorelationService#searchCwBdVouchtempAllData(int, int, int, com.dhcc.finance.main.domain.CwBdScenariorelation)
	 */
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private   CwBdScenariorelationMapper  cwBdScenariorelationMapper;
	
	@Autowired
	private  CwBdScenarioMapper  cwBdScenarioMapper;
	
	@Override
	public RestfulResponse<PageInfo<CwBdScenariorelation>, CurrencyMeta> searchcwBdScenariorelationAllData(int pageNum,
			int pageSize, int navigatePages,String ywlx,String jym,String jycjmc) {
		// TODO Auto-generated method stub
		
		PageHelper.startPage(pageNum, pageSize,"JYM");
		CwBdScenariorelationExample cwBdScenariorelationExample = new CwBdScenariorelationExample();
		Criteria createCriteria = cwBdScenariorelationExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		if (StringUtils.isNotBlank(ywlx)) {
			createCriteria.andYwlxEqualTo(ywlx);
		}
		if (StringUtils.isNotBlank(jym)) {
			createCriteria.andJymLike("%"+jym+"%");
		}
		if (StringUtils.isNotBlank(jycjmc)) {
			createCriteria.andJycjmcLike("%"+jycjmc+"%");
		}
		List<CwBdScenariorelation> selectByExample = cwBdScenariorelationMapper.selectByExample(cwBdScenariorelationExample);
		PageInfo<CwBdScenariorelation> pageInfo = new PageInfo<>(selectByExample, navigatePages);
		return new RestfulResponse<PageInfo<CwBdScenariorelation>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (non-Javadoc)
	 * @see com.dhcc.finance.main.service.CwBdScenariorelationService#addCwBdScenariorelation(com.dhcc.finance.main.domain.CwBdScenariorelation)
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdScenariorelation(CwBdScenariorelation cwBdScenariorelation) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdScenariorelation.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdScenariorelation.setCreateTime(new Date());
		cwBdScenariorelation.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		String rowKey = SnowflakeIdWorker.getInstance().nextIdString();
		cwBdScenariorelation.setId(rowKey);
		cwBdScenariorelationMapper.insert(cwBdScenariorelation);
 		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (non-Javadoc)
	 * @see com.dhcc.finance.main.service.CwBdScenariorelationService#updateCwBdScenariorelation(com.dhcc.finance.main.domain.CwBdScenariorelation)
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdScenariorelation(CwBdScenariorelation cwBdScenariorelation) {
		// TODO Auto-generated method stub
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdScenariorelation.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdScenariorelation.setUpdateTime(new Date());
		int updateByPrimaryKey = cwBdScenariorelationMapper.updateByPrimaryKeySelective(cwBdScenariorelation);
		if(updateByPrimaryKey>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.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
		}
	}

	/* (non-Javadoc)
	 * @see com.dhcc.finance.main.service.CwBdScenariorelationService#deleteCwBdScenariorelation(java.lang.String)
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdScenariorelation(String id) {
		// TODO Auto-generated method stub
		int deleteCwBdScenariorelatioByPrimaryKey = cwBdScenariorelationMapper.deleteCwBdScenariorelatioByPrimaryKey(id);
		if(deleteCwBdScenariorelatioByPrimaryKey>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.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		}
	}

	/* (non-Javadoc) <br/> 
	 * Title: selectScenarioreName <br/>  
	 * Description: TODO <br/>  
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdScenariorelationService#selectScenarioreName()  
	 */
	@Override
	public RestfulResponse<Map<String,List<String>>, CurrencyMeta> selectScenarioreName() {
		// TODO Auto-generated method stub
		List<String> selectScenarioreName = cwBdScenariorelationMapper.selectScenarioreName();
		List<String> selectScenarioreJyM = cwBdScenarioMapper.selectScenarioreJyM();
		Map<String,List<String>>     map=new  HashMap<String, List<String>>();
		map.put("ScenarioreName", selectScenarioreName);
		map.put("ScenarioreJyM", selectScenarioreJyM);
		return new RestfulResponse<Map<String,List<String>>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,map,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
