package com.hai.service.impl;

import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.WxUtils;
import com.hai.dao.SecConfigMapper;
import com.hai.entity.SecConfig;
import com.hai.entity.SecConfigExample;
import com.hai.entity.SecDictionary;
import com.hai.entity.SecDictionaryExample;
import com.hai.service.ApiMerchantsService;
import com.hai.service.SecConfigService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

@Service("secConfigServiceImpl")
public class SecConfigServiceImpl implements SecConfigService {

    @Resource
    private SecConfigMapper secConfigMapper;

    @Resource
    private ApiMerchantsService apiMerchantsService;

    @Override
    public SecConfig findById(Integer id) {
        return secConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertSecConfig(SecConfig secConfig) {
        secConfigMapper.insert(secConfig);
    }

    @Override
    public void updateSecConfig(SecConfig secConfig) {
        secConfigMapper.updateByPrimaryKeySelective(secConfig);
    }

    @Override
    public SecConfig findByCodeType(String codeType) {
        SecConfigExample example = new SecConfigExample();
        example.createCriteria().andCodeTypeEqualTo(codeType);
        return secConfigMapper.selectByExample(example).get(0);
    }

    @Override
    public Boolean isConfig(String codeType, String codeValue) {
        SecConfigExample example = new SecConfigExample();
        example.createCriteria().andCodeTypeEqualTo(codeType).andCodeValueEqualTo(codeValue);
        List<SecConfig> list = secConfigMapper.selectByExample(example);
        return list.size() > 0;
    }


    @Override
    public Boolean isSignVerify(Map<String, Object> map, String sign) throws Exception {
        String signPost = WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5);
        return signPost.equals(sign);
    }

}
