package com.ruoyi.dashboard.domain.revenueStat;

import java.time.LocalDate;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.areaJoin.domain.AreaJoinQuery;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.bonus.domain.BonusQuery;
import com.ruoyi.bst.bonus.domain.enums.BonusArrivalType;
import com.ruoyi.bst.bonus.domain.enums.BonusStatus;
import com.ruoyi.bst.bonusRefund.domain.BonusRefundQuery;
import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.fault.domain.FaultQuery;
import com.ruoyi.bst.mchApply.domain.MchApplyQuery;
import com.ruoyi.bst.model.domain.ModelQuery;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.refund.domain.RefundQuery;
import com.ruoyi.bst.refund.domain.enums.RefundStatus;
import com.ruoyi.bst.withdraw.domain.WithdrawQuery;
import com.ruoyi.system.user.domain.UserQuery;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class RevenueStatQuery {
    @ApiModelProperty("日期范围")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private List<LocalDate> dateRange;

    @ApiModelProperty("查询数据")
    private List<String> keys;

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("运营区ID")
    private Long areaId;

    @ApiModelProperty("数据隔离")
    private Boolean scope;
    
    // 基础订单查询
    private OrderQuery baseOrderQuery() {
        OrderQuery query = new OrderQuery();
        query.setStatusList(OrderStatus.valid());
        query.setAreaId(areaId);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        return query;
    }

    // 基础分成查询
    private BonusQuery baseBonusQuery() {
        BonusQuery query = new BonusQuery();
        query.setStatusList(BonusStatus.settle());
        query.setScope(scope);
        return query;
    }

    // 基础分成退款查询
    private BonusRefundQuery baseBonusRefundQuery() {   
        BonusRefundQuery query = new BonusRefundQuery();
        query.setBonusStatusList(BonusStatus.settle());
        query.setScope(scope);
        return query;
    }

    // 订单查询
    public OrderQuery toOrderQuery() {
        OrderQuery query = baseOrderQuery();
        query.setCreateDateRange(dateRange);
        return query;
    }

    // 订单查询（完成订单）
    public OrderQuery toOrderQueryForEnd() {
        OrderQuery query = baseOrderQuery();
        query.setEndDateRange(dateRange);
        query.setStatusList(OrderStatus.finishedList());
        return query;
    }

    // 用户分成查询
    public BonusQuery toBonusQuery() {
        BonusQuery query = baseBonusQuery();
        query.setArrivalId(userId);
        query.setAreaId(areaId);
        query.setArrivalTypes(BonusArrivalType.userList());
        query.setWaitDateRange(dateRange);
        return query;
    }

    // 平台分成查询
    public BonusQuery toPlatformBonusQuery() {
        BonusQuery query = new BonusQuery();
        query.setWaitDateRange(dateRange);
        query.setArrivalTypes(BonusArrivalType.platformList());
        query.setStatusList(BonusStatus.settle());
        return query;
    }

    // 用户分成退款查询
    public BonusRefundQuery toBonusRefundQuery() {
        BonusRefundQuery query = baseBonusRefundQuery();
        query.setBonusArrivalId(userId);
        query.setBonusAreaId(areaId);
        query.setBonusArrivalTypes(BonusArrivalType.userList());
        query.setCreateDateRange(dateRange);
        return query;
    }

    // 平台分成退款查询
    public BonusRefundQuery toPlatformBonusRefundQuery() {
        BonusRefundQuery query = baseBonusRefundQuery();
        query.setCreateDateRange(dateRange);
        query.setBonusArrivalTypes(BonusArrivalType.platformList());
        return query;
    }

    // 运营区查询
    public AreaQuery toAreaQuery() {
        AreaQuery query = new AreaQuery();
        query.setCreateDateRange(dateRange);
        query.setId(areaId);
        query.setUserId(userId);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.AREA_VIEW.getCode());
        return query;
    }

    // 运营区加盟商查询
    public AreaJoinQuery toAreaJoinQuery() {
        AreaJoinQuery query = new AreaJoinQuery();
        query.setCreateDateRange(dateRange);
        query.setAreaId(areaId);
        query.setUserId(userId);
        query.setScope(scope);
        return query;
    }

    // 设备查询
    public DeviceQuery toDeviceQuery() {
        DeviceQuery query = new DeviceQuery();
        query.setCreateDateRange(dateRange);
        query.setAreaId(areaId);
        query.setMchId(userId);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        return query;
    }

    // 提现查询
    public WithdrawQuery toWithdrawQuery() {
        WithdrawQuery query = new WithdrawQuery();
        query.setCreateDateRange(dateRange);
        query.setUserId(userId);
        query.setScope(scope);
        return query;
    }

    // 加盟商申请查询
    public MchApplyQuery toMchApplyQuery() {
        MchApplyQuery query = new MchApplyQuery();
        query.setCreateDateRange(dateRange);
        query.setUserId(userId);
        query.setScope(scope);
        return query;
    }

    // 故障查询
    public FaultQuery toFaultQuery() {
        FaultQuery query = new FaultQuery();
        query.setCreateDateRange(dateRange);
        query.setStoreId(userId);
        query.setAreaId(areaId);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.FAULT_VIEW.getCode());
        return query;
    }

    // 订单退款查询
    public RefundQuery toOrderRefundQuery() {
        RefundQuery query = new RefundQuery();
        query.setStatusList(RefundStatus.valid());
        query.setCreateDateRange(dateRange);
        query.setPayAreaId(areaId);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        return query;
    }

    // 用户查询
    public UserQuery toUserQuery() {
        UserQuery query = new UserQuery();
        query.setCreateDateRange(dateRange);
        query.setUserId(userId);
        query.setScope(scope);
        return query;
    }

    // 车型查询
    public ModelQuery toModelQuery() {
        ModelQuery query = new ModelQuery();
        query.setCreateDateRange(dateRange);
        query.setUserId(userId);
        query.setScope(scope);
        return query;
    }
}
