package com.example.sjkbf.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sjkbf.entity.UserServerProject;
import com.example.sjkbf.mapper.UserServerProjectMapper;
import com.example.sjkbf.service.UserServerProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bozhiqiang
 * @since 2025-04-19
 */
@Service
public class UserServerProjectServiceImpl extends ServiceImpl<UserServerProjectMapper, UserServerProject> implements UserServerProjectService {



    @Override
    public void addpro(String ip, String name, int time, String userName) {
        UserServerProject one = getOne(new QueryWrapper<UserServerProject>().eq("server_ip", ip).eq("project_name", name));
        if (one==null){
            UserServerProject userServerProject = new UserServerProject();
            userServerProject.setServerIp(ip);
            userServerProject.setProjectName(name);
            userServerProject.setUserName(userName);
            userServerProject.setOpen(1);
            DateTime date = DateUtil.date();
            DateTime newDate2 = DateUtil.offsetDay(date, time);
            userServerProject.setExpirationDate(newDate2);
            save(userServerProject);
        }else {
            Date expirationDate = one.getExpirationDate();
            DateTime newDate2 = DateUtil.offsetDay(expirationDate, time);
            one.setExpirationDate(newDate2);
            update(one, new QueryWrapper<UserServerProject>().eq("server_ip", ip).eq("project_name", name));
        }
    }

    @Override
    public List<UserServerProject> getpro(String ip) {

        List<UserServerProject> list = list(new QueryWrapper<UserServerProject>().eq("server_ip", ip).ge("expiration_date", DateUtil.date()));


        return list;
    }

    @Override
    public List<UserServerProject> getproAndOpen(String ip) {

        List<UserServerProject> list = list(new QueryWrapper<UserServerProject>().eq("server_ip", ip).eq("open", 1).ge("expiration_date", DateUtil.date()));


        return list;
    }

    @Override
    public UserServerProject getByIPAndName(String serverIp, String projectName) {

        return getOne(new QueryWrapper<UserServerProject>().eq("server_ip", serverIp).eq("project_name", projectName).ge("expiration_date", DateUtil.date()));

    }

    @Override
    public List<UserServerProject> getByUserName(String userName) {

        return list(new QueryWrapper<UserServerProject>().eq("user_name", userName).ge("expiration_date", DateUtil.date()));
    }

    @Override
    public String setproOpen(String ip, String projectName, String open, String userName) {
        UserServerProject one = getOne(new QueryWrapper<UserServerProject>().eq("server_ip", ip).eq("project_name", projectName));
        if (one!=null){
            one.setOpen(Integer.parseInt(open));
            update(one, new QueryWrapper<UserServerProject>().eq("server_ip", ip).eq("project_name", projectName));
            return "操作成功";
        }else {
            return "账号下不存在此项目，请检查项目所有权是否为该账号";
        }
    }


}
