package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.RenderWorkerMapper;
import com.ycwl.basic.model.pc.renderWorker.entity.RenderWorkerEntity;
import com.ycwl.basic.model.pc.renderWorker.req.RenderWorkerReqQuery;
import com.ycwl.basic.model.task.req.ClientStatusReqVo;
import com.ycwl.basic.repository.RenderWorkerRepository;
import com.ycwl.basic.service.pc.RenderWorkerService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 15:09
 */
@Service
public class RenderWorkerServiceImpl implements RenderWorkerService {

    @Autowired
    private RenderWorkerMapper renderWorkerMapper;
    @Autowired
    private RenderWorkerRepository renderWorkerRepository;

    @Override
    public ApiResponse<PageInfo<RenderWorkerEntity>> pageQuery(RenderWorkerReqQuery renderWorkerReqQuery) {
        PageHelper.startPage(renderWorkerReqQuery.getPageNum(), renderWorkerReqQuery.getPageSize());
        List<RenderWorkerEntity> list = renderWorkerMapper.list(renderWorkerReqQuery);
        list.forEach(worker -> {
            ClientStatusReqVo clientStatus = renderWorkerRepository.getWorkerHostStatus(worker.getId());
            if (clientStatus == null) {
                return;
            }
            worker.setCpuCount(clientStatus.getCpu_count());
            worker.setCpuUsage(clientStatus.getCpu_usage());
            // 上报的是字节，存储的是兆
            worker.setMemoryAvailable(clientStatus.getMemory_available().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));
            worker.setMemoryTotal(clientStatus.getMemory_total().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));

            worker.setPlatform(clientStatus.getPlatform());
            worker.setRuntimeVersion(clientStatus.getRuntime_version());
            worker.setSupportFeature(String.join(",", clientStatus.getSupport_feature()));
            worker.setVersion(clientStatus.getVersion());
            worker.setUpdateAt(clientStatus.getUpdateAt());
        });
        PageInfo<RenderWorkerEntity> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<RenderWorkerEntity>> list(RenderWorkerReqQuery renderWorkerReqQuery) {
        List<RenderWorkerEntity> list = renderWorkerMapper.list(renderWorkerReqQuery);
        list.forEach(worker -> {
            ClientStatusReqVo clientStatus = renderWorkerRepository.getWorkerHostStatus(worker.getId());
            if (clientStatus == null) {
                return;
            }
            worker.setCpuCount(clientStatus.getCpu_count());
            worker.setCpuUsage(clientStatus.getCpu_usage());
            // 上报的是字节，存储的是兆
            worker.setMemoryAvailable(clientStatus.getMemory_available().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));
            worker.setMemoryTotal(clientStatus.getMemory_total().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));

            worker.setPlatform(clientStatus.getPlatform());
            worker.setRuntimeVersion(clientStatus.getRuntime_version());
            worker.setSupportFeature(String.join(",", clientStatus.getSupport_feature()));
            worker.setVersion(clientStatus.getVersion());
            worker.setUpdateAt(clientStatus.getUpdateAt());
        });
        return ApiResponse.success(list);
    }

    @Override
    public ApiResponse<RenderWorkerEntity> detail(Long id) {
        RenderWorkerEntity worker = renderWorkerMapper.getById(id);

        ClientStatusReqVo clientStatus = renderWorkerRepository.getWorkerHostStatus(worker.getId());
        if (clientStatus != null) {
            worker.setCpuCount(clientStatus.getCpu_count());
            worker.setCpuUsage(clientStatus.getCpu_usage());
            // 上报的是字节，存储的是兆
            worker.setMemoryAvailable(clientStatus.getMemory_available().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));
            worker.setMemoryTotal(clientStatus.getMemory_total().divide(BigDecimal.valueOf(1024 * 1024), RoundingMode.CEILING));

            worker.setPlatform(clientStatus.getPlatform());
            worker.setRuntimeVersion(clientStatus.getRuntime_version());
            worker.setSupportFeature(String.join(",", clientStatus.getSupport_feature()));
            worker.setVersion(clientStatus.getVersion());
            worker.setUpdateAt(clientStatus.getUpdateAt());
        }
        return ApiResponse.success(worker);
    }

    @Override
    public ApiResponse<Integer> add(RenderWorkerEntity renderWorker) {
        renderWorker.setId(SnowFlakeUtil.getLongId());
        if (StringUtils.isEmpty(renderWorker.getAccessKey())) {
            renderWorker.setAccessKey(SnowFlakeUtil.getId());
        }
        renderWorker.setStatus(0);
        int add = renderWorkerMapper.add(renderWorker);
        if (add == 0) {
            return ApiResponse.fail("渲染机添加失败");
        }else {
            return ApiResponse.success(add);
        }
    }

    @Override
    public ApiResponse<Integer> deleteById(Long id) {
        renderWorkerRepository.clearCache(id);
        return ApiResponse.success(renderWorkerMapper.deleteById(id));
    }

    @Override
    public ApiResponse<Integer> update(RenderWorkerEntity renderWorker) {
        renderWorkerRepository.clearCache(renderWorker.getId());
        int update = renderWorkerMapper.update(renderWorker);
        if (update == 0) {
            return ApiResponse.fail("渲染机修改失败");
        }else {
            return ApiResponse.success(update);
        }
    }

    @Override
    public ApiResponse<Integer> updateStatus(Long id) {
        renderWorkerRepository.clearCache(id);
        return ApiResponse.success(renderWorkerMapper.updateStatus(id));
    }
}
