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

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

import org.springframework.beans.BeanUtils;
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.CwBdProductcellDetailMapper;
import com.dhcc.finance.main.dao.CwBdProductcellMapper;
import com.dhcc.finance.main.domain.CwBdProductcell;
import com.dhcc.finance.main.domain.CwBdProductcellDetail;
import com.dhcc.finance.main.domain.CwBdProductcellDetailExample;
import com.dhcc.finance.main.domain.CwBdProductcellDetailExample.Criteria;
import com.dhcc.finance.main.model.CwBdProductcellWeb;
import com.dhcc.finance.main.service.CwBdProductcellDetailService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;

/**
 * 功能描述：项目单元详情
 * @author dml
 * @date 2019年10月2日 上午10:13:32
 * @修改日志：
 */
@Service
public class CwBdProductcellDetailServiceImpl implements CwBdProductcellDetailService{

	@Autowired
	CwBdProductcellDetailMapper cwBdProductcellDetailMapper;
	@Autowired
	CwBdProductcellMapper cwBdProductcellMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/*
	 * (非 Javadoc)  
	 * Title: searchCwBdProductcellAllData 
	 * Description:根据项目单元编号查询详情
	 * @param cellCode
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdProductcellDetailService#searchCwBdProductcellAllData(java.lang.String)    
	 * @author dml
	 * @date 2019年10月2日 上午10:15:51
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdProductcellDetail>, CurrencyMeta> searchCwBdProductcellDetailByCode(String cellCode) {
		 CwBdProductcellDetailExample cwBdProductcellDetailExample = new CwBdProductcellDetailExample();
		Criteria createCriteria = cwBdProductcellDetailExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andCellCodeEqualTo(cellCode);
		List<CwBdProductcellDetail> res = cwBdProductcellDetailMapper.selectByExample(cwBdProductcellDetailExample);
		return new RestfulResponse<List<CwBdProductcellDetail>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,res,
				MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: addCwBdProductcellDetail</p>  
	 * <p>Description:新增项目单元 </p>
	 * @param cwBdProductcellWeb
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdProductcellDetailService#addCwBdProductcellDetail(com.dhcc.finance.main.model.CwBdProductcellWeb)    
	 * @author huangpeng
	 * @date 2019年10月6日 上午9:24:13
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> addCwBdProductcellDetail(CwBdProductcellWeb cwBdProductcellWeb) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		CwBdProductcell cwBdProductcell =new CwBdProductcell();
		String id = SnowflakeIdWorker.getInstance().nextIdString();
		BeanUtils.copyProperties(cwBdProductcellWeb, cwBdProductcell);
		cwBdProductcell.setId(id);
		cwBdProductcell.setCellType("1");
		cwBdProductcell.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
		cwBdProductcell.setCreateTime(new Date());
		if(sysUserInfo.getUserId()!=null) {
			cwBdProductcell.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdProductcellMapper.insert(cwBdProductcell);
		List<CwBdProductcellDetail> list=cwBdProductcellWeb.getCwBdProductcellDetailList();
		for (CwBdProductcellDetail cwBdProductcellDetail : list) {
			cwBdProductcellDetail.setCellId(id);
			cwBdProductcellDetail.setId(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdProductcellDetail.setCellCode(cwBdProductcell.getCellCode());
			cwBdProductcellDetail.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdProductcellDetail.setCreateTime(new Date());
			if(sysUserInfo.getUserId()!=null) {
				cwBdProductcellDetail.setCreateUser(sysUserInfo.getUserId());
			}
		}
		cwBdProductcellDetailMapper.insertBatch(list);
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,
				MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdProductcellDetailById</p>  
	 * <p>Description:根据项目单元主键查询详情 </p>
	 * @param cellId
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdProductcellDetailService#searchCwBdProductcellDetailById(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年10月6日 上午10:44:07
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<List<CwBdProductcellDetail>, CurrencyMeta> searchCwBdProductcellDetailById(String cellId) {
		CwBdProductcellDetailExample cwBdProductcellDetailExample = new CwBdProductcellDetailExample();
		Criteria createCriteria = cwBdProductcellDetailExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andCellIdEqualTo(cellId);
		List<CwBdProductcellDetail> res = cwBdProductcellDetailMapper.selectByExample(cwBdProductcellDetailExample);
		return new RestfulResponse<List<CwBdProductcellDetail>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,res,
				MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: updateCwBdProductcellDetail</p>  
	 * <p>Description: 修改单元</p>
	 * @param cwBdProductcellWeb
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdProductcellDetailService#updateCwBdProductcellDetail(com.dhcc.finance.main.model.CwBdProductcellWeb)    
	 * @author huangpeng
	 * @date 2019年10月6日 上午11:15:17
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> updateCwBdProductcellDetail(CwBdProductcellWeb cwBdProductcellWeb) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		CwBdProductcell cwBdProductcell =new CwBdProductcell();
		BeanUtils.copyProperties(cwBdProductcellWeb, cwBdProductcell);
		cwBdProductcell.setUpdateTime(new Date());
		if(sysUserInfo.getUserId()!=null) {
			cwBdProductcell.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdProductcellMapper.updateByPrimaryKeySelective(cwBdProductcell);
		
		CwBdProductcellDetail cwBdProductcellDetail1 =new CwBdProductcellDetail();
		cwBdProductcellDetail1.setUseState(FinanceDictConsts.Syzt.INVALID);
		CwBdProductcellDetailExample cwBdProductcellDetailExample = new CwBdProductcellDetailExample();
		Criteria createCriteria = cwBdProductcellDetailExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andCellIdEqualTo(cwBdProductcell.getId());
		cwBdProductcellDetailMapper.updateByExampleSelective(cwBdProductcellDetail1, cwBdProductcellDetailExample);
		
		List<CwBdProductcellDetail> list=cwBdProductcellWeb.getCwBdProductcellDetailList();
		for (CwBdProductcellDetail cwBdProductcellDetail : list) {
			cwBdProductcellDetail.setCellId(cwBdProductcell.getId());
			cwBdProductcellDetail.setId(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdProductcellDetail.setCellCode(cwBdProductcell.getCellCode());
			cwBdProductcellDetail.setUseState(FinanceDictConsts.Syzt.EFFECTIVE);
			cwBdProductcellDetail.setCreateTime(new Date());
			if(sysUserInfo.getUserId()!=null) {
				cwBdProductcellDetail.setCreateUser(sysUserInfo.getUserId());
			}
		}
		cwBdProductcellDetailMapper.insertBatch(list);
		
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,
				MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdProductcellDeatilOfList</p>  
	 * <p>Description:获取项目单元表详情数据列表 </p>
	 * @param cellId
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdProductcellDetailService#searchCwBdProductcellDeatilOfList(java.lang.String, int, int, int)    
	 * @author huangpeng
	 * @date 2019年10月6日 上午11:46:33
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<PageInfo<CwBdProductcellDetail>, CurrencyMeta> searchCwBdProductcellDeatilOfList(String cellId,
			int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize,"XTZXMBH");
		CwBdProductcellDetailExample cwBdProductcellDetailExample = new CwBdProductcellDetailExample();
		Criteria createCriteria = cwBdProductcellDetailExample.createCriteria();
		createCriteria.andUseStateEqualTo(FinanceDictConsts.Syzt.EFFECTIVE);
		createCriteria.andCellIdEqualTo(cellId);
		List<CwBdProductcellDetail> list = cwBdProductcellDetailMapper.selectByExample(cwBdProductcellDetailExample);
		PageInfo<CwBdProductcellDetail> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdProductcellDetail>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
}
