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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.module.base.controller.admin.materialhasproperties.vo.*;
import com.zt.plat.module.base.dal.dataobject.materialhasproperties.MaterialHasPropertiesDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.base.dal.dao.materialhasproperties.MaterialHasPropertiesMapper;

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.*;

/**
 * 物料持有属性 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class MaterialHasPropertiesServiceImpl implements MaterialHasPropertiesService {

    @Resource
    private MaterialHasPropertiesMapper materialHasPropertiesMapper;

    @Override
    public MaterialHasPropertiesRespVO createMaterialHasProperties(MaterialHasPropertiesSaveReqVO createReqVO) {
        // 插入
        MaterialHasPropertiesDO materialHasProperties = BeanUtils.toBean(createReqVO, MaterialHasPropertiesDO.class);
        materialHasPropertiesMapper.insert(materialHasProperties);
        // 返回
        return BeanUtils.toBean(materialHasProperties, MaterialHasPropertiesRespVO.class);
    }

    @Override
    public void updateMaterialHasProperties(MaterialHasPropertiesSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialHasPropertiesExists(updateReqVO.getId());
        // 更新
        MaterialHasPropertiesDO updateObj = BeanUtils.toBean(updateReqVO, MaterialHasPropertiesDO.class);
        materialHasPropertiesMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialHasProperties(Long id) {
        // 校验存在
        validateMaterialHasPropertiesExists(id);
        // 删除
        materialHasPropertiesMapper.deleteById(id);
    }

    @Override
    public void deleteMaterialHasPropertiesListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialHasPropertiesExists(ids);
        // 删除
        materialHasPropertiesMapper.deleteByIds(ids);
    }

    private void validateMaterialHasPropertiesExists(List<Long> ids) {
        List<MaterialHasPropertiesDO> list = materialHasPropertiesMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_HAS_PROPERTIES_NOT_EXISTS);
        }
    }

    private void validateMaterialHasPropertiesExists(Long id) {
        if (materialHasPropertiesMapper.selectById(id) == null) {
            throw exception(MATERIAL_HAS_PROPERTIES_NOT_EXISTS);
        }
    }

    @Override
    public MaterialHasPropertiesDO getMaterialHasProperties(Long id) {
        return materialHasPropertiesMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialHasPropertiesDO> getMaterialHasPropertiesPage(MaterialHasPropertiesPageReqVO pageReqVO) {
        return materialHasPropertiesMapper.selectPage(pageReqVO);
    }

}