package com.tofly.feesapi.bjgl.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.bjgl.entity.YhCbkxxLog;
import com.tofly.feesapi.bjgl.entity.dto.YhCbkxxFilter;
import com.tofly.feesapi.bjgl.service.YhCbkxxLogService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.util.ToolsUtils;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.bjgl.mapper.YhCbkxxLogMapper;

import java.util.List;

@Service
public class YhCbkxxLogServiceImpl extends ServiceImpl<YhCbkxxLogMapper, YhCbkxxLog> implements YhCbkxxLogService {

    @Override
    public PageResult<YhCbkxxLog> getYhCbkxxLogPage(PageDto pageDto, YhCbkxxFilter filter) {
        PageDto<YhCbkxxLog> page = baseMapper.getYhCbkxxLogPage(pageDto, filter);
        List<YhCbkxxLog> records = page.getRecords();

        if (CollUtil.isNotEmpty(records)) {
            for (YhCbkxxLog record : records) {
                String oldJson = record.getOldJson();
                String newJson = record.getNewJson();

                if (StrUtil.isNotEmpty(oldJson) && StrUtil.isNotEmpty(newJson)) {
                    YhCbkxx cbkxx1 = JSONUtil.toBean(oldJson, YhCbkxx.class);
                    YhCbkxx cbkxx2 = JSONUtil.toBean(newJson, YhCbkxx.class);
                    record.setDifferentFields(ToolsUtils.compareObjects(cbkxx1, cbkxx2, false));
                }
            }
        }

        return page.toVo();
    }
}
