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.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.CwHsGlorgbookMapper;
import com.dhcc.finance.main.domain.CwHsGlorgbook;
import com.dhcc.finance.main.domain.CwHsGlorgbookExample;
import com.dhcc.finance.main.domain.CwHsGlorgbookExample.Criteria;
import com.dhcc.finance.main.domain.CwHsSubcodemap;
import com.dhcc.finance.main.service.CwHsGlorgbookService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
@Service
public class CwHsGlorgbookServiceImpl implements CwHsGlorgbookService{
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	CwHsGlorgbookMapper cwHsGlorgbookMapper;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwHsGlorgbookAllData</p>  
	 * <p>Description: </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#searchCwHsGlorgbookAllData(int, int, int)    
	 * @author huangpeng
	 * @date 2019年7月26日 下午7:51:39
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwHsGlorgbook>, CurrencyMeta> searchCwHsGlorgbookAllData(int pageNum, int pageSize,
			int navigatePages,String glorgbookCode) {
		PageHelper.startPage(pageNum, pageSize,"GLORGBOOK_CODE");
		List<CwHsGlorgbook> list=cwHsGlorgbookMapper.selectCwHsGlorgbookAllData(glorgbookCode);
		PageInfo<CwHsGlorgbook> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwHsGlorgbook>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchAllData</p>  
	 * <p>Description: 查询所有数据</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#searchAllData()    
	 * @author huangpeng
	 * @date 2019年7月26日 下午10:07:23
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwHsGlorgbook>, CurrencyMeta> searchAllData() {
		CwHsGlorgbookExample cwHsGlorgbookExample = new CwHsGlorgbookExample();
		Criteria createCriteria = cwHsGlorgbookExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		cwHsGlorgbookExample.setOrderByClause("GLORGBOOK_CODE");
		List<CwHsGlorgbook> list=cwHsGlorgbookMapper.selectByExample(cwHsGlorgbookExample);
		return new RestfulResponse<List<CwHsGlorgbook>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/* (non-Javadoc) <br/> 
	 * Title: deletCwHsGlorgbookinfo <br/>  
	 * Description: TODO 按照帐套Id删除帐套数据<br/>  
	 * @param Id
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#deletCwHsGlorgbookinfo(java.lang.String)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deletCwHsGlorgbookinfo(List<String>  IdList) {
		// TODO Auto-generated method stub
		int deletCwHsGlorgbookinfo = cwHsGlorgbookMapper.deletCwHsGlorgbookinfo(IdList);
		 if(deletCwHsGlorgbookinfo>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,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		} 
	}
	/* (non-Javadoc) <br/> 
	 * Title: addcwHsGlorgbook <br/>  
	 * Description: TODO 添加帐套信息<br/>  
	 * @param cwHsGlorgbook
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#addcwHsGlorgbook(com.dhcc.finance.main.domain.CwHsGlorgbook)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addcwHsGlorgbook(CwHsGlorgbook cwHsGlorgbook) {
		// TODO Auto-generated method stub
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwHsGlorgbook.setCreateUser(sysUserInfo.getUserId());
			cwHsGlorgbook.setInputMan(sysUserInfo.getUserId());
		}
		    cwHsGlorgbook.setCreateTime(new Date());
		    cwHsGlorgbook.setInputTime(new Date());
		    cwHsGlorgbook.setId(SnowflakeIdWorker.getInstance().nextIdString());
		    cwHsGlorgbook.setUseState("1");
		    cwHsGlorgbook.setCompanyFlag("5");
		    cwHsGlorgbook.setProductId(cwHsGlorgbook.getGlorgbookCode());
		    cwHsGlorgbook.setMapFlag("1");
		    if(cwHsGlorgbook.getImportMode().equals("")) {
		    	 cwHsGlorgbook.setImportMode("1");
		    }
		    int insert = cwHsGlorgbookMapper.insert(cwHsGlorgbook);
		    if(insert>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,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
			}
	}
	/* (non-Javadoc) <br/> 
	 * Title: updatacwHsGlorgbook <br/>  
	 * Description: TODO 修改帐套信息 <br/>  
	 * @param cwHsGlorgbook
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#updatacwHsGlorgbook(com.dhcc.finance.main.domain.CwHsGlorgbook)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updatacwHsGlorgbook(CwHsGlorgbook cwHsGlorgbook) {
		// TODO Auto-generated method stub
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwHsGlorgbook.setUpdateUser(sysUserInfo.getUserId());
		}
		    cwHsGlorgbook.setUpdateTime(new Date());
		    int updateByPrimaryKeySelective = cwHsGlorgbookMapper.updateByPrimaryKeySelective(cwHsGlorgbook);
		    if(updateByPrimaryKeySelective>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,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
			}
	}
	/* (non-Javadoc) <br/> 
	 * Title: searchCwHsSubcodemapAllData <br/>  
	 * Description: TODO查询帐套信息 <br/>  
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param projectId
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#searchCwHsSubcodemapAllData(int, int, int, java.lang.String)  
	 */
	@Override
	public RestfulResponse<PageInfo<CwHsSubcodemap>, CurrencyMeta> selectCwHsGlorgbookAndCwHsSub(int pageNum,
			int pageSize, int navigatePages, String productId,String subCode,String subCodeV) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		subCode=subCode.equals("")?null:subCode;
		subCodeV=subCodeV.equals("")?null:subCodeV;
		List<CwHsSubcodemap> listcwHsSubcode = cwHsGlorgbookMapper.selectCwHsGlorgbookAndCwHsSub(productId, subCode, subCodeV);
		PageInfo<CwHsSubcodemap> pageInfo = new PageInfo<>(listcwHsSubcode, navigatePages);
		return new RestfulResponse<PageInfo<CwHsSubcodemap>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/* (non-Javadoc) <br/> 
	 * Title: insertForBatch <br/>  
	 * Description: TODO <br/>  
	 * @param projectId
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#insertForBatch(java.lang.String)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> insertForBatch(String productId) {
		// TODO Auto-generated method stub
		int insertForbatch = cwHsGlorgbookMapper.insertForbatch(productId);
		cwHsGlorgbookMapper.updateMapFlog(productId);
		if(insertForbatch>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,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		}
	}
	/* (non-Javadoc) <br/> 
	 * Title: selectCwHsGlorgbookCount <br/>  
	 * Description: TODO <br/>  
	 * @param productId
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsGlorgbookService#selectCwHsGlorgbookCount(java.lang.String)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> selectCwHsGlorgbookCount(String productId) {
		// TODO Auto-generated method stub
		int selectCwHsGlorgbookCount = cwHsGlorgbookMapper.selectCwHsGlorgbookCount(productId);
		return new RestfulResponse<Object, CurrencyMeta> (SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,selectCwHsGlorgbookCount,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
