package com.zt.plat.module.base.service.processinginfomation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.module.base.controller.admin.processinginfomation.vo.*;
import com.zt.plat.module.base.dal.dataobject.processinginfomation.ProcessingInfomationDO;
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 com.zt.plat.module.base.dal.dao.processinginfomation.ProcessingInfomationMapper;

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.base.enums.ErrorCodeConstants.*;

/**
 * 工艺信息 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class ProcessingInfomationServiceImpl implements ProcessingInfomationService {

    @Resource
    private ProcessingInfomationMapper processingInfomationMapper;

    private String normalizeDictionaryValue(String value) {
        if (StrUtil.isBlank(value)) {
            return null;
        }
        String trimmed = value.trim();
        int idx = trimmed.length();
        while (idx > 0 && Character.isDigit(trimmed.charAt(idx - 1))) {
            idx--;
        }
        if (idx == trimmed.length()) {
            // no trailing digits
            return trimmed;
        }
        String digits = trimmed.substring(idx);
        String prefix = trimmed.substring(0, idx);
        if (StrUtil.isEmpty(prefix) || isSafeDictionaryPrefix(prefix)) {
            return digits;
        }
        return trimmed;
    }

    private void normalizeProcessingInfomation(ProcessingInfomationDO data) {
        if (data == null) {
            return;
        }
        data.setTypeValue(normalizeDictionaryValue(data.getTypeValue()));
        data.setStatusValue(normalizeDictionaryValue(data.getStatusValue()));
    }

    private boolean isSafeDictionaryPrefix(String prefix) {
        for (int i = 0; i < prefix.length(); i++) {
            char ch = prefix.charAt(i);
            if (!(Character.isLetter(ch) || ch == '_' || ch == '-' || ch == ':')) {
                return false;
            }
        }
        return true;
    }

    @Override
    public ProcessingInfomationRespVO createProcessingInfomation(ProcessingInfomationSaveReqVO createReqVO) {
        // 插入
        ProcessingInfomationDO processingInfomation = BeanUtils.toBean(createReqVO, ProcessingInfomationDO.class);
        normalizeProcessingInfomation(processingInfomation);
        processingInfomationMapper.insert(processingInfomation);
        normalizeProcessingInfomation(processingInfomation);
        // 返回
        return BeanUtils.toBean(processingInfomation, ProcessingInfomationRespVO.class);
    }

    @Override
    public void updateProcessingInfomation(ProcessingInfomationSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessingInfomationExists(updateReqVO.getId());
        // 更新
        ProcessingInfomationDO updateObj = BeanUtils.toBean(updateReqVO, ProcessingInfomationDO.class);
        normalizeProcessingInfomation(updateObj);
        processingInfomationMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessingInfomation(Long id) {
        // 校验存在
        validateProcessingInfomationExists(id);
        // 删除
        processingInfomationMapper.deleteById(id);
    }

    @Override
        public void deleteProcessingInfomationListByIds(List<Long> ids) {
        // 校验存在
        validateProcessingInfomationExists(ids);
        // 删除
        processingInfomationMapper.deleteByIds(ids);
        }

    private void validateProcessingInfomationExists(List<Long> ids) {
        List<ProcessingInfomationDO> list = processingInfomationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PROCESSING_INFOMATION_NOT_EXISTS);
        }
    }

    private void validateProcessingInfomationExists(Long id) {
        if (processingInfomationMapper.selectById(id) == null) {
            throw exception(PROCESSING_INFOMATION_NOT_EXISTS);
        }
    }

    @Override
    public ProcessingInfomationDO getProcessingInfomation(Long id) {
        ProcessingInfomationDO data = processingInfomationMapper.selectById(id);
        normalizeProcessingInfomation(data);
        return data;
    }

    @Override
    public PageResult<ProcessingInfomationDO> getProcessingInfomationPage(ProcessingInfomationPageReqVO pageReqVO) {
        PageResult<ProcessingInfomationDO> pageResult = processingInfomationMapper.selectPage(pageReqVO);
        List<ProcessingInfomationDO> records = pageResult.getList();
        if (CollUtil.isNotEmpty(records)) {
            records.forEach(this::normalizeProcessingInfomation);
        }
        return pageResult;
    }

    @Override
    public List<ProcessingInfomationDO> getProcessingInfomationList() {
        List<ProcessingInfomationDO> list = processingInfomationMapper.selectList();
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(this::normalizeProcessingInfomation);
        }
        return list;
    }

}