package com.zksy.data.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.api.client.TableDataClient;
import com.zksy.api.dto.tableData.AnBaseinfo;
import com.zksy.api.dto.tableData.AnSubcapital;
import com.zksy.common.domain.Result;
import com.zksy.common.exception.DbException;
import com.zksy.data.annotation.XhDataLogRecord;
import com.zksy.data.domain.po.XcrEnterpriseAnnualReportShareholder;
import com.zksy.data.mapper.XcrEnterpriseAnnualReportShareholderMapper;
import com.zksy.data.service.XcrEnterpriseAnnualReportShareholderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author 邵洋
* @description 针对表【xcr_enterprise_annual_report_shareholder(企业年报股东及出资信息表)】的数据库操作Service实现
* @createDate 2024-10-21 14:12:19
*/
@Service
@Slf4j
public class XcrEnterpriseAnnualReportShareholderServiceImpl extends ServiceImpl<XcrEnterpriseAnnualReportShareholderMapper, XcrEnterpriseAnnualReportShareholder>
    implements XcrEnterpriseAnnualReportShareholderService{
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private TableDataClient tableDataClient;

    @Override
    @XhDataLogRecord("同步企业年报股东及出资信息")
    public void saveDataByUniCode(String address) {
        Result<List<AnBaseinfo>> annualReportAll = tableDataClient.getAnBaseinfo();
        if(annualReportAll.getCode() != 200){
            throw new RuntimeException(annualReportAll.getMsg());
        }
        Result<List<AnSubcapital>> baseinfoAll = tableDataClient.getAnSubcapital();
        if(baseinfoAll.getCode() != 200){
            throw new RuntimeException(baseinfoAll.getMsg());
        }
        // 将股东信息转换为实体类对象
        List<XcrEnterpriseAnnualReportShareholder> toDatabaseList = BeanUtil.copyToList(baseinfoAll.getData(), XcrEnterpriseAnnualReportShareholder.class);

        // 创建一个映射，将处理过的 ENTCHEYEAR 映射到年报基本信息对象
        Map<String, AnBaseinfo> annualReportMap = annualReportAll.getData().stream()
                .collect(Collectors.toMap(info -> getProcessedEntcheyear(info.getEntcheyear()), Function.identity()));

        // 设置 uniCode 属性
        toDatabaseList.forEach(q -> {
            String processedEntcheyear = getProcessedEntcheyear(q.getEntcheyear());
            AnBaseinfo annualReport = annualReportMap.get(processedEntcheyear);
            if (annualReport != null) {
                q.setUniCode(annualReport.getUniscid());
            }
        });
        transactionTemplate.execute((s)->{
            try {
                this.remove(null);
                this.saveBatch(toDatabaseList);
            } catch (Exception e) {
                s.setRollbackOnly();
                throw new DbException(e);
            }
            return null;
        });
    }
    // 辅助方法：处理 ENTCHEYEAR 字段，返回下划线前的数据
    private String getProcessedEntcheyear(String entcheyear) {
        if (entcheyear == null || entcheyear.isEmpty()) {
            return "";
        }
        int underscoreIndex = entcheyear.indexOf('_');
        if (underscoreIndex > -1) {
            return entcheyear.substring(0, underscoreIndex);
        }
        return entcheyear; // 如果没有下划线，则直接返回原字符串
    }
}




