package com.chushang.inspection.work.vo;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.chushang.common.dict.annotation.DictFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WrkInfoVO implements Serializable {

    /**
     * id
     */
    private Long wrkId;
    /**
     * 租户id
     */
    private Long deptId;
    private String deptName;
    /**
     * 总任务id
     */
    private Long taskId;
    private String taskName;
    /**
     * 本级id
     */
    private Long lowerTaskId;
    private String lowerTaskName;
    /**
     * 业务员id
     */
    private Long userId;
    /**
     * 业务员名字
     */
    private String userName;
    /**
     * 工单编号
     */
    private String workNo;
    /**
     * 工单类型
     */
    private Integer workType;
    /**
     * 巡检方式
     */
    private Integer workMethod;
    /**
     * 工单优先级
     */
    private Integer workSort;
    /**
     * 工单来源
     */
    private Integer workSource;
    /**
     * 业务员处理时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    private LocalDateTime disposeTime;

    /**
     * 客户经理
     */
    private String accountManager;

    /**
     * 经理电话
     */
    private String accountPhone;
    /**
     * 服务结果
     */
    @DictFormat(dictType = "service_results")
    private Integer serviceResult;
    /**
     * 备注
     */
    private String remark;
    /**
     * 结束时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    private LocalDateTime endTime;
    /**
     * 工单状态
     */
    private Integer wrkStatus;
    /**
     * 门头照片
     */
    private String doorImage;
    /**
     * 剩余时间
     */
    private String overtimeTime;
    /**
     * 派单时间
     */
    @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    private LocalDateTime createTime;

    /**
     * 派单人
     */
    private String createBy;

    public String getOvertimeTime() {
        if (endTime == null) {
            return null;
        }
        DateTime date = ObjectUtil.isNotEmpty(disposeTime) ? DateUtil.date(disposeTime) : DateUtil.date();

        DateTime time = DateUtil.date(endTime);

        String[] split = DateUtil.formatBetween(date, time).split("小时");

        return StrUtil.format("{}{}小时",
                DateUtil.between(date, time, DateUnit.HOUR, false) > 0 ? "剩余" : "超时",
                split.length != 1 ? split[0] : "1");
    }

    @Serial
    private static final long serialVersionUID = 1L;
}
