package com.xy.dto;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xy.convert.CustomFenToYuanConverter;
import com.xy.utils.PageBean;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 设备统计数据
 * </p>
 *
 * @author lijin
 * @since 2023-01-11
 */
public class DeviceDataDto {
    @Data
    @Accessors(chain = true)
    public static class CountDTO {

        @ApiModelProperty(value = "天=1，周=2，月=3")
        private Integer timeType;


    }

    @Data
    @Accessors(chain = true)
    public static class CountVO {

        @ApiModelProperty(value = "总数")
        private Integer totalNum;

        @ApiModelProperty(value = "新增")
        private Integer newNum;


        @ApiModelProperty(value = "活跃数")
        private Integer activeNum;


        @ApiModelProperty(value = "欠费")
        private long debtNum;


        @ApiModelProperty(value = "停运数量")
        private Integer outOfServiceNum;

    }

    @Data
    @Accessors(chain = true)
    public static class MercHomePageCountVO {
        @ApiModelProperty(value = "在线设备数")
        private Integer onlineNum;

        @ApiModelProperty(value = "离线设备数")
        private Integer offlineNum;

        @ApiModelProperty(value = "待激活数量")
        private Integer unActiveNum;

        @ApiModelProperty(value = "停运数量")
        private Integer outOfServiceNum;
    }

    @Data
    @Accessors(chain = true)
    public static class MercHomePageCount {
        @ApiModelProperty(value = "商户ID")
        private Long mercId;
        @ApiModelProperty(value = "拥有的设备ID", hidden = true)
        private List<Long> myDeviceIds;
    }


    @Data
    @Accessors(chain = true)
    public static class DayChartDto {
        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @ApiModelProperty(value = "统计类型（day,month,year）", required = true)
        @NotNull(message = "统计类型不能为空")
        private String type;

        @NotNull(message = "开始时间不能为空")
        @ApiModelProperty(value = "开始时间", required = true)
        @JsonFormat(pattern = "yyyy-MM-dd")
        private LocalDate beginDate;

        @NotNull(message = "结束时间不能为空")
        @JsonFormat(pattern = "yyyy-MM-dd")
        @ApiModelProperty(value = "结束时间", required = true)
        private LocalDate endDate;

        public Integer getBeginInt() {
            return Integer.valueOf(StrUtil.replace(beginDate.toString(), "-", ""));
        }

        public Integer getEndDateInt() {
            return Integer.valueOf(StrUtil.replace(endDate.toString(), "-", ""));
        }
    }

    @Data
    @Accessors(chain = true)
    public static class DayChartVo {
        @ApiModelProperty("时间")
        private Integer time;

        @ApiModelProperty("笔数")
        private Integer num;

        @ApiModelProperty("销售额(分)")
        private Integer totalMoney;

        @ApiModelProperty("销售额(元)")
        private BigDecimal totalMoneyYuan;

        public BigDecimal getTotalMoneyYuan() {
            return BigDecimal.valueOf(totalMoney).divide(new BigDecimal(100));
        }
    }

    @Data
    @Accessors(chain = true)
    public static class SumCountVo {
        @ApiModelProperty(value = "营业额;单位分")
        private Integer salesMoney;

        @ApiModelProperty(value = "销售笔数")
        private Integer salesCount;

        @ApiModelProperty(value = "销售商品数")
        private Integer goodsCount;

        @ApiModelProperty(value = "退款金额;单位分")
        private Integer refundMoney;

        @ApiModelProperty(value = "退款笔数")
        private Integer refundCount;

        @ApiModelProperty(value = "风险订单数")
        private Integer riskCount;

        @ApiModelProperty(value = "0元订单笔数")
        private Integer zeroCount;
    }

    @Data
    @Accessors(chain = true)
    public static class SumCountDto {
        @ApiModelProperty(value = "商户ID", required = true)
        private Long mercId;

        @NotNull(message = "统计类型不能为空")
        @ApiModelProperty(value = "统计类型", required = true)
        private String type;

