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.main.dao.CwBdAccassitemMapper;
import com.dhcc.finance.main.domain.CwBdAccassitem;
import com.dhcc.finance.main.domain.CwBdAccassitemExample;
import com.dhcc.finance.main.domain.CwBdAccassitemExample.Criteria;
import com.dhcc.finance.main.model.CwBdAccassitemLike;
import com.dhcc.finance.main.service.AssistedAccountingService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**
 * 功能描述：辅助核算项设置操作服务
 * @author baiyc
 * @date 
 * @修改日志：
 */
@Service
public class AssistedAccountingServiceImpl implements AssistedAccountingService{
	@Autowired
	CwBdAccassitemMapper cwBdAccassitemMapper;
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchAssistedAccountingInfo</p>  
	 * <p>Description:根据类别名称查询辅助核算项设置 </p>
	 * @param pk_accassitem
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#searchAssistedAccountingInfo(java.lang.String, int, int, int)    
	 * @author baiyc
	 * @date 2019年4月1日 上午11:41:21
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdAccassitem>, CurrencyMeta> searchAssistedAccountingInfo(CwBdAccassitemLike cwBdAccassitemLike,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		String nameLike = cwBdAccassitemLike.getNameLike();
		CwBdAccassitemExample cwBdAccassitemExample = new CwBdAccassitemExample();
		Criteria create = cwBdAccassitemExample.createCriteria();
		create.andUseStateEqualTo("1");
		if(nameLike!=null) {
		create.andNameLike("%"+nameLike+"%");
		}
		List<CwBdAccassitem> list = cwBdAccassitemMapper.selectByExample(cwBdAccassitemExample);
		PageInfo<CwBdAccassitem> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdAccassitem>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: addAssistedAccounting</p>  
	 * <p>Description:新增辅助项设置 </p>
	 * @param cwBdAccassitem
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#addAssistedAccounting(com.dhcc.finance.main.domain.CwBdAccassitem)    
	 * @author baiyc
	 * @date 2019年4月1日 上午11:52:40
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addAssistedAccounting(CwBdAccassitem cwBdAccassitem) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccassitem.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdAccassitem.setCreateTime(new Date());
		cwBdAccassitem.setPkAccassitem(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdAccassitem.setUseState("1");
		cwBdAccassitem.setEnablestate("1");
		
		int res = cwBdAccassitemMapper.insert(cwBdAccassitem);
		if (res > 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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwBdAccassitemByPk</p>  
	 * <p>Description: 修改辅助项设置</p>
	 * @param cwBdAccassitem
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#updateCwBdAccassitemByPk(com.dhcc.finance.main.domain.CwBdAccassitem)    
	 * @author baiyc
	 * @date 2019年4月4日 上午9:54:38
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdAccassitemByPk(CwBdAccassitem cwBdAccassitem) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccassitem.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdAccassitem.setUpdateTime(new Date());
		int res = cwBdAccassitemMapper.updateByPrimaryKeySelective(cwBdAccassitem);
		if (res > 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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: deleteCwBdAccassByPk</p>  
	 * <p>Description: 删除辅助项设置</p>
	 * @param pkAccassitem
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#deleteCwBdAccassByPk(java.lang.String)    
	 * @author baiyc
	 * @date 2019年4月4日 上午9:55:05
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdAccassByPk(String pkAccassitem) {
		CwBdAccassitem cwBdAccassitem = new CwBdAccassitem();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccassitem.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdAccassitem.setUpdateTime(new Date());
		cwBdAccassitem.setUseState("0");
		cwBdAccassitem.setPkAccassitem(pkAccassitem);
		int res = cwBdAccassitemMapper.updateByPrimaryKeySelective(cwBdAccassitem);
		if (res > 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.SERVER_UNKNOWN_ERROR.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: updateEnablestate</p>  
	 * <p>Description:更新状态码 </p>
	 * @param cwBdAccassitem
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#updateEnablestate(com.dhcc.finance.main.domain.CwBdAccassitem)    
	 * @author baiyc
	 * @date 2019年4月11日 下午7:44:55
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Integer, CurrencyMeta> updateEnablestate(CwBdAccassitem cwBdAccassitem) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdAccassitem.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdAccassitem.setUpdateTime(new Date());
		int res = cwBdAccassitemMapper.updateEnablestate(cwBdAccassitem);
		return new RestfulResponse<Integer, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,res,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccassitem</p>  
	 * <p>Description: 查询辅助项所有数据</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.AssistedAccountingService#searchCwBdAccassitem()    
	 * @author baiyc
	 * @date 2019年4月11日 下午7:44:19
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdAccassitem>, CurrencyMeta> searchCwBdAccassitem() {
		CwBdAccassitemExample cwBdAccassitemExample = new CwBdAccassitemExample();
        Criteria createCriteria = cwBdAccassitemExample.createCriteria();
        createCriteria.andUseStateEqualTo("1");
        List<CwBdAccassitem> listCwBdAccassitem = cwBdAccassitemMapper.selectByExample(cwBdAccassitemExample);
        return new RestfulResponse<List<CwBdAccassitem>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,listCwBdAccassitem,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
