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.base.controller.admin.base.vo.MaterialDestroyPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialDestroyRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialDestroySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.MaterialDestroyDO;
import com.zt.plat.module.base.dal.mysql.base.MaterialDestroyMapper;
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_DESTROY_NOT_EXISTS;

/**
 * 物料回收率 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialDestroyServiceImpl implements MaterialDestroyService {

    @Resource
    private MaterialDestroyMapper materialDestroyMapper;

    @Override
    public MaterialDestroyRespVO createMaterialDestroy(MaterialDestroySaveReqVO createReqVO) {
        // 插入
        MaterialDestroyDO materialDestroy = BeanUtils.toBean(createReqVO, MaterialDestroyDO.class);
        if (materialDestroy.getIsEnable()==null||materialDestroy.getIsEnable().isEmpty()) {
            materialDestroy.setIsEnable("1");
        }
        materialDestroyMapper.insert(materialDestroy);
        // 返回
        return BeanUtils.toBean(materialDestroy, MaterialDestroyRespVO.class);
    }

    @Override
    public void updateMaterialDestroy(MaterialDestroySaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialDestroyExists(updateReqVO.getId());
        // 更新
        MaterialDestroyDO updateObj = BeanUtils.toBean(updateReqVO, MaterialDestroyDO.class);
        materialDestroyMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialDestroy(Long id) {
        // 校验存在
        validateMaterialDestroyExists(id);
        // 删除
        materialDestroyMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialDestroyListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialDestroyExists(ids);
        // 删除
        materialDestroyMapper.deleteByIds(ids);
        }

    private void validateMaterialDestroyExists(List<Long> ids) {
        List<MaterialDestroyDO> list = materialDestroyMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_DESTROY_NOT_EXISTS);
        }
    }

    private void validateMaterialDestroyExists(Long id) {
        if (materialDestroyMapper.selectById(id) == null) {
            throw exception(MATERIAL_DESTROY_NOT_EXISTS);
        }
    }

    @Override
    public MaterialDestroyDO getMaterialDestroy(Long id) {
        return materialDestroyMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialDestroyDO> getMaterialDestroyPage(MaterialDestroyPageReqVO pageReqVO) {
        return materialDestroyMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableMaterialDestroyList(List<MaterialDestroyRespVO> saveReqVOS) {
        List<MaterialDestroyDO> updateObj = BeanUtils.toBean(saveReqVOS, MaterialDestroyDO.class);
        List<BatchResult> count =  materialDestroyMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(MATERIAL_DESTROY_NOT_EXISTS);
        }
    }

}