        @NotNull(message = "排序字段不能为空")
        @ApiModelProperty(value = "排序字段", required = true)
        private String orderByKey;

        @NotNull(message = "排序方式不能为空")
        @ApiModelProperty(value = "排序方式", required = true)
        private String orderBy;

        @ApiModelProperty("开始时间")
        private String beginDate;

        @ApiModelProperty("结束时间")
        private String endDate;

        @ApiModelProperty(value = "商户拥有的设备ID")
        private List<Long> mercDeviceIds;

        public String getBeginDate() {
            return StrUtil.replace(beginDate, "-", "");
        }

        public String getEndDate() {
            return StrUtil.replace(endDate, "-", "");
        }
    }

    @Data
    @Accessors(chain = true)
    public static class SumPageDto extends SumCountDto {
        @NotNull(message = "分页信息不能为空")
        @ApiModelProperty(value = "分页信息", required = true)
        private PageBean page;
        @ApiModelProperty(value = "商户拥有的设备ID")
        private List<Long> mercDeviceIds;
    }

    @Data
    @Accessors(chain = true)
    public static class SumPageVo {
        @ApiModelProperty(value = "平台商品id")
        private Long deviceId;

        @ApiModelProperty(value = "设备名称")
        private String deviceName;

        @ApiModelProperty(value = "营业额;单位分")
        private Integer salesMoney;

        @ApiModelProperty(value = "销售笔数")
        private Integer salesCount;

        @ApiModelProperty(value = "销售商品数")
        private Integer goodsCount;

        @ApiModelProperty(value = "退款金额;单位分")
        private Integer refundMoney;

        @ApiModelProperty(value = "退款笔数")
        private Integer refundCount;

        @ApiModelProperty(value = "风险订单数")
        private Integer riskCount;

        @ApiModelProperty(value = "0元订单笔数")
        private Integer zeroCount;
    }

    @Data
    @Accessors(chain = true)
    public static class Page extends Vo {

        @ApiModelProperty(value = "分页对象", required = true)
        private PageBean page;

        @JsonFormat(pattern = "yyyy-MM-dd")
        @ApiModelProperty(value = "创建时间-起")
        private LocalDate beginCreateTime;

        @JsonFormat(pattern = "yyyy-MM-dd")
        @ApiModelProperty(value = "创建时间-始")
        private LocalDate endCreateTime;

        public LocalDateTime getBeginCreateTime() {
            return beginCreateTime == null ? null : beginCreateTime.atTime(0, 0, 0);
        }

        public LocalDateTime getEndCreateTime() {
            return endCreateTime == null ? null : endCreateTime.atTime(23, 59, 59);
        }
    }

    @Data
    @Accessors(chain = true)
    public static class SaveOrAccum extends Vo {

        @NotNull(message = "deviceId不能为空")
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
    }

    @Data
    @Accessors(chain = true)
    public static class Vo {

        @ApiModelProperty(value = "id")
        private Integer id;

        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "统计类型;year|month|day")
        private String type;

        @ApiModelProperty(value = "统计日期;2022|202206|20220603")
        private Integer dateValue;

        @ApiModelProperty(value = "营业额;单位分")
        private Integer salesMoney;

        @ApiModelProperty(value = "销售笔数")
        private Integer salesCount;

        @ApiModelProperty(value = "销售商品数")
        private Integer goodsCount;

        @ApiModelProperty(value = "退款金额;单位分")
        private Integer refundMoney;

        @ApiModelProperty(value = "退款笔数")
        private Integer refundCount;

        @ApiModelProperty(value = "风险订单数")
        private Integer riskCount;

        @ApiModelProperty(value = "0元订单笔数")
        private Integer zeroCount;

        @ApiModelProperty(value = "设备故障数")
        private Integer deviceFaultCount;

        @ApiModelProperty(value = "利润")
        private Integer profit;

