package com.njcn.advance.service.assess.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.assess.AssessDicHarmonicCharacterMapper;
import com.njcn.advance.pojo.param.assess.AssessDicHarmonicCharacterQuery;
import com.njcn.advance.pojo.po.assess.AssessDicHarmonicCharacter;
import com.njcn.advance.service.assess.IAssessDicHarmonicCharacterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 谐波源特性字典 服务实现类
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@Service
public class AssessDicHarmonicCharacterServiceImpl extends ServiceImpl<AssessDicHarmonicCharacterMapper, AssessDicHarmonicCharacter>
        implements IAssessDicHarmonicCharacterService {

    @Override
    public AssessDicHarmonicCharacter getByHarmId(String harmId) {
        return this.getOne(Wrappers.<AssessDicHarmonicCharacter>lambdaQuery()
                .eq(AssessDicHarmonicCharacter::getHarmId, harmId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AssessDicHarmonicCharacterQuery param) {
        AssessDicHarmonicCharacter entity = new AssessDicHarmonicCharacter();
        int count = this.count(new LambdaQueryWrapper<AssessDicHarmonicCharacter>()
                .eq(AssessDicHarmonicCharacter::getHarmName, param.getHarmName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.HARMONICCHARACTER_NAME_REPETITION);
        }
        BeanUtil.copyProperties(param, entity);
        return this.save(entity);
    }

    @Override
    public boolean update(AssessDicHarmonicCharacterQuery.UpdateAssessDicHarmonicCharacterQuery param) {
        AssessDicHarmonicCharacter entity = new AssessDicHarmonicCharacter();
        int count = this.count(new LambdaQueryWrapper<AssessDicHarmonicCharacter>()
                .ne(AssessDicHarmonicCharacter::getHarmId, param.getHarmId())
                .eq(AssessDicHarmonicCharacter::getHarmName, param.getHarmName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.HARMONICCHARACTER_NAME_REPETITION);
        }
        BeanUtil.copyProperties(param, entity);
        return this.updateById(entity);
    }
}