package com.yn.bftl.thirdparty.modules.erp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.configuration.enums.SyncStatus;
import com.yn.bftl.common.modules.order.entity.Order;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.business.dto.ErpLoggerCreateDTO;
import com.yn.bftl.thirdparty.modules.business.service.ErpLoggerService;
import com.yn.bftl.thirdparty.modules.erp.service.ErpService;
import com.yn.bftl.thirdparty.modules.erp.util.ErpUtil;
import com.yn.bftl.thirdparty.modules.erp.vo.ErpResponseDataVO;
import com.yn.bftl.thirdparty.modules.erp.vo.ErpResponseVO;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * ERP服务实现
 *
 * @author huabiao
 * @create 2023/7/6  17:29
 **/
@Service
public class ErpServiceImpl implements ErpService {

    @Resource
    private ErpUtil erpUtil;
    @Resource
    private ErpLoggerService erpLoggerService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ErpResponseVO deleteWholeLine(Order order) {
        if (order.getErpAccountSet() == null) {
            throw new YnceErrorException(YnceError.YNCE_206008);
        }
        ErpResponseVO erpResponseVO = new ErpResponseVO();
        ErpLoggerCreateDTO erpLoggerCreateDTO;
        String requestParameters = null;
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("docno", order.getErpNumber());
            jsonObject.put("srcdocno", order.getSn());
            jsonObject.put("money", order.getPayAmount().toString());
            requestParameters = jsonObject.toJSONString();
            erpResponseVO = erpUtil.postErpMethod(order.getErpAccountSet(), requestParameters, 8);
            // 拼接ERP日志信息
            erpLoggerCreateDTO = ErpLoggerCreateDTO.builder()
                    .type("晚安关闭销售订单")
                    .reason(erpResponseVO.getErrmsg())
                    .parameters(jsonObject.toJSONString())
                    .documentType("销售订单关闭")
                    .documentId(Optional.ofNullable(erpResponseVO.getData()).orElseGet(ErpResponseDataVO::new).getId())
                    .documentDetails(Optional.ofNullable(erpResponseVO.getData()).orElseGet(ErpResponseDataVO::new).getDocNo())
                    .errorCode(erpResponseVO.getErrcode())
                    .syncStatus(erpResponseVO.getData() != null ? SyncStatus.SUCCESS : SyncStatus.FAILED)
                    .erpAccountSet(order.getErpAccountSet())
                    .build();
        } catch (Exception exception) {
            // 拼接错误信息
            erpResponseVO.setErrcode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
            erpResponseVO.setErrmsg(exception.getMessage());
            // TODO 后续若有更多的ERP请求，可将yn-bftl-scheduling项目中ErpRequestType迁移到公共包统一使用
            erpLoggerCreateDTO = ErpLoggerCreateDTO.builder()
                    .type("晚安关闭销售订单")
                    .reason(exception.getMessage())
                    .parameters(requestParameters)
                    .documentType("销售订单关闭")
                    .syncStatus(SyncStatus.FAILED)
                    .build();
        }
        erpLoggerCreateDTO.setErpAccountSet(order.getErpAccountSet());
        erpLoggerCreateDTO.setCompany(order.getSellerCompany());
        erpLoggerCreateDTO.setOrderSn(order.getSn());
        erpLoggerService.createErpLogger(erpLoggerCreateDTO);
        return erpResponseVO;
    }
}
