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.MaterialInventoryOutboundDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryOutboundDetailDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialInventoryOutboundDetailMapper;
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.MATERIAL_INVENTORY_OUTBOUND_DETAIL_NOT_EXISTS;

/**
 * 出库明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialInventoryOutboundDetailServiceImpl implements MaterialInventoryOutboundDetailService {

    @Resource
    private MaterialInventoryOutboundDetailMapper materialInventoryOutboundDetailMapper;

    @Override
    public MaterialInventoryOutboundDetailRespVO createMaterialInventoryOutboundDetail(MaterialInventoryOutboundDetailSaveReqVO createReqVO) {
        // 插入
        MaterialInventoryOutboundDetailDO materialInventoryOutboundDetail = BeanUtils.toBean(createReqVO, MaterialInventoryOutboundDetailDO.class);
        materialInventoryOutboundDetailMapper.insert(materialInventoryOutboundDetail);
        // 返回
        return BeanUtils.toBean(materialInventoryOutboundDetail, MaterialInventoryOutboundDetailRespVO.class);
    }

    @Override
    public void updateMaterialInventoryOutboundDetail(MaterialInventoryOutboundDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialInventoryOutboundDetailExists(updateReqVO.getId());
        // 更新
        MaterialInventoryOutboundDetailDO updateObj = BeanUtils.toBean(updateReqVO, MaterialInventoryOutboundDetailDO.class);
        materialInventoryOutboundDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialInventoryOutboundDetail(Long id) {
        // 校验存在
        validateMaterialInventoryOutboundDetailExists(id);
        // 删除
        materialInventoryOutboundDetailMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialInventoryOutboundDetailListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialInventoryOutboundDetailExists(ids);
        // 删除
        materialInventoryOutboundDetailMapper.deleteByIds(ids);
        }

    private void validateMaterialInventoryOutboundDetailExists(List<Long> ids) {
        List<MaterialInventoryOutboundDetailDO> list = materialInventoryOutboundDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_INVENTORY_OUTBOUND_DETAIL_NOT_EXISTS);
        }
    }

    private void validateMaterialInventoryOutboundDetailExists(Long id) {
        if (materialInventoryOutboundDetailMapper.selectById(id) == null) {
            throw exception(MATERIAL_INVENTORY_OUTBOUND_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialInventoryOutboundDetailDO getMaterialInventoryOutboundDetail(Long id) {
        return materialInventoryOutboundDetailMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialInventoryOutboundDetailDO> getMaterialInventoryOutboundDetailPage(MaterialInventoryOutboundDetailPageReqVO pageReqVO) {
        return materialInventoryOutboundDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<MaterialInventoryOutboundDetailDO> outboundDetailDOS) {
        materialInventoryOutboundDetailMapper.insertBatch(outboundDetailDOS);
    }

    @Override
    public PageResult<MaterialInventoryOutboundDetailRespVO> getOutboundDetailPageWithMaterialInfo(MaterialInventoryOutboundDetailPageReqVO pageReqVO) {

        return materialInventoryOutboundDetailMapper.selectPageWithMaterialInfo(pageReqVO);
    }

}