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

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

import com.dhcc.finance.main.dao.CwBdAccassitemMapper;
import com.dhcc.finance.main.dao.CwGlFreevalueMapper;
import com.dhcc.finance.main.domain.CwBdAccassitem;
import org.apache.commons.lang3.StringUtils;
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.finance.main.dao.CwBdSubjTransformationMapper;
import com.dhcc.finance.main.dao.CwGlBalanceMapper;
import com.dhcc.finance.main.domain.CwBdSubjTransformation;
import com.dhcc.finance.main.service.CwBdSubjTransformationService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

@Service
public class CwBdSubjTransformationServiceImpl implements CwBdSubjTransformationService {
	
	@Autowired
	CwBdSubjTransformationMapper cwBdSubjTransformationMapper;

	@Autowired
	CwBdAccassitemMapper cwBdAccassitemMapper;

	@Autowired
	CwGlFreevalueMapper cwGlFreevalueMapper;
	
	@Autowired
	CwGlBalanceMapper cwGlBalanceMapper;
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * (非 Javadoc) 
	 * <p>Title: selectCwBdSubjTransformation</p> 
	 * <p>Description:按照账套主键和科目代码查询科目转换信息 </p> 
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param subjcode
	 * @param pkGlorgbook
	 * @return 
	 * @see com.dhcc.finance.main.service.CwBdSubjTransformationService#selectCwBdSubjTransformation(int, int, int, java.lang.String, java.lang.String)
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdSubjTransformation>, CurrencyMeta> selectCwBdSubjTransformation(int pageNum, int pageSize,
			int navigatePages, String subjcode, String pkGlorgbook) {
		// TODO Auto-generated method stub
		subjcode=StringUtils.isBlank(subjcode)?null:subjcode; 
		PageHelper.startPage(pageNum, pageSize);
		List<CwBdSubjTransformation> list = cwBdSubjTransformationMapper.searchSubjTransInfo(subjcode, pkGlorgbook);
		PageInfo<CwBdSubjTransformation> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdSubjTransformation>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));

	}
	/**
	 * @Title: updateCwBdSubjschemeByPk
	 * @Description: TODO(根据账套编号和科目代码转换科目)
	 * @author lipeng
	 * @param @param pkGlorgbook
	 * @param @param pk_accsubj
	 * @param @param subjcodeN
	 * @param @param subjname
	 * @param @param subjcodeO
	 * @param @return
	 * @return RestfulResponse<Object,CurrencyMeta>
	 * @see
	 * @throws
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public RestfulResponse<Object, CurrencyMeta> updatecwBdSubjTransformation(String pkGlorgbook, String pk_accsubj,
			String subjcodeN, String subjname, String subjcodeO) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		String userId = sysUserInfo.getUserId();
		cwGlBalanceMapper.updateBalanceforSubjcode(pkGlorgbook,pk_accsubj, subjcodeN, subjname,subjcodeO,userId);

		List<CwBdAccassitem> listO = new ArrayList();
		List<CwBdAccassitem> listN = new ArrayList();
		listO = cwBdAccassitemMapper.selectCheckType(subjcodeO);
		listN = cwBdAccassitemMapper.selectCheckType(subjcodeN);
		if(listO.size() == 0 || listN.size() == 0 ) {
            int updateSubjTransInfo = cwBdSubjTransformationMapper.updateSubjTransInfo(pkGlorgbook, pk_accsubj, subjcodeN, subjname, subjcodeO);
			return response(updateSubjTransInfo);
        }
		if (!listO.containsAll(listN)) {
		    for (int i = 0; i < listO.size(); i++) {
                String checkTypeO = listO.get(i).getPkAccassitem();
                String checkTypeN = listN.get(i).getPkAccassitem();
                cwGlFreevalueMapper.updateCheckType(pkGlorgbook, subjcodeO, checkTypeO, checkTypeN);
            }
			int updateSubjTransInfo = cwBdSubjTransformationMapper.updateSubjTransInfo(pkGlorgbook, pk_accsubj, subjcodeN, subjname, subjcodeO);
			return response(updateSubjTransInfo);
        } else {
			int updateSubjTransInfo = cwBdSubjTransformationMapper.updateSubjTransInfo(pkGlorgbook, pk_accsubj, subjcodeN, subjname, subjcodeO);
			return response(updateSubjTransInfo);
        }
	}
	public RestfulResponse<Object, CurrencyMeta> response(int count) {
		if (count > 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));
		}
	}
}
