package com.chuanxia.mcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuanxia.mcp.bean.dto.DomainTimeRuleBindDto;
import com.chuanxia.mcp.bean.dto.DomainTimeRuleQueryDto;
import com.chuanxia.mcp.bean.model.CdnDomain;
import com.chuanxia.mcp.bean.model.CdnDomainTimeRule;
import com.chuanxia.mcp.bean.model.CdnTimeRule;
import com.chuanxia.mcp.bean.vo.CdnDomainTimeRuleVo;
import com.chuanxia.mcp.bean.vo.Result;
import com.chuanxia.mcp.exception.ResultException;
import com.chuanxia.mcp.mapper.CdnDomainMapper;
import com.chuanxia.mcp.mapper.CdnDomainTimeRuleMapper;
import com.chuanxia.mcp.mapper.CdnTimeRuleMapper;
import com.chuanxia.mcp.service.CdnTimeRuleService;
import com.chuanxia.mcp.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
public class CdnTimeRuleServiceImpl extends ServiceImpl<CdnTimeRuleMapper, CdnTimeRule> implements CdnTimeRuleService {
    private final UserService userService;
    private final CdnDomainMapper domainMapper;
    private final CdnTimeRuleMapper ruleMapper;
    private final CdnDomainTimeRuleMapper domainTimeRuleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> bindTimeRule(DomainTimeRuleBindDto timeRule) {
        Integer startTime = timeRule.getStartTime();
        Integer endTime = timeRule.getEndTime();
        if (startTime == null || endTime == null) {
            throw new ResultException(500, "开始时间/结束时间不能为空");
        }
        if (startTime > endTime) {
            throw new ResultException(500, "开始时间不能大于结束时间");
        }
        LambdaQueryWrapper<CdnTimeRule> query = Wrappers.lambdaQuery();
        query.eq(CdnTimeRule::getStartTime, startTime).eq(CdnTimeRule::getEndTime, endTime);
        CdnTimeRule cdnTimeRule = ruleMapper.selectOne(query);
        if (cdnTimeRule == null) {
            //不存在这个时间规则,创建
            cdnTimeRule = new CdnTimeRule();
            cdnTimeRule.setStartTime(startTime);
            cdnTimeRule.setEndTime(endTime);
            cdnTimeRule.setCreateBy(userService.getLoginUser().getId());
            ruleMapper.insert(cdnTimeRule);
        }
        CdnDomainTimeRule rule = new CdnDomainTimeRule();
        CdnDomain domain = domainMapper.selectById(timeRule.getDomainId());
        rule.setDomainId(timeRule.getDomainId());
        rule.setDomain(domain.getDomain());
        rule.setTimeRuleId(cdnTimeRule.getId());
        rule.setStartTime(cdnTimeRule.getStartTime());
        rule.setEndTime(cdnTimeRule.getEndTime());
        domainTimeRuleMapper.insert(rule);
        return Result.success("操作成功");
    }

    @Override
    public Result<IPage<CdnDomainTimeRuleVo>> listPage(DomainTimeRuleQueryDto queryDto) {
        IPage<CdnDomainTimeRuleVo> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        page = domainTimeRuleMapper.listPage(page, queryDto);
        return Result.success(page);
    }

    @Override
    public Result deleteById(Integer id) {
        domainTimeRuleMapper.deleteById(id);
        return Result.success();
    }
}
