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

import java.io.File;
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 org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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.ExcelUtils;
import com.dhcc.common.util.SnowflakeIdWorker;
import com.dhcc.finance.main.dao.CwBdVouchtempcomMapper;
import com.dhcc.finance.main.domain.CwBdVouchtempcom;
import com.dhcc.finance.main.domain.CwBdVouchtempcomExample;
import com.dhcc.finance.main.domain.CwBdVouchtempcomExample.Criteria;
import com.dhcc.finance.main.model.CwBdVouchtempcomLike;
import com.dhcc.finance.main.model.FileUploadRequest;
import com.dhcc.finance.main.service.CwBdVouchtempcomService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class CwBdVouchtempcomServiceImpl implements CwBdVouchtempcomService{
	@Autowired
	CwBdVouchtempcomMapper cwBdVouchtempcomMapper;
	
	@Autowired
	private Tracer tracer;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdVouchtempcomInfo</p>  
	 * <p>Description: 模糊查询</p>
	 * @param cwBdVouchtempcomLike
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#searchCwBdVouchtempcomInfo(com.dhcc.finance.main.model.CwBdVouchtempcomLike, int, int, int)    
	 * @author baiyc
	 * @date 2019年4月5日 上午10:50:21
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwBdVouchtempcom>, CurrencyMeta> searchCwBdVouchtempcomByNameLike(
			CwBdVouchtempcomLike cwBdVouchtempcomLike, int pageNum, int pageSize, int navigatePages) {
		PageHelper.startPage(pageNum, pageSize);
		String nameLike = cwBdVouchtempcomLike.getNameLike();
		CwBdVouchtempcomExample CwBdVouchtempcomExample = new CwBdVouchtempcomExample();
		Criteria create = CwBdVouchtempcomExample.createCriteria();
		create.andUseStateEqualTo("1");
		if(nameLike!=null) {
		create.andVouchtempnameLike("%"+nameLike+"%");
		}
		List<CwBdVouchtempcom> list = cwBdVouchtempcomMapper.selectByExample(CwBdVouchtempcomExample);
		PageInfo<CwBdVouchtempcom> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdVouchtempcom>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: addCwBdVouchtempcom</p>  
	 * <p>Description:新增单据和凭证模板对应关系 </p>
	 * @param cwBdVouchtempcom
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#addCwBdVouchtempcom(com.dhcc.finance.main.domain.CwBdVouchtempcom)    
	 * @author baiyc
	 * @date 2019年4月5日 上午10:52:04
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> addCwBdVouchtempcom(CwBdVouchtempcom cwBdVouchtempcom) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempcom.setCreateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempcom.setCreateTime(new Date());
		cwBdVouchtempcom.setPkVouchtempcom(SnowflakeIdWorker.getInstance().nextIdString());
		cwBdVouchtempcom.setUseState("1");
		int res = cwBdVouchtempcomMapper.insert(cwBdVouchtempcom);
		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: updateCwBdVouchtempcomByPk</p>  
	 * <p>Description: 修改单据和凭证模板对应关系</p>
	 * @param cwBdVouchtempcom
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#updateCwBdVouchtempcomByPk(com.dhcc.finance.main.domain.CwBdVouchtempcom)    
	 * @author baiyc
	 * @date 2019年4月5日 上午10:52:25
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> updateCwBdVouchtempcomByPk(CwBdVouchtempcom cwBdVouchtempcom) {
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempcom.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempcom.setUpdateTime(new Date());
		int res = cwBdVouchtempcomMapper.updateByPrimaryKeySelective(cwBdVouchtempcom);
		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: deleteCwBdVouchtempcomByPk</p>  
	 * <p>Description: 逻辑删除单据和凭证模板对应关系</p>
	 * @param pkVouchtempcom
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#deleteCwBdVouchtempcomByPk(java.lang.String)    
	 * @author baiyc
	 * @date 2019年4月5日 上午10:52:44
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<Object, CurrencyMeta> deleteCwBdVouchtempcomByPk(String pkVouchtempcom) {
		CwBdVouchtempcom cwBdVouchtempcom = new CwBdVouchtempcom();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate, SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);
		if (sysUserInfo != null) {
			cwBdVouchtempcom.setUpdateUser(sysUserInfo.getUserId());
		}
		cwBdVouchtempcom.setUpdateTime(new Date());
		cwBdVouchtempcom.setUseState("0");
		cwBdVouchtempcom.setPkVouchtempcom(pkVouchtempcom);
		int res = cwBdVouchtempcomMapper.updateByPrimaryKeySelective(cwBdVouchtempcom);
		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: searchCwBdVouchtempcomInfo</p>  
	 * <p>Description: 查询所有数据</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#searchCwBdVouchtempcomInfo()    
	 * @author baiyc
	 * @date 2019年4月5日 上午11:26:39
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<List<CwBdVouchtempcom>, CurrencyMeta> searchCwBdVouchtempcomInfo() {
		CwBdVouchtempcomExample CwBdVouchtempcomExample = new CwBdVouchtempcomExample();
		Criteria create = CwBdVouchtempcomExample.createCriteria();
		create.andUseStateEqualTo("1");
		//List<CwBdVouchtempcom> list = cwBdVouchtempcomMapper.selectAll();
		List<CwBdVouchtempcom> list = cwBdVouchtempcomMapper.selectByExample(CwBdVouchtempcomExample);
		return  new RestfulResponse<List<CwBdVouchtempcom>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: uploadVouchtempcomInfo</p>  
	 * <p>Description: Excel数据导入</p>
	 * @param fileUploadRequest
	 * @param type
	 * @return  
	 * @see com.dhcc.finance.main.service.CwBdVouchtempcomService#uploadVouchtempcomInfo(com.dhcc.finance.main.model.FileUploadRequest, java.lang.String)    
	 * @author baiyc
	 * @date 2019年4月9日 上午11:40:08
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<String, CurrencyMeta> uploadVouchtempcomInfo(
			FileUploadRequest fileUploadRequest, String type) {
		MultipartFile[] files = fileUploadRequest.getFiles();
        StringBuffer result = new StringBuffer("");
        try {
            for (MultipartFile multipartFile : files) {
                String fileRealName = multipartFile.getOriginalFilename();
                int pointIndex = fileRealName.lastIndexOf('.');
                String fileSuffix = fileRealName.substring(pointIndex);//截取文件后缀
                File excelFile = File.createTempFile("vouchtempcomInfo", fileSuffix);
                multipartFile.transferTo(excelFile);
                SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate,
                        SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO, SysUserInfo.class);         
                    List<CwBdVouchtempcom> cwBdVouchtempcom = ExcelUtils.readExcelFile(CwBdVouchtempcom.class, excelFile);              
                    if ("".equals(result.toString())) {
                    	cwBdVouchtempcom.forEach(data -> {
                            result.append(data.checkExcelInputDataLegal(cwBdVouchtempcom.indexOf(data) + ""));
                            String id = SnowflakeIdWorker.getInstance().nextIdString();
                            data.setPkVouchtempcom(id);
                            data.setCreateUser(sysUserInfo.getUserId());
                            data.setCreateTime(new Date());
                            data.setUseState("1");
                        });
                        cwBdVouchtempcomMapper.batchInsertCwBdVouchtempcom(cwBdVouchtempcom);
                    }                     
                }
              
        } catch (Exception e) {
            log.error("数据流操作异常", e);
        }
        if("".equals(result.toString())){
            return new RestfulResponse<String, CurrencyMeta>(SysConstants.CODE.SUCCESS,
                    SysConstants.BASIC_JDBC_RES_MESSAGE.SAVE_SUCCESS, "数据插入成功",
                    MetaDataUtil.getMeta(tracer, SysStatusCode.OPERATION_SUCCESS));
        }
        return new RestfulResponse<String, CurrencyMeta>(SysConstants.CODE.FAIL,
                SysConstants.BASIC_JDBC_RES_MESSAGE.SAVE_FAIL, result.toString(),
                MetaDataUtil.getMeta(tracer, SysStatusCode.OPERATION_FAIL)); 
	}
		
}
