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.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.SnowflakeIdWorker;
import com.dhcc.finance.constant.FinanceDictConsts;
import com.dhcc.finance.main.dao.CwBdAccassMapper;
import com.dhcc.finance.main.domain.CwBdAccass;
import com.dhcc.finance.main.domain.CwBdAccassExample;
import com.dhcc.finance.main.domain.CwBdAccassExample.Criteria;
import com.dhcc.finance.main.service.CwBdAccassService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**
 * 功能描述：科目辅助配置服务
 * @author dml
 * @date 2019年3月22日 下午1:51:06
 * @修改日志：
 */
@Service
public class CwBdAccassServiceImpl implements CwBdAccassService{
	
	@Autowired
	CwBdAccassMapper cwBdAccassMapper;
	
	@Autowired
	private Tracer tracer;
	
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: addCwBdSubjscheme</p>  
	 * <p>Description:新增科目辅助配置
	 * @param cwBdAccass
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdAccassService#addCwBdSubjscheme(com.dhcc.finance.main.domain.CwBdAccass)    
	 * @author dml
	 * @date 2019年3月22日 下午1:56:38
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdAccass(CwBdAccass cwBdAccass) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccass.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdAccass.setCreateTime(new Date());
		cwBdAccass.setPkAccass(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdAccass.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		cwBdAccass.setIsmust("1");
		int res = cwBdAccassMapper.insert(cwBdAccass);
		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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwBdAccsubjByPk</p>  
	 * <p>Description: 修改科目辅助配置
	 * @param cwBdAccass
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdAccassService#updateCwBdAccsubjByPk(com.dhcc.finance.main.domain.CwBdAccass)    
	 * @author dml
	 * @date 2019年3月22日 下午1:56:45
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdAccassByPk(CwBdAccass cwBdAccass) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccass.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdAccass.setUpdateTime(new Date());
		int res = cwBdAccassMapper.updateByPrimaryKeySelective(cwBdAccass);
		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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdAccsubjByPk</p>  
	 * <p>Description: 逻辑删除科目辅助配置
	 * @param pkAccass
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdAccassService#deleteCwBdAccsubjByPk(java.lang.String)    
	 * @author dml
	 * @date 2019年3月22日 下午1:56:53
	 * @修改日志：
	 */
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdAccassByPk(String pkAccass) {
		Long count = cwBdAccassMapper.selectIsExistsFreeForVoucher(pkAccass);
		if(count>0){
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL, FinanceStatusCode.VOUNCHER_HAS_ACCASS.message(), 
					MetaDataUtil.getMeta(tracer, FinanceStatusCode.VOUNCHER_HAS_ACCASS));
		}
		CwBdAccass cwBdAccass = new CwBdAccass();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccass.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdAccass.setUpdateTime(new Date());
		cwBdAccass.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwBdAccass.setPkAccass(pkAccass);
		int res = cwBdAccassMapper.updateByPrimaryKeySelective(cwBdAccass);
		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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccsubjAllData</p>  
	 * <p>Description: 根据科目查询科目辅助配置
	 * @param pkAccsubj
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdAccassService#searchCwBdAccsubjAllData(java.lang.String, int, int, int)    
	 * @author dml
	 * @date 2019年3月22日 下午1:56:56
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdAccass>, CurrencyMeta> searchCwBdAccassInfo(String pkAccsubj, int pageNum,
			int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize,"ASSORDER");
		CwBdAccassExample cwBdAccassExample = new CwBdAccassExample();
		cwBdAccassExample.setOrderByClause("SUBJCODE");
		Criteria create = cwBdAccassExample.createCriteria();
		create.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		create.andPkAccsubjEqualTo(pkAccsubj);
		List<CwBdAccass> selectByExample = cwBdAccassMapper.selectByExample(cwBdAccassExample);
		PageInfo<CwBdAccass> pageInfo = new PageInfo<>(selectByExample, navigatePages);
		return new RestfulResponse<PageInfo<CwBdAccass>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccassforAddVoucher</p>  
	 * <p>Description:  根据科目主键科目版本主键查询科目下辅助配置信息</p>
	 * @param pkAccsubj
	 * @param pkSubjscheme
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdAccassService#searchCwBdAccassforAddVoucher(java.lang.String, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年6月17日 上午10:26:36
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdAccass>, CurrencyMeta> searchCwBdAccassforAddVoucher(String pkAccsubj,
			String pkSubjscheme) {
		CwBdAccassExample cwBdAccassExample = new CwBdAccassExample();
		Criteria create = cwBdAccassExample.createCriteria();
		create.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		create.andPkAccsubjEqualTo(pkAccsubj);
		create.andPkSubjschemeEqualTo(pkSubjscheme);
		List<CwBdAccass> list = cwBdAccassMapper.selectByExample(cwBdAccassExample);
		return new RestfulResponse<List<CwBdAccass>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
