package com.malk.lilin.Controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.malk.lilin.Service.LiLinService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.aliwork.YDSearch;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.service.aliwork.YDClient;
import com.malk.service.aliwork.YDService;
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.io.IOException;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/ll")
public class LiLinController {
    @Autowired
    private LiLinService liLinService;
    @Autowired
    private YDClient ydClient;
    @Autowired
    private YDService ydService;
    @Autowired
    private YDConf ydConf;
    @Autowired
    private DDConf ddConf;


    @GetMapping("/test")
    public McR test() throws IOException {
//        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
//                .formUuid("FORM-754586D31BF6414586E5C20DB3774A7320CV")
//                .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
//                        new YDSearch("selectField_ma0w9yrb", "已审批已付款", "SelectField",
//                                YDSearch.Type.RADIO_FIELD, YDSearch.Operator.EQ),
//                        new YDSearch("radioField_mimhsd0h", "否", "是否创建付款通知",
//                                YDSearch.Type.RADIO_FIELD, YDSearch.Operator.EQ)
//                )))
//                .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();
//        for (Map item : list) {
//            boolean processSuccess = false;
//            String formInstanceId = null;
//            String mainInstructionId = null;
//
//            try {
////                formInstanceId = UtilMap.getString(item, "formInstanceId");
//                ydClient.operateData(YDParam.builder()
//                        .formUuid("FORM-754586D31BF6414586E5C20DB3774A7320CV")
//                        .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("radioField_mimhsd0h", "是")))
//                        .build(), YDConf.FORM_OPERATION.multi_update);
//            }
//        }
//        ydClient.operateData(YDParam.builder()
//
//                .formInstanceId("1ea42aeb-dcb9-4a10-9bec-ff297130a8a3")
////
//                .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_mbh8usao, textField_megl2h3a","四川大学华西第二医院, 四川大学华西第二医院")))//xz04
////               .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("employeeField_lwrrbl0i",Arrays.asList("1007332"))))//ht01
////               .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("employeeField_lwsvj15o",Arrays.asList("2312"))))//ht02
////               .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_lvw4umfr, textField_m0dh8wmp","昆山博语艺术培训有限公司 -合同章, 3315241551458668845")))
//                .useLatestVersion(true)
//                .build(), YDConf.FORM_OPERATION.update);
//        return McR.success();
         liLinService.lilinLS(new HashMap<>());
        return McR.success();
    }

    @PostMapping("/lilinUpsert")
    public McR lilinUpsert(@RequestBody Map<String, Object> map) {
        if (Objects.nonNull(map.get("rqqj"))) {
            List<String> rqqj = (List<String>) map.get("rqqj");
            String bdbh = String.valueOf(map.get("ddbh"));
            return liLinService.lilinUpsert(rqqj, bdbh);

        }
        return McR.errorParam("实例id不能为空！");
    }

    @PostMapping("/lilinhqbank")//获取银行联行号
    public McR hqbank(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinhqbank:{}", JSON.toJSONString(map));
        return liLinService.hqbank(map);
    }

    @PostMapping("/lilinZD")//付款制单
    public McR lilinZD(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinZD:{}", JSON.toJSONString(map));
        liLinService.lilinZD(map);
        return McR.success();

    }

    @PostMapping("/lilinJG")//支付结果查询
    public McR lilinJG(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinJG:{}", JSON.toJSONString(map));
        return liLinService.lilinJG(map);

    }
    @PostMapping("/lilinPLJG")//支付结果查询
    public McR lilinPLJG(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinJG:{}", JSON.toJSONString(map));
        return liLinService.taskPLJG();

    }
    @PostMapping("/lilinLS")//查银行流水
    public McR lilinLS(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinLS:{}", JSON.toJSONString(map));
        return liLinService.lilinLS(map);

    }

    @PostMapping("/lilinLSCF")//流水拆分
    public McR lilinLSCF(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinLSCF:{}", JSON.toJSONString(map));
        return liLinService.lilinLSCF(map);

    }

    @PostMapping("/lilinBatchUpdate")//批量更新付款单
    public McR lilinBatchUpdate(@RequestBody Map<String, Object> map) throws IOException {
        log.info("lilinBatchUpdate:{}", JSON.toJSONString(map));
        liLinService.lilinBatchUpdate(map);
        return McR.success();

    }
//    @PostMapping("/lilinUpsert1")
//    public McR lilinUpsert1(@RequestBody Map<String,Object> map) {
//        if (Objects.nonNull(map.get("rqqj"))) {
//            // 获取当前日期，替换为当天的起止时间
//            LocalDate today = LocalDate.now();
//            LocalDateTime startOfDay = today.atStartOfDay();
//            LocalDateTime endOfDay = today.atTime(LocalTime.MAX);
//
//            // 格式化为字符串，格式：yyyy-MM-dd HH:mm:ss
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
//            List<String> rqqj = Arrays.asList(
//                    startOfDay.format(formatter),
//                    endOfDay.format(formatter)
//            );
//
//            System.out.println("r===" + rqqj);
//
//            String bdbh = String.valueOf(map.get("bdbh"));
//            return liLinService.lilinUpsert(rqqj, bdbh);
//        }
//        if (Objects.nonNull(map.get("rqqj"))){
//            List<String> rqqj = (List<String>) map.get("rqqj");
//            System.out.println("r==="+rqqj);
////            String  bdbh = String.valueOf(map.get("ddbh"));
////            return liLinService.lilinUpsert(rqqj,bdbh);
//        }
//        return McR.errorParam("实例id不能为空！");
//    }
}
