package com.xisc.pm.app.service.imports;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xisc.pm.domain.entity.PmMidSlabAddvBt;
import com.xisc.pm.domain.entity.PmMidSlabAddvBtTra;
import com.xisc.pm.domain.repository.PmMidSlabAddvBtRepository;
import com.xisc.pm.domain.repository.PmMidSlabAddvBtTraRepository;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.DetailsHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.hzero.boot.imported.app.service.IBatchImportService;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024/9/25 15:12
 */
@Slf4j
@ImportService(templateCode = "PM.MID_SLAB_ADDV_BT")
public class PmMidSlabAddvBtImportImpl implements IBatchImportService {

    private final ObjectMapper objectMapper;

    private final PmMidSlabAddvBtRepository pmMidSlabAddvRepository;

    private final PmMidSlabAddvBtTraRepository pmMidSlabAddvTraRepository;

    public PmMidSlabAddvBtImportImpl(ObjectMapper objectMapper, PmMidSlabAddvBtRepository pmMidSlabAddvRepository, PmMidSlabAddvBtTraRepository pmMidSlabAddvTraRepository) {
        this.objectMapper = objectMapper;
        this.pmMidSlabAddvRepository = pmMidSlabAddvRepository;
        this.pmMidSlabAddvTraRepository = pmMidSlabAddvTraRepository;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean doImport(List<String> data) {
        // 如果集合为空，则返回
        if (CollectionUtils.isEmpty(data)) {
            throw new CommonException("pm.import_data_is_empty_list");
        }
        // 获取租户id和当前时间以及用户信息
        Long tenantId = 0L;
        Date now = new Date();
        String username = DetailsHelper.getUserDetails().getUsername();
        if (DetailsHelper.getUserDetails() != null && DetailsHelper.getUserDetails().getTenantId() != null) {
            tenantId = DetailsHelper.getUserDetails().getTenantId();
        }
        // 定义要插入的List
        List<PmMidSlabAddvBt> list = new ArrayList<>(data.size());
        List<PmMidSlabAddvBtTra> tralist = new ArrayList<>(data.size());

        // 数据转对象
        for (String json : data) {
            try {
                PmMidSlabAddvBt pmMidSlabAddvBt = objectMapper.readValue(json, PmMidSlabAddvBt.class);
                pmMidSlabAddvBt.setTenantId(tenantId);
                list.add(pmMidSlabAddvBt);

            } catch (JsonProcessingException e) {
                log.error(e.getMessage(), e);
            }
        }

        // 如果集合为空，则结束
        if (CollectionUtils.isEmpty(list)) {
            throw new CommonException("pm.import_data_is_empty_list");
        }
        // 插入表
        pmMidSlabAddvRepository.batchInsertSelective(list);

        // 获取履历表数据
        for (PmMidSlabAddvBt pmMidSlabAddvBt : list) {
            PmMidSlabAddvBtTra pmMidSlabAddvBtTra = new PmMidSlabAddvBtTra();
            BeanUtils.copyProperties(pmMidSlabAddvBt, pmMidSlabAddvBtTra);
            // 设置操作类型为新增（I），并记录操作时间和用户
            pmMidSlabAddvBtTra.setOperationType("I");
            pmMidSlabAddvBtTra.setOperationTime(now);
            pmMidSlabAddvBtTra.setOperationUser(username);
            tralist.add(pmMidSlabAddvBtTra);
        }
        // 插入履历表
        pmMidSlabAddvTraRepository.batchInsertSelective(tralist);
        return true;
    }
}
