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

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.businessdepartmentindicator.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessdepartmentindicator.BusinessDepartmentIndicatorDO;
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.businessdepartmentindicator.BusinessDepartmentIndicatorMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.*;

/**
 * 部门持有指标 Service 实现类
 *
 * @author YangXiaofeng
 */
@Service
@Validated
public class BusinessDepartmentIndicatorServiceImpl implements BusinessDepartmentIndicatorService {

    @Resource
    private BusinessDepartmentIndicatorMapper businessDepartmentIndicatorMapper;

    @Override
    public BusinessDepartmentIndicatorRespVO createBusinessDepartmentIndicator(BusinessDepartmentIndicatorSaveReqVO createReqVO) {
        // 插入
        BusinessDepartmentIndicatorDO businessDepartmentIndicator = BeanUtils.toBean(createReqVO, BusinessDepartmentIndicatorDO.class);
        businessDepartmentIndicatorMapper.insert(businessDepartmentIndicator);
        // 返回
        return BeanUtils.toBean(businessDepartmentIndicator, BusinessDepartmentIndicatorRespVO.class);
    }

    @Override
    public void updateBusinessDepartmentIndicator(BusinessDepartmentIndicatorSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessDepartmentIndicatorExists(updateReqVO.getId());
        // 更新
        BusinessDepartmentIndicatorDO updateObj = BeanUtils.toBean(updateReqVO, BusinessDepartmentIndicatorDO.class);
        businessDepartmentIndicatorMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessDepartmentIndicator(Long id) {
        // 校验存在
        validateBusinessDepartmentIndicatorExists(id);
        // 删除
        businessDepartmentIndicatorMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessDepartmentIndicatorListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessDepartmentIndicatorExists(ids);
        // 删除
        businessDepartmentIndicatorMapper.deleteByIds(ids);
        }

    private void validateBusinessDepartmentIndicatorExists(List<Long> ids) {
        List<BusinessDepartmentIndicatorDO> list = businessDepartmentIndicatorMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_DEPARTMENT_INDICATOR_NOT_EXISTS);
        }
    }

    private void validateBusinessDepartmentIndicatorExists(Long id) {
        if (businessDepartmentIndicatorMapper.selectById(id) == null) {
            throw exception(BUSINESS_DEPARTMENT_INDICATOR_NOT_EXISTS);
        }
    }

    @Override
    public BusinessDepartmentIndicatorDO getBusinessDepartmentIndicator(Long id) {
        return businessDepartmentIndicatorMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessDepartmentIndicatorDO> getBusinessDepartmentIndicatorPage(BusinessDepartmentIndicatorPageReqVO pageReqVO) {
        return businessDepartmentIndicatorMapper.selectPage(pageReqVO);
    }

}