package com.hzs.sale.pub.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.core.enums.EDealerYesNo;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.sale.ext.SaOrderChargeLogExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.sale.deliver.dto.OrderSelfDTO;
import com.hzs.sale.deliver.service.ISaDeliverBusinessService;
import com.hzs.sale.order.dto.OrderChangeAddressDTO;
import com.hzs.sale.order.dto.OrderChangeProductDTO;
import com.hzs.sale.order.dto.OrderChangeProductDetailDTO;
import com.hzs.sale.order.dto.OrderReturnDTO;
import com.hzs.sale.order.param.OrderCancelParam;
import com.hzs.sale.order.service.IOrderBusinessService;
import com.hzs.sale.order.service.ISaOrderChargeLogService;
import com.hzs.sale.order.service.ISaOrderItemsService;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.sale.pub.dto.*;
import com.hzs.sale.pub.service.ISaleApprovalBusiness;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 订单模块统一的审批结束回调入口
 */
@Service
@Slf4j
public class SaleApprovalBusiness implements ISaleApprovalBusiness {

    @Autowired
    private ISaOrderService iSaOrderService;
    @Autowired
    private ISaOrderItemsService iSaOrderItemsService;
    @Autowired
    private ISaOrderChargeLogService iSaOrderChargeLogService;
    @Autowired
    private IOrderBusinessService iOrderBusinessService;
    @Autowired
    private ISaDeliverBusinessService iSaDeliverBusinessService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @DubboReference
    IAreaServiceApi iAreaServiceApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approvalCallback(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        // 返回内容
        String str = null;
        if (EApproveStatus.FINISH.getValue() == approvalBusinessResultDTO.getEApproveStatus().getValue()) {
            // 已完成处理
            switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                case ORDER_CANCEL:
                    // 撤销订单
                    str = iOrderBusinessService.cancelOrderApprovalAgree(approvalBusinessResultDTO, EApprovalBusiness.ORDER_CANCEL.getValue());
                    break;
                case ORDER_CANCEL_PRODUCT:
                    // 撤销产品订单
                    str = iOrderBusinessService.cancelOrderApprovalAgree(approvalBusinessResultDTO, EApprovalBusiness.ORDER_CANCEL_PRODUCT.getValue());
                    break;
                case ORDER_CHANGE_ADDRESS:
                    // 订单修改地址
                    str = iOrderBusinessService.changeAddressApprovalAgree(approvalBusinessResultDTO);
                    break;
                case ORDER_CHANGE_PRODUCT:
                    // 订单调换货物
                    str = iOrderBusinessService.changeProductApprovalAgree(approvalBusinessResultDTO);
                    break;
                case ORDER_RETURN:
                    // 订单退单
                    str = iOrderBusinessService.returnOrderApprovalAgree(approvalBusinessResultDTO);
                    break;
                case SELF_DELIVERY_MANAGE:
                    // 订单自提
                    str = iSaDeliverBusinessService.selfDeliverApprovalAgree(approvalBusinessResultDTO);
                    break;
                case ORDER_DEDUCTION:
                    // 订单抵扣
                    str = iOrderBusinessService.orderDeductionAgree(approvalBusinessResultDTO);
                    break;
            }
        } else {
            // 已驳回处理
            switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                case ORDER_CANCEL:
                    // 撤销订单
                    str = iOrderBusinessService.cancelOrderApprovalReject(approvalBusinessResultDTO, EApprovalBusiness.ORDER_CANCEL.getValue());
                    break;
                case ORDER_CANCEL_PRODUCT:
                    // 撤销产品订单
                    str = iOrderBusinessService.cancelOrderApprovalReject(approvalBusinessResultDTO, EApprovalBusiness.ORDER_CANCEL_PRODUCT.getValue());
                    break;
                case ORDER_CHANGE_ADDRESS:
                    // 订单修改地址
                    str = iOrderBusinessService.changeAddressApprovalReject(approvalBusinessResultDTO);
                    break;
                case ORDER_CHANGE_PRODUCT:
                    // 订单调换货物
                    str = iOrderBusinessService.changeProductApprovalReject(approvalBusinessResultDTO);
                    break;
                case ORDER_RETURN:
                    // 订单退单
                    str = iOrderBusinessService.returnOrderApprovalReject(approvalBusinessResultDTO);
                    break;
                case SELF_DELIVERY_MANAGE:
                    // 订单自提
                    str = iSaDeliverBusinessService.selfDeliverApprovalReject(approvalBusinessResultDTO);
                    break;
                case ORDER_DEDUCTION:
                    // 订单抵扣
                    str = iOrderBusinessService.orderDeductionReject(approvalBusinessResultDTO);
                    break;
            }
        }
        if (StringUtils.isNotEmpty(str)) {
            // 返回有信息，说明有问题，抛出异常
            throw new ServiceException(str);
        }
    }

    @Override
    public Object approvalData(EApprovalBusiness eApprovalBusiness, String businessCode, String toLanguageTag) {
        // 订单修改日志
        List<SaOrderChargeLogExt> saOrderChargeLogList = iSaOrderChargeLogService.queryDataByTypeAndCode(eApprovalBusiness.getValue(), businessCode, null);
        if (CollectionUtil.isEmpty(saOrderChargeLogList)) {
            // 订单修改日志为空
            throw new ServiceException("业务操作日志已不存在");
        }

        switch (eApprovalBusiness) {
            case ORDER_CHANGE_ADDRESS:
                // 订单修改地址
                List<ChangeAddressDTO> changeAddressList = new ArrayList<>(saOrderChargeLogList.size());

                for (SaOrderChargeLogExt saOrderChargeLog : saOrderChargeLogList) {
                    OrderChangeAddressDTO oldChangeAddress = JSONUtil.toBean(saOrderChargeLog.getBeforeData(), OrderChangeAddressDTO.class);
                    OrderChangeAddressDTO newChangeAddress = JSONUtil.toBean(saOrderChargeLog.getAfterData(), OrderChangeAddressDTO.class);

                    // 地址列表
                    Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(saOrderChargeLog.getPkCountry()).getData();

                    // 处理旧地址
                    StringBuilder oldAddress = new StringBuilder();
                    String oldProvince = areaMap.get(oldChangeAddress.getRecProvince());
                    if (StringUtils.isNotEmpty(oldProvince)) {
                        oldAddress.append(oldProvince);
                    }
                    String oldCity = areaMap.get(oldChangeAddress.getRecCity());
                    if (StringUtils.isNotEmpty(oldCity)) {
                        oldAddress.append(oldCity);
                    }
                    String oldCounty = areaMap.get(oldChangeAddress.getRecCounty());
                    if (StringUtils.isNotEmpty(oldCounty)) {
                        oldAddress.append(oldCounty);
                    }
                    oldAddress.append(oldChangeAddress.getRecAddress());

                    // 处理新地址
                    StringBuilder newAddress = new StringBuilder();
                    String newProvince = areaMap.get(newChangeAddress.getRecProvince());
                    if (StringUtils.isNotEmpty(newProvince)) {
                        newAddress.append(newProvince);
                    }
                    String newCity = areaMap.get(newChangeAddress.getRecCity());
                    if (StringUtils.isNotEmpty(newCity)) {
                        newAddress.append(newCity);
                    }
                    String newCounty = areaMap.get(newChangeAddress.getRecCounty());
                    if (StringUtils.isNotEmpty(newCounty)) {
                        newAddress.append(newCounty);
                    }
                    newAddress.append(newChangeAddress.getRecAddress());

                    changeAddressList.add(ChangeAddressDTO.builder()
                            .orderCode(saOrderChargeLog.getOrderCode())
                            .memberCode(saOrderChargeLog.getMemberCode())
                            .memberName(saOrderChargeLog.getMemberName())
                            .oldItem(ChangeAddressDetailDTO.builder()
                                    .recName(oldChangeAddress.getRecName())
                                    .recPhone(oldChangeAddress.getRecPhone())
                                    .recAddress(oldAddress.toString())
                                    .build())
                            .newItem(ChangeAddressDetailDTO.builder()
                                    .recName(newChangeAddress.getRecName())
                                    .recPhone(newChangeAddress.getRecPhone())
                                    .recAddress(newAddress.toString())
                                    .build())
                            .build());
                }
                return changeAddressList;
            case ORDER_CHANGE_PRODUCT:
                // 订单调换货物
                SaOrderChargeLogExt changeProductChargeLog = saOrderChargeLogList.get(0);

                OrderChangeProductDTO oldChangeProduct = JSONUtil.toBean(changeProductChargeLog.getBeforeData(), OrderChangeProductDTO.class);
                OrderChangeProductDTO newChangeProduct = JSONUtil.toBean(changeProductChargeLog.getAfterData(), OrderChangeProductDTO.class);

                // 查询翻译数据
                Map<String, String> changeProductMap = iTransactionCommonService.exportEnumTransactionToTag(toLanguageTag, EDealerYesNo.values());

                List<ChangeProductDetailDTO> oldItemList = new ArrayList<>(oldChangeProduct.getDetailList().size());
                // 旧明细数据处理
                for (OrderChangeProductDetailDTO orderChangeProductDetailDTO : oldChangeProduct.getDetailList()) {
                    ChangeProductDetailDTO tmpOldItems = BeanUtil.copyProperties(orderChangeProductDetailDTO, ChangeProductDetailDTO.class);
                    tmpOldItems.setIsGiftVal(changeProductMap.get(EnumsPrefixConstants.DEALER_YES_NO + tmpOldItems.getIsGift()));
                    oldItemList.add(tmpOldItems);
                }

                List<ChangeProductDetailDTO> newItemList = new ArrayList<>(newChangeProduct.getDetailList().size());
                // 新明细数据处理
                for (OrderChangeProductDetailDTO orderChangeProductDetailDTO : newChangeProduct.getDetailList()) {
                    ChangeProductDetailDTO tmpNewItems = BeanUtil.copyProperties(orderChangeProductDetailDTO, ChangeProductDetailDTO.class);
                    tmpNewItems.setIsGiftVal(changeProductMap.get(EnumsPrefixConstants.DEALER_YES_NO + tmpNewItems.getIsGift()));
                    newItemList.add(tmpNewItems);
                }

                List<ChangeProductDTO> changeProductList = new ArrayList<>();
                changeProductList.add(ChangeProductDTO.builder()
                        .orderCode(changeProductChargeLog.getOrderCode())
                        .memberCode(changeProductChargeLog.getMemberCode())
                        .memberName(changeProductChargeLog.getMemberName())
                        .oldPostage(oldChangeProduct.getPostage())
                        .newPostage(newChangeProduct.getPostage())
                        .oldItemList(oldItemList)
                        .newItemList(newItemList)
                        .build());
                return changeProductList;
            case ORDER_RETURN:
                // 订单退单
                List<ReturnDTO> returnList = new ArrayList<>(saOrderChargeLogList.size());
                for (SaOrderChargeLogExt returnChargeLog : saOrderChargeLogList) {
                    OrderReturnDTO newReturn = JSONUtil.toBean(returnChargeLog.getAfterData(), OrderReturnDTO.class);
                    returnList.add(ReturnDTO.builder()
                            .orderCode(returnChargeLog.getOrderCode())
                            .memberCode(returnChargeLog.getMemberCode())
                            .memberName(returnChargeLog.getMemberName())
                            .returnAmount(newReturn.getReturnAmount())
                            .returnRemark(newReturn.getReturnRemark())
                            .build());
                }
                return returnList;
            case ORDER_CANCEL: // 撤销订单、单点撤单、合并撤单
            case ORDER_CANCEL_PRODUCT: // 撤销产品
                List<CancelOrderDTO> cancelOrderDTOList = new ArrayList<>(saOrderChargeLogList.size());
                for (SaOrderChargeLogExt saOrderChargeLogExt : saOrderChargeLogList) {
                    OrderCancelParam orderCancelParam = JSONUtil.toBean(saOrderChargeLogExt.getAfterData(), OrderCancelParam.class);
                    CancelOrderDTO cancelOrderDTO = CancelOrderDTO.builder()
                            .orderCode(orderCancelParam.getOrderCode())
                            .memberCode(orderCancelParam.getMemberCode())
                            .memberName(orderCancelParam.getMemberName())
                            .cancelOrderCode(saOrderChargeLogExt.getBusinessCode())
                            .build();
                    if (orderCancelParam.getOrderCancelDetailParamList() != null && orderCancelParam.getOrderCancelDetailParamList().size() > 0) {
                        List<ChangeProductDetailDTO> changeProductDetailDTOList = new ArrayList<>();
                        orderCancelParam.getOrderCancelDetailParamList().forEach(orderCancelDetailParam -> {
                            changeProductDetailDTOList.add(ChangeProductDetailDTO.builder()
                                    .productName(orderCancelDetailParam.getProductName())
                                    .build());
                        });
                        cancelOrderDTO.setCancelItemsList(changeProductDetailDTOList);
                    }
                    cancelOrderDTOList.add(cancelOrderDTO);

                }
                return cancelOrderDTOList;
            case SELF_DELIVERY_MANAGE:
                // 订单自提
                // 订单明细ID
                List<Long> itemIdList = new ArrayList<>();

                for (SaOrderChargeLogExt saOrderChargeLogExt : saOrderChargeLogList) {
                    OrderSelfDTO orderSelfDTO = JSONUtil.toBean(saOrderChargeLogExt.getAfterData(), OrderSelfDTO.class);

                    itemIdList.addAll(orderSelfDTO.getItemIdList());
                }
                // 查询所有订单明细
                List<SaOrderItemsExt> saOrderItemsExtList = iSaOrderItemsService.queryListByItemId(itemIdList, null, null);
                return BeanUtil.copyToList(saOrderItemsExtList, SelfDTO.class);
            case ORDER_DEDUCTION:
                // 订单抵扣
                SaOrderChargeLogExt deductionChargeLogExt = saOrderChargeLogList.get(0);
                // 抵扣返回实体
                OrderDeductionDTO orderDeductionDTO = JSONUtil.toBean(deductionChargeLogExt.getAfterData(), OrderDeductionDTO.class);
                // 旧订单
                orderDeductionDTO.setOldOrder(iSaOrderService.queryByPkIdOrCode(orderDeductionDTO.getPkOrderOld(), null, null));
                // 新订单
                orderDeductionDTO.setNewOrder(iSaOrderService.queryByPkIdOrCode(orderDeductionDTO.getPkOrderNew(), null, null));
                return orderDeductionDTO;
        }
        return null;
    }

}
