package com.gxwebsoft.cms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.Website;
import com.gxwebsoft.cms.mapper.DomainMapper;
import com.gxwebsoft.cms.service.DomainService;
import com.gxwebsoft.cms.entity.Domain;
import com.gxwebsoft.cms.param.DomainParam;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 网站域名记录表Service实现
 *
 * @author 科技小王子
 * @since 2024-03-26 21:27:29
 */
@Service
public class DomainServiceImpl extends ServiceImpl<DomainMapper, Domain> implements DomainService {

    @Override
    public PageResult<Domain> pageRel(DomainParam param) {
        PageParam<Domain, DomainParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, status desc,create_time desc");
        List<Domain> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Domain> listRel(DomainParam param) {
        List<Domain> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Domain, DomainParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, status desc,create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Domain getByIdRel(Integer id) {
        DomainParam param = new DomainParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public void saveAsync(Website website) {
      final Domain domain = baseMapper.selectOne(new LambdaQueryWrapper<Domain>().eq(Domain::getType, 1).last("limit 1"));
      final String freeDomain = website.getWebsiteCode().concat(".wsdns.cn");
      if(ObjectUtil.isNotEmpty(domain)){
        domain.setDomain(freeDomain);
        domain.setWebsiteId(website.getWebsiteId());
        domain.setAppId(website.getTenantId());
        domain.setTenantId(website.getTenantId());
        updateById(domain);
      }
      else
      {
        Domain add = new Domain();
        add.setStatus(1);
        add.setHostName("@");
        add.setDomain(freeDomain);
        add.setType(1);
        add.setWebsiteId(website.getWebsiteId());
        add.setAppId(website.getTenantId());
        save(add);
      }

      // 查重
      if (count(new LambdaQueryWrapper<Domain>().eq(Domain::getDomain,freeDomain)) > 1) {
        throw new BusinessException(freeDomain + "该域名已被使用");
      }


//      if (ObjectUtil.isNotEmpty(domain)) {
//        domain.setDomain(domainStr);
//        domain.setHostName("@");
//        domain.setStatus(1);
//        updateById(domain);
//      }
//      if (ObjectUtil.isEmpty(domain)) {
//        final Domain add = new Domain();
//        add.setStatus(1);
//        add.setHostName("@");
//        add.setDomain(domainStr);
//        save(add);
//      }
    }

}
