/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.install.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.common.service.IFileInfoService;
import com.tofly.install.dto.ApplyInstallQuery;
import com.tofly.install.entity.ApplyInstall;
import com.tofly.install.mapper.ApplyInstallMapper;
import com.tofly.install.service.ApplyInstallService;
import com.tofly.install.vo.ApplyInstallVo;
import com.tofly.stocks.entity.CheckPlan;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


/**
 * ${comments}
 *
 * @author zhoux
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("applyInstallService")
public class ApplyInstallServiceImpl extends ServiceImpl<ApplyInstallMapper, ApplyInstall> implements ApplyInstallService {

	
    @Autowired
    private IFileInfoService fileInfoService;
    
    @Value("${flow.ftp.stocks.installApply}")
    private String path;
    
    
	
	@Override
	public ApplyInstall saveApplyInstall(ApplyInstall applyInstall, List<MultipartFile> files) throws Exception {
		
    	SimpleDateFormat year =new SimpleDateFormat("yyyy");
		StringBuffer sb =new StringBuffer();		
		sb.append("德水申（");
		sb.append(year.format(new Date())+"）第");
		
		List<ApplyInstall> applyList = baseMapper.selectList(Wrappers.<ApplyInstall>lambdaQuery().
				eq(ApplyInstall::getDelFlag,1).orderByDesc(ApplyInstall::getCreateTime));
    	
    	if(applyList.size()==0) {
    		sb.append("1号");
    	}else {
    		String existApplyNo =applyList.get(0).getApplyInstallNo();
    		String existApplyNoNum= existApplyNo.substring(existApplyNo.lastIndexOf("第")+1,existApplyNo.length()-1);    		
    		Integer saveNum = Integer.valueOf(existApplyNoNum)+1;   		 
    		sb.append(String.valueOf(saveNum)+"号");
    	}
    	
		applyInstall.setApplyInstallNo(sb.toString());
		applyInstall.setDelFlag(1);
		applyInstall.setCreateTime(new Date());
		applyInstall.setRegisterUserId(SecurityUtils.getUserId());
		
		applyInstall.setAnnex(fileInfoService.uploadFiles(files,applyInstall.getAnnex()
	                ,applyInstall.getAnnex(),path,"TF_YWPN_APPLY_INSTALL"));
	    this.save(applyInstall);
	    
	    return applyInstall;
	}

	@Override
	public ApplyInstall editApplyInstall(ApplyInstall applyInstall, List<MultipartFile> files) throws Exception {				
		ApplyInstall data = this.getById(applyInstall.getId());

		
		applyInstall.setAnnex(fileInfoService.uploadFiles(files,data.getAnnex()
                ,applyInstall.getAnnex(),path,"TF_YWPN_APPLY_INSTALL"));		
		ApplyInstall ai =new ApplyInstall();
		ai.setId(data.getId());
		BeanUtils.copyProperties(applyInstall, ai);
		this.updateById(ai);
		return applyInstall;
	}

	@Override
	public Page<ApplyInstallVo> getDataPage(Page page, ApplyInstallQuery applyInstallQuery) {		
		applyInstallQuery.setApprovalPersonId(SecurityUtils.getUserId());		
		
		if(applyInstallQuery.getQueryType()==2) {
			return page.setRecords(baseMapper.getDataPage(page, applyInstallQuery));
		}else {
			return page.setRecords(baseMapper.getApplyDataPage(page, applyInstallQuery));
		}
		
		
		
		
	}

	@Override
	public Page<ApplyInstallVo> getDataOtherPage(Page page, ApplyInstallQuery applyInstallQuery) {
		return page.setRecords(baseMapper.getDataOtherPage(page, applyInstallQuery));
	}	
}