

package com.srm.bpm.facade.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Strings;

import com.alibaba.fastjson.JSONObject;
import com.srm.bpm.facde.dto.BillItemDTO;
import com.srm.bpm.facde.oa.OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client;
import com.srm.bpm.facde.oa.ReceiveRequestInfoByJsonDto;
import com.srm.bpm.infra.service.FormThirdItemLogic;
import com.srm.bpm.logic.converts.BillBasicConvert;
import com.srm.bpm.logic.service.BillLogic;
import com.srm.bpm.logic.util.StringUtil;
import com.srm.bpm.logic.vo.BillItemVO;
import com.srm.common.data.rest.R;

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 lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/third/part/bill")
@RequiredArgsConstructor
public class ThirdPartBillController {
    private final FormThirdItemLogic formThirdItemLogic;
    private final BillLogic billLogic;
    private final BillBasicConvert billBasicConvert;

    @PostMapping("/submit")
    public R<BillItemDTO> submitFormData(@RequestBody JSONObject jsonObject) {
        final Long processId = Long.valueOf(StringUtil.ob2str(jsonObject.get("processId")));
        final Long billId = Long.valueOf(StringUtil.ob2str(jsonObject.get("billId")));
        String nextApprover = StringUtil.ob2str(jsonObject.get("nextApprover"));
        String billCode = StringUtil.ob2str(jsonObject.get("billCode"));
        if (Strings.isNullOrEmpty(nextApprover)) {
            nextApprover = "0";
        }
        final String toForm = formThirdItemLogic.analysisToForm(jsonObject.getString("data"), processId);
        final BillItemVO billItem = billLogic.startFlow(processId, billId, toForm, nextApprover,billCode);
        /*
         * 链接OA统一webservices.ofs.weaver.com.cn
         * Author ： yuhai
         * date ： 2023-11-08
         * */
        BillItemVO billItemVO = new BillItemVO();
        billItemVO.setId(billItem.getId());
        List<ReceiveRequestInfoByJsonDto> requestInfoByJsonDtos2 = billLogic.findrequestInfo(billItemVO);
        String token =billLogic.getToken4();
        OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client o1= new OfsTodoDataWebServicePortType_OfsTodoDataWebServiceHttpPort_Client();
        for (ReceiveRequestInfoByJsonDto requestInfoByJsonDto:requestInfoByJsonDtos2){
            if ("1".equals(requestInfoByJsonDto.getIsremark())){
                String  requestData = o1.getRequestData(requestInfoByJsonDto,"0");
        //          o1.OfsTodoDataWebService(requestData);
                o1.ofsTodoDatabyEten(requestData,token);
            }

        }




        return R.ok(billBasicConvert.billItemVOToDTO(billItem));
    }

    @PostMapping("/save")
    public R<BillItemDTO> saveFormData(@RequestBody JSONObject jsonObject) {
        final Long processId = Long.valueOf(StringUtil.ob2str(jsonObject.get("processId")));
        final Long billId = Long.valueOf(StringUtil.ob2str(jsonObject.get("billId")));
        final String toForm = formThirdItemLogic.analysisToForm(jsonObject.getString("data"), processId);
        final BillItemVO billItem = billLogic.saveDrafts(processId, billId, toForm);
        return R.ok(billBasicConvert.billItemVOToDTO(billItem));
    }

    @PostMapping("/convert")
    public R convert(String data, Long processId) {
        final String toForm = formThirdItemLogic.analysisToForm(data, processId);
        return R.ok(toForm);
    }

}
