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

import cn.hutool.core.bean.BeanUtil;
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.AssessDicTransformerMapper;
import com.njcn.advance.pojo.param.assess.AssessDicTransformerQuery;
import com.njcn.advance.pojo.po.assess.AssessDicCableType;
import com.njcn.advance.pojo.po.assess.AssessDicTransformer;
import com.njcn.advance.service.assess.IAssessDicTransformerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 变压器参数字典 服务实现类
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@Service
public class AssessDicTransformerServiceImpl extends ServiceImpl<AssessDicTransformerMapper, AssessDicTransformer>
        implements IAssessDicTransformerService {

    @Override
    public AssessDicTransformer getByTranstypeId(String transtypeId) {
        return this.getOne(Wrappers.<AssessDicTransformer>lambdaQuery()
                .eq(AssessDicTransformer::getTranstypeId, transtypeId));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessDicTransformerQuery.UpdateAssessDicTransformerQuery updateQuery) {
        AssessDicTransformer entity = this.getByTranstypeId(updateQuery.getTranstypeId());
        if (entity != null) {
            int count = this.count(new LambdaQueryWrapper<AssessDicTransformer>()
                    .ne(AssessDicTransformer::getTranstypeId, updateQuery.getTranstypeId())
                    .eq(AssessDicTransformer::getTranstypeName, updateQuery.getTranstypeName())
            );
            if (count > 0) {
                throw new BusinessException(AdvanceResponseEnum.TRANSFORMER_NAME_REPETITION);
            }
            BeanUtil.copyProperties(updateQuery,entity);
            this.updateById(entity);
        }
    }
}