package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.FollowRecord;
import com.ruoyi.web.work.domain.dto.FollowRecordDto;
import com.ruoyi.web.work.mapper.FollowRecordMapper;
import com.ruoyi.web.work.mapper.FollowTemplateMapper;
import com.ruoyi.web.work.service.IFollowRecordService;
import com.sun.jna.platform.win32.ShellAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lsw
 * @date 2024-07-18
 */
@Service
public class FollowRecordServiceImpl extends ServiceImpl<FollowRecordMapper, FollowRecord> implements IFollowRecordService {
    @Autowired
    private FollowRecordMapper followRecordMapper;

    @Autowired
    private FollowTemplateMapper followTemplateMapper;

    @Override
    public List<FollowRecord> selectList(FollowRecord followRecord) {
        return followRecordMapper.selectList(followRecord);
    }

    @Override
    public AjaxResult detail(Long id) {
        FollowRecord followRecord = getById(id);
        if (followRecord == null || !followRecord.getPatientId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("回访不存在或非法操作");
        }
        return AjaxResult.success(followRecord);
    }

    @Override
    public AjaxResult push(FollowRecordDto dto) {
        FollowRecord followRecord = getById(dto.getId());
        if (followRecord == null || !followRecord.getPatientId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("回访不存在或非法操作");
        }
        followRecord.setOp(dto.getOp());
        followRecord.setState(1);
        if (!updateById(followRecord)) {
            throw new ServiceException("提交回访记录失败");
        }
        return AjaxResult.success();
    }
}