        @ApiModelProperty(value = "创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime createTime;

        @ApiModelProperty(value = "更新时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime updateTime;


    }

    @Data
    @Accessors(chain = true)
    public static class ListDTO {

        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "设备ID们")
        private List<Long> deviceIds;

        @ApiModelProperty(value = "统计类型;year|month|day")
        private String type;

        @ApiModelProperty(value = "统计日期;2022|202206|20220603")
        private Integer dateValue;

        @ApiModelProperty(value = "营业额;单位分")
        private Integer salesMoney;

        @ApiModelProperty(value = "销售笔数")
        private Integer salesCount;

        @ApiModelProperty(value = "销售商品数")
        private Integer goodsCount;

        @ApiModelProperty(value = "退款金额;单位分")
        private Integer refundMoney;

        @ApiModelProperty(value = "退款笔数")
        private Integer refundCount;

        @ApiModelProperty(value = "风险订单数")
        private Integer riskCount;

        @ApiModelProperty(value = "0元订单笔数")
        private Integer zeroCount;

        @ApiModelProperty(value = "设备故障数")
        private Integer deviceFaultCount;


    }

    /**
     * B端小程序订单分页统计 按天
     */
    @Data
    @Accessors(chain = true)
    public static class MercOrdersDayCountVO {
        @ApiModelProperty(value = "总订单数")
        private Integer orderNum;

        @ApiModelProperty(value = "订单总金额")
        private Integer orderTotalAmount;

    }

    @Data
    @Accessors(chain = true)
    public static class MercOrdersDayCountDTO {

        @JsonFormat(pattern = "yyyy-MM-dd")
        @ApiModelProperty(value = "日期（yyyy-MM-dd）")
        private DateTime orderDate;

    }

    @Data
    @Accessors(chain = true)
    public static class CountByDay {

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @ApiModelProperty(value = "日期")
        private String dateValue;


    }

    @Data
    @Accessors(chain = true)
    public static class CountByMonth {

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @ApiModelProperty(value = "日期")
        private String dateValue;


    }

    @Data
    @Accessors(chain = true)
    public static class PageByMercAndDate {

        @ApiModelProperty(value = "分页信息", required = true)
        private PageBean page;

        @NotNull(message = "mercId不能为空")
        @ApiModelProperty(value = "商户ID")
        private Long mercId;

        @NotBlank(message = "type不能为空")
        @ApiModelProperty(value = "统计类型;year|month|day")
        private String type;

        @NotNull(message = "beginDateValue不能为空")
        @ApiModelProperty(value = "统计开始日期")
        private Integer beginDateValue;

        @NotNull(message = "beginDateValue不能为空")
        @ApiModelProperty(value = "统计结束日期")
        private Integer endDateValue;

        @ApiModelProperty(value = "设备ID")
        private List<Long> deviceId;
    }

    @Data
    @Accessors(chain = true)
    public static class DeviceFlowDetailDTO {
        @ApiModelProperty(value = "是否统计所有，用于导出明细（false）")
        private Boolean countAll;

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "月份-起")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime beginCreateTime;

