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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.MaterialInventoryCheckItemPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckItemRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckItemSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryCheckDetailDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryCheckItemDO;
import com.zt.plat.module.qms.resource.material.dal.mapper.MaterialInventoryCheckItemMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
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_CHECK_ITEM_NOT_EXISTS;

/**
 * 库存盘点项 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialInventoryCheckItemServiceImpl implements MaterialInventoryCheckItemService {

    @Resource
    private MaterialInventoryCheckItemMapper materialInventoryCheckItemMapper;

    @Override
    public MaterialInventoryCheckItemRespVO createMaterialInventoryCheckItem(MaterialInventoryCheckItemSaveReqVO createReqVO) {
        // 插入
        MaterialInventoryCheckItemDO materialInventoryCheckItem = BeanUtils.toBean(createReqVO, MaterialInventoryCheckItemDO.class);
        materialInventoryCheckItemMapper.insert(materialInventoryCheckItem);
        // 返回
        return BeanUtils.toBean(materialInventoryCheckItem, MaterialInventoryCheckItemRespVO.class);
    }

    @Override
    public void updateMaterialInventoryCheckItem(MaterialInventoryCheckItemSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialInventoryCheckItemExists(updateReqVO.getId());
        // 更新
        MaterialInventoryCheckItemDO updateObj = BeanUtils.toBean(updateReqVO, MaterialInventoryCheckItemDO.class);
        materialInventoryCheckItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialInventoryCheckItem(Long id) {
        // 校验存在
        validateMaterialInventoryCheckItemExists(id);
        // 删除
        materialInventoryCheckItemMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialInventoryCheckItemListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialInventoryCheckItemExists(ids);
        // 删除
        materialInventoryCheckItemMapper.deleteByIds(ids);
        }

    private void validateMaterialInventoryCheckItemExists(List<Long> ids) {
        List<MaterialInventoryCheckItemDO> list = materialInventoryCheckItemMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_INVENTORY_CHECK_ITEM_NOT_EXISTS);
        }
    }

    private void validateMaterialInventoryCheckItemExists(Long id) {
        if (materialInventoryCheckItemMapper.selectById(id) == null) {
            throw exception(MATERIAL_INVENTORY_CHECK_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public MaterialInventoryCheckItemDO getMaterialInventoryCheckItem(Long id) {
        return materialInventoryCheckItemMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialInventoryCheckItemDO> getMaterialInventoryCheckItemPage(MaterialInventoryCheckItemPageReqVO pageReqVO) {
        return materialInventoryCheckItemMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<MaterialInventoryCheckItemDO> checkItemDOS) {
        if (CollUtil.isEmpty(checkItemDOS)) return;
        materialInventoryCheckItemMapper.insertBatch(checkItemDOS);
    }

    @Override
    public List<MaterialInventoryCheckItemRespVO> getListWithPdtInfoByLfcId(Long lfcId) {
        return materialInventoryCheckItemMapper.selectListWithPdtInfoByLfcId(lfcId);
    }

    @Override
    public void deleteItemsByLfcId(Long lfcId) {
        materialInventoryCheckItemMapper.delete(Wrappers.lambdaQuery(MaterialInventoryCheckItemDO.class)
                .eq(MaterialInventoryCheckItemDO::getParentId, lfcId));
    }

    @Override
    public MaterialInventoryCheckItemDO getItemByCheckIdAndPdtId(Long checkId, Long productId) {

        return materialInventoryCheckItemMapper.selectOne(Wrappers.lambdaQuery(MaterialInventoryCheckItemDO.class)
                .eq(MaterialInventoryCheckItemDO::getParentId, checkId)
                .eq(MaterialInventoryCheckItemDO::getProductId, productId));
    }

    @Override
    public void updateById(MaterialInventoryCheckItemDO item) {
        materialInventoryCheckItemMapper.updateById(item);
    }

    @Override
    public List<MaterialInventoryCheckItemDO> getListByLfcId(Long id) {
        return materialInventoryCheckItemMapper.selectList(Wrappers.lambdaQuery(MaterialInventoryCheckItemDO.class)
                .eq(MaterialInventoryCheckItemDO::getParentId, id));
    }

    @Override
    public void updateBatch(List<MaterialInventoryCheckItemDO> items) {
        materialInventoryCheckItemMapper.updateBatch(items);
    }

    @Override
    public void save(MaterialInventoryCheckItemDO item) {
        materialInventoryCheckItemMapper.insert(item);
    }

}