package com.zt.plat.module.qms.resource.material.service;

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.qms.resource.material.controller.vo.*;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInfomationPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInfomationRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInfomationSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInfomationDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialInfomationMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

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

/**
 * 物料实例 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialInfomationServiceImpl implements MaterialInfomationService {

    @Resource
    private MaterialInfomationMapper materialInfomationMapper;

    @Override
    public MaterialInfomationRespVO createMaterialInfomation(MaterialInfomationSaveReqVO createReqVO) {
        // 插入
        MaterialInfomationDO materialInfomation = BeanUtils.toBean(createReqVO, MaterialInfomationDO.class);
        materialInfomationMapper.insert(materialInfomation);
        // 返回
        return BeanUtils.toBean(materialInfomation, MaterialInfomationRespVO.class);
    }

    @Override
    public void updateMaterialInfomation(MaterialInfomationSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialInfomationExists(updateReqVO.getId());
        // 更新
        MaterialInfomationDO updateObj = BeanUtils.toBean(updateReqVO, MaterialInfomationDO.class);
        materialInfomationMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialInfomation(Long id) {
        // 校验存在
        validateMaterialInfomationExists(id);
        // 删除
        materialInfomationMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialInfomationListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialInfomationExists(ids);
        // 删除
        materialInfomationMapper.deleteByIds(ids);
        }

    private void validateMaterialInfomationExists(List<Long> ids) {
        List<MaterialInfomationDO> list = materialInfomationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_INFOMATION_NOT_EXISTS);
        }
    }

    private void validateMaterialInfomationExists(Long id) {
        if (materialInfomationMapper.selectById(id) == null) {
            throw exception(MATERIAL_INFOMATION_NOT_EXISTS);
        }
    }

    @Override
    public MaterialInfomationDO getMaterialInfomation(Long id) {
        return materialInfomationMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialInfomationDO> getMaterialInfomationPage(MaterialInfomationPageReqVO pageReqVO) {
        return materialInfomationMapper.selectPage(pageReqVO);
    }

}