package com.gree.mall.miniapp.bean.workorder;


import com.gree.mall.miniapp.enums.MaterialExamineStatusEnum;
import com.gree.mall.miniapp.enums.workorder.OrderFlagEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.PgOrderBase;
import com.gree.mall.miniapp.plus.entity.PgOrderFlag;
import com.gree.mall.miniapp.plus.entity.RpMaterialOrder;
import com.gree.mall.miniapp.plus.service.PgOrderBaseService;
import com.gree.mall.miniapp.plus.service.PgOrderFlagService;
import com.gree.mall.miniapp.plus.service.RpMaterialOrderService;
import com.gree.mall.miniapp.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务单标识
 */
@Service
public class OrderFlagLogic {

    @Autowired
    PgOrderFlagService pgOrderFlagService;
    @Autowired
    PgOrderBaseService pgOrderBaseService;
    @Autowired
    CommonLogic commonLogic;

    @Autowired
    RpMaterialOrderService rpMaterialOrderService;

    /**
     * 保存服务单标识
     * @param orderBaseIds
     * @param orderFlagEnum
     */
    @Transactional
    public void saveOrderFlag(List<String> orderBaseIds, OrderFlagEnum orderFlagEnum){
        if (orderFlagEnum.getKey().equals(OrderFlagEnum.YWG.getKey())){
            this.ifWeiBao(orderBaseIds);
        }
        this.saveOrderFlag(orderBaseIds,orderFlagEnum,"");
    }

    private void ifWeiBao(List<String> orderBaseIds) {
        List<PgOrderBase> list = pgOrderBaseService.lambdaQuery().in(PgOrderBase::getId, orderBaseIds).list();
        for (PgOrderBase pgOrderBase : list) {
            if (StringUtil.isEmpty(pgOrderBase.getRpProjectRepairId()))
                continue;

            if (rpMaterialOrderService.lambdaQuery().eq(RpMaterialOrder::getWorkerOrderId,pgOrderBase.getId())
                    .in(RpMaterialOrder::getExamineStatus, MaterialExamineStatusEnum.FAIL.getKey(),MaterialExamineStatusEnum.WAIT.getKey()).count()
                    > 0) {
                throw new RemoteServiceException("尚有未走完流程的费用详情");
            }

        }
    }

    @Transactional
    public void saveOrderFlag(List<String> orderBaseIds, OrderFlagEnum orderFlagEnum, String remark){
        if(CollectionUtils.isEmpty(orderBaseIds) || orderFlagEnum == null){
            return;
        }
        orderBaseIds = orderBaseIds.stream()
                .distinct()
                .collect(Collectors.toList());
        pgOrderFlagService.lambdaUpdate()
                .in(PgOrderFlag::getOrderBaseId,orderBaseIds)
                .eq(PgOrderFlag::getTag,orderFlagEnum.getKey())
                .remove();
        List<PgOrderFlag> orderFlags = new ArrayList<>();
        for(String orderBaseId : orderBaseIds) {
            PgOrderFlag pgOrderFlag = new PgOrderFlag();
            pgOrderFlag.setOrderBaseId(orderBaseId);
            pgOrderFlag.setTag(orderFlagEnum.getKey());
            pgOrderFlag.setTagName(orderFlagEnum.getRemark());
            pgOrderFlag.setSortNum(orderFlagEnum.getSortNum());
            pgOrderFlag.setRemark(remark);
            if(orderFlagEnum.getSortNum() != null && orderFlagEnum.getSortNum().intValue() == -1){
                pgOrderFlag.setType(0);
            }
            orderFlags.add(pgOrderFlag);
        }
        pgOrderFlagService.saveBatch(orderFlags);

        //如果是异常反馈，标识主表字段
        if(orderFlagEnum.getKey().equals(OrderFlagEnum.YI_CHANGE.getKey())){
            pgOrderBaseService.lambdaUpdate().set(PgOrderBase::getIsException,true).in(PgOrderBase::getId,orderBaseIds).update();
        }
    }




    private PgOrderFlag supplyOrderFlag(String orderBaseId, OrderFlagEnum orderFlagEnum,String tagName){
        PgOrderFlag pgOrderFlag = new PgOrderFlag();
        pgOrderFlag.setOrderBaseId(orderBaseId);
        pgOrderFlag.setTag(orderFlagEnum.getKey());
        pgOrderFlag.setTagName(orderFlagEnum.getRemark());
        pgOrderFlag.setSortNum(orderFlagEnum.getSortNum());
        if(StringUtils.isNotBlank(tagName)){
            pgOrderFlag.setTagName(tagName);
        }
        return pgOrderFlag;
    }


    public List<String> queryOrderBaseId(OrderFlagEnum orderFlagEnum){
        List<String> orderBaseIds = pgOrderFlagService.lambdaQuery()
                .select(PgOrderFlag::getOrderBaseId)
                .eq(PgOrderFlag::getTag, orderFlagEnum.getKey())
                .list().stream().map(PgOrderFlag::getOrderBaseId).collect(Collectors.toList());
        return orderBaseIds;
    }

    /**
     * 查询服务单标识-列表
     */
    public List<PgOrderFlag> list(String orderBaseId){
        return this.list(orderBaseId,1);
    }

    public Map<String, List<PgOrderFlag>> list(List<String> orderBaseIds){
        if(CollectionUtils.isEmpty(orderBaseIds)){
            return Collections.emptyMap();
        }
        return this.list(orderBaseIds,1);
    }

    /**
     * 服务单标识-列表
     * @param orderBaseId 信息编号
     * @param type 0=系统内部标注用  1=需要展示在服务单列表上的
     * @return
     */
    public List<PgOrderFlag> list(String orderBaseId,Integer type){
        return pgOrderFlagService.lambdaQuery()
                .eq(PgOrderFlag::getOrderBaseId, orderBaseId)
                .eq(type != null,PgOrderFlag::getType,type)
                .orderByAsc(PgOrderFlag::getSortNum)
                .list();
    }

    /**
     * 查询服务单标识-列表
     */
    public Map<String, List<PgOrderFlag>> list(List<String> orderBaseIds,Integer type){
        if(CollectionUtils.isEmpty(orderBaseIds)){
            return Collections.emptyMap();
        }
        return pgOrderFlagService.lambdaQuery()
                .in(PgOrderFlag::getOrderBaseId, orderBaseIds)
                .eq(PgOrderFlag::getType,type)
                .orderByAsc(PgOrderFlag::getSortNum)
                .list()
                .stream()
                .collect(Collectors.groupingBy(PgOrderFlag::getOrderBaseId));
    }


    /**
     * 查询服务单标识-是否存在
     */
    public Boolean isExists(String orderBaseId,OrderFlagEnum orderFlagEnum){
        if(StringUtils.isEmpty(orderBaseId) || orderFlagEnum == null){
            return false;
        }
        Integer count = pgOrderFlagService.lambdaQuery()
                .eq(PgOrderFlag::getOrderBaseId, orderBaseId)
                .eq(PgOrderFlag::getTag, orderFlagEnum.getKey()).count();
        return count > 0;
    }


    /**
     * 删除服务单标识
     */
    @Transactional
    public void deleteOrderFlag(String orderBaseId, OrderFlagEnum orderFlagEnum){
        pgOrderFlagService.lambdaUpdate().eq(PgOrderFlag::getOrderBaseId,orderBaseId).eq(PgOrderFlag::getTag,orderFlagEnum.getKey()).remove();
        //如果是异常反馈，标识主表字段
        if(orderFlagEnum.getKey().equals(OrderFlagEnum.YI_CHANGE.getKey())){
            pgOrderBaseService.lambdaUpdate().set(PgOrderBase::getIsException,false).in(PgOrderBase::getId,orderBaseId).update();
        }
    }



}
