package com.zm.order.tool.server.helper;

import com.alibaba.fastjson.JSON;
import com.github.dozermapper.core.Mapper;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zm.order.tool.server.component.ShardingKeyGenerator;
import com.zm.order.tool.server.dao.MetadataDao;
import com.zm.order.tool.server.dao.OrderStatusFlowsDao;
import com.zm.order.tool.server.dto.ChooseDTO;
import com.zm.order.tool.server.dto.MetadataDTO;
import com.zm.order.tool.server.dto.NewOrderDTO;
import com.zm.order.tool.server.dto.OrderAddrDTO;
import com.zm.order.tool.server.dto.OrderDetailDTO;
import com.zm.order.tool.server.dto.OrderDiscountDTO;
import com.zm.order.tool.server.dto.OrderItemDTO;
import com.zm.order.tool.server.dto.OrderStatusFlowsDTO;
import com.zm.order.tool.server.dto.UpdateMetadataDTO;
import com.zm.order.tool.server.enums.BizTypeEnum;
import com.zm.order.tool.server.enums.RoleEnum;
import com.zm.order.tool.server.model.BaseIndexOrder;
import com.zm.order.tool.server.model.Metadata;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.OrderAddr;
import com.zm.order.tool.server.model.OrderDiscount;
import com.zm.order.tool.server.model.OrderItem;
import com.zm.order.tool.server.model.OrderOperation;
import com.zm.order.tool.server.model.OrderStatusFlows;
import com.zm.order.tool.server.model.SimpleMetadataRequest;
import com.zm.order.tool.server.query.ListMetadataQuery;
import com.zm.order.tool.server.query.ListStatusFlowsQuery;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.query.SearchOrderQuery;
import com.zm.order.tool.server.request.CreateStatusFlowsRequest;
import com.zm.order.tool.server.request.ListStatusFlowsRequest;
import com.zm.order.tool.server.response.OrderStatusFlowsVO;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.utils.CommonUtils;
import com.zm.order.tool.server.utils.ConvertUtils;
import com.zm.util.dto.ExceptionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 陈标
 * @Description
 * @Date 2020/7/20 0020 11:13
 */

@Component
public class NewPageServiceHelper {

    private static final Logger logger = LoggerFactory.getLogger(NewPageServiceHelper.class);

    private static final String SPLIT = ",";
    private static final String STATUS = "状态";
    private static final String ORDER_TYPE = "单据类型";
    private static final String OPERATE = "操作类型";

    private static final String NOT_ACTIVATIE = "否";
    private static final String ACTIVATIE = "是";

    private Map<String, String> cyOperateMap = new HashMap<>();
    private Map<String, String> cyOrderTypeMap = new HashMap<>();
    private Map<String, String> cyStatusMap = new HashMap<>();

    private Map<String, String> dxpOperateMap = new HashMap<>();
    private Map<String, String> dxpOrderTypeMap = new HashMap<>();
    private Map<String, String> dxpStatusMap = new HashMap<>();

    private Map<String, String> lsOperateMap = new HashMap<>();
    private Map<String, String> lsOrderTypeMap = new HashMap<>();
    private Map<String, String> lsStatusMap = new HashMap<>();

    @Autowired
    private Mapper mapper;
    @Autowired
    private ShardingKeyGenerator shardingKeyGenerator;
    @Autowired
    private OrderStatusFlowsDao orderStatusFlowsDao;
    @Autowired
    private MetadataDao metadataDao;


