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.FactoryPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.FactoryRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.FactorySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.FactoryDO;
import com.zt.plat.module.base.dal.mysql.base.FactoryMapper;
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.FACTORY_NOT_EXISTS;

/**
 * 工厂 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class FactoryServiceImpl implements FactoryService {

    @Resource
    private FactoryMapper factoryMapper;

    @Override
    public FactoryRespVO createFactory(FactorySaveReqVO createReqVO) {
        // 插入
        FactoryDO factory = BeanUtils.toBean(createReqVO, FactoryDO.class);
        // 工厂编码自动生成，格式 GC-0001,依次新增
        String maxCode = factoryMapper.selectMaxCode();
        if (maxCode == null) {
            factory.setNumber("GC-0001");
        } else {
            String prefix = "GC-";
            String numberPart = maxCode.substring(prefix.length());
            int nextNumber = Integer.parseInt(numberPart) + 1;
            String nextCode = prefix + String.format("%04d", nextNumber);
            factory.setNumber(nextCode);
        }
        factoryMapper.insert(factory);
        // 返回
        return BeanUtils.toBean(factory, FactoryRespVO.class);
    }

    @Override
    public void updateFactory(FactorySaveReqVO updateReqVO) {
        // 校验存在
        validateFactoryExists(updateReqVO.getId());
        // 更新
        FactoryDO updateObj = BeanUtils.toBean(updateReqVO, FactoryDO.class);
        factoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteFactory(Long id) {
        // 校验存在
        validateFactoryExists(id);
        // 删除
        factoryMapper.deleteById(id);
    }

    @Override
    public void deleteFactoryListByIds(List<Long> ids) {
        // 校验存在
        validateFactoryExists(ids);
        // 删除
        factoryMapper.deleteByIds(ids);
    }

    private void validateFactoryExists(List<Long> ids) {
        List<FactoryDO> list = factoryMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(FACTORY_NOT_EXISTS);
        }
    }

    private void validateFactoryExists(Long id) {
        if (factoryMapper.selectById(id) == null) {
            throw exception(FACTORY_NOT_EXISTS);
        }
    }

    @Override
    public FactoryDO getFactory(Long id) {
        return factoryMapper.selectById(id);
    }

    @Override
    public PageResult<FactoryDO> getFactoryPage(FactoryPageReqVO pageReqVO) {
        return factoryMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableFactoryList(List<FactoryRespVO> saveReqVOS) {
        List<FactoryDO> updateObj = BeanUtils.toBean(saveReqVOS, FactoryDO.class);
        List<BatchResult> count = factoryMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(FACTORY_NOT_EXISTS);
        }
    }

}