package com.malk.laidi.controller;

import com.alibaba.fastjson.JSONObject;
import com.malk.laidi.util.HttpUtil;
import com.malk.laidi.util.ddutil;
import com.malk.server.common.McR;
import com.malk.service.h3yun.CYClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 错误抛出与拦截详见 CatchException
 */
@Slf4j
@RestController
@RequestMapping("shenji")
public class SJController {


    @Autowired
    private CYClient cyClient;


    /**
     * 同步氚云 数据到付款单
     */
    @PostMapping("h3yun-http")
    McR http(@RequestBody Map<String, String> data, @RequestParam String code) {
        //写入付款单

        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(data);
        try {
            HttpUtil.post("https://connector.dingtalk.com/webhook/trigger/data/sync?webhookId=10298fc91bbe0bf8e42e000n",null,jsonObject.toJSONString(),null);
            HashMap<String, String> map = new HashMap<>();
            map.put("status", "是");
            return McR.success(map);
        } catch (Exception exception) {
            exception.printStackTrace();
        }


        return McR.success(UtilMap.map("status", "否"));
    }

    /**
     *
     */
    @PostMapping("approved")
    McR approved(@RequestBody Map<String, String> data) {

        try {
            JSONObject jsonObject = new JSONObject();

            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = sdf.format(date);
            System.out.println(currentDate);

            jsonObject.put("FPAYSTATE","已支付");
            jsonObject.put("FPAYTIME",currentDate);
            cyClient.updatetForm(ddutil.gettoken(), "D148951Sfsbr88lvzwpxtugt7pc5rl4a6", data.get("h3_objid"),jsonObject.toJSONString() );
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return McR.success();
    }
}
