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.DrRaninsres;
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.domain.po.XcrInspectionResults;
import com.zksy.data.domain.response.XcrInspectionResultsResponse;
import com.zksy.data.mapper.XcrInspectionResultsMapper;
import com.zksy.data.service.XcrInspectionResultsService;
import com.zksy.data.utils.RedisService;
import com.zksy.data.utils.XhRequestUtil;
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.Vector;

/**
* @author Administrator
* @description 针对表【xcr_inspection_results(抽查结果信息)】的数据库操作Service实现
* @createDate 2024-10-22 14:04:10
*/
@Service
@Slf4j
public class XcrInspectionResultsServiceImpl extends ServiceImpl<XcrInspectionResultsMapper, XcrInspectionResults>
    implements XcrInspectionResultsService{
    @Autowired
    private TableDataClient tableDataClient;
    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override

    @XhDataLogRecord("同步抽查结果信息")
    public void saveDataByUniCode(String address) {
        Result<List<DrRaninsres>> drRaninsresAll = tableDataClient.getDrRaninsres();
        if(drRaninsresAll.getCode() != 200){
            throw new RuntimeException(drRaninsresAll.getMsg());
        }
        List<XcrInspectionResults> toDatabaseList = BeanUtil.copyToList(drRaninsresAll.getData(), XcrInspectionResults.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;
        });
    }
}