        @ApiModelProperty(value = "月份-止")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime endCreateTime;

    }


    @Data
    @Accessors(chain = true)
    public static class DeviceFlowCountDTO {
        @ApiModelProperty("分页对象")
        private PageBean page;

        @ApiModelProperty("是否导出当前页")
        private Boolean exportPage = true;
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
        @ApiModelProperty(value = "月份-起")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime beginCreateTime;
        @ApiModelProperty(value = "月份-止")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime endCreateTime;
        @ApiModelProperty(value = "投放时间-起")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime activeStartTime;
        @ApiModelProperty(value = "投放时间-止")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime activeEndStartTime;
        @ApiModelProperty(value = "商户id")
        private Long curMercId;
        @ApiModelProperty(value = "指定商户id")
        private Long chooseMercId;
        @ApiModelProperty(value = "商户ID")
        private List<Long> mercIds;

        // size -1 不分页
        public PageBean getPage() {
            if (!this.exportPage) {
                this.page.setSize(-1L);
            }
            return page;
        }


    }


    @Data
    @Accessors(chain = true)
    public static class DeviceFlowCountExcelVo {
        @ApiModelProperty(value = "月度流水列表")
        List<DeviceFlowMonthVO> monthflowList;
        @ExcelProperty("设备ID")
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
        @ExcelProperty("设备")
        @ApiModelProperty(value = "设备")
        private String deviceName;
        @ExcelProperty("商户名称")
        @ApiModelProperty(value = "商户名称")
        private String mercName;
        @ApiModelProperty(value = "区域")
        private String districtName;
        @ApiModelProperty(value = "点位")
        private String placeName;
        @ApiModelProperty(value = "投放时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime activeTime;
        @ApiModelProperty(value = "月平均流水")
        private BigDecimal monthAvgMoney;
        @ApiModelProperty(value = "总计流水")
        private Integer totalMoney;


    }

    @Data
    @Accessors(chain = true)
    public static class DeviceFlowCountVO {
        @ApiModelProperty(value = "月度流水列表")
        List<DeviceFlowMonthVO> monthflowList;
        @ApiModelProperty(value = "商户ID")
        private Long mercId;
        @ApiModelProperty(value = "商户名称")
        private String mercName;
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
        @ApiModelProperty(value = "设备名称")
        private String deviceName;
        @ApiModelProperty(value = "区域")
        private String districtName;
        @ApiModelProperty(value = "点位")
        private String placeName;
        /**
         * 点位id
         */
        private Long placeId;
        @ApiModelProperty(value = "投放时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime activeTime;
        @ApiModelProperty(value = "月平均流水")
        private BigDecimal monthAvgMoney;
        @ApiModelProperty(value = "总计流水")
        private Integer totalMoney;


    }

    /**
     * 每月流水
     */
    @Data
    @Accessors(chain = true)
    public static class DeviceFlowMonthVO {
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
        @ApiModelProperty(value = "月份")
        private Integer month;
        @ApiModelProperty(value = "当月流水")
        private Integer monthMoney;

    }

    @Data
    @Accessors(chain = true)
    public static class DeviceFlowDetailExcelVO {
        @ExcelProperty("月份")
        @ApiModelProperty(value = "月份")
        private Integer month;

//        @ExcelProperty("设备编号")
//        @ApiModelProperty(value = "设备ID")
//        private Long deviceId;

        @ApiModelProperty(value = "当月流水")
        @ExcelProperty(value = "当月流水", converter = CustomFenToYuanConverter.class)
        @NumberFormat("#0.00")
        private Integer monthMoney;

        @ApiModelProperty(value = "收入")
        @ExcelProperty(value = "收入", converter = CustomFenToYuanConverter.class)
        @NumberFormat("#0.00")
        private Integer incomeMoney;

        @ApiModelProperty(value = "支出")
        @ExcelProperty(value = "支出", converter = CustomFenToYuanConverter.class)
        @NumberFormat("#0.00")
        private Integer expenditureMoney;


    }

    /**
     * 流水明细
     */
    @Data
    @Accessors(chain = true)
    public static class DeviceFlowDetailVO {
        @ApiModelProperty(value = "投放时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private LocalDateTime activeTime;

        @ApiModelProperty(value = "设备ID")
        private Long deviceId;

        @ApiModelProperty(value = "月平均流水")
        private BigDecimal monthAvgMoney;

        @ApiModelProperty(value = "总计流水")
        private Integer totalMoney;

        @ApiModelProperty(value = "流水明细")
        private List<DeviceFlowDetail> details;

    }

    @Data
    @Accessors(chain = true)
    public static class DeviceFlowDetail {
        @ApiModelProperty(value = "设备ID")
        private Long deviceId;
        @ApiModelProperty(value = "月份")
        private Integer month;
        @ApiModelProperty(value = "当月流水")
        private Integer monthMoney;

        @ApiModelProperty(value = "收入")
        private Integer incomeMoney;

        @ApiModelProperty(value = "支出")
        private Integer expenditureMoney;

    }

}
