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.apache.commons.lang3.StringUtils;
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.main.dao.CwHsSubcodemapMapper;
import com.dhcc.finance.main.domain.CwHsSubcodemap;
import com.dhcc.finance.main.service.CwHsSubcodemapService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

@Service
public class CwHsSubcodemapServiceImpl implements CwHsSubcodemapService{
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	CwHsSubcodemapMapper cwHsSubcodemapMapper;
	/* (non-Javadoc) <br/> 
	 * Title: searchCwHsSubcodemapAllData <br/>  
	 * Description: TODO按照科目代码和估值系统科目代码查询财务估值导出科目对照表的信息 <br/>  
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param cwHsSubcodemap
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsSubcodemapService#searchCwHsSubcodemapAllData(int, int, int, com.dhcc.finance.main.domain.CwHsSubcodemap)  
	 */
	@Override
	public RestfulResponse<PageInfo<CwHsSubcodemap>, CurrencyMeta> searchCwHsSubcodemapAllData(int pageNum,
			int pageSize, int navigatePages, CwHsSubcodemap cwHsSubcodemap) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		cwHsSubcodemap.setUseState("1");
		List<CwHsSubcodemap> listcwHsSubcode = cwHsSubcodemapMapper.selectCwHsSubcodemapInfo(cwHsSubcodemap);
		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: searchCwHsSubcodemapCodeAndCodeV <br/>  
	 * Description: TODO查询科目对照表的所有科目代码和估值科目代码 <br/>  
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsSubcodemapService#searchCwHsSubcodemapCodeAndCodeV()  
	 */
	@Override
	public RestfulResponse<Map<String, List<String>>, CurrencyMeta> searchCwHsSubcodemapCodeAndCodeV() {
		Map<String, List<String>> map=new  HashMap<String, List<String>>();
		List<String> selectCwHsSubcodeList = cwHsSubcodemapMapper.selectCwHsSubcodeList();
		List<String> selectCwHsSubcodeVList = cwHsSubcodemapMapper.selectCwHsSubcodeVList();
		map.put("selectCwHsSubcodeList", selectCwHsSubcodeList);
		map.put("selectCwHsSubcodeVList", selectCwHsSubcodeVList);
		return new RestfulResponse<Map<String, List<String>>, CurrencyMeta> (SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,map,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/* (non-Javadoc) <br/> 
	 * Title: deleteCwHsSubcodemapCodeAndCodeV <br/>  
	 * Description: TODO <br/>  
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsSubcodemapService#deleteCwHsSubcodemapCodeAndCodeV()  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwHsSubcodemapInfo(List<String>  IdList) {
		// TODO Auto-generated method stub
		int deleteByPrimaryKey = cwHsSubcodemapMapper.deleteById(IdList);
		if(deleteByPrimaryKey>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.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		}
	}
	/* (non-Javadoc) <br/> 
	 * Title: addCwHsSubcodemapInfo <br/>  
	 * Description: TODO 新增估值导出科目对照表 <br/>  
	 * @param cwHsSubcodemap
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsSubcodemapService#addCwHsSubcodemapInfo(com.dhcc.finance.main.domain.CwHsSubcodemap)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwHsSubcodemapInfo(CwHsSubcodemap cwHsSubcodemap) {
		// TODO Auto-generated method stub
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwHsSubcodemap.setCreateUser(sysUserInfo.getUserId());
		}
		cwHsSubcodemap.setId(SnowflakeIdWorker.getInstance().nextIdString());
		cwHsSubcodemap.setBookCode(1);  // 帐套代码默认为1
		cwHsSubcodemap.setCreateTime(new  Date());
		cwHsSubcodemap.setCompanyFlag(StringUtils.isBlank(cwHsSubcodemap.getCompanyFlag())?"5":cwHsSubcodemap.getCompanyFlag());
		cwHsSubcodemap.setUseState("1"); // 使用状态默认为1
		if(cwHsSubcodemap.getImportFlag().equals("")) {
			cwHsSubcodemap.setImportFlag("2");
		}
		int insert = cwHsSubcodemapMapper.insert(cwHsSubcodemap);
		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.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		}
	}
	/* (non-Javadoc) 修正估值导出科目对照表 <br/> 
	 * Title: updataCwHsSubcodemapInfo <br/>  
	 * Description: TODO <br/>  
	 * @param cwHsSubcodemap
	 * @return  
	 * @see com.dhcc.finance.main.service.CwHsSubcodemapService#updataCwHsSubcodemapInfo(com.dhcc.finance.main.domain.CwHsSubcodemap)  
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updataCwHsSubcodemapInfo(CwHsSubcodemap cwHsSubcodemap) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwHsSubcodemap.setUpdateUser(sysUserInfo.getUserId());
		}
		cwHsSubcodemap.setUpdateTime(new  Date());
		int update = cwHsSubcodemapMapper.updateByPrimaryKeySelective(cwHsSubcodemap);
		if(update>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.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_FAIL,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));	
		}

	}
	

}
