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

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.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.controller.admin.erp.vo.WarehouseFactoryPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.WarehouseFactoryRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.WarehouseFactorySaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.WarehouseFactoryDO;
import com.zt.plat.module.erp.dal.mysql.erp.WarehouseFactoryMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.WAREHOUSE_FACTORY_NOT_EXISTS;


/**
 * 仓库工厂关系 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class WarehouseFactoryServiceImpl implements WarehouseFactoryService {

    @Resource
    private WarehouseFactoryMapper warehouseFactoryMapper;

    @Override
    public WarehouseFactoryRespVO createWarehouseFactory(WarehouseFactorySaveReqVO createReqVO) {
        // 插入
        WarehouseFactoryDO warehouseFactory = BeanUtils.toBean(createReqVO, WarehouseFactoryDO.class);
        warehouseFactoryMapper.insert(warehouseFactory);
        // 返回
        return BeanUtils.toBean(warehouseFactory, WarehouseFactoryRespVO.class);
    }

    @Override
    public void updateWarehouseFactory(WarehouseFactorySaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseFactoryExists(updateReqVO.getId());
        // 更新
        WarehouseFactoryDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseFactoryDO.class);
        warehouseFactoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseFactory(Long id) {
        // 校验存在
        validateWarehouseFactoryExists(id);
        // 删除
        warehouseFactoryMapper.deleteById(id);
    }

    @Override
    public void deleteWarehouseFactoryListByIds(List<Long> ids) {
        // 校验存在
        validateWarehouseFactoryExists(ids);
        // 删除
        warehouseFactoryMapper.deleteByIds(ids);
    }

    private void validateWarehouseFactoryExists(List<Long> ids) {
        List<WarehouseFactoryDO> list = warehouseFactoryMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WAREHOUSE_FACTORY_NOT_EXISTS);
        }
    }

    private void validateWarehouseFactoryExists(Long id) {
        if (warehouseFactoryMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_FACTORY_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseFactoryDO getWarehouseFactory(Long id) {
        return warehouseFactoryMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseFactoryDO> getWarehouseFactoryPage(WarehouseFactoryPageReqVO pageReqVO) {
        return warehouseFactoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseFactoryRespVO> getWarehouseFactoryByMainId(String mainId) {
        List<WarehouseFactoryDO> warehouseFactoryDOS = warehouseFactoryMapper.selectList(WarehouseFactoryDO::getMainWarehouseId, mainId);
        List<WarehouseFactoryRespVO> warehouseFactoryRespVOS = new ArrayList<>();
        for (WarehouseFactoryDO warehouseFactoryDO : warehouseFactoryDOS) {
            WarehouseFactoryRespVO bean = BeanUtils.toBean(warehouseFactoryDO, WarehouseFactoryRespVO.class);
            bean.setMmsiType(warehouseFactoryDO.getMmsiType());
            warehouseFactoryRespVOS.add(bean);
        }
        return warehouseFactoryRespVOS;
    }

    @Override
    public List<WarehouseFactoryRespVO> getWarehouseFactoryByParams(Long mainId, String mmsiType, String operationType) {
        return BeanUtils.toBean(
                warehouseFactoryMapper.selectList(new LambdaQueryWrapperX<WarehouseFactoryDO>()
                        .eq(WarehouseFactoryDO::getMainWarehouseId, mainId)
                        .eqIfPresent(WarehouseFactoryDO::getMmsiType, mmsiType)
                        .eqIfPresent(WarehouseFactoryDO::getOperationType, operationType)),
                WarehouseFactoryRespVO.class);
    }

}