package jnpf.model.personnels.dto.rewardspunishments;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import jnpf.entity.workflow.PunishmentsApproval;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
public class FtbPenaltySubmissionDTO {

    /**
     * 惩罚核算日
     */
    @NotNull(message = "惩罚核算日不能为空")
    private Date day;

    /**
     * 惩罚类别
     */
    @NotBlank(message = "惩罚类别不能为空")
    private String type;

    /**
     * 惩罚金额
     */
    @NotNull(message = "惩罚金额不能为空")
    private BigDecimal amount;

    /**
     * 生效时间
     */
    private Date effectiveDate;

    /**
     * 惩罚原因
     */
    private String rewardReason;

    /**
     * 备注
     */
    private String reason;

    /**
     * 照片
     */
    private String fileJson;

    /**
     * 流程任务主键
     */
    private String fFlowtaskid;

    /**
     * 流程id
     */
    @NotBlank(message = "流程id不能为空")
    private String taskId;

    /**
     * 唯一id
     */
    private String uniqueId;

    /**
     * 流程标题
     */
    private String flowTitle;

    /**
     * 申请人员
     */
    private List<String> applyUser;

    /**
     * 用户集合
     */
    @NotNull(message = "惩罚成员不能为空")
    @Size(min = 1, message = "惩罚成员不能为空")
    private List<String> userDtos;

    public static PunishmentsApproval convertPunishmentsApproval(FtbPenaltySubmissionDTO ftbPenaltySubmissionDTO) {
        if (ftbPenaltySubmissionDTO == null) {
            return null;
        }
        PunishmentsApproval punishmentsApproval = new PunishmentsApproval();
        punishmentsApproval.setDay(ftbPenaltySubmissionDTO.getDay());
        punishmentsApproval.setType(ftbPenaltySubmissionDTO.getType());
        punishmentsApproval.setAmount(ftbPenaltySubmissionDTO.getAmount());
        punishmentsApproval.setEffectiveDate(ftbPenaltySubmissionDTO.getEffectiveDate());
        punishmentsApproval.setRewardReason(ftbPenaltySubmissionDTO.getRewardReason());
        punishmentsApproval.setReason(ftbPenaltySubmissionDTO.getReason());
        punishmentsApproval.setFFlowtaskid(ftbPenaltySubmissionDTO.getFFlowtaskid());
        punishmentsApproval.setFFlowid(ftbPenaltySubmissionDTO.getTaskId());
        punishmentsApproval.setUniqueId(ftbPenaltySubmissionDTO.getUniqueId());
        punishmentsApproval.setFlowTitle(ftbPenaltySubmissionDTO.getFlowTitle());
        punishmentsApproval.setApplyUser(ftbPenaltySubmissionDTO.getApplyUser().get(0));
        // 照片
        if (StrUtil.isNotEmpty(ftbPenaltySubmissionDTO.getFileJson())) {
            List<FtbAwardSubmissionDTO.PictureMap> pictureMaps = JSON.parseArray(ftbPenaltySubmissionDTO.getFileJson(), FtbAwardSubmissionDTO.PictureMap.class);
            punishmentsApproval.setFileJson(pictureMaps.get(0).getUrl());
        }
        return punishmentsApproval;
    }

    @Data
    public static class PictureMap {
        private String url;
        private String fileId;
        private String previewUrl;
    }


}
