package com.zt.plat.module.base.service.businessdimension;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import com.zt.plat.module.base.controller.admin.businessdimension.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessdimension.BusinessDimensionDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.dal.dao.businessdimension.BusinessDimensionMapper;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.*;

/**
 * 经营指标维度 Service 实现类
 *
 * @author 陈鹏
 */
@Service
@Validated
public class BusinessDimensionServiceImpl implements BusinessDimensionService {

    @Resource
    private BusinessDimensionMapper businessDimensionMapper;

    @Override
    public BusinessDimensionRespVO createBusinessDimension(BusinessDimensionSaveReqVO createReqVO) {
        // 插入
        BusinessDimensionDO businessDimension = BeanUtils.toBean(createReqVO, BusinessDimensionDO.class);
        businessDimensionMapper.insert(businessDimension);
        // 返回
        return BeanUtils.toBean(businessDimension, BusinessDimensionRespVO.class);
    }

    @Override
    public void updateBusinessDimension(BusinessDimensionSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessDimensionExists(updateReqVO.getId());
        // 更新
        BusinessDimensionDO updateObj = BeanUtils.toBean(updateReqVO, BusinessDimensionDO.class);
        businessDimensionMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessDimension(Long id) {
        // 校验存在
        validateBusinessDimensionExists(id);
        // 删除
        businessDimensionMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessDimensionListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessDimensionExists(ids);
        // 删除
        businessDimensionMapper.deleteByIds(ids);
        }

    private void validateBusinessDimensionExists(List<Long> ids) {
        List<BusinessDimensionDO> list = businessDimensionMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_DIMENSION_NOT_EXISTS);
        }
    }

    private void validateBusinessDimensionExists(Long id) {
        if (businessDimensionMapper.selectById(id) == null) {
            throw exception(BUSINESS_DIMENSION_NOT_EXISTS);
        }
    }

    @Override
    public BusinessDimensionDO getBusinessDimension(Long id) {
        return businessDimensionMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessDimensionDO> getBusinessDimensionPage(BusinessDimensionPageReqVO pageReqVO) {
        return businessDimensionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BusinessDimensionDO> getBusinessDimensionListByParentId(Long parentId, Integer level) {
        return businessDimensionMapper.selectListByParentId(parentId, level);
    }

}