package com.cool.store.vo.point;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.entity.PointAuditRecordDO;
import com.cool.store.utils.StringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author zhangchenbiao
 * @FileName: AuditRecordVO
 * @Description:
 * @date 2024-04-08 11:36
 */
@Data
public class PointAuditRecordVO {

    @ApiModelProperty("实际处理人")
    private UserBaseInfoVO handlerUser;

    @ApiModelProperty("处理人列表")
    private List<UserBaseInfoVO> handlerUserList;

    @ApiModelProperty("处理时间")
    private Date handlerTime;

    @ApiModelProperty("原因")
    private String reason;

    @ApiModelProperty("0待处理,1通过,2拒绝")
    private String auditStatus;

    @ApiModelProperty("signTime")
    private Date signTime;

    @ApiModelProperty("签到地址")
    private String signAddress;

    @ApiModelProperty("图片")
    private String pictureUrl;

    @ApiModelProperty("是否超时")
    private Boolean isTimeout;

    @ApiModelProperty("节点")
    private Integer nodeNo;

    public static List<PointAuditRecordVO> convert(List<PointAuditRecordDO> auditRecordList, Map<String, EnterpriseUserDO> userMap) {
        List<PointAuditRecordVO> resultList = new ArrayList<>();
        for (PointAuditRecordDO pointAuditRecord : auditRecordList) {
            PointAuditRecordVO pointAuditRecordVO = new PointAuditRecordVO();
            pointAuditRecordVO.setHandlerTime(pointAuditRecord.getFinishTaskTime());
            pointAuditRecordVO.setReason(pointAuditRecord.getReason());
            pointAuditRecordVO.setAuditStatus(pointAuditRecord.getAuditStatus().toString());
            pointAuditRecordVO.setSignTime(pointAuditRecord.getSignTime());
            pointAuditRecordVO.setSignAddress(pointAuditRecord.getSignAddress());
            pointAuditRecordVO.setPictureUrl(pointAuditRecord.getPictureUrl());
            pointAuditRecordVO.setIsTimeout(Boolean.FALSE);
            pointAuditRecordVO.setNodeNo(pointAuditRecord.getNodeNo());
            if(Objects.nonNull(pointAuditRecord.getReceiveTaskTime())){
                Date time = Objects.isNull(pointAuditRecord.getFinishTaskTime()) ? new Date() : pointAuditRecord.getFinishTaskTime();
                LocalDateTime localDateTime = LocalDateTime.ofInstant(pointAuditRecord.getReceiveTaskTime().toInstant(), ZoneId.systemDefault());
                LocalDateTime localDateTime1 = LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault());
                Duration duration = Duration.between(localDateTime, localDateTime1);
                long hours = duration.toHours();
                pointAuditRecordVO.setIsTimeout(hours >= 48);
            }
            pointAuditRecordVO.setHandlerUser(UserBaseInfoVO.convert(pointAuditRecord.getHandlerUserId(), userMap));
            List<String> handlerUserIds = JSONObject.parseArray(pointAuditRecord.getHandlerUserIds(), String.class);
            pointAuditRecordVO.setHandlerUserList(UserBaseInfoVO.convert(handlerUserIds, userMap));
            resultList.add(pointAuditRecordVO);
        }
        return resultList;
    }


}
