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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.materialgradeext.vo.MaterialGradeExtPageReqVO;
import com.zt.plat.module.base.controller.admin.materialgradeext.vo.MaterialGradeExtRespVO;
import com.zt.plat.module.base.controller.admin.materialgradeext.vo.MaterialGradeExtSaveReqVO;
import com.zt.plat.module.base.dal.dao.materialgradeext.MaterialGradeExtMapper;
import com.zt.plat.module.base.dal.dataobject.materialgradeext.MaterialGradeExtDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collections;
import java.util.List;

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

/**
 * 物料牌号扩展 Service 实现
 */
@Service
@Validated
public class MaterialGradeExtServiceImpl implements MaterialGradeExtService {

    @Resource
    private MaterialGradeExtMapper materialGradeExtMapper;

    @Override
    public MaterialGradeExtRespVO create(MaterialGradeExtSaveReqVO reqVO) {
        MaterialGradeExtDO entity = BeanUtils.toBean(reqVO, MaterialGradeExtDO.class);
        materialGradeExtMapper.insert(entity);
        return BeanUtils.toBean(entity, MaterialGradeExtRespVO.class);
    }

    @Override
    public void update(MaterialGradeExtSaveReqVO reqVO) {
        validateExists(reqVO.getId());
        MaterialGradeExtDO entity = BeanUtils.toBean(reqVO, MaterialGradeExtDO.class);
        materialGradeExtMapper.updateById(entity);
    }

    @Override
    public void delete(Long id) {
        validateExists(id);
        materialGradeExtMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        validateExists(ids);
        materialGradeExtMapper.deleteBatchIds(ids);
    }

    @Override
    public MaterialGradeExtRespVO get(Long id) {
        MaterialGradeExtDO entity = materialGradeExtMapper.selectById(id);
        return BeanUtils.toBean(entity, MaterialGradeExtRespVO.class);
    }

    @Override
    public PageResult<MaterialGradeExtRespVO> getPage(MaterialGradeExtPageReqVO pageReqVO) {
        PageResult<MaterialGradeExtDO> page = materialGradeExtMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(page.getList())) {
            return new PageResult<>(Collections.emptyList(), page.getTotal());
        }
        List<MaterialGradeExtRespVO> list = BeanUtils.toBean(page.getList(), MaterialGradeExtRespVO.class);
        return new PageResult<>(list, page.getTotal());
    }

    private void validateExists(Long id) {
        if (id == null || materialGradeExtMapper.selectById(id) == null) {
            throw exception(MATERIAL_GRADE_EXT_NOT_EXISTS);
        }
    }

    private void validateExists(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw exception(MATERIAL_GRADE_EXT_NOT_EXISTS);
        }
        Long count = materialGradeExtMapper.selectCount(null);
        // 快速校验: 逐个检查
        for (Long id : ids) {
            validateExists(id);
        }
    }
}