package com.cool.store.entity;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.enums.AuditStatusEnum;
import com.cool.store.enums.NodeNoEnum;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;
import javax.persistence.*;

@Data
@Table(name = "xfsg_point_audit_record")
public class PointAuditRecordDO {

    public final static String SUBMIT_TASK = "提交审批任务";
    public final static String RECEIVE_TASK = "收到任务";
    public final static String AUDIT_PASS = "审批通过";
    public final static String AUDIT_REJECT = "审批拒绝";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 点位id
     */
    @Column(name = "point_id")
    private Long pointId;

    /**
     * 当前节点 0提交审批任务 1-5级审批, 100完成
     */
    @Column(name = "node_no")
    private Integer nodeNo;

    /**
     * 轮次
     */
    @Column(name = "cycle_count")
    private Integer cycleCount;

    /**
     * 动作备注
     */
    @Column(name = "action_remark")
    private String actionRemark;

    /**
     * 实际处理人
     */
    @Column(name = "handler_user_id")
    private String handlerUserId;

    /**
     * 处理人集合
     */
    @Column(name = "handler_user_ids")
    private String handlerUserIds;

    /**
     * 收到任务时间
     */
    @Column(name = "receive_task_time")
    private Date receiveTaskTime;

    /**
     * 完成任务时间
     */
    @Column(name = "finish_task_time")
    private Date finishTaskTime;

    /**
     * 审核状态0待处理,1通过,2拒绝
     */
    @Column(name = "audit_status")
    private Integer auditStatus;

    /**
     * 签到时间
     */
    @Column(name = "sign_time")
    private Date signTime;

    /**
     * 签到位置
     */
    @Column(name = "sign_address")
    private String signAddress;

    /**
     * 图片
     */
    @Column(name = "picture_url")
    private String pictureUrl;

    /**
     * 原因
     */
    private String reason;

    /**
     * 删除标识
     */
    private Boolean deleted;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    public static List<PointTodoInfoDO> convertTODO(PointAuditRecordDO nextAuditRecord){
        List<PointTodoInfoDO> resultList = new ArrayList<>();
        List<String> handlerUserIds = JSONObject.parseArray(nextAuditRecord.getHandlerUserIds(), String.class);
        //去重
        List<String> finalHandlerUserIds = new ArrayList<>(new HashSet<>(handlerUserIds));
        for (String userId : finalHandlerUserIds) {
            PointTodoInfoDO todo = new PointTodoInfoDO();
            todo.setPointId(nextAuditRecord.getPointId());
            todo.setNodeNo(nextAuditRecord.getNodeNo());
            todo.setHandlerUserId(userId);
            todo.setCycleCount(nextAuditRecord.getCycleCount());
            resultList.add(todo);
        }
        return resultList;
    }

    public static PointAuditRecordDO convert(Long auditRecordId, String handlerUserId, AuditStatusEnum auditStatus, String reason, NodeNoEnum nodeNoEnum){
        PointAuditRecordDO updateAuditRecord = new PointAuditRecordDO();
        updateAuditRecord.setId(auditRecordId);
        updateAuditRecord.setHandlerUserId(handlerUserId);
        updateAuditRecord.setAuditStatus(auditStatus.getCode());
        updateAuditRecord.setReason(reason);
        updateAuditRecord.setActionRemark(nodeNoEnum.getTypeName() + " " +auditStatus.getName());
        updateAuditRecord.setFinishTaskTime(new Date());
        return updateAuditRecord;
    }

    public static PointAuditRecordDO convert(Long auditRecordId, Date receiveTaskTime){
        PointAuditRecordDO updateAuditRecord = new PointAuditRecordDO();
        updateAuditRecord.setId(auditRecordId);
        updateAuditRecord.setReceiveTaskTime(receiveTaskTime);
        return updateAuditRecord;
    }

    public static PointAuditRecordDO convert(Long auditRecordId, String handlerUserId, AuditStatusEnum auditStatus, String reason, NodeNoEnum nodeNoEnum, Date signTime, String signAddress, String pictureUrl){
        PointAuditRecordDO updateAuditRecord = new PointAuditRecordDO();
        updateAuditRecord.setId(auditRecordId);
        updateAuditRecord.setHandlerUserId(handlerUserId);
        updateAuditRecord.setAuditStatus(auditStatus.getCode());
        updateAuditRecord.setReason(reason);
        updateAuditRecord.setActionRemark(nodeNoEnum.getTypeName() + " " +auditStatus.getName());
        updateAuditRecord.setSignTime(signTime);
        updateAuditRecord.setSignAddress(signAddress);
        updateAuditRecord.setPictureUrl(pictureUrl);
        updateAuditRecord.setFinishTaskTime(new Date());
        return updateAuditRecord;
    }

}