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

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

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.constant.FinanceDictConsts;
import com.dhcc.finance.main.dao.CwBdAccassitemMapper;
import com.dhcc.finance.main.dao.CwGlFreevalueMapper;
import com.dhcc.finance.main.domain.CwBdAccassitem;
import com.dhcc.finance.main.domain.CwGlFreevalue;
import com.dhcc.finance.main.domain.CwGlFreevalueExample;
import com.dhcc.finance.main.domain.CwGlFreevalueExample.Criteria;
import com.dhcc.finance.main.model.CwGlFreevalueInfo;
import com.dhcc.finance.main.service.CwGlFreevalueService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
/**
 * 功能描述：辅助标识实现
 * @author dml
 * @date 2019年3月28日 下午7:40:50
 * @修改日志：
 */
@Service
public class CwGlFreevalueServiceImpl implements CwGlFreevalueService{
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	CwGlFreevalueMapper cwGlFreevalueMapper;

	@Autowired
	CwBdAccassitemMapper cwBdAccassitemMapper;//辅助项表
	/*
	 * (非 Javadoc)  
	 * Title: searchCwGlFreevalueAllData 
	 * Description:查询全部辅助标识
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlFreevalueService#searchCwGlFreevalueAllData(int, int, int)    
	 * @author dml
	 * @date 2019年3月28日 下午7:44:33
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwGlFreevalue>, CurrencyMeta> searchCwGlFreevalueAllData(int pageNum, int pageSize,
			int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		CwGlFreevalueExample cwGlFreevalueExample = new CwGlFreevalueExample();
		Criteria createCriteria = cwGlFreevalueExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		List<CwGlFreevalue> listCwGlFreevalue = cwGlFreevalueMapper.selectByExample(cwGlFreevalueExample);
		PageInfo<CwGlFreevalue> pageInfo = new PageInfo<>(listCwGlFreevalue, navigatePages);
		return new RestfulResponse<PageInfo<CwGlFreevalue>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: addCwGlFreevalue 
	 * Description:新增辅助标识
	 * @param cwGlFreevalue
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlFreevalueService#addCwGlFreevalue(com.dhcc.finance.main.domain.CwGlFreevalue)    
	 * @author dml
	 * @date 2019年3月28日 下午7:44:55
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwGlFreevalue(CwGlFreevalue cwGlFreevalue) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlFreevalue.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlFreevalue.setUpdateTime(new Date());
		cwGlFreevalue.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		cwGlFreevalue.setPkFreevalue(SnowflakeIdWorker.getInstance().nextIdString());
		cwGlFreevalueMapper.insert(cwGlFreevalue);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: updateCwGlFreevalueByPk 
	 * Description:修改辅助标识
	 * @param cwGlFreevalue
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlFreevalueService#updateCwGlFreevalueByPk(com.dhcc.finance.main.domain.CwGlFreevalue)    
	 * @author dml
	 * @date 2019年3月28日 下午7:45:07
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwGlFreevalueByPk(CwGlFreevalue cwGlFreevalue) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlFreevalue.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlFreevalue.setUpdateTime(new Date());
		cwGlFreevalueMapper.updateByPrimaryKeySelective(cwGlFreevalue);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: deleteCwGlFreevalueByPk 
	 * Description:逻辑删除辅助标识
	 * @param pkDetail
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlFreevalueService#deleteCwGlFreevalueByPk(java.lang.String)    
	 * @author dml
	 * @date 2019年3月28日 下午7:45:17
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwGlFreevalueByPk(String pkFreevalue) {
		CwGlFreevalue cwGlFreevalue = new CwGlFreevalue();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwGlFreevalue.setUpdateUser(sysUserInfo.getUserId());
		}
		cwGlFreevalue.setUpdateTime(new Date());
		cwGlFreevalue.setUseState(FinanceDictConsts.Syzt.INVALID);
		cwGlFreevalue.setPkFreevalue(pkFreevalue);
		cwGlFreevalueMapper.updateByPrimaryKeySelective(cwGlFreevalue);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

	/*
	 * (非 Javadoc)  
	 * Title: searchCwGlFreevalueByAssid 
	 * Description:根据assid查询辅助项
	 * @param assid
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwGlFreevalueService#searchCwGlFreevalueByAssid(java.lang.String, int, int, int)    
	 * @author dml
	 * @date 2019年8月7日 下午2:19:12
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwGlFreevalueInfo>, CurrencyMeta> searchCwGlFreevalueByAssid(String assid, int pageNum,
			int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		List<CwGlFreevalueInfo> listCwGlFreevalue = cwGlFreevalueMapper.searchByAssid(assid);
		listCwGlFreevalue.forEach(y -> {
			CwBdAccassitem cwBdAccassitem = cwBdAccassitemMapper.selectByPrimaryKey(y.getChecktype());
			// 拼接动态sql TODO 因各个模块字段未统一调整暂时不加使用状态
			String sql = "select "+cwBdAccassitem.getCodefield()+" as checkname from "+ cwBdAccassitem.getTablecode()+
					"  where "+cwBdAccassitem.getPkfield() + " = '"+y.getCheckvalue()+"'";
			String res = cwBdAccassitemMapper.selectCheckname(sql);
			if (StringUtils.isNotBlank(res)) {
				y.setCheckname(res);
			} else {
				y.setCheckname(y.getCheckvalue());
			}
			y.setChecktype(y.getChecktype());
			y.setCheckvalue(y.getCheckvalue());
			y.setDebitamount(y.getDebitamount());
			y.setPkSystem(y.getPkSystem());
			y.setName(cwBdAccassitem.getName());
		});
		PageInfo<CwGlFreevalueInfo> pageInfo = new PageInfo<>(listCwGlFreevalue, navigatePages);
		return new RestfulResponse<PageInfo<CwGlFreevalueInfo>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,
				pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
