package com.malk.cloudpure.controller;

import com.malk.cloudpure.service.CPClient;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Extension;
import com.malk.service.vika.VKClient;
import com.malk.utils.UtilDateTime;
import com.malk.utils.UtilMap;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.net.URLDecoder;
import java.time.LocalDate;
import java.util.Date;
import java.util.Map;

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

    @Autowired
    private CPClient cpClient;

    /**
     * OA 审批 连接器
     */
    @PostMapping("oa-update")
    McR update(@RequestBody Map data) {

        McException.assertParamException_Null(data, "userId", "type");
        String compId = "";
        if ("售前".equals(data.get("type"))) {
            compId = "numberField_llw07mxz";
        }
        if ("转单".equals(data.get("type"))) {
            compId = "numberField_llw07mxx";
        }
        if (StringUtils.isNotBlank(compId)) {
            String date = UtilDateTime.formatDate(new Date());
            cpClient.upsertDailyRecord(data.get("userId").toString(), date, UtilMap.map(compId + ", type", 1, compId));
        }
        return McR.success();
    }

    /**
     * 酷应用 连接器 [卡片实例数据源]
     */
//    @PostMapping("application")
//    Map application() {
//
//        return cpClient.applicationData();
//    }

    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDClient_Extension ddClient_extension;

    @Autowired
    private DDConf ddConf;

    /// 酷应用卡片推送, 宜搭导入完成率
    @SneakyThrows
    @PostMapping("robot")
    McR robot() {

        Thread.sleep(3000);
        String tDate = UtilDateTime.formatLocalDate(LocalDate.now());
        cpClient.sendCardMessage(tDate, "组织数字化团队", "业务数字化团队", "协同数字化团队, 测试群");
        return McR.success();
    }

    @Autowired
    private VKClient vkClient;

    @SneakyThrows
    @PostMapping("test")
    McR test() {

//        cpClient.syncBusinessUserInfo();
//        cpClient.syncXBongBongForRecord_all(LocalDate.now());

        //        ddClient_extension.sendGroupMessages(ddClient.getAccessToken(), UtilMap.map("content", "xxx12"), "sampleText", "cid/Y3nHe5fkCnHg2qsP43loQ==", ddConf.getRobotCode(), "");


//        return McR.success(vkClient.getRecords("dst6ip677mZUxCnLiS", UtilMap.map("filterByFormula", "find('钉专 CSM', 程婕) > 0")));

        String condition = URLDecoder.decode("OR(find(%22%E7%A8%8B%E5%A9%95%22%2C%20%7B%E9%92%89%E4%B8%93%20CSM%7D)%20%3E%200%2C%20find(%22%E7%A8%8B%E5%A9%95%22%2C%20%7B%E9%92%89%E4%B8%93%20CSM%7D)%20%3E%200)", "UTF-8");
        log.info("xxxx, {}", condition);
        Map data = new HashedMap();
        data.put("filterByFormula", condition);
        return McR.success(vkClient.getRecords("dst6ip677mZUxCnLiS", data));
    }


}


