package com.chuanxia.mcp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
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.DomainOperatorBindDto;
import com.chuanxia.mcp.bean.dto.OperatorQueryDto;
import com.chuanxia.mcp.bean.model.CdnDomain;
import com.chuanxia.mcp.bean.model.CdnDomainOperator;
import com.chuanxia.mcp.bean.model.CdnOperator;
import com.chuanxia.mcp.bean.model.SystemUser;
import com.chuanxia.mcp.bean.vo.CdnOperatorVo;
import com.chuanxia.mcp.bean.vo.Result;
import com.chuanxia.mcp.exception.ResultException;
import com.chuanxia.mcp.mapper.CdnDomainMapper;
import com.chuanxia.mcp.mapper.CdnOperatorMapper;
import com.chuanxia.mcp.service.CdnDomainOperatorService;
import com.chuanxia.mcp.service.CdnOperatorService;
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;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CdnOperatorServiceImpl extends ServiceImpl<CdnOperatorMapper, CdnOperator> implements CdnOperatorService {
    private final CdnOperatorMapper operatorMapper;
    private final CdnDomainMapper domainMapper;
    private final UserService userService;
    private final CdnDomainOperatorService domainOperatorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> saveOperator(CdnOperator operator) {
        SystemUser loginUser = userService.getLoginUser();
        operator.setCreateBy(loginUser.getId());
        operatorMapper.insert(operator);
        return Result.success("操作成功");
    }

    @Override
    public Result<String> updateOperator(CdnOperator operator) {
        SystemUser loginUser = userService.getLoginUser();
        operator.setUpdateBy(loginUser.getId());
        operatorMapper.updateById(operator);
        return Result.success("操作成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> bindCdn(DomainOperatorBindDto bindDto) {
        Integer domainId = bindDto.getDomainId();
        CdnDomain domain = domainMapper.selectById(domainId);
        if (domain == null) {
            throw new ResultException(500, "域名id不存在");
        }
        List<Integer> operatorIds = bindDto.getOperatorIds();
        if (CollectionUtil.isEmpty(operatorIds)) {
            throw new ResultException(500, "运营商id不能为空");
        }
        SystemUser loginUser = userService.getLoginUser();
        List<CdnDomainOperator> arr = new ArrayList<>(operatorIds.size());
        for (Integer operatorId : operatorIds) {
            CdnOperator cdnOperator = operatorMapper.selectById(operatorId);
            if (cdnOperator == null) {
                throw new ResultException(500, "运营商不存在");
            }
            CdnDomainOperator domainOperator = new CdnDomainOperator();
            domainOperator.setOperatorId(operatorId);
            domainOperator.setDomainId(domainId);
            domainOperator.setDomain(domain.getDomain());
            domainOperator.setCreateBy(loginUser.getId());
            domainOperator.setOperatorName(cdnOperator.getOperatorName());
            arr.add(domainOperator);
        }
        LambdaQueryWrapper<CdnDomainOperator> del = Wrappers.lambdaQuery();
        del.eq(CdnDomainOperator::getDomainId, domainId);
        domainOperatorService.remove(del);
        domainOperatorService.saveBatch(arr);
        return Result.success("操作成功");
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> removeBind(Integer id) {
        domainOperatorService.removeById(id);
        return Result.success("操作成功");
    }
}
