package com.malk.junengtai.test;

import com.alibaba.fastjson.JSONObject;
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.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/test")
public class test {
    @Autowired
    private YDClient ydClient;
    @Autowired
    private YDConf ydConf;

    @PostMapping("/test1")
    McR test1(){
        System.out.println("接口正常！");
        return McR.success("接口正常！");
    }

    /*批量修改数据*/
    @PostMapping("/test2")
    McR test2(){
        //1、查询所有的收票数据
        String instanceId = "FORM-27421C0D0ACA41DFB85D05A9878161B1SDCE";
        YDParam ydParam = YDParam.builder().formUuid(instanceId).build();
        ydParam.setPageSize(1);
        long totalCount = ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form).getTotalCount();
        System.out.println(totalCount);

        List<Map> dataList = new ArrayList<>();
        ydParam.setCurrentPage(1);
        ydParam.setPageSize(100);
        for (int page = 1; page <= (Math.ceil(totalCount / 100 )!=0?Math.ceil(totalCount / 100 + 1):1); page++) {
            ydParam.setCurrentPage(page);
            dataList.addAll((List<Map>) ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form).getData());
        }
        //2、遍历收票数据
        int i = 0;
        for(Map li: dataList){
            i ++ ;
            String currentInstanceId = (String) li.get("formInstanceId");
            System.out.println(currentInstanceId);
            //3、遍历子表单，求和明细数据
            Map data =ydClient.queryData(YDParam.builder().formInstId(currentInstanceId)
                    .appType(ydConf.getAppType()).systemToken(ydConf.getSystemToken())
                    .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            Double A_total = 0.0;
            List<Map> jsonString = (List<Map>)data.get("tableField_m8oaxbru");
            for (Map map1 : jsonString) {
                Object o = map1.get("numberField_m8oaxbry");
                System.out.println(o);
                double d = ((Number) o).doubleValue();
                System.out.println(d);
                A_total = A_total + d;
            }
            System.out.println("总金额：" + A_total);
            //4、更新发票汇总字段
            ydClient.operateData(YDParam.builder()
                    .appType(ydConf.getAppType())
                    .systemToken(ydConf.getSystemToken())
                    .formInstanceId(currentInstanceId)
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_mkax9d6k",A_total)))
                    .build(), YDConf.FORM_OPERATION.update);
            log.info("{}/{}",i,dataList.size());
        }
        return McR.success();
    }

}