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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.tenant.core.aop.TenantIgnore;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseEnableDisableReqVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehousePageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseRespVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.internalwarehouse.InternalWarehouseDO;
import com.zt.plat.module.erp.dal.mysql.erp.internalwarehouse.InternalWarehouseMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;


import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.INTERNAL_WAREHOUSE_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.INTERNAL_WAREHOUSE_NOT_EXISTS;


/**
 * ERP库位 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class InternalWarehouseServiceImpl implements InternalWarehouseService {

    @Resource
    private InternalWarehouseMapper internalWarehouseMapper;

    @Override
    public InternalWarehouseRespVO createInternalWarehouse(InternalWarehouseSaveReqVO createReqVO) {
        // 插入
        InternalWarehouseDO internalWarehouse = BeanUtils.toBean(createReqVO, InternalWarehouseDO.class);
        //校验所绑定的库位是否已经存在
        validateInternalWarehouseExists(createReqVO.getNumber(), "insert", null);
        internalWarehouseMapper.insert(internalWarehouse);
        // 返回
        return BeanUtils.toBean(internalWarehouse, InternalWarehouseRespVO.class);
    }


    public void validateInternalWarehouseExists(String number, String type, Long id) {
        InternalWarehouseDO internalWarehouseDO = internalWarehouseMapper.selectInternalWarehouseDOByNumber(number);
        if (internalWarehouseDO != null && "insert".equals(type)) {
            throw exception(INTERNAL_WAREHOUSE_EXISTS);
        } else if (internalWarehouseDO != null && "update".equals(type) && !Objects.equals(id, internalWarehouseDO.getId())) {
            throw exception(INTERNAL_WAREHOUSE_EXISTS);
        }

    }

    @Override
    public void updateInternalWarehouse(InternalWarehouseSaveReqVO updateReqVO) {
        // 校验存在
        validateInternalWarehouseExists(updateReqVO.getId());
        // 校验所绑定的库位是否已经存在
        validateInternalWarehouseExists(updateReqVO.getNumber(), "update", updateReqVO.getId());
        // 更新
        InternalWarehouseDO updateObj = BeanUtils.toBean(updateReqVO, InternalWarehouseDO.class);
        internalWarehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteInternalWarehouse(Long id) {
        // 校验存在
        validateInternalWarehouseExists(id);
        // 删除
        internalWarehouseMapper.deleteById(id);
    }

    @Override
    public void deleteInternalWarehouseListByIds(List<Long> ids) {
        // 校验存在
        validateInternalWarehouseExists(ids);
        // 删除
        internalWarehouseMapper.deleteByIds(ids);
    }

    private void validateInternalWarehouseExists(List<Long> ids) {
        List<InternalWarehouseDO> list = internalWarehouseMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(INTERNAL_WAREHOUSE_NOT_EXISTS);
        }
    }

    private void validateInternalWarehouseExists(Long id) {
        if (internalWarehouseMapper.selectById(id) == null) {
            throw exception(INTERNAL_WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public InternalWarehouseDO getInternalWarehouse(Long id) {
        return internalWarehouseMapper.selectById(id);
    }

    @Override
    public PageResult<InternalWarehouseDO> getInternalWarehousePage(InternalWarehousePageReqVO pageReqVO) {
        return internalWarehouseMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enableDisable(List<InternalWarehouseEnableDisableReqVO> reqVOS) {
        reqVOS.forEach(reqVO -> {
            // 校验存在
            validateInternalWarehouseExists(Long.valueOf(reqVO.getId()));
            // 更新
            InternalWarehouseDO updateObj = new InternalWarehouseDO();
            updateObj.setId(Long.valueOf(reqVO.getId()));
            updateObj.setIsEnable(reqVO.getIsEnable());
            internalWarehouseMapper.updateById(updateObj);
        });
    }

    @Override
    public InternalWarehouseDO getInternalWarehouseByFactoryCodeAndWarehouseCode(String factoryCode, String warehouseCode) {
        return internalWarehouseMapper.selectOne(
                new LambdaQueryWrapperX<InternalWarehouseDO>()
                        .eq(InternalWarehouseDO::getFactoryNumber, factoryCode)
                        .eq(InternalWarehouseDO::getNumber, warehouseCode)
                        .eq(InternalWarehouseDO::getIsEnable, '1')
        );
    }


}