package com.huimv.receive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultUtil;
import com.huimv.receive.entity.BaseVisiting;
import com.huimv.receive.mapper.BaseVisitingMapper;
import com.huimv.receive.service.IBaseVisitingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-06
 */
@Service
public class BaseVisitingServiceImpl extends ServiceImpl<BaseVisitingMapper, BaseVisiting> implements IBaseVisitingService {

    @Autowired
    private BaseVisitingMapper baseVisitingMapper;

    @Override
    public Result add(HttpServletRequest httpServletRequest, BaseVisiting baseVisiting) {
        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if (baseVisitingMapper.selectCount(new QueryWrapper<BaseVisiting>().lambda()
                .eq(BaseVisiting::getVisitingName,baseVisiting.getVisitingName())) > 0) {
            return new Result(10001,"来访类型已存在!",false);
        }
        baseVisiting.setCreateUser(userId);
        baseVisiting.setCreateTime(LocalDateTime.now());
        return ResultUtil.addResult(baseVisitingMapper.insert(baseVisiting));
    }

    @Override
    public Result delete(String id) {
        return ResultUtil.deleteResult(baseVisitingMapper.deleteById(id));
    }


}
