package com.citygis.web.dataEncrypt.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.dataEncrypt.domain.ColumnInfo;
import com.citygis.web.dataEncrypt.domain.JiaMi;
import com.citygis.web.dataEncrypt.mapper.JiaMiMapper;
import com.citygis.web.dataEncrypt.service.DataEncryptService;
import com.citygis.web.dataEncrypt.service.JiaMiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class JiaMiServiceImpl implements JiaMiService {

    @Resource
    DataEncryptService dataEncryptService;

    @Resource
    JiaMiMapper jiaMiMapper;

    //查询字段值
    @Override
    public AjaxResult handleEncryption(JiaMi jiaMi) throws Exception {

        List<ColumnInfo> columnList = jiaMiMapper.handleEncryption(jiaMi.getIdColumn(), jiaMi.getTableName(), jiaMi.getColumn());

        int row = 0;
        if (!CollectionUtils.isEmpty(columnList) && columnList.size() > 0) {
            for (ColumnInfo info : columnList) {
                String encryString = dataEncryptService.dataEncrypt(info.getValue());
                row = row + updateEncryption(jiaMi.getIdColumn(), jiaMi.getTableName(), jiaMi.getColumn(), info.getId(), encryString);
            }
        }
        return AjaxResult.success("成功" + row + "行");
    }


    //更新字段值
    @Override
    public int updateEncryption(String idColumn, String tableName, String column, Long id, String encryString) {
        return jiaMiMapper.updateEncryption(idColumn, tableName, column, id, encryString);
    }

    @Override
    public AjaxResult dataCompleteness(String tableName, String idColumn) {
        List<LinkedHashMap<String, Object>> dateBySql = jiaMiMapper.getData(tableName);

        dateBySql.forEach(map -> {
            StringBuilder text = new StringBuilder();
            map.remove("HAMC_CODE");
            map.remove("IS_ENCRYPT");
            map.forEach((key, value) -> {
                if (text.length() > 0) {
                    text.append(",");
                }
                text.append(value);
            });
            String result = text.toString();
            // 处理 result，如保存到集合或输出
            try {
                String hmacText = dataEncryptService.generateHmac(result);
                jiaMiMapper.updateData(tableName, idColumn, hmacText, map.get(idColumn).toString());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        return AjaxResult.success("hmac运算：" + dateBySql.size() + "条");
    }

    @Override
    public AjaxResult verifyDataCompleteness(String tableName, String idColumn) {
        List<String> errorData = new ArrayList<>();

        List<LinkedHashMap<String, Object>> dateBySql = jiaMiMapper.getVerifyData(tableName);
        dateBySql.forEach(map -> {
            StringBuilder text = new StringBuilder();
            String hamcCode = map.get("HAMC_CODE").toString();
            map.remove("HAMC_CODE");
            map.remove("IS_ENCRYPT");
            map.forEach((key, value) -> {
                if (text.length() > 0) {
                    text.append(",");
                }
                text.append(value);
            });
            String result = text.toString();
            // 处理 result，如保存到集合或输出
            try {
                String hmacText = dataEncryptService.generateHmac(result);
                if (!hmacText.equals(hamcCode)) {
                    errorData.add(map.get(idColumn).toString());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        return AjaxResult.success("验证运算：" + dateBySql.size() + "条，错误数据：" + errorData.size() + "条,id为--->", errorData);
    }

}