    public ExceptionInfo checklistOrder(SearchOrderQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public NewOrderDTO buildNewOrderDTO(BaseIndexOrder baseIndexOrder) {
        NewOrderDTO newOrderDTO = ConvertUtils.convertObject(baseIndexOrder, NewOrderDTO.class);
        if (Objects.isNull(newOrderDTO)) {
            return null;
        }
        int db = CommonUtils.getDb(baseIndexOrder.getUserId());
        int tb = CommonUtils.getTb(baseIndexOrder.getUserId());
        newOrderDTO.setDbIndex(db);
        newOrderDTO.setTbIndex(tb);
        if (Objects.equals(baseIndexOrder.getPayStatus(),1)){
            newOrderDTO.setPayStatus("已支付");
        }else {
            newOrderDTO.setPayStatus("未支付");
        }
        newOrderDTO.setUpdatedAt(baseIndexOrder.getUpdatedAt());
        return newOrderDTO;

    }

    public ExceptionInfo checkCompleteOrder(SearchOrderQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (CollectionUtils.isEmpty(query.getOrderNoList())) {
            return ExceptionConstants.PARAM_ORDER_NO_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkListOrderStatusFlows(ListStatusFlowsQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (Objects.isNull(query.getStart())) {
            return ExceptionConstants.PARAM_PAGES_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkCreateOrderStatusFlows(OrderStatusFlowsDTO orderStatusFlowsDTO) {
        if (Objects.isNull(orderStatusFlowsDTO)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (Objects.isNull(orderStatusFlowsDTO.getBeforeStatusS())) {
            return ExceptionConstants.PARAM_BEFORE_STATUS_IS_EMPTY;
        }
        if (Objects.isNull(orderStatusFlowsDTO.getAfterStatus())) {
            return ExceptionConstants.PARAM_AFTER_STATUS_IS_EMPTY;
        }
        if (Objects.isNull(orderStatusFlowsDTO.getBizType())) {
            return ExceptionConstants.PARAM_BIZ_TYPES_IS_EMPTY;
        }
        if (Objects.isNull(orderStatusFlowsDTO.getDocTypes())) {
            return ExceptionConstants.PARAM_DOC_TYPES_IS_EMPTY;
        }
        String beforeStatusS = orderStatusFlowsDTO.getBeforeStatusS();
        if (beforeStatusS.contains(SPLIT)) {
            String[] beforeStatusList = beforeStatusS.split(",");
            List<String> bizTypeList = Arrays.asList(beforeStatusList);
            for (String s : bizTypeList) {
                if (!org.apache.commons.lang3.StringUtils.isNumeric(s)) {
                    return ExceptionConstants.PARAM_BIZ_TYPES_NOT_STANDARD;
                }
            }
        } else {
            if (!org.apache.commons.lang3.StringUtils.isNumeric(orderStatusFlowsDTO.getBeforeStatusS())) {
                return ExceptionConstants.PARAM_BIZ_TYPES_NOT_STANDARD;
            }
        }
        String docTypes = orderStatusFlowsDTO.getDocTypes();
        if (docTypes.contains(SPLIT)) {
            String[] docTypesSplit = docTypes.split(",");
            List<String> docTypeList = Arrays.asList(docTypesSplit);
            for (String s : docTypeList) {
                if (!org.apache.commons.lang3.StringUtils.isNumeric(s)) {
                    return ExceptionConstants.PARAM_DOC_TYPES_NOT_STANDARD;
                }
            }
        } else {
            if (!org.apache.commons.lang3.StringUtils.isNumeric(orderStatusFlowsDTO.getDocTypes())) {
                return ExceptionConstants.PARAM_DOC_TYPES_NOT_STANDARD;
            }
        }
        return ExceptionConstants.OK;

    }

    public List<OrderStatusFlows> buildOrderStatusFlows(OrderStatusFlowsDTO orderStatusFlowsDTO) {

        List<OrderStatusFlows> list = new ArrayList<>();
        List<String> beforeStatusList = new ArrayList<>();
        List<String> docTypeList = new ArrayList<>();
        String beforeStatusS = orderStatusFlowsDTO.getBeforeStatusS();
        if (beforeStatusS.contains(SPLIT)) {
            String[] beforeStatusSplit = beforeStatusS.split(",");
            beforeStatusList = Arrays.asList(beforeStatusSplit);
        } else {
            beforeStatusList.add(beforeStatusS);
        }
        String docTypes = orderStatusFlowsDTO.getDocTypes();
        if (docTypes.contains(SPLIT)) {
            String[] docTypesSplit = docTypes.split(SPLIT);
            docTypeList = Arrays.asList(docTypesSplit);
        } else {
            docTypeList.add(docTypes);
        }
        for (String docType : docTypeList) {
            for (String beforeStatus : beforeStatusList) {
                OrderStatusFlows orderStatusFlows = ConvertUtils.convertObject(orderStatusFlowsDTO, OrderStatusFlows.class);
                if (Objects.isNull(orderStatusFlows)) {
                    continue;
                }
                orderStatusFlows.setDocType(Integer.valueOf(docType));
                orderStatusFlows.setBeforeStatus(Integer.valueOf(beforeStatus));
                if (Objects.equals(orderStatusFlows.getAfterStatus(), 0)) {
                    orderStatusFlows.setAfterStatus(orderStatusFlows.getBeforeStatus());
                }
                list.add(orderStatusFlows);
            }
        }
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }

        // 去重
        // 1.查寻数据库原有数据
        ListStatusFlowsQuery query = new ListStatusFlowsQuery();
        query.setOperation(orderStatusFlowsDTO.getOperation());
        List<OrderStatusFlows> orderStatusFlowsList = orderStatusFlowsDao.selectList(query);
        List<String> removeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderStatusFlowsList)) {
            for (OrderStatusFlows orderStatusFlows : orderStatusFlowsList) {
                String s = String.valueOf(orderStatusFlows.getBizType()) + orderStatusFlows.getDocType() +
                        orderStatusFlows.getBeforeStatus() + orderStatusFlows.getOperation();
                removeList.add(s);
            }
            // 构建新建数据map
            Map<String, OrderStatusFlows> map = new HashMap<>(list.size());
            for (OrderStatusFlows orderStatusFlows : list) {
                String s = String.valueOf(orderStatusFlows.getBizType()) + orderStatusFlows.getDocType() +
                        orderStatusFlows.getBeforeStatus() + orderStatusFlows.getOperation();
                map.put(s, orderStatusFlows);
            }
            // 遍历数据, 去重
            for (String s : removeList) {
                if (map.containsKey(s)) {
                    map.remove(s);
                }
            }
            Collection<OrderStatusFlows> values = map.values();
            list = new ArrayList<>(values);
        }
        return list;
    }

    public ExceptionInfo checkListOrderOperate(String orderNo) {
        if (Objects.isNull(orderNo)) {
            return ExceptionConstants.PARAM_ORDER_NO_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }


    public ExceptionInfo checkCountStatusFlows(ListStatusFlowsQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkGetOrderDetail(OrderQuery orderQuery) {

        if (Objects.isNull(orderQuery)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (Objects.isNull(orderQuery.getOrderNo())) {
            return ExceptionConstants.PARAM_ORDER_NO_IS_EMPTY;
        }
        if (Objects.isNull(orderQuery.getUserId())) {
            return ExceptionConstants.PARAM_USER_ID_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }


    public OrderDetailDTO buildOrderDetail(Order order, List<OrderItem> orderItemList, List<OrderDiscount> orderDiscountList, OrderAddr orderAddr) {
        OrderDetailDTO orderDetailDTO = mapper.map(order, OrderDetailDTO.class);
        orderDetailDTO.setOrderId(order.getId());
        orderDetailDTO.setPayInfoMap(convert(order.getPayInfo()));
        orderDetailDTO.setPerformanceInfoMap(convert(order.getPerformanceInfo()));
        if (!CollectionUtils.isEmpty(orderItemList)) {
            List<OrderItemDTO> orderItemDtoList = Lists.newArrayListWithCapacity(orderItemList.size());
            orderItemList.forEach(orderItem -> {
                OrderItemDTO orderItemDTO = mapper.map(orderItem, OrderItemDTO.class);
                orderItemDtoList.add(orderItemDTO);
            });
            orderDetailDTO.setItemList(orderItemDtoList);
        }
        if (!CollectionUtils.isEmpty(orderDiscountList)) {
            List<OrderDiscountDTO> orderDiscountDtoList = Lists.newArrayListWithCapacity(orderDiscountList.size());
            orderDiscountList.forEach(orderDiscount -> {
                OrderDiscountDTO orderDiscountDTO = mapper.map(orderDiscount, OrderDiscountDTO.class);
                orderDiscountDtoList.add(orderDiscountDTO);
            });
            orderDetailDTO.setDiscountList(orderDiscountDtoList);
        }
        OrderAddrDTO orderAddrDto = ConvertUtils.convertObject(orderAddr, OrderAddrDTO.class);
        orderDetailDTO.setAddr(orderAddrDto);
        return orderDetailDTO;
    }


    private Map<String, Object> convert(String info) {
        try {
            if (!StringUtils.isEmpty(info)) {
                return JSON.parseObject(info, Map.class);
            }
        } catch (Throwable throwable) {
            logger.error("convert info:{} failed, cause by:{}", info, Throwables.getStackTraceAsString(throwable));
        }
        return null;
    }

    public ExceptionInfo checkActivation(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkCreateMetadata(MetadataDTO metadataDTO) {
        if (Objects.isNull(metadataDTO)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (Objects.isNull(metadataDTO.getBizType())) {
            return ExceptionConstants.PARAM_BIZ_TYPE_IS_EMPTY;
        }
        if (Objects.isNull(metadataDTO.getProperty())) {
            return ExceptionConstants.PARAM_PROPERTY_IS_EMPTY;
        }
        if (CollectionUtils.isEmpty(metadataDTO.getSimpleMetadata())) {
            return ExceptionConstants.PARAM_METADATA_LIST_IS_EMPTY;
        }
        for (SimpleMetadataRequest simpleMetadata : metadataDTO.getSimpleMetadata()) {
            if (StringUtils.isEmpty(simpleMetadata.getValue())) {
                return ExceptionConstants.PARAM_VALUE_IS_EMPTY;
            }
            if (StringUtils.isEmpty(simpleMetadata.getDescription())) {
                return ExceptionConstants.PARAM_DESCRIPTION_IS_EMPTY;
            }
        }
        return ExceptionConstants.OK;
    }

    public Metadata buildMetadataDTO(Metadata metadata) {
        metadata.setId(shardingKeyGenerator.generateSnowflakeNumKey());
        return metadata;

    }

    public ExceptionInfo checkListMetadata(ListMetadataQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        if (Objects.isNull(query.getStart())) {
            return ExceptionConstants.PARAM_PAGES_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkCountMetadata(ListMetadataQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkListOrderType(ListMetadataQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkListStatus(ListMetadataQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public ExceptionInfo checkListOperation(ListMetadataQuery query) {
        if (Objects.isNull(query)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }

    public List<ChooseDTO> buildChooseDTO(List<Metadata> metadataList) {

        List<ChooseDTO> list = new ArrayList<>();
        for (Metadata metadata : metadataList) {
            ChooseDTO chooseDTO = new ChooseDTO();
            chooseDTO.setKey(metadata.getDescription());
            chooseDTO.setValue(metadata.getValue());
            list.add(chooseDTO);

        }
        list = list.stream().distinct().collect(Collectors.toList());
        return list;
    }

    public List<Metadata> buildMetadataList(MetadataDTO metadataDTO) {

        List<Metadata> list = new ArrayList<>();
        List<SimpleMetadataRequest> simpleMetadata = metadataDTO.getSimpleMetadata();
        for (SimpleMetadataRequest simpleMeta : simpleMetadata) {
            Metadata metadata = ConvertUtils.convertObject(metadataDTO, Metadata.class);
            metadata.setValue(simpleMeta.getValue());
            metadata.setDescription(simpleMeta.getDescription());
            list.add(metadata);
        }
        return list;
    }

    public ExceptionInfo checkDeleteMetadata(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ExceptionConstants.PARAM_IS_EMPTY;
        }
        return ExceptionConstants.OK;

    }

    public ExceptionInfo checkUpdateMetadata(UpdateMetadataDTO updateMetadataDTO) {
        if (Objects.isNull(updateMetadataDTO.getId())) {
            return ExceptionConstants.PARAM_ID_TASK_IS_EMPTY;
        }
        if (StringUtils.isEmpty(updateMetadataDTO.getValue())) {
            return ExceptionConstants.PARAM_VALUE_IS_EMPTY;
        }
        if (StringUtils.isEmpty(updateMetadataDTO.getDescription())) {
            return ExceptionConstants.PARAM_DESCRIPTION_IS_EMPTY;
        }
        return ExceptionConstants.OK;
    }


    public List<OrderStatusFlowsVO> buildOrderStatusFlowsVO(List<OrderStatusFlowsDTO> data) {

        ListMetadataQuery query = new ListMetadataQuery();
        // 餐饮元数据信息
        query.setBizType(BizTypeEnum.CY.getDesc());
        query.setProperty(STATUS);
        List<Metadata> statusList = metadataDao.selectList(query);
        cyStatusMap = statusList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(OPERATE);
        List<Metadata> operateList = metadataDao.selectList(query);
        cyOperateMap = operateList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(ORDER_TYPE);
        List<Metadata> orderTypeList = metadataDao.selectList(query);
        cyOrderTypeMap = orderTypeList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        // 零售元数据信息
        query.setBizType(BizTypeEnum.LS.getDesc());
        query.setProperty(STATUS);
        List<Metadata> lsStatusList = metadataDao.selectList(query);
        lsStatusMap = lsStatusList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(OPERATE);
        List<Metadata> lsOperateList = metadataDao.selectList(query);
        lsOperateMap = lsOperateList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(ORDER_TYPE);
        List<Metadata> lsOrderTypeList = metadataDao.selectList(query);
        lsOrderTypeMap = lsOrderTypeList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        // 店小铺元数据信息
        query.setBizType(BizTypeEnum.PT.getDesc());
        query.setProperty(STATUS);
        List<Metadata> dxpStatusList = metadataDao.selectList(query);
        dxpStatusMap = dxpStatusList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(OPERATE);
        List<Metadata> dxpOperateList = metadataDao.selectList(query);
        dxpOperateMap = dxpOperateList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));
        query.setProperty(ORDER_TYPE);
        List<Metadata> dxpOrderTypeList = metadataDao.selectList(query);
        dxpOrderTypeMap = dxpOrderTypeList.stream().collect(Collectors.toMap(Metadata::getValue, Metadata::getDescription));


        // 构建数据 返回
        List<OrderStatusFlowsVO> voList = new ArrayList<>();
        for (OrderStatusFlowsDTO orderStatusFlowsDTO : data) {
            OrderStatusFlowsVO vo = convert(orderStatusFlowsDTO);
            voList.add(vo);
        }
        return voList;
    }


    /**
     * 转换数据
     *
     * @param orderStatusFlowsDTO
     * @return
     */
    private OrderStatusFlowsVO convert(OrderStatusFlowsDTO orderStatusFlowsDTO) {
        OrderStatusFlowsVO vo = mapper.map(orderStatusFlowsDTO, OrderStatusFlowsVO.class);
        if (Objects.equals(orderStatusFlowsDTO.getBizType(), BizTypeEnum.CY.getType())) {
            vo.setBizType(BizTypeEnum.CY.getDesc());
            if (Objects.nonNull(cyOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()))) {
                vo.setDocType("(" + cyOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()) + ")" + orderStatusFlowsDTO.getDocType());
            }
            if (Objects.nonNull(cyStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()))) {
                vo.setBeforeStatus("(" + cyStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()) + ")" + orderStatusFlowsDTO.getBeforeStatus());
            }
            if (Objects.nonNull(cyStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()))) {
                vo.setAfterStatus("(" + cyStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()) + ")" + orderStatusFlowsDTO.getAfterStatus());
            }
            if (Objects.nonNull(cyOperateMap.get(orderStatusFlowsDTO.getOperation()))) {
                vo.setOperation("(" + cyOperateMap.get(orderStatusFlowsDTO.getOperation()) + ")" + orderStatusFlowsDTO.getOperation());
            }

        }
        if (Objects.equals(orderStatusFlowsDTO.getBizType(), BizTypeEnum.LS.getType())) {
            vo.setBizType(BizTypeEnum.LS.getDesc());
            if (Objects.nonNull(lsOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()))) {
                vo.setDocType("(" + lsOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()) + ")" + orderStatusFlowsDTO.getDocType());
            }
            if (Objects.nonNull(lsStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()))) {
                vo.setBeforeStatus("(" + lsStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()) + ")" + orderStatusFlowsDTO.getBeforeStatus());
            }
            if (Objects.nonNull(lsStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()))) {
                vo.setAfterStatus("(" + lsStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()) + ")" + orderStatusFlowsDTO.getAfterStatus());
            }
            if (Objects.nonNull(lsOperateMap.get(orderStatusFlowsDTO.getOperation()))) {
                vo.setOperation("(" + lsOperateMap.get(orderStatusFlowsDTO.getOperation()) + ")" + orderStatusFlowsDTO.getOperation());
            }

        }
        if (Objects.equals(orderStatusFlowsDTO.getBizType(), BizTypeEnum.PT.getType())) {
            vo.setBizType(BizTypeEnum.PT.getDesc());
            if (Objects.nonNull(dxpOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()))) {
                vo.setDocType("(" + dxpOrderTypeMap.get(orderStatusFlowsDTO.getDocType().toString()) + ")" + orderStatusFlowsDTO.getDocType());
            }
            if (Objects.nonNull(dxpStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()))) {
                vo.setBeforeStatus("(" + dxpStatusMap.get(orderStatusFlowsDTO.getBeforeStatus().toString()) + ")" + orderStatusFlowsDTO.getBeforeStatus());
            }
            if (Objects.nonNull(dxpStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()))) {
                vo.setAfterStatus("(" + dxpStatusMap.get(orderStatusFlowsDTO.getAfterStatus().toString()) + ")" + orderStatusFlowsDTO.getAfterStatus());
            }
            if (Objects.nonNull(dxpOperateMap.get(orderStatusFlowsDTO.getOperation()))) {
                vo.setOperation("(" + dxpOperateMap.get(orderStatusFlowsDTO.getOperation()) + ")" + orderStatusFlowsDTO.getOperation());
            }
        }

        if (Objects.equals(orderStatusFlowsDTO.getIsActivated(), 0)) {
            vo.setShowActivatie(NOT_ACTIVATIE);
        } else {
            vo.setShowActivatie(ACTIVATIE);
        }
        return vo;
    }

    public OrderStatusFlowsDTO buildOrderStatusFlowsDTO(CreateStatusFlowsRequest request) {
        OrderStatusFlowsDTO orderStatusFlowsDTO = new OrderStatusFlowsDTO();
        //
        if (Objects.equals(request.getBizType(), "餐饮")) {
            orderStatusFlowsDTO.setBizType(1);
        }
        if (Objects.equals(request.getBizType(), "零售")) {
            orderStatusFlowsDTO.setBizType(2);
        }
        if (Objects.equals(request.getBizType(), "店小铺")) {
            orderStatusFlowsDTO.setBizType(3);
        }
        orderStatusFlowsDTO.setDocTypes(request.getDocTypes());
        orderStatusFlowsDTO.setBeforeStatusS(request.getBeforeStatusS());
        orderStatusFlowsDTO.setAfterStatus(request.getAfterStatus());
        orderStatusFlowsDTO.setOperation(request.getOperation());
        return orderStatusFlowsDTO;
    }

    public ListStatusFlowsQuery buildListStatusFlowsQuery(ListStatusFlowsRequest request) {
        ListStatusFlowsQuery query = new ListStatusFlowsQuery();
        if (Objects.equals(request.getBizType(), "餐饮")) {
            query.setBizType(1);
        }
        if (Objects.equals(request.getBizType(), "零售")) {
            query.setBizType(2);
        }
        if (Objects.equals(request.getBizType(), "店小铺")) {
            query.setBizType(3);
        }
        if (Objects.nonNull(request.getDocType())) {
            query.setDocType(request.getDocType());
        }
        if (Objects.nonNull(request.getAfterStatus())) {
            query.setAfterStatus(request.getAfterStatus());
        }
        if (Objects.nonNull(request.getBeforeStatus())) {
            query.setBeforeStatus(request.getBeforeStatus());
        }
        if (!StringUtils.isEmpty(request.getOperation())) {
            query.setOperation(request.getOperation());
        }
        if (Objects.nonNull(request.getPageNo())) {
            query.setPageNo(request.getPageNo());
        }
        if (Objects.nonNull(request.getPageSize())) {
            query.setPageSize(request.getPageSize());
        }
        return query;
    }



    public OrderOperation buildOrderOperation(BaseIndexOrder baseIndexOrder) {
        OrderOperation orderOperation = new OrderOperation();
        orderOperation.setId(shardingKeyGenerator.generateSnowflakeNumKey());
        orderOperation.setUserId(baseIndexOrder.getUserId());
        orderOperation.setOrderNo(baseIndexOrder.getOrderNo());
        orderOperation.setRole(RoleEnum.ORDER_CENTER_TOOL.getValue());
        orderOperation.setOperator(8L);
        orderOperation.setOperation(RoleEnum.ORDER_CENTER_TOOL.getDesc());
        orderOperation.setBeforeStatus(baseIndexOrder.getStatus());
        orderOperation.setAfterStatus(5);
        Map<String, Object> extraMap = Maps.newHashMap();
        extraMap.put("operateOrder", "修改订单完成时间");
        orderOperation.setExtraMap(extraMap);
        return orderOperation;

    }
}
