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.common.domain.Result;
import com.zksy.common.exception.DbException;
import com.zksy.data.annotation.XhDataLogRecord;
import com.zksy.data.domain.po.XcrAnnualReportBaseInfo;
import com.zksy.data.mapper.XcrAnnualReportBaseInfoMapper;
import com.zksy.data.service.XcrAnnualReportBaseInfoService;
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;

/**
* @author 邵洋
* @description 针对表【xcr_annual_report_base_info(企业年报基本信息表)】的数据库操作Service实现
* @createDate 2024-10-24 11:31:18
*/
@Service
@Slf4j
public class XcrAnnualReportBaseInfoServiceImpl extends ServiceImpl<XcrAnnualReportBaseInfoMapper, XcrAnnualReportBaseInfo>
    implements XcrAnnualReportBaseInfoService{
    @Autowired
    private TableDataClient tableDataClient;
    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    @XhDataLogRecord("同步企业年报基本信息")
    public void saveDataByUniCode(String address) {
        Result<List<AnBaseinfo>> baseinfoAll = tableDataClient.getAnBaseinfo();
        if(baseinfoAll.getCode() != 200){
            throw new RuntimeException(baseinfoAll.getMsg());
        }
        List<XcrAnnualReportBaseInfo> toDatabaseList = BeanUtil.copyToList(baseinfoAll.getData(), XcrAnnualReportBaseInfo.class);
        toDatabaseList.forEach(q->{
            q.setUniCode(q.getUniscid());
        });
        transactionTemplate.execute((s)->{
            try {
                this.remove(null);
                this.saveBatch(toDatabaseList);
            } catch (Exception e) {
                s.setRollbackOnly();
                throw new DbException(e);
            }
            return null;
        });
    }
}




