package com.cool.store.controller.webb;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.request.AuditResultRequest;
import com.cool.store.request.LicenseBizContentRequest;
import com.cool.store.request.XfsgOpenApiRequest;
import com.cool.store.response.LicenseApiResponse;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.KdzApiService;
import com.cool.store.utils.EncryptUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "酷店掌API")
@RestController
@RequestMapping("/{enterprise-id}/api")
@Slf4j
public class KdzApiController {

    String allowEnterpriseId = "45f92210375346858b6b6694967f44de,e17cd2dc350541df8a8b0af9bd27f77d,28c20a7b42b94171acb1ab3f631d69e1,9ee7b8b48e2447f9a2075b5a46e94d08";

    @Resource
    KdzApiService kdzApiService;

    @ApiOperation("审核结果")
    @PostMapping("/audit/result")
    public ResponseResult<Boolean> auditResult(@PathVariable(value = "enterprise-id") String eid,
                                               @RequestBody XfsgOpenApiRequest request) {
        log.info("auditResult requestBody :{}", JSONObject.toJSONString(request));
        if (!verifyMD5(request, eid)) {
            log.error("验签失败,request：{};eid:{}", JSONObject.toJSONString(request), eid);
            return ResponseResult.fail(ErrorCodeEnum.VERIFY_MD5_FALSE);
        }
        if (eid == null || request.getBizContent() == null) {
            log.error("参数校验失败,BizContent：{}", JSONObject.toJSONString(request.getBizContent()));
            return ResponseResult.fail(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        AuditResultRequest auditResultRequest = JSONObject.parseObject(request.getBizContent(), AuditResultRequest.class);
        return ResponseResult.success(kdzApiService.auditResult(auditResultRequest));
    }

    @ApiOperation("证照信息回调")
    @PostMapping("/license")
    public ResponseResult<LicenseApiResponse> license(@PathVariable(value = "enterprise-id") String eid,
                                                      @RequestBody XfsgOpenApiRequest request) {
        log.info("license requestBody :{}", JSONObject.toJSONString(request));
        if (!verifyMD5(request, eid)) {
            log.error("验签失败,request：{};eid:{}", JSONObject.toJSONString(request), eid);
            return ResponseResult.fail(ErrorCodeEnum.VERIFY_MD5_FALSE);
        }
        if (eid == null || request.getBizContent() == null) {
            log.error("参数校验失败,BizContent：{}", JSONObject.toJSONString(request.getBizContent()));
            return ResponseResult.fail(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        LicenseBizContentRequest storeNum = JSONObject.parseObject(request.getBizContent(), LicenseBizContentRequest.class);
        return ResponseResult.success(kdzApiService.license(storeNum.getStoreNum()));
    }


    public static boolean verifyMD5(XfsgOpenApiRequest request, String eid) {
        //签名
        StringBuffer sb = new StringBuffer();
        //用户唯一标识id
        sb.append("timestamp=").append(request.getTimestamp()).append("&");
        //企业唯一标识enterpriseId
        sb.append("enterpriseId=").append(eid);
        String md5 = EncryptUtil.xfsgMd5(sb.toString());
        return md5.equals(request.getSign());
    }

}
