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

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.untInfo.vo.*;
import com.zt.plat.module.base.dal.dataobject.untInfo.UntInfoDO;
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.untInfo.UntInfoMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.unitmanagement.enums.UnitErrorCodeConstants.*;

/**
 * 计量单位 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class UntInfoServiceImpl implements UntInfoService {

    @Resource
    private UntInfoMapper untInfoMapper;

    @Override
    public UntInfoRespVO createUntInfo(UntInfoSaveReqVO createReqVO) {
        // 插入
        UntInfoDO untInfo = BeanUtils.toBean(createReqVO, UntInfoDO.class);
        untInfoMapper.insert(untInfo);
        // 返回
        return BeanUtils.toBean(untInfo, UntInfoRespVO.class);
    }

    @Override
    public void updateUntInfo(UntInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateUntInfoExists(updateReqVO.getId());
        // 更新
        UntInfoDO updateObj = BeanUtils.toBean(updateReqVO, UntInfoDO.class);
        untInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteUntInfo(Long id) {
        // 校验存在
        validateUntInfoExists(id);
        // 删除
        untInfoMapper.deleteById(id);
    }

    @Override
        public void deleteUntInfoListByIds(List<Long> ids) {
        // 校验存在
        validateUntInfoExists(ids);
        // 删除
        untInfoMapper.deleteByIds(ids);
        }

    private void validateUntInfoExists(List<Long> ids) {
        List<UntInfoDO> list = untInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(UNT_INFO_NOT_EXISTS);
        }
    }

    private void validateUntInfoExists(Long id) {
        if (untInfoMapper.selectById(id) == null) {
            throw exception(UNT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public UntInfoDO getUntInfo(Long id) {
        return untInfoMapper.selectById(id);
    }

    @Override
    public PageResult<UntInfoDO> getUntInfoPage(UntInfoPageReqVO pageReqVO) {
        return untInfoMapper.selectPage(pageReqVO);
    }

}