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.WarehousePageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.WarehouseRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.WarehouseSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.WarehouseDO;
import com.zt.plat.module.base.dal.mysql.base.WarehouseMapper;
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.WAREHOUSE_CODE_EXISTS;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;

/**
 * 仓库 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public WarehouseRespVO createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 插入
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        // 库位编码自动生成，格式 KW-0001,依次新增
//        String maxCode = warehouseMapper.selectMaxCode();
//        if (maxCode == null) {
//            warehouse.setCoding("KW-0001");
//        } else {
//            String prefix = "KW-";
//            String numberPart = maxCode.substring(prefix.length());
//            int nextNumber = Integer.parseInt(numberPart) + 1;
//            String nextCode = prefix + String.format("%04d", nextNumber);
//            warehouse.setCoding(nextCode);
//        }
        validateWarehouseCodeExists(warehouse.getCoding());
        warehouseMapper.insert(warehouse);
        // 返回
        return BeanUtils.toBean(warehouse, WarehouseRespVO.class);
    }

    @Override
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseExists(updateReqVO.getId());
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Long id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    @Override
    public void deleteWarehouseListByIds(List<Long> ids) {
        // 校验存在
        validateWarehouseExists(ids);
        // 删除
        warehouseMapper.deleteByIds(ids);
    }

    private void validateWarehouseExists(List<Long> ids) {
        List<WarehouseDO> list = warehouseMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    private void validateWarehouseExists(Long id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    private void validateWarehouseCodeExists(String code) {
        WarehouseDO warehouse = warehouseMapper.selectByCode(code);
        if (warehouse != null) {
            throw exception(WAREHOUSE_CODE_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableWarehouseList(List<WarehouseRespVO> saveReqVOS) {
        List<WarehouseDO> updateObj = BeanUtils.toBean(saveReqVOS, WarehouseDO.class);
        List<BatchResult> count = warehouseMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

}