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.EBaseinfo;
import com.zksy.common.domain.Result;
import com.zksy.common.exception.DbException;
import com.zksy.data.annotation.XhDataLogRecord;
import com.zksy.data.domain.po.XcrEBaseinfo;
import com.zksy.data.mapper.XcrEBaseinfoMapper;
import com.zksy.data.service.XcrEBaseinfoService;
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 hxb
* @description 针对表【xcr_e_baseinfo(企业基本信息)】的数据库操作Service实现
* @createDate 2024-11-11 16:40:43
*/
@Service
@Slf4j
public class XcrEBaseinfoServiceImpl extends ServiceImpl<XcrEBaseinfoMapper, XcrEBaseinfo>
    implements XcrEBaseinfoService{
    @Autowired
    private TableDataClient tableDataClient;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Override
    @XhDataLogRecord("企业基本总信息")
    public void saveDataByUniCode(String address) {
        Result<List<EBaseinfo>> baseinfoAll = tableDataClient.getEBaseinfoAll();
        if(baseinfoAll.getCode() != 200){
            throw new RuntimeException(baseinfoAll.getMsg());
        }
        List<XcrEBaseinfo> toDatabaseList = BeanUtil.copyToList(baseinfoAll.getData(), XcrEBaseinfo.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;
        });
    }
}




