package cn.iocoder.yudao.module.system.service.tenantpoints;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
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.module.system.controller.admin.tenantpoints.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantpoints.TenantPointsDO;
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 cn.iocoder.yudao.module.system.dal.mysql.tenantpoints.TenantPointsMapper;

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.module.system.enums.ErrorCodeConstants.*;

/**
 * 租户积分记录 Service 实现类
 *
 * @author 总后台
 */
@Service
@Validated
public class TenantPointsServiceImpl implements TenantPointsService {

    @Resource
    private TenantPointsMapper tenantPointsMapper;

    @Override
    public Long createTenantPoints(TenantPointsSaveReqVO createReqVO) {
        // 插入
        TenantPointsDO tenantPoints = BeanUtils.toBean(createReqVO, TenantPointsDO.class);
        tenantPointsMapper.insert(tenantPoints);
        // 返回
        return tenantPoints.getId();
    }

    @Override
    public void updateTenantPoints(TenantPointsSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantPointsExists(updateReqVO.getId());
        // 更新
        TenantPointsDO updateObj = BeanUtils.toBean(updateReqVO, TenantPointsDO.class);
        tenantPointsMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantPoints(Long id) {
        // 校验存在
        validateTenantPointsExists(id);
        // 删除
        tenantPointsMapper.deleteById(id);
    }

    @Override
        public void deleteTenantPointsListByIds(List<Long> ids) {
        // 校验存在
        validateTenantPointsExists(ids);
        // 删除
        tenantPointsMapper.deleteByIds(ids);
        }

    private void validateTenantPointsExists(List<Long> ids) {
        List<TenantPointsDO> list = tenantPointsMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TENANT_POINTS_NOT_EXISTS);
        }
    }

    private void validateTenantPointsExists(Long id) {
        if (tenantPointsMapper.selectById(id) == null) {
            throw exception(TENANT_POINTS_NOT_EXISTS);
        }
    }

    @Override
    public TenantPointsDO getTenantPoints(Long id) {
        return tenantPointsMapper.selectById(id);
    }

    @Override
    public PageResult<TenantPointsPageRespVO> getTenantPointsPage(TenantPointsPageReqVO pageReqVO) {
        Long tenantId = TenantContextHolder.getTenantId();

        if (tenantId == 1) {
            IPage<TenantPointsPageRespVO> iPage = new Page<>(pageReqVO.getPageNo(),pageReqVO.getPageSize());
            IPage<TenantPointsPageRespVO> tenantPointsPageRespVOIPage = tenantPointsMapper.selectPageWithXML(iPage, pageReqVO);
            return new  PageResult<>(tenantPointsPageRespVOIPage.getRecords(),tenantPointsPageRespVOIPage.getTotal());
        }

        IPage<TenantPointsPageRespVO> iPage = new Page<>(pageReqVO.getPageNo(),pageReqVO.getPageSize());
        IPage<TenantPointsPageRespVO> tenantPointsPageRespVOIPage = tenantPointsMapper.selectPageWithTenantIdXML(iPage,pageReqVO,tenantId);
        return new  PageResult<>(tenantPointsPageRespVOIPage.getRecords(),tenantPointsPageRespVOIPage.getTotal());

    }

    @Override
    public PageResult<TenantPointsDO> getTenantTransactionHistoryPointsPage(PageParam pageReqVO, Long tenantId) {
        return tenantPointsMapper.TenantTransactionHistoryPointsPage(pageReqVO,tenantId);
    }

}