package cn.iocoder.yudao.module.tkdata.service.newhosts;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.AllocationHostsSaveReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.NewHostsPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.NewHostsSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.newhosts.NewHostsDO;
import cn.iocoder.yudao.module.tkdata.dal.mysql.newhosts.NewHostsMapper;
import cn.iocoder.yudao.module.tkdata.enums.ErrorCodeConstants;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 主播数据 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class NewHostsServiceImpl implements NewHostsService {

    @Resource
    private NewHostsMapper newHostsMapper;

    @Override
    public Long createNewHosts(NewHostsSaveReqVO createReqVO) {
        // 插入
        NewHostsDO newHosts = BeanUtils.toBean(createReqVO, NewHostsDO.class);
        newHostsMapper.insert(newHosts);
        // 返回
        return newHosts.getId();
    }

    @Override
    public void updateNewHosts(NewHostsSaveReqVO updateReqVO) {
        // 校验存在
        validateNewHostsExists(updateReqVO.getId());
        // 更新
        NewHostsDO updateObj = BeanUtils.toBean(updateReqVO, NewHostsDO.class);
        newHostsMapper.updateById(updateObj);
    }

    @Override
    public void deleteNewHosts(Long id) {
        // 校验存在
        validateNewHostsExists(id);
        // 删除
        newHostsMapper.deleteById(id);
    }

    @Override
        public void deleteNewHostsListByIds(List<Long> ids) {
        // 校验存在
        validateNewHostsExists(ids);
        // 删除
        newHostsMapper.deleteByIds(ids);
        }

    private void validateNewHostsExists(List<Long> ids) {
        List<NewHostsDO> list = newHostsMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ErrorCodeConstants.NEW_HOSTS_NOT_EXISTS);
        }
    }

    private void validateNewHostsExists(Long id) {
        if (newHostsMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.NEW_HOSTS_NOT_EXISTS);
        }
    }

    @Override
    public NewHostsDO getNewHosts(Long id) {
        return newHostsMapper.selectById(id);
    }

    @Override
    public PageResult<NewHostsDO> getNewHostsPage(NewHostsPageReqVO pageReqVO) {
        pageReqVO.setTenantId(TenantContextHolder.getTenantId());
        IPage<NewHostsDO> iPage = new Page<>(pageReqVO.getPageNo(),pageReqVO.getPageSize());
        IPage<NewHostsDO> newHostsDOIPage = newHostsMapper.selectPageWithXML(iPage,pageReqVO);
        return new  PageResult<>(newHostsDOIPage.getRecords(),newHostsDOIPage.getTotal());
    }

    @Override
    public void batchUpdateNewHosts(List<NewHostsSaveReqVO> updateReqVO) {
        ArrayList<NewHostsDO> newHostsDOS = new ArrayList<>();
        for (NewHostsSaveReqVO newHostsSaveReqVO : updateReqVO) {
            NewHostsDO newHostsDO = new NewHostsDO();
            BeanUtils.copyProperties(newHostsSaveReqVO,newHostsDO);
            newHostsDOS.add(newHostsDO);
        }
        newHostsMapper.updateBatch(newHostsDOS);
    }

    @Override
    public void deleteExportData(List<NewHostsDO> list) {
        newHostsMapper.deleteBatchIds(list);
    }

}