package com.yida.data.form.apply.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.customForm.dto.apply.ApprovalRemindDTO;
import com.yida.data.customForm.vo.apply.ApprovalProcessInfoVO;
import com.yida.data.customForm.vo.apply.ApprovalRemindInfoVO;
import com.yida.data.form.apply.mapper.CoreApprovalRemindMapper;
import com.yida.data.form.apply.service.CoreApprovalRemindService;
import com.yida.data.common.core.entity.applyForm.CoreApprovalRemind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 审核提醒策略表 Service
 *
 * @author ZYJ
 * @date 2023/11/23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CoreApprovalRemindServiceImpl extends ServiceImpl<CoreApprovalRemindMapper, CoreApprovalRemind>
        implements CoreApprovalRemindService {

    @Override
    public ApprovalRemindInfoVO getApprovalRemindInfo() {
        // 返回信息
        ApprovalRemindInfoVO vo = new ApprovalRemindInfoVO();
        CoreApprovalRemind approvalRemind = getOne(Wrappers.lambdaQuery(new CoreApprovalRemind())
                .eq(CoreApprovalRemind::getDeptId, FebsUtil.getDeptId()));
        if (Objects.nonNull(approvalRemind)) {
            BeanUtils.copyProperties(approvalRemind, vo);
        }
        return vo;
    }

    @Override
    public void saveApprovalRemind(ApprovalRemindDTO dto) {
        CoreApprovalRemind approvalRemind = getOne(Wrappers.lambdaQuery(new CoreApprovalRemind())
                .eq(CoreApprovalRemind::getDeptId, FebsUtil.getDeptId()));
        if (Objects.isNull(approvalRemind)) {
            approvalRemind = new CoreApprovalRemind();
            approvalRemind.setDeptId(FebsUtil.getDeptId());
        }
        approvalRemind.setApprovalRemindStatus(dto.getApprovalRemindStatus());
        approvalRemind.setApprovalResultStatus(dto.getApprovalResultStatus());
        saveOrUpdate(approvalRemind);
    }
}
