package com.fs.store.service.impl;

import cn.hutool.http.HttpUtil;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.his.config.FsSysConfig;
import com.fs.his.utils.ConfigUtil;
import com.fs.store.domain.FsUserAddress;
import com.fs.store.mapper.FsUserAddressMapper;
import com.fs.store.service.IFsUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址Service业务层处理
 * 
 * @author fs
 * @date 2022-03-15
 */
@Service
public class FsUserAddressServiceImpl implements IFsUserAddressService 
{
    @Autowired
    private FsUserAddressMapper fsUserAddressMapper;
    @Autowired
    private ConfigUtil ConfigUtil;

    /**
     * 查询用户地址
     * 
     * @param id 用户地址ID
     * @return 用户地址
     */
    @Override
    public FsUserAddress selectFsUserAddressById(Long id)
    {
        return fsUserAddressMapper.selectFsUserAddressById(id);
    }

    /**
     * 查询用户地址列表
     * 
     * @param fsUserAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<FsUserAddress> selectFsUserAddressList(FsUserAddress fsUserAddress)
    {
        return fsUserAddressMapper.selectFsUserAddressList(fsUserAddress);
    }

    /**
     * 新增用户地址
     * 
     * @param fsUserAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertFsUserAddress(FsUserAddress fsUserAddress)
    {
        fsUserAddress.setDetail(fsUserAddress.getDetail().trim());
        fsUserAddress.setCreateTime(DateUtils.getNowDate());
        return fsUserAddressMapper.insertFsUserAddress(fsUserAddress);
    }

    /**
     * 修改用户地址
     * 
     * @param fsUserAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateFsUserAddress(FsUserAddress fsUserAddress)
    {
        fsUserAddress.setDetail(fsUserAddress.getDetail().trim());
        fsUserAddress.setUpdateTime(DateUtils.getNowDate());
        return fsUserAddressMapper.updateFsUserAddress(fsUserAddress);
    }

    /**
     * 批量删除用户地址
     * 
     * @param ids 需要删除的用户地址ID
     * @return 结果
     */
    @Override
    public int deleteFsUserAddressByIds(Long[] ids)
    {
        return fsUserAddressMapper.deleteFsUserAddressByIds(ids);
    }

    /**
     * 删除用户地址信息
     * 
     * @param id 用户地址ID
     * @return 结果
     */
    @Override
    public int deleteFsUserAddressById(Long id)
    {
        return fsUserAddressMapper.deleteFsUserAddressById(id);
    }

    @Override
    public int clearIsDefalut(long userId) {
        return fsUserAddressMapper.clearIsDefalut(userId);
    }

    @Override
    public Integer selectFsUserAddressCountsByUserId(long userId) {
        return fsUserAddressMapper.selectFsUserAddressCountsByUserId(userId);
    }

    @Override
    public Integer delAllAddress(Long userId) {
        return fsUserAddressMapper.delAllAddress(userId);
    }

    @Override
    public String getKdnAddress(String address) {
        String   requestData = "{'Content':'" + address + "'}";
        Map<String, Object> params = new HashMap<>();
        FsSysConfig sysConfig = ConfigUtil.getSysConfig();
        try {
            params.put("RequestData", URLEncoder.encode(requestData, "UTF-8"));
            params.put("EBusinessID",sysConfig.getKdnId().trim());
            params.put("RequestType", "6001");
            String dataSign = encrypt(requestData, sysConfig.getKdnKeyId().trim(), "UTF-8");
            params.put("DataSign", URLEncoder.encode(dataSign, "UTF-8"));
            params.put("DataType", "2");
            String result = HttpUtil.post(sysConfig.getKdnAddressUrl().trim(), params);

            return result;
        } catch (Exception e) {
            throw  new CustomException(e.getMessage());
        }
    }

    private String encrypt(String content, String keyValue, String charset) {
        if (keyValue != null) {
            content = content + keyValue;
        }
        byte[] src;
        try {
            src = MD5(content, charset).getBytes(charset);
            return Base64Utils.encodeToString(src);
        } catch (Exception e) {
        }

        return null;
    }

    private String MD5(String str, String charset) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes(charset));
        byte[] result = md.digest();
        StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < result.length; i++) {
            int val = result[i] & 0xff;
            if (val <= 0xf) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }
}
