package com.friday.custody.service.basicService.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.PullDataFailureLogDao;
import com.friday.custody.entity.PullDataFailureLogEntity;
import com.friday.custody.service.basicService.PullDataFailureLogService;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service("pullDataFailureLogService")
public class PullDataFailureLogServiceImpl extends ServiceImpl<PullDataFailureLogDao, PullDataFailureLogEntity> implements PullDataFailureLogService {


    @Override
    public boolean saveErrorData(String admin, Map<String, Object> errorDataMap) {
        try {
            log.info("pp"+ JSON.toJSONString(errorDataMap));
            return baseMapper.insert(PullDataFailureLogEntity.builder()
                    .admin(admin)
                    .trustNo(String.valueOf(errorDataMap.get("trustNo")))
                    .api(String.valueOf(errorDataMap.get("custodyErrorApi")))
                    .headers(String.valueOf(errorDataMap.get("headers")))
                    .params(String.valueOf(errorDataMap.get("params")))
                    .errorMsg(String.valueOf(errorDataMap.get("errorResp")))
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build()) == 1;
        } catch (Exception e) {
            log.error("PullDataFailureLogServiceImpl:saveErrorData:", e);
            return false;
        }
    }

    @Override
    public R tranferAndSaveDataBatch(PullDataFailureLogEntity pullDataFailureLogEntities) {
        log.error("中转项目拉取数据失败：内容为{}",pullDataFailureLogEntities);
        int insert = this.baseMapper.insert(pullDataFailureLogEntities);
        if(insert>0){
            return R.ok();
        }else {
            log.error("保存PullDataFailureLogEntity数据失败：{}",pullDataFailureLogEntities);
            return R.error("保存PullDataFailureLogEntity数据失败");
        }
    }
}