package com.tax.file.service.impl;


import com.google.api.client.util.Lists;
import com.tax.file.constants.SqlConstants;
import com.tax.file.mapper.FileSyncMapper;
import com.tax.file.model.FileSync;
import com.tax.file.model.FileSyncExample;
import com.tax.file.model.TkFile;
import com.tax.file.service.api.FileSyncService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.file.dao", "com.tax.file.integration"})
public class FileSyncServiceImpl extends AbstractService<FileSyncMapper,FileSyncExample,FileSync> implements FileSyncService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int[] updateBatchJdbc(String sql, List<Object[]> batchArgs){
        int[] result = jdbcTemplate.batchUpdate(sql, batchArgs);
        return result;
    }

    public void saveSyncList(List<TkFile> invList, String exp_index, String expType, String uuidFlag) {
        List<Object[]> invArrList = Lists.newArrayList();
        for(TkFile entity:invList) {
            //file_id, user_name, taxno, exp_index, exp_type, exp_flag,
            // inv_date_start, inv_date_end, uuid_flag, file_md5, inv_spec, inv_type
            Object[] valueArr = new Object[12];
            valueArr[0] = entity.getId();
            valueArr[1] = entity.getUserName();
            valueArr[2] = entity.getTaxno();
            valueArr[3] = exp_index;
            valueArr[4] = expType;
            valueArr[5] = "1";
            valueArr[6] = entity.getInvDateStart();
            valueArr[7] = entity.getInvDateEnd();
            valueArr[8] = uuidFlag;
            valueArr[9] = entity.getFileMd5();
            valueArr[10] = entity.getInvSpec();
            valueArr[11] = entity.getInvType();
            invArrList.add(valueArr);
        }
        updateBatchJdbc(SqlConstants.BATCH_INSERT_FILE_SYNC_SQL, invArrList);
    }

    public List<FileSync> findFileSyncList(String userName, String taxno, String taskNo) {
        FileSync queryEntity = new FileSync();
        queryEntity.setUserName(userName);
//        queryEntity.setTaxno(taxno);
        queryEntity.setExpIndex(taskNo);
        return baseMapper.findFileSyncList(queryEntity);
    }

    public void updateFileExpSyncList(List<FileSync> invSyncList, String expType) {
        List<Object[]> invArrList = Lists.newArrayList();
        FileSync syncEn = invSyncList.get(0);

        Date curDate = DateUtils.getDate();
        for(FileSync entity:invSyncList) {
            Object[] valueArr = new Object[2];
            valueArr[0] = "2";
            valueArr[1] = entity.getFileId();
            invArrList.add(valueArr);
        }
        updateBatchJdbc(SqlConstants.BATCH_UPDATE_FILE_SYNC_RESULT_SQL, invArrList);
        syncEn.setExpFlag("2");
        syncEn.setExpResDate(curDate);
        baseMapper.updateFileSync(syncEn);
    }
}
