package com.hzya.frame.seeyon.recbill.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.seeyon.cbs8.entity.PaymentEntity;
import com.hzya.frame.seeyon.recbill.dao.IRecBillDao;
import com.hzya.frame.seeyon.recbill.entity.RecBillEntity;
import com.hzya.frame.seeyon.recbill.service.IRecBillService;
import com.hzya.frame.seeyon.util.OABipUtil;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @content 付款单同步BIP
 * @author laborer
 * @date 2024/6/20 0020 15:20
 *
 */

@Service("RecBillServiceImpl")
public class RecBillServiceImpl extends BaseService<PaymentEntity,String> implements IRecBillService {
    private static final Logger logger = LoggerFactory.getLogger(RecBillServiceImpl.class);

    @Autowired
    private IRecBillDao payBillDao;
    /**
     *
     * @content 工程付款单数据同步BIP
     * @author laborer
     * @date 2024/6/20 0020 11:24
     *
     */
    @Override
    public JsonResultEntity sendRecBillToBip(JSONObject requestJson) {
        RecBillEntity entity = new RecBillEntity();
        requestJson.put("db_code","OA");
        entity.setDataSourceCode(requestJson.getString("db_code"));
        List<RecBillEntity>payBillEntityList = payBillDao.getOaRecBill(entity);
        if(CollectionUtils.isNotEmpty(payBillEntityList)){
            for(RecBillEntity pay : payBillEntityList){
                String token = OABipUtil.getBipToken("yonyou","8000230000");
                JSONObject main = bindingAdd(pay);
                logger.info("银行流水收款信息数据{}",main.toString());
                String result = OABipUtil.sendU9cTOBipEsb(main.toString(),"8000230016",token);
                logger.info("银行流水收款信息数据{}",result);
                JSONObject resultObj = JSON.parseObject(result);
                boolean flag = resultObj.getBoolean("success");
                if(flag){
                    pay.setState("Y");
                }else{
                    pay.setState("N");
                }
                pay.setDataSourceCode(requestJson.getString("db_code"));
                payBillDao.updateState(pay);
                // todo 后续在写吧（没字段等OA开了外网在创建），修改推送状态，避免再次查询
            }
        }
        return null;
    }

    @NotNull
    private JSONObject bindingAdd(RecBillEntity pay) {
        JSONObject head = new JSONObject();
        head.put("pk_org","");//所属组织
        head.put("pk_group","");//集团
        head.put("bill_type","F4");//单据类型 默认F5
        head.put("trade_type","D4");//付款结算类型 默认D5
        head.put("source_flag","2");//付款结算类型 默认2
        head.put("bill_date",pay.getBillDate());//单据日期
        head.put("primal_money",pay.getPrimalMoney());//付款原币金额
        head.put("pk_currtype","CNY");//币种
        head.put("billmaker","");//制单人
        //处理明细数据，按照明细付款 多个明细生成多个付款结算单
        JSONArray detailsArr = new JSONArray();
        JSONObject body = new JSONObject();
        body.put("pk_org","");//所属组织
        body.put("pk_group","");//集团
        body.put("bill_type","F4");//单据类型 默认F5
        body.put("trade_type","D4");//付款结算类型 默认D5
        body.put("pk_currtype","CNY");//币种
        body.put("bill_date",pay.getBillDate());//单据日期
        body.put("pay_primal",pay.getPrimalMoney());//付款原币金额
        body.put("creationtime",pay.getBillDate());//创建时间
        body.put("direction","-1");//方向 :1=收;-1=付;
        body.put("objecttype","0");//交易对象
        body.put("pk_customer",pay.getPkCustomer());//客户
        body.put("pk_account",pay.getPkAccount());//收款银行账号
        detailsArr.add(body);
        JSONObject main = new JSONObject();
        main.put("head",head);//表头
        main.put("body",detailsArr);//明细数据
        return main;
    }
}
