package org.jeecg.modules.okki.blog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.okki.blog.entity.OkkiBlog;
import org.jeecg.modules.okki.blog.mapper.OkkiBlogMapper;
import org.jeecg.modules.okki.blog.service.IOkkiBlogService;
import org.jeecg.modules.okki.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description: 博客
 * @Author: jeecg-boot
 * @Date:   2024-05-09
 * @Version: V1.0
 */
@Slf4j
@Service
public class OkkiBlogServiceImpl extends ServiceImpl<OkkiBlogMapper, OkkiBlog> implements IOkkiBlogService {

    @Value("${OKKI.BASE_URL}")
    private String URL;

    @Value("${OKKI.CLIENT_SECRET}")
    private String CLIENT_SECRET;

    @Resource
    private OkkiBlogMapper okkiBlogMapper;

    @Override
    public boolean changeStatus(OkkiBlog okkiBlog) {
        OkkiBlog okkiBlog1 = okkiBlogMapper.selectById(okkiBlog.getId());
        // 请求okki平台接口
        Map<String, String> query = new TreeMap<>();
        query.put("sign_method", "hmac-md5");
        query.put("timestamp", String.valueOf(System.currentTimeMillis()));
        query.put("site_id", String.valueOf(okkiBlog1.getSiteId()));
        query.put("method", "save_blog");
        okkiBlog.setStatus(1);
        Map<String, Object> postData = new HashMap<>();
        try {
            String queryStr = HttpClientUtils.buildQueryString(query);
            postData.put("title", okkiBlog1.getTitle());
            postData.put("pic", okkiBlog1.getPic());
            postData.put("url", okkiBlog1.getUrl());
            postData.put("content", okkiBlog1.getContent());
            // 时间转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            postData.put("addtime", sdf.format(okkiBlog1.getCreateTime()));
            postData.put("uid", okkiBlog1.getUid());
            if (okkiBlog.getReportScore() != null) {
                postData.put("report_score", okkiBlog1.getReportScore());
            }
            if (okkiBlog.getReportLink() != null) {
                postData.put("report_link", okkiBlog1.getReportLink());
            }
            String body = HttpClientUtils.toJsonString(postData);
            String signStr = queryStr + body;
            query.put("signature", HttpClientUtils.generateHmacMD5(signStr, CLIENT_SECRET));
            String okkiUrl = URL + "?" + HttpClientUtils.buildQueryString(query);
            log.info("url:" + okkiUrl + ",body:" + body);
            String res = HttpClientUtils.doPost(okkiUrl, body);
            log.info(res);
            // {"code":0,"msg":"success","now":"2024-05-06 15:34:54","data":[]}
            JSONObject jsonObject= JSONObject.parseObject(res);
            if (jsonObject.get("code").equals(0)) {
                int result = okkiBlogMapper.updateById(okkiBlog);
                if (result == 0) {
                    return false;
                }else {
                    return true;
                }
            }else {
                throw new RuntimeException(jsonObject.get("msg").toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
