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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother.vo.BigBrotherPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother.vo.BigBrotherSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.bigbrother.BigBrotherDO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.employeehosts.EmployeeHostsDO;
import cn.iocoder.yudao.module.tkdata.dal.mysql.bigbrother.BigBrotherMapper;
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 org.springframework.transaction.annotation.Transactional;

import java.util.*;

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


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.tkdata.enums.ErrorCodeConstants.BIG_BROTHER_NOT_EXISTS;


/**
 * 大哥数据 Service 实现类
 *
 * @author 总后台
 */
@Service
@Validated
public class BigBrotherServiceImpl implements BigBrotherService {

    @Resource
    private BigBrotherMapper bigBrotherMapper;

    @Override
    public Integer createBigBrother(BigBrotherSaveReqVO createReqVO) {
        // 插入
        BigBrotherDO bigBrother = BeanUtils.toBean(createReqVO, BigBrotherDO.class);
        bigBrotherMapper.insert(bigBrother);
        // 返回
        return bigBrother.getId();
    }

    @Override
    public void updateBigBrother(BigBrotherSaveReqVO updateReqVO) {
        // 校验存在
        validateBigBrotherExists(updateReqVO.getId());
        // 更新
        BigBrotherDO updateObj = BeanUtils.toBean(updateReqVO, BigBrotherDO.class);
        bigBrotherMapper.updateById(updateObj);
    }

    @Override
    public void deleteBigBrother(Integer id) {
        // 校验存在
        validateBigBrotherExists(id);
        // 删除
        bigBrotherMapper.deleteById(id);
    }

    @Override
        public void deleteBigBrotherListByIds(List<Integer> ids) {
        // 校验存在
        validateBigBrotherExists(ids);
        // 删除
        bigBrotherMapper.deleteByIds(ids);
        }

    private void validateBigBrotherExists(List<Integer> ids) {
        List<BigBrotherDO> list = bigBrotherMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BIG_BROTHER_NOT_EXISTS);
        }
    }

    private void validateBigBrotherExists(Integer id) {
        if (bigBrotherMapper.selectById(id) == null) {
            throw exception(BIG_BROTHER_NOT_EXISTS);
        }
    }

    @Override
    public BigBrotherDO getBigBrother(Integer id) {
        return bigBrotherMapper.selectById(id);
    }

    @Override
    public PageResult<BigBrotherDO> getBigBrotherPage(BigBrotherPageReqVO pageReqVO) {
        pageReqVO.setTenantId(TenantContextHolder.getTenantId());
        IPage<BigBrotherDO> iPage = new Page<>(pageReqVO.getPageNo(),pageReqVO.getPageSize());
        IPage<BigBrotherDO> bigBrotherDOIPage = bigBrotherMapper.selectPageWithXML(iPage, pageReqVO);
        return new  PageResult<>(bigBrotherDOIPage.getRecords(),bigBrotherDOIPage.getTotal());
    }

    @Override
    public void deleteExportData(List<BigBrotherDO> list) {
        bigBrotherMapper.deleteByIds(list);
    }

}