package com.tuoheng.admin.service.accidentread;

import com.tuoheng.admin.entity.AccidentRead;
import com.tuoheng.admin.enums.AccidentEnum;
import com.tuoheng.admin.mapper.AccidentReadMapper;
import com.tuoheng.admin.service.accident.IAccidentService;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author ChengWang
 * @Date 2023/3/2
 */
@Service
@Slf4j
public class AccidentReadServiceImpl implements IAccidentReadService {

    @Autowired
    private AccidentReadMapper accidentReadMapper;

    /**
     * 应急记录已读数据保存
     * @param accidentId
     * @return
     */
    @Override
    public JsonResult add(String accidentId) {
        //校验
        if(StringUtils.isEmpty(accidentId)){
            return JsonResult.error(AccidentEnum.DATA_IS_NULL.getCode(),AccidentEnum.DATA_IS_NULL.getMsg());
        }
        //当前用户信息
        String userId = CurrentUserUtil.getUserId();
        if(StringUtils.isEmpty(userId)){
            throw new ServiceException("用户id为空");
        }
        AccidentRead accidentRead = new AccidentRead();
        accidentRead.setAccidentId(accidentId);
        accidentRead.setCreateTime(DateUtils.now());
        accidentRead.setCreateUser(userId);
        accidentRead.setUserId(userId);
        accidentRead.setStatus(3);
        int result = accidentReadMapper.insert(accidentRead);
        if(result<=0){
            return JsonResult.error();
        }

        return JsonResult.success();
    }

}
