package jnpf.model.personnels.dto.rewardspunishments;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import jnpf.entity.workflow.RewardApproval;
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 FtbAwardSubmissionDTO {

    /**
     * 奖励核算日
     */
    @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;

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

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

}
