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.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.util.List;

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

    @Autowired
    private RenderWorkerMapper renderWorkerMapper;

    @Override
    public ApiResponse<PageInfo<RenderWorkerEntity>> pageQuery(RenderWorkerReqQuery renderWorkerReqQuery) {
        PageHelper.startPage(renderWorkerReqQuery.getPageNum(), renderWorkerReqQuery.getPageSize());
        List<RenderWorkerEntity> list = renderWorkerMapper.list(renderWorkerReqQuery);
        PageInfo<RenderWorkerEntity> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<RenderWorkerEntity>> list(RenderWorkerReqQuery renderWorkerReqQuery) {
        return ApiResponse.success(renderWorkerMapper.list(renderWorkerReqQuery));
    }

    @Override
    public ApiResponse<RenderWorkerEntity> detail(Long id) {
        return ApiResponse.success(renderWorkerMapper.getById(id));
    }

    @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) {
        return ApiResponse.success(renderWorkerMapper.deleteById(id));
    }

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

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