package com.srs.stureg.service.impl;

import java.time.Duration;
import java.util.List;
import com.srs.common.utils.DateUtils;
import com.srs.stureg.domain.SrsFeedback;
import com.srs.stureg.mapper.SrsFeedbackMapper;
import com.srs.stureg.service.ISrsFeedbackService;
import com.srs.stureg.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 新生报到反馈Service业务层处理
 * 
 * @author srs
 * @date 2023-07-31
 */
@Service
public class SrsFeedbackServiceImpl implements ISrsFeedbackService
{
    @Autowired
    private SrsFeedbackMapper srsFeedbackMapper;
    //redis
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询新生报到反馈
     * 
     * @param id 新生报到反馈主键
     * @return 新生报到反馈
     */
    @Override
    public SrsFeedback selectSrsFeedbackById(Long id)
    {
        return srsFeedbackMapper.selectSrsFeedbackById(id);
    }

    /**
     * 查询新生报到反馈列表
     * 
     * @param srsFeedback 新生报到反馈
     * @return 新生报到反馈
     */
    @Override
    public List<SrsFeedback> selectSrsFeedbackList(SrsFeedback srsFeedback)
    {
        return srsFeedbackMapper.selectSrsFeedbackList(srsFeedback);
    }

    /**
     * 新增新生报到反馈
     * 
     * @param srsFeedback 新生报到反馈
     * @return 结果
     */
    @Override
    public int insertSrsFeedback(SrsFeedback srsFeedback, HttpServletRequest request)
    {
        String ip=IpUtil.getIpaddrS(request);//iP地址
        srsFeedback.setIp(ip);
        if (getUsername()!= null) {
            srsFeedback.setCreateBy(getUsername());
        }
        srsFeedback.setCreateTime(DateUtils.getNowDate());
        int i = srsFeedbackMapper.insertSrsFeedback(srsFeedback);//添加
        if (i!=1){
            return 2;
        }
        //把ip从redis取出来
        String s = redisTemplate.opsForValue().get("_IP_ADDR_" + ip);
        if (StringUtils.isEmpty(s)){
            s="1";
            //没有取到就将反馈ip存入到redis
            redisTemplate.opsForValue().set("_IP_ADDR_"+ip,s,
                    Duration.ofMinutes(1440));
        }else {
            //有则更新
            if (Integer.parseInt(s) >= 5) {
                return 0;
            }
            s = String.valueOf(Integer.parseInt(s) + 1);
            redisTemplate.opsForValue().increment("_IP_ADDR_" + ip);
        }
        return 1;
    }

    /**
     * 修改新生报到反馈
     * 
     * @param srsFeedback 新生报到反馈
     * @return 结果
     */
    @Override
    public int updateSrsFeedback(SrsFeedback srsFeedback)
    {
        if (getUsername()!=null){
            srsFeedback.setUpdateBy(getUsername());
        }
        srsFeedback.setUpdateTime(DateUtils.getNowDate());
        return srsFeedbackMapper.updateSrsFeedback(srsFeedback);
    }

    /**
     * 批量删除新生报到反馈
     * 
     * @param ids 需要删除的新生报到反馈主键
     * @return 结果
     */
    @Override
    public int deleteSrsFeedbackByIds(Long[] ids)
    {
        return srsFeedbackMapper.deleteSrsFeedbackByIds(ids);
    }

    /**
     * 删除新生报到反馈信息
     * 
     * @param id 新生报到反馈主键
     * @return 结果
     */
    @Override
    public int deleteSrsFeedbackById(Long id)
    {
        return srsFeedbackMapper.deleteSrsFeedbackById(id);
    }
}
