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

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.api.dto.MaterialOtherDTO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.MaterialOtherDO;
import com.zt.plat.module.base.dal.mysql.base.MaterialOtherMapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
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.base.enums.ErrorCodeConstants.MATERIAL_OTHER_NOT_EXISTS;

/**
 * 物料拓展数据 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialOtherServiceImpl implements MaterialOtherService {

    @Resource
    private MaterialOtherMapper materialOtherMapper;

    @Override
    public MaterialOtherRespVO createMaterialOther(MaterialOtherSaveReqVO createReqVO) {
        // 插入
        MaterialOtherDO materialOther = BeanUtils.toBean(createReqVO, MaterialOtherDO.class);
        materialOtherMapper.insert(materialOther);
        // 返回
        return BeanUtils.toBean(materialOther, MaterialOtherRespVO.class);
    }

    @Override
    public void updateMaterialOther(MaterialOtherSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialOtherExists(updateReqVO.getId());
        // 更新
        MaterialOtherDO updateObj = BeanUtils.toBean(updateReqVO, MaterialOtherDO.class);
        materialOtherMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialOther(Long id) {
        // 校验存在
        validateMaterialOtherExists(id);
        // 删除
        materialOtherMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialOtherListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialOtherExists(ids);
        // 删除
        materialOtherMapper.deleteByIds(ids);
        }

    private void validateMaterialOtherExists(List<Long> ids) {
        List<MaterialOtherDO> list = materialOtherMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_OTHER_NOT_EXISTS);
        }
    }

    private void validateMaterialOtherExists(Long id) {
        if (materialOtherMapper.selectById(id) == null) {
            throw exception(MATERIAL_OTHER_NOT_EXISTS);
        }
    }

    @Override
    public MaterialOtherDO getMaterialOther(Long id) {
        return materialOtherMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialOtherDO> getMaterialOtherPage(MaterialOtherPageReqVO pageReqVO) {
        return materialOtherMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableMaterialOtherList(List<MaterialOtherRespVO> saveReqVOS) {
        List<MaterialOtherDO> updateObj = BeanUtils.toBean(saveReqVOS, MaterialOtherDO.class);
        List<BatchResult> count =  materialOtherMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(MATERIAL_OTHER_NOT_EXISTS);
        }
    }

    @Override
    public List<MaterialOtherDTO> getMaterialOtherNoPage(MaterialOtherDTO dto) {
        List<MaterialOtherDO> list = materialOtherMapper.selectList(dto);
        return BeanUtils.toBean(list, MaterialOtherDTO.class);
    }

}