package com.gxwebsoft.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.app.entity.AppResource;
import com.gxwebsoft.app.mapper.AppResourceMapper;
import com.gxwebsoft.app.param.AppResourceParam;
import com.gxwebsoft.app.service.AppResourceService;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开发者资源 Service 实现
 *
 * @author 科技小王子
 * @since 2026-03-31
 */
@Slf4j
@Service
public class AppResourceServiceImpl extends ServiceImpl<AppResourceMapper, AppResource>
        implements AppResourceService {

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

    @Override
    public List<AppResource> listRel(AppResourceParam param) {
        return baseMapper.selectListRel(param);
    }

    @Override
    public AppResource getByIdRel(Long resourceId) {
        AppResourceParam param = new AppResourceParam();
        param.setResourceId(resourceId);
        List<AppResource> list = baseMapper.selectListRel(param);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppResource addResource(AppResource resource, Integer userId) {
        resource.setUserId(userId);
        resource.setDeleted(0);
        resource.setCreateTime(LocalDateTime.now());
        resource.setUpdateTime(LocalDateTime.now());
        // 默认状态
        if (resource.getStatus() == null) {
            resource.setStatus("running");
        }
        save(resource);
        log.info("新增资源成功, type={}, name={}, userId={}", resource.getResourceType(), resource.getName(), userId);
        return resource;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppResource updateResource(AppResource resource) {
        if (resource.getResourceId() == null) {
            throw new RuntimeException("资源ID不能为空");
        }
        resource.setUpdateTime(LocalDateTime.now());
        updateById(resource);
        return getByIdRel(resource.getResourceId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeResource(Long resourceId, Integer userId) {
        AppResource resource = getById(resourceId);
        if (resource == null) {
            throw new RuntimeException("资源不存在");
        }
        if (!resource.getUserId().equals(userId)) {
            throw new RuntimeException("无权操作此资源");
        }
        resource.setDeleted(1);
        resource.setUpdateTime(LocalDateTime.now());
        updateById(resource);
        log.info("删除资源成功, resourceId={}, userId={}", resourceId, userId);
    }

    @Override
    public Map<String, Long> countByType(Integer userId, Integer tenantId) {
        List<Map<String, Object>> raw = baseMapper.countByType(userId, tenantId);
        Map<String, Long> result = new HashMap<>();
        // 初始化所有类型为 0
        for (String type : new String[]{"server", "database", "storage", "domain", "ssl"}) {
            result.put(type, 0L);
        }
        for (Map<String, Object> row : raw) {
            String type = (String) row.get("resourceType");
            Long cnt = ((Number) row.get("cnt")).longValue();
            result.put(type, cnt);
        }
        return result;
    }
}
