package com.tax.file.service.impl;


import com.google.api.client.util.Lists;
import com.tax.file.constants.SqlConstants;
import com.tax.file.mapper.FileApplyLogMapper;
import com.tax.file.model.FileApplyLog;
import com.tax.file.model.FileApplyLogExample;
import com.tax.file.model.fileManage.ApplyFileReqModel;
import com.tax.file.service.api.FileApplyLogService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.MD5Util;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.file.dao", "com.tax.file.integration"})
public class FileApplyLogServiceImpl extends AbstractService<FileApplyLogMapper, FileApplyLogExample, FileApplyLog> implements FileApplyLogService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

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

    @Override
    public void saveLog(String uuidFlag, String userName, String zoneCode, List<ApplyFileReqModel> applyInfoList) {
        try {
            //taxno, inv_spec, inv_type, inv_date_start, inv_date_end, user_name, force_apply_flag, zone_code, file_md5,
            // file_month, file_data_type, error_code, error_message, file_error_message, uuid_flag, create_date
            List<Object[]> invArrList = Lists.newArrayList();
            for(ApplyFileReqModel entity:applyInfoList) {
                Object[] valueArr = new Object[15];
                valueArr[0] = entity.getTaxno();
                valueArr[1] = entity.getInvSpec();
                valueArr[2] = entity.getInvType();
                valueArr[3] = entity.getApplyDateStart();
                valueArr[4] = entity.getApplyDateEnd();
                valueArr[5] = userName;
                valueArr[6] = entity.getForceApplyFlag();
                valueArr[7] = zoneCode;
                valueArr[8] = MD5Util.MD5Encode(entity.getTaxno() + entity.getInvSpec() + entity.getInvType() + entity.getApplyDateStart() + entity.getApplyDateEnd());

                String fileDataType = "1";
                Date dateStart = DateUtils.str2Date(entity.getApplyDateStart(),  new SimpleDateFormat("yyyy-MM-dd"));
                Date dateEnd = DateUtils.str2Date(entity.getApplyDateEnd(),  new SimpleDateFormat("yyyy-MM-dd"));
                int daysOfMonth = DateUtils.getDaysOfMonth(dateEnd);
                long days = DateUtils.getDaySub(dateStart, dateEnd) + 1;
                String beMonth = DateUtils.getMonth(dateEnd);
                if(daysOfMonth == days) {
                    fileDataType = "1";
                } else {
                    fileDataType = "0";
                }
                valueArr[9] = beMonth;
                valueArr[10] = fileDataType;
                valueArr[11] = entity.getErrorCode();
                valueArr[12] = entity.getErrorMessage();
                valueArr[13] = entity.getFileErrorMessage();
                valueArr[14] = uuidFlag;
                invArrList.add(valueArr);
            }
            updateBatchJdbc(SqlConstants.BATCH_INSERT_FILE_APPLY_SQL, invArrList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
