package com.zm.order.tool.server.cipher;

import cn.qmai.data.encrypt.service.DataEncryptService;
import com.zm.order.tool.server.model.Refund;
import net.sf.jsqlparser.JSQLParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;


/**
 * 退款单加解密处理器
 *
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2023/12/19 5:07 下午
 */
@Component
public class RefundEncryptionHandler extends AbstractEncryptionHandler {

    @Autowired
    private DataEncryptService dataEncryptService;

    /**
     * 加密过程
     *
     * <p>
     *
     * </p>
     *
     * @param refund datasource
     */
    public void encrypt(Refund refund) {
        if (refund == null) {
            return;
        }
        try {
            refund.setMobileCipher(dataEncryptService.encrypt(refund.getMobile()));
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
//        Map<String, Object> extraMap = refund.getExtraMap();
//        if (!CollectionUtils.isEmpty(extraMap)) {
//            handleExt(extraMap);
//        }
//        if (!StringUtils.isEmpty(refund.getMobile())) {
//
//        }
    }

    protected void handleExt(Map<String, Object> extraMap) {
        refundExt.forEach((key, value) -> parseExt(extraMap, key, value, CipherEnum.ENCRYPT));
    }

    /**
     * 解密过程
     *
     * <p>
     *
     * </p>
     *
     * @param refund datasource
     */
    public void decrypt(Refund refund) {
        if (refund == null) {
            return;
        }
        Map<String, Object> extraMap = refund.getExtraMap();
        if (!CollectionUtils.isEmpty(extraMap)) {
            handleExtDecrypt(extraMap);
        }
    }

    private void handleExtDecrypt(Map<String, Object> extraMap) {
        ext.forEach((key, value) -> parseExt(extraMap, key, value, CipherEnum.DECRYPT));
    }
}
