package com.usky.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usky.ems.domain.EmsChannelType;
import com.usky.ems.domain.EmsChannelTypeParameter;
import com.usky.ems.domain.EmsChannelTypeParameterValue;
import com.usky.ems.mapper.EmsChannelTypeMapper;
import com.usky.ems.mapper.EmsChannelTypeParameterMapper;
import com.usky.ems.mapper.EmsChannelTypeParameterValueMapper;
import com.usky.ems.service.EmsChannelTypeService;
import com.usky.ems.service.vo.EmsChannelTypeParameterShowVO;
import com.usky.ems.service.vo.EmsChannelTypeParameterValueShowVO;
import com.usky.ems.service.vo.EmsChannelTypeShowVO;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 通道类型 服务实现类
 * </p>
 *
 * @author ya
 * @since 2026-03-16
 */
@Service
public class EmsChannelTypeServiceImpl extends AbstractCrudService<EmsChannelTypeMapper, EmsChannelType> implements EmsChannelTypeService {

    @Autowired
    private EmsChannelTypeMapper emsChannelTypeMapper;
    @Autowired
    private EmsChannelTypeParameterMapper channelTypeParameterMapper;
    @Autowired
    private EmsChannelTypeParameterValueMapper channelTypeParameterValueMapper;

    @Override
    public List<EmsChannelTypeShowVO> show() {
        List<EmsChannelType> types = emsChannelTypeMapper.selectList(new LambdaQueryWrapper<>());
        List<EmsChannelTypeShowVO> result = new ArrayList<>(types.size());
        for (EmsChannelType type : types) {
            EmsChannelTypeShowVO vo = new EmsChannelTypeShowVO();
            BeanUtils.copyProperties(type, vo);
            List<EmsChannelTypeParameter> params = channelTypeParameterMapper.selectList(
                    new LambdaQueryWrapper<EmsChannelTypeParameter>()
                            .eq(EmsChannelTypeParameter::getChannelTypeId, type.getId())
                            .orderByAsc(EmsChannelTypeParameter::getSort));
            List<EmsChannelTypeParameterShowVO> paramVos = params.stream().map(p -> {
                EmsChannelTypeParameterShowVO pvo = new EmsChannelTypeParameterShowVO();
                BeanUtils.copyProperties(p, pvo);
                List<EmsChannelTypeParameterValue> values = channelTypeParameterValueMapper.selectList(
                        new LambdaQueryWrapper<EmsChannelTypeParameterValue>()
                                .eq(EmsChannelTypeParameterValue::getChannelTypeParameterId, p.getId())
                                .orderByAsc(EmsChannelTypeParameterValue::getSort));
                pvo.setChannelTypeValueList(values.stream().map(v -> {
                    EmsChannelTypeParameterValueShowVO vvo = new EmsChannelTypeParameterValueShowVO();
                    BeanUtils.copyProperties(v, vvo);
                    return vvo;
                }).collect(Collectors.toList()));
                return pvo;
            }).collect(Collectors.toList());
            vo.setChannelTypeParameterList(paramVos);
            result.add(vo);
        }
        return result;
    }
}
