/**
 * 
 */
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.CwGlBalanceMapper;
import com.dhcc.finance.main.dao.CwGlDetailMapper;
import com.dhcc.finance.main.dao.CwGlInitbuildMapper;
import com.dhcc.finance.main.dao.CwGlVoucherMapper;
import com.dhcc.finance.main.domain.CwGlInitbuild;
import com.dhcc.finance.main.domain.CwGlInitbuildExample;
import com.dhcc.finance.main.domain.CwGlInitbuildExample.Criteria;
import com.dhcc.finance.main.domain.CwGlVoucher;
import com.dhcc.finance.main.service.CwGlInitbuildService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**
 * @author hp
 *
 */
@Service
public class CwGlInitbuildServiceImpl implements CwGlInitbuildService{
	@Autowired
	CwGlInitbuildMapper cwGlInitbuildMapper;
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	private static final String PERIODSUFFIX ="-00";//期初会计期间后缀
	
	@Autowired
	CwGlVoucherMapper cwGlVoucherMapper;//凭证信息
	
	@Autowired
	CwGlDetailMapper cwGlDetailMapper;//凭证分录信息
	
	@Autowired
	CwGlBalanceMapper cwGlBalanceMapper;//辅助余额表
	/* (非 Javadoc)  
	 * <p>Title: searchCwGlInitbuildAllData</p>  
	 * <p>Description: 查询全部期初余额设置</p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlInitbuildService#searchCwGlInitbuildAllData(int, int, int)    
	 * @author huangpeng
	 * @date 2019年4月3日 下午3:45:42
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<PageInfo<CwGlInitbuild>, CurrencyMeta> searchCwGlInitbuildAllData(int pageNum, int pageSize,
			int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		CwGlInitbuildExample cwGlInitbuildExample = new CwGlInitbuildExample();
		Criteria createCriteria = cwGlInitbuildExample.createCriteria();
		createCriteria.andUseStateEqualTo("1");
		List<CwGlInitbuild> list = cwGlInitbuildMapper.selectByExample(cwGlInitbuildExample);
		PageInfo<CwGlInitbuild> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwGlInitbuild>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/**
	 * 功能描述：TODO 期初导入后半段功能先加在这（2019/06/17 需求变更后需修改没分期募集编号）
	 *  void
	 * @author dml
	 * @date 2019年4月29日 上午9:28:46
	 * @修改日志：
	 */
	public void tempMethod(String pkGlorgbook,String period) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		// 生成大的凭证信息
		CwGlVoucher cwGlVoucher = new CwGlVoucher();
		String userId = sysUserInfo == null?null:sysUserInfo.getUserId();//用户号
		period = period.substring(0, 4) + PERIODSUFFIX;//会计期间
		String pkVoucher = SnowflakeIdWorker.getInstance().nextIdString();//凭证主键
		cwGlVoucher.setCreateUser(userId);
		cwGlVoucher.setPkPrepared(userId);
		cwGlVoucher.setPkVoucher(pkVoucher);
		cwGlVoucher.setPkGlorgbook(pkGlorgbook);
		cwGlVoucher.setPreparedate(new Date());
		cwGlVoucher.setCreateTime(new Date());
		cwGlVoucher.setPeriod(period);
		// 插入凭证信息
		cwGlVoucherMapper.insertForInitbuild(cwGlVoucher);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pkVoucher", pkVoucher);
		map.put("createUser", sysUserInfo == null?null:sysUserInfo.getUserId());
		map.put("createTime", new Date());
		map.put("pkGlorgbook", pkGlorgbook);
		// 生成凭证分录信息
		cwGlDetailMapper.insertForInitbuild(map);
		// 组成审核数据
		CwGlVoucher cwGlVoucher2 = new CwGlVoucher();
		cwGlVoucher2.setPkVoucher(pkVoucher);
		cwGlVoucher2.setUpdateUser(userId);
		cwGlVoucher2.setPkAudit(userId);
		cwGlVoucher2.setUpdateTime(new Date());
		cwGlVoucher2.setAuditdate(new Date());
		cwGlVoucher2.setIsaudit(FinanceDictConsts.Syzt.EFFECTIVE);
		cwGlVoucher2.setVouchertype(FinanceDictConsts.Cwpzzt.YSH);
		// 审核
		cwGlVoucherMapper.updateByPrimaryKeySelective(cwGlVoucher2);
		// 组成记账数据
		CwGlVoucher cwGlVoucher3 = new CwGlVoucher();
		cwGlVoucher3.setUpdateUser(userId);
		cwGlVoucher3.setPkSign(userId);
		cwGlVoucher3.setPkVoucher(pkVoucher);
		cwGlVoucher3.setUpdateTime(new Date());
		cwGlVoucher3.setSigndate(new Date());
		cwGlVoucher3.setIssign(FinanceDictConsts.Syzt.EFFECTIVE);
		cwGlVoucher3.setVouchertype(FinanceDictConsts.Cwpzzt.YJZ);
		// 记账
		cwGlVoucherMapper.updateByPrimaryKeySelective(cwGlVoucher3);
		// 初始化科目辅助余额表
		Map<String, Object> intMap = new HashMap<String, Object>();
        intMap.put("pkGlorgbook", pkGlorgbook);
        intMap.put("period", period);
        intMap.put("createUser", userId);
        intMap.put("createTime", new Date());
        cwGlBalanceMapper.insertRecalculatedBalance(intMap);
	}
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> uploadCwGlBalance(String pkGlorgbook, String period) {
		// TODO Auto-generated method stub
		tempMethod(pkGlorgbook, period);
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
