package com.cool.store.dto.point;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.entity.PointAuditRecordDO;
import com.cool.store.entity.PointTodoInfoDO;
import com.cool.store.enums.AuditStatusEnum;
import com.cool.store.enums.NodeNoEnum;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: AuditNodeDTO
 * @Description:
 * @date 2024-04-02 15:10
 */
@Data
public class AuditNodeDTO {

    @ApiModelProperty("流程号")
    private Integer nodeNo;

    @ApiModelProperty("是否待处理")
    private Boolean isToDo;

    @ApiModelProperty("动作")
    private String actionRemark;

    @ApiModelProperty("用户id")
    private List<String> userIds;

    public AuditNodeDTO(Integer nodeNo, List<String> userIds) {
        this.nodeNo = nodeNo;
        this.userIds = userIds;
    }

    public AuditNodeDTO(Integer nodeNo, String actionRemark, Boolean isToDo, List<String> userIds) {
        this.nodeNo = nodeNo;
        this.actionRemark = actionRemark;
        this.isToDo = isToDo;
        this.userIds = userIds;
    }

    public static List<PointAuditRecordDO> convertDO(Long pointId, Integer cycleCount, List<AuditNodeDTO> nodeList){
        if(CollectionUtils.isEmpty(nodeList)){
            return Lists.newArrayList();
        }
        List<PointAuditRecordDO> resultList = new ArrayList<>();
        for (AuditNodeDTO auditNode : nodeList) {
            PointAuditRecordDO node = new PointAuditRecordDO();
            node.setPointId(pointId);
            node.setNodeNo(auditNode.getNodeNo());
            node.setCycleCount(cycleCount);
            node.setActionRemark(auditNode.getActionRemark());
            node.setHandlerUserIds(JSONObject.toJSONString(auditNode.getUserIds()));
            if(Objects.nonNull(auditNode.isToDo) && auditNode.isToDo){
                node.setReceiveTaskTime(new Date());
            }
            if(NodeNoEnum.NODE_NO_0.getCode().equals(auditNode.getNodeNo())){
                node.setHandlerUserId(auditNode.getUserIds().get(0));
                node.setReceiveTaskTime(new Date());
                node.setFinishTaskTime(new Date());
                node.setAuditStatus(AuditStatusEnum.PASS.getCode());
            }
            resultList.add(node);
        }
        return resultList;
    }

    public static List<PointTodoInfoDO> convertTODO(Long pointId, Integer cycleCount, List<AuditNodeDTO> nodeList){
        if(CollectionUtils.isEmpty(nodeList)){
            return Lists.newArrayList();
        }
        List<PointTodoInfoDO> resultList = new ArrayList<>();
        for (AuditNodeDTO auditNode : nodeList) {
            if(Objects.nonNull(auditNode.isToDo) && auditNode.isToDo){
                for (String userId : auditNode.getUserIds()) {
                    PointTodoInfoDO todo = new PointTodoInfoDO();
                    todo.setPointId(pointId);
                    todo.setNodeNo(auditNode.getNodeNo());
                    todo.setHandlerUserId(userId);
                    todo.setCycleCount(cycleCount);
                    resultList.add(todo);
                }
            }
        }
        return resultList;
    }
}
