package com.tax.file.service.async.apply.repeat;

import com.tax.file.model.TkFile;
import com.tax.file.model.TkFileExample;
import com.tax.file.model.TkTaxUser;
import com.tax.file.model.TkTaxUserExample;
import com.tax.file.service.api.TkFileService;
import com.tax.file.service.api.TkTaxUserService;
import com.yxcheng.token.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DataRepeatApplyService {
    protected   final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected TkTaxUserService tkTaxUserService;
    @Autowired
    protected TkFileService tkFileService;


    public boolean init(TkTaxUser taxUser, TkTaxUser parentTaxUser){
        TkFileExample tkFileExample = new TkFileExample();
        tkFileExample.createCriteria().andTaxnoEqualTo(taxUser.getTaxno())
                                      .andUserNameEqualTo(parentTaxUser.getUserName())
                                      .andDownStatusEqualTo("1");


        List<TkFile> tkFiles = tkFileService.selectByExample(tkFileExample);
        copyData(tkFiles,taxUser);
        taxUser.setInit("1");

        taxUser.setApplyDownDate(parentTaxUser.getApplyDownDate());
        taxUser.setApplyStatus("2");
        tkTaxUserService.updateByPrimaryKeySelective(taxUser);

        return true;
    }


    public boolean copyData(List<TkFile> tkFiles, TkTaxUser taxUser){
        boolean flag = true;

        if(tkFiles!=null && tkFiles.size()>0){
            for(TkFile tkFile : tkFiles){
                if(!"2".equals(tkFile.getApplyStatus())){
                    flag = false;
                    continue;
                }
                insertTkFile(tkFile,taxUser);
            }
        }else{
            flag = false;
        }

        return flag;
    }


    private void insertTkFile(TkFile tkFile, TkTaxUser taxUser) {
        TkFileExample tkFileExample = new TkFileExample();
        tkFileExample.createCriteria()
                .andTaxnoEqualTo(tkFile.getTaxno())
                .andUserNameEqualTo(taxUser.getUserName())
                .andInvSpecEqualTo(tkFile.getInvSpec())
                .andInvTypeEqualTo(tkFile.getInvType())
                .andInvDateStartEqualTo(DateUtils.toDate(tkFile.getInvDateStart(),"yyyy-MM-dd"))
                .andInvDateEndEqualTo(DateUtils.toDate(tkFile.getInvDateEnd(),"yyyy-MM-dd"));
        List<TkFile> readyFile = tkFileService.selectByExample(tkFileExample);
        if(readyFile==null || readyFile.size()==0){
            TkFile insertData = assembleTkFile(tkFile,taxUser);
            tkFileService.insertSelective(insertData);
        }
    }

    private TkFile assembleTkFile(TkFile tkFile, TkTaxUser taxUser) {
        TkFile entity = new TkFile();
        entity.setTaxno(tkFile.getTaxno());
        entity.setInvSpec(tkFile.getInvSpec());
        entity.setInvType(tkFile.getInvType());
        entity.setDecPwd(tkFile.getDecPwd());
        entity.setUrl(tkFile.getUrl());
        entity.setApplyStatus(tkFile.getApplyStatus());
        entity.setDownStatus(tkFile.getDownStatus());
        entity.setInvDateStart(tkFile.getInvDateStart());
        entity.setInvDateEnd(tkFile.getInvDateEnd());
        entity.setFileName(tkFile.getFileName());
        entity.setUploadTime(tkFile.getUploadTime());
        entity.setFileKey(tkFile.getFileKey());
        entity.setApplyDate(tkFile.getApplyDate());
        entity.setUserName(taxUser.getUserName());
        return entity;
    }


    public void downFinish(TkTaxUser parentTaxUser, List<TkFile> successFile, boolean isFinish) {
        TkTaxUserExample tkTaxUserExample = new TkTaxUserExample();
        tkTaxUserExample.createCriteria().andTaxnoEqualTo(parentTaxUser.getTaxno()).andIsRepeatEqualTo("1");

        List<TkTaxUser> tkTaxUsers = tkTaxUserService.selectByExample(tkTaxUserExample);
        if(tkTaxUsers!=null && tkTaxUsers.size()>0){
            for(TkTaxUser tkTaxUser : tkTaxUsers){
                copyData(successFile,tkTaxUser);
                if(isFinish){
                    tkTaxUser.setApplyDownDate(parentTaxUser.getApplyDownDate());
                    tkTaxUser.setApplyStatus("2");
                    tkTaxUserService.updateByPrimaryKeySelective(tkTaxUser);
                }
            }
        }
    }
}
