package org.jeecg.modules.okki.websitequality.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.okki.websitequality.entity.OkkiWebsiteQuality;
import org.jeecg.modules.okki.websitequality.param.OkkiWebsiteQualityParam;
import org.jeecg.modules.okki.websitequality.service.IOkkiWebsiteQualityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/website")
public class OkkiWebsiteQualityApiController {

    @Resource
    private IOkkiWebsiteQualityService okkiWebsiteQualityService;
    /**
     *   小满推送数据
     *
     * @param okkiWebsiteQualityParam
     * @return
     */
    @AutoLog(value = "网站质量-小满推送数据")
    @PostMapping(value = "/websiteQuality")
    public Result<String> add(@RequestBody OkkiWebsiteQualityParam okkiWebsiteQualityParam, @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization) throws ParseException {
        log.info("请求参数 param" + okkiWebsiteQualityParam.toString() + "authorization" + authorization);
        String Authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlYW4iLCJleHAiOjE3MTQ0NjU5Njl9.QdCBYR8mioXcKGi1n5RdMQ5RbJHCSGbE8dkdwYuOXh4";
        if (!authorization.equals(Authorization)) {
            return Result.error("sign 验证不通过");
        }
        OkkiWebsiteQuality okkiWebsiteQuality = new OkkiWebsiteQuality();
        okkiWebsiteQuality.setWebsiteQualityId(okkiWebsiteQualityParam.getId());
        okkiWebsiteQuality.setSiteId(okkiWebsiteQualityParam.getSite_id());
        okkiWebsiteQuality.setClientId(okkiWebsiteQualityParam.getClient_id());
        okkiWebsiteQuality.setSitemapStatusCode(okkiWebsiteQualityParam.getSitemap_status().getCode());
        okkiWebsiteQuality.setSitemapStatusProblems(Arrays.toString(okkiWebsiteQualityParam.getSitemap_status().getProblems()));
        okkiWebsiteQuality.setGaStatusCode(okkiWebsiteQualityParam.getGa_status().getCode());
        okkiWebsiteQuality.setGaStatusProblems(Arrays.toString(okkiWebsiteQualityParam.getGa_status().getProblems()));
        okkiWebsiteQuality.setDomainStatusCode(okkiWebsiteQualityParam.getSitemap_status().getCode());
        okkiWebsiteQuality.setDomainStatusProblems(Arrays.toString(okkiWebsiteQualityParam.getDomain_status().getProblems()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        date = sdf.parse(okkiWebsiteQualityParam.getCreate_time());
        okkiWebsiteQuality.setCreateTime(date);
        okkiWebsiteQuality.setInquiryEmailStatusCode(okkiWebsiteQualityParam.getInquiry_email_status().getCode());
        okkiWebsiteQuality.setInquiryEmailStatusProblems(Arrays.toString(okkiWebsiteQualityParam.getInquiry_email_status().getProblems()));
        okkiWebsiteQuality.setProgress(okkiWebsiteQualityParam.getProgress());
        okkiWebsiteQuality.setLoadTime(okkiWebsiteQualityParam.getLoad_time());
        okkiWebsiteQuality.setLoadSpeedStatusCode(okkiWebsiteQualityParam.getLoad_speed_status().getCode());
        okkiWebsiteQuality.setLoadSpeedStatusProblems(Arrays.toString(okkiWebsiteQualityParam.getLoad_speed_status().getProblems()));
        okkiWebsiteQualityService.save(okkiWebsiteQuality);
        return Result.OK("添加成功！");
    }
}
