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

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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 org.springframework.web.client.RestTemplate;

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.PKDataUtil;
import com.dhcc.common.util.SnowflakeIdWorker;
import com.dhcc.finance.constant.FinanceDictConsts;
import com.dhcc.finance.main.dao.CwBdAccassMapper;
import com.dhcc.finance.main.dao.CwBdAccsubjMapper;
import com.dhcc.finance.main.dao.CwBdSubjschemeMapper;
import com.dhcc.finance.main.domain.CwBdAccsubj;
import com.dhcc.finance.main.domain.CwBdSubjscheme;
import com.dhcc.finance.main.model.CwBdSubjschemeAdd;
import com.dhcc.finance.main.service.CwBdSubjschemeService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
/**
 * 功能描述：会计科目版本操作接口实现
 * @author dml
 * @date 2019年3月21日 下午5:01:01
 * @修改日志：
 */
@Service
public class CwBdSubjschemeServiceImpl implements CwBdSubjschemeService {
	@Autowired
	CwBdSubjschemeMapper cwBdSubjschemeMapper;
	
	@Autowired
	CwBdAccsubjMapper cwBdAccsubjMapper;
	
	@Autowired
	CwBdAccassMapper cwBdAccassMapper;

	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
    private RestTemplate loadBalancedRestTemplate;
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: addCwBdSubjscheme</p>  
	 * <p>Description: 新增会计科目版本
	 * @param cwBdSubjscheme
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#addCwBdSubjscheme(com.dhcc.finance.main.domain.CwBdSubjscheme)    
	 * @author dml
	 * @date 2019年3月21日 下午7:08:55
	 * @修改日志：
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdSubjscheme(CwBdSubjschemeAdd cwBdSubjschemeAdd) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdSubjschemeAdd.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdSubjschemeAdd.setCreateTime(new Date());
		String rowkey = String.valueOf(SnowflakeIdWorker.getInstance().nextIdString());
		String code = PKDataUtil.getPK(FinanceDictConsts.PK.subjscheme,stringRedisTemplate,loadBalancedRestTemplate);
		cwBdSubjschemeAdd.setCode(code);  
		cwBdSubjschemeAdd.setPkSubjscheme(rowkey);
		cwBdSubjschemeAdd.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		
		CwBdSubjscheme cwBdSubjscheme = new CwBdSubjscheme();
		BeanUtils.copyProperties(cwBdSubjschemeAdd, cwBdSubjscheme);
		cwBdSubjschemeMapper.insert(cwBdSubjscheme);
		if (StringUtils.isNotBlank(cwBdSubjschemeAdd.getChoosesubject())) {// 选择了复制版本
			//复制科目信息
			cwBdAccsubjMapper.insertCpCwBdAccsubj(cwBdSubjschemeAdd);
			//复制完毕后刷新父节点
			cwBdAccsubjMapper.updateParentid(cwBdSubjschemeAdd);
			//复制辅助信息
			cwBdAccassMapper.insertCpCwBdAccass(cwBdSubjschemeAdd);
		}else {//未选择复制版本新增一条顶级科目
			List<CwBdAccsubj>  cwBdAccsubjList=new  ArrayList<CwBdAccsubj>(); 
			CwBdAccsubj cwBdAccsubj = new CwBdAccsubj();
			CwBdAccsubj cwBdAccsubjzc = new CwBdAccsubj();
			CwBdAccsubj cwBdAccsubjfz = new CwBdAccsubj();
			CwBdAccsubj cwBdAccsubjsyzqy = new CwBdAccsubj();
			CwBdAccsubj cwBdAccsubsyl = new CwBdAccsubj();
			if (sysUserInfo != null) {
				cwBdAccsubj.setCreateUser(sysUserInfo.getUserId());
				cwBdAccsubjzc.setCreateUser(sysUserInfo.getUserId());
				cwBdAccsubjfz.setCreateUser(sysUserInfo.getUserId());
				cwBdAccsubjsyzqy.setCreateUser(sysUserInfo.getUserId());
				cwBdAccsubsyl.setCreateUser(sysUserInfo.getUserId());
			}
			/*会计类*/
			String valueOfKj = String.valueOf(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccsubj.setPkSubjscheme(rowkey);
			cwBdAccsubj.setCreateTime(new Date());
			cwBdAccsubj.setPkAccsubj(valueOfKj);
			cwBdAccsubj.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdAccsubj.setSubjcode("00");
			cwBdAccsubj.setSubjlev("0");
			cwBdAccsubj.setSubjname("会计科目");
			cwBdAccsubj.setDispname("会计科目");
			cwBdAccsubj.setParentid("-1");
			cwBdAccsubj.setEnablestate("1");
			cwBdAccsubj.setFinalstage("0");
			/*资产类*/
			cwBdAccsubjzc.setPkSubjscheme(rowkey);
			cwBdAccsubjzc.setCreateTime(new Date());
			cwBdAccsubjzc.setPkAccsubj(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccsubjzc.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdAccsubjzc.setSubjcode("01");
			cwBdAccsubjzc.setSubjlev("0");
			cwBdAccsubjzc.setSubjname("资产类");
			cwBdAccsubjzc.setDispname("资产类");
			cwBdAccsubjzc.setParentid(valueOfKj);
			cwBdAccsubjzc.setEnablestate("1");
			cwBdAccsubjzc.setFinalstage("0");
			/*负债类*/
			cwBdAccsubjfz.setPkSubjscheme(rowkey);
			cwBdAccsubjfz.setCreateTime(new Date());
			cwBdAccsubjfz.setPkAccsubj(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccsubjfz.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdAccsubjfz.setSubjcode("02");
			cwBdAccsubjfz.setSubjlev("0");
			cwBdAccsubjfz.setSubjname("负债类");
			cwBdAccsubjfz.setDispname("负债类");
			cwBdAccsubjfz.setParentid(valueOfKj);
			cwBdAccsubjfz.setEnablestate("1");
			cwBdAccsubjfz.setFinalstage("0");
            /*所有者权益类*/
			cwBdAccsubjsyzqy.setPkSubjscheme(rowkey);
			cwBdAccsubjsyzqy.setCreateTime(new Date());
			cwBdAccsubjsyzqy.setPkAccsubj(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccsubjsyzqy.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdAccsubjsyzqy.setSubjcode("03");
			cwBdAccsubjsyzqy.setSubjlev("0");
			cwBdAccsubjsyzqy.setSubjname("所有者权益类");
			cwBdAccsubjsyzqy.setDispname("所有者权益类");
			cwBdAccsubjsyzqy.setParentid(valueOfKj);
			cwBdAccsubjsyzqy.setEnablestate("1");
			cwBdAccsubjsyzqy.setFinalstage("0");
			/*损益类*/
			cwBdAccsubsyl.setPkSubjscheme(rowkey);
			cwBdAccsubsyl.setCreateTime(new Date());
			cwBdAccsubsyl.setPkAccsubj(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccsubsyl.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdAccsubsyl.setSubjcode("04");
			cwBdAccsubsyl.setSubjlev("0");
			cwBdAccsubsyl.setSubjname("损益类");
			cwBdAccsubsyl.setDispname("损益类");
			cwBdAccsubsyl.setParentid(valueOfKj);
			cwBdAccsubsyl.setEnablestate("1");
			cwBdAccsubsyl.setFinalstage("0");
			cwBdAccsubjList.add(cwBdAccsubj);
			cwBdAccsubjList.add(cwBdAccsubjzc);
			cwBdAccsubjList.add(cwBdAccsubjfz);
			cwBdAccsubjList.add(cwBdAccsubjsyzqy);
			cwBdAccsubjList.add(cwBdAccsubsyl);
			cwBdAccsubjMapper.inserts(cwBdAccsubjList);
		}
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdSubjschemeAllData</p>  
	 * <p>Description: 查询全部版本
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#searchCwBdSubjschemeAllData(int, int, int)    
	 * @author dml
	 * @date 2019年3月21日 下午7:20:09
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdSubjscheme>, CurrencyMeta> searchCwBdSubjschemeAllData(int pageNum,
			int pageSize, int navigatePages, String nameLike) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwBdSubjscheme> selectByExample = cwBdSubjschemeMapper.searchCwBdSubjschemeAllData(nameLike);
		PageInfo<CwBdSubjscheme> pageInfo = new PageInfo<>(selectByExample, navigatePages);
		return new RestfulResponse<PageInfo<CwBdSubjscheme>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwBdSubjschemeByPk</p>  
	 * <p>Description:通过主键修改版本信息
	 * @param postId
	 * @param sysPostInfo
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#updateCwBdSubjschemeByPk(java.lang.String, com.dhcc.finance.main.domain.CwBdSubjscheme)    
	 * @author dml
	 * @date 2019年3月21日 下午7:52:14
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdSubjschemeByPk(CwBdSubjscheme cwBdSubjscheme) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdSubjscheme.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdSubjscheme.setUpdateTime(new Date());
		int res = cwBdSubjschemeMapper.updateByPrimaryKeySelective(cwBdSubjscheme);
		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_FAIL));
		}
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdSubjschemeByPk</p>  
	 * <p>Description: 逻辑删除版本信息
	 * @param pk
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#deleteCwBdSubjschemeByPk(java.lang.String)    
	 * @author dml
	 * @date 2019年3月21日 下午8:16:58
	 * @修改日志：
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdSubjschemeByPk(String pkSubjscheme) {
		CwBdSubjscheme cwBdSubjscheme = new CwBdSubjscheme();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdSubjscheme.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdSubjscheme.setUpdateTime(new Date());
		cwBdSubjscheme.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwBdSubjscheme.setPkSubjscheme(pkSubjscheme);
		cwBdSubjschemeMapper.updateByPrimaryKeySelective(cwBdSubjscheme);//逻辑删除科目版本
		cwBdAccsubjMapper.updateByPkSubjscheme(cwBdSubjscheme);//逻辑删除科目
		cwBdAccassMapper.updateByPkSubjscheme(cwBdSubjscheme);//逻辑删除科目辅助
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * <p>Title: updateEnablestate</p>  
	 * <p>Description: 会计科目版本启动
	 * @param cwBdSubjscheme
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#updateEnablestate(com.dhcc.finance.main.domain.CwBdSubjscheme)    
	 * @author dml
	 * @date 2019年3月23日 上午11:46:52
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Integer, CurrencyMeta> updateEnablestate(CwBdSubjscheme cwBdSubjscheme) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdSubjscheme.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdSubjscheme.setUpdateTime(new Date());
		int res = cwBdSubjschemeMapper.updateEnablestate(cwBdSubjscheme);
		return new RestfulResponse<Integer, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,res,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (非 Javadoc)  
	 * <p>Title: searchCwBdSubjschemePKandCode</p>  
	 * <p>Description: </p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#searchCwBdSubjschemePKandCode()    
	 * @author huangpeng
	 * @date 2019年3月29日 下午3:12:04
	 * @修改日志：
	 */
	
	@Override
	public RestfulResponse<List<CwBdSubjscheme>, CurrencyMeta> searchCwBdSubjschemePKandCode() {
		List<CwBdSubjscheme> list = cwBdSubjschemeMapper.selectAll();
		return new RestfulResponse<List<CwBdSubjscheme>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/* (non-Javadoc) <br/> 
	 * Title: selectByEnablestate <br/>  
	 * Description: TODO <br/>  
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdSubjschemeService#selectByEnablestate()  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> selectNumByEnablestate() {
		// TODO Auto-generated method stub
		int selectNumByEnablestate = cwBdSubjschemeMapper.selectNumByEnablestate();
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,selectNumByEnablestate,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	@Override
	public RestfulResponse<List<CwBdAccsubj>, CurrencyMeta> selectAllSubjCode() {
		// TODO Auto-generated method stub
		List<CwBdAccsubj> selectAllSubjcode = cwBdSubjschemeMapper.selectAllSubjcode();
		return new RestfulResponse<List<CwBdAccsubj>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,selectAllSubjcode,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
