package com.malk.shantai.controller;

import com.alibaba.fastjson.JSONObject;
import com.malk.server.common.McR;
import com.malk.shantai.service.StDingProcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/ykb/")
public class StYkbController {

    @Autowired
    private StDingProcService stDingProcService;

    @PostMapping("callback")
    public McR test(@RequestBody JSONObject param) {
        log.info("进入易快报回调接口， param:{}",param);
        Map formSpecification = (Map) param.get("formSpecification");
        String state = param.get("state").toString().toUpperCase();
        String specificationId = formSpecification.get("specificationId").toString();
        String templateId = specificationId.substring(0, specificationId.indexOf(':'));
        //若是对外付款单且状态已支付 则进行评论同步
        if (templateId.equals("ID01yVMv53gDSL") && state.equals("PAID")){
            String flowId = param.getString("flowId");
            //获取单据信息
            Map flow = stDingProcService.getFlowInfo(flowId);
            if (Objects.nonNull(flow)){
                //提交评论
                stDingProcService.commentSync(flow,flowId);
            }
        }


        return McR.success();
    }

    // {"userInfo":{"id":"pQccdcNGsM3U00:16278700718435315","name":"李溯"},"formSpecification":{"specificationId":"ID01yVMv53gDSL:5976eb8c57a30399ebd81a1e8162597ad35f9c1e","specificationName":"对外付款单"},"corporationId":"pQccdcNGsM3U00","action":"backlog.paying","messageId":"ID01zbSwmdvPqL","state":"paying","flowId":"ID01z0IUnU0943","nodeId":"FLOW:2135271494:945281472","actionName":"待支付"}{"userInfo":{"id":"pQccdcNGsM3U00:16278700718435315","name":"李溯"},"formSpecification":{"specificationId":"ID01yVMv53gDSL:5976eb8c57a30399ebd81a1e8162597ad35f9c1e","specificationName":"对外付款单"},"corporationId":"pQccdcNGsM3U00","action":"backlog.paying","messageId":"ID01zbSwmdvPqL","state":"paying","flowId":"ID01z0IUnU0943","nodeId":"FLOW:2135271494:945281472","actionName":"待支付"}

    //每小时定时同步评论水单
    @Scheduled(cron = "0 0 0/1 * * ? ")
    @GetMapping("syncComment")
    public McR syncComment() {
        log.info("开始同步评论水单， time:{}",new Date().getTime());

        stDingProcService.commentSyncAll();

        return McR.success();
    }
}
