package com.malk.lianan.controller;

import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONObject;
import com.malk.server.common.McR;
import com.malk.utils.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/budget/")
@Slf4j
public class LianAnBudgetController {

    private static String SIGN_KEY="be56e057f20f883e";

    @PostMapping("datasync")
    public McR dataSync(@RequestBody JSONObject param){
        log.info("全面预算接口:{}",param);
        if(PublicUtil.isNull(param,"timestamp","sign","msgType","data")){
            return McR.error("201","参数校验不通过!");
        }
        if(!signCheck(param)){
            return McR.error("202","签名校验不通过!");
        }
        String msgType = param.getString("msgType");
        if("T01".equals(msgType)){
            log.info("科目数据:{}",param.get("data"));
        }else if("T02".equals(msgType)){
            log.info("产品数据:{}",param.get("data"));
        }else if("T03".equals(msgType)){
            log.info("项目数据:{}",param.get("data"));
        }else if("Y01".equals(msgType)){
            log.info("预算数据:{}",param.get("data"));
        }else{
            return McR.error("203","msgType错误!");
        }
        return McR.success();
    }

    private static boolean signCheck(JSONObject param){
        String data=SIGN_KEY+param.getString("timestamp")+param.getString("data");
        MD5 md5 = new MD5();
        String digestHex = md5.digestHex(data);
        return digestHex.equals(param.getString("sign"));
    }
}
