package com.dxhy.extend.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.http.HttpUtils;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.R;
import com.dxhy.extend.dao.GsClientMapper;
import com.dxhy.extend.entity.GsClient;
import com.dxhy.extend.model.SNSAPObject;
import com.dxhy.extend.service.bb.VouncherSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author wangzhikun
 * @Date 2023/3/30 2023/3/30
 */
@Controller
@RequestMapping("vouncherSync")
@Slf4j
public class VouncherSyncController {
    @Resource
    private VouncherSyncService vouncherSyncService;
    @Value("${sdny.record.environment}")
    private String environment;
    @Resource
    private GsClientMapper gsClientMapper;
    @PostMapping("getSapData")
    public Object getSapData(@RequestBody Map<String,String> map){
        //是否全量 X为全量推送，空为增量推送。
        //全量推送逻辑为 查询出的符合条件的凭证全部推送，全量推送不再检查是否在自定义表中已传输。
        //增量推送，已传输的凭证数据不再推送（检查传输记录表是否已传输成功）
        try{
        String companyCodes = map.get("companyCode");
        String[] split = companyCodes.split(",");
        StringBuilder sb = new StringBuilder();
        for (String str:split){
            /**
             * 传参逻辑   年  月 必传  期间只能传一个月的
             *
             */
            String ifAll=map.get("ifAll");
            //公司代码

            //开始日期
            String startTime= map.get("startTime");
            String endTime = map.get("endTime");
            if(startTime!=null && startTime!=""){
                startTime=startTime.replace("-","");
            }
            if(endTime!=null && endTime!=""){
                endTime=endTime.replace("-","");
            }
            //结束日期
            String month=map.get("monat");
            //GJAHR  nian
            String gjahr=map.get("gjahr");

            SNSAPObject object = new SNSAPObject();
            object.setSYSID("FPXT");
            object.setIFYWID("FI842");
            object.setBSKEY(UUID.randomUUID().toString().replace("-", ""));
            //object.setSAPKEY("");

//        object.setZFILED1("");
//        object.setZFILED2("");
//        object.setZFILED3("");
//        object.setZFILED4("");
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            List<GsClient> gsClients = gsClientMapper.selectByGsdm(str);
            if(gsClients!=null && gsClients.size()>0){
                object.setZFILED5(gsClients.get(0).getClient());
            }else {
                object.setZFILED5(environment);
            }
            Map<String,String> requestMap = new HashMap<>();
            List<Object> list = new ArrayList<>();
            requestMap.put("ZGSDM",str);
            requestMap.put("GJAHR",gjahr);
            requestMap.put("MONAT",month);
            requestMap.put("ZDATEF",startTime);
            requestMap.put("ZDATET",endTime);
            requestMap.put("ZFLAG",ifAll);
            object.setZDATA(JSONObject.toJSONString(requestMap));
            JSONObject request = new JSONObject();
            request.put("IS_INPUT",object);

            String s = vouncherSyncService.sendPo(JSONObject.toJSONString(request));
            Map map1 = JSONObject.parseObject(s, Map.class);
            Map output = (Map)map1.get("ES_OUTPUT");
            String ztype =(String) output.get("ZTYPE");
            if(ztype.equals("S")){

            }else {
                sb.append(str+output.get("ZDATA"));
            }
        }
        if(sb.length()>0){
            return ResponseEntity.ok(R.ok().put("data",sb));
        }else{
            return ResponseEntity.ok(R.ok().put("data","拉取成功"));
        }
        }catch (Exception e ){
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


}
