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.AssessTransformerParamMapper;
import com.njcn.advance.pojo.param.assess.AssessTransformerParamQuery;
import com.njcn.advance.pojo.po.assess.AssessShockLoadParam;
import com.njcn.advance.pojo.po.assess.AssessTransformerParam;
import com.njcn.advance.service.assess.IAssessTransformerParamService;
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 AssessTransformerParamServiceImpl extends ServiceImpl<AssessTransformerParamMapper, AssessTransformerParam>
        implements IAssessTransformerParamService {

    @Override
    public AssessTransformerParam getByTransId(String transId) {
        return this.getOne(Wrappers.<AssessTransformerParam>lambdaQuery()
                .eq(AssessTransformerParam::getTransId, transId));
    }

    @Override
    public List<AssessTransformerParam> listByAssessId(String assessId) {
        return this.list(Wrappers.<AssessTransformerParam>lambdaQuery()
                .eq(AssessTransformerParam::getAssessId, assessId));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessTransformerParamQuery.UpdateAssessTransformerParamQuery updateQuery) {
        AssessTransformerParam entity = this.getByTransId(updateQuery.getTransId());
        int count = this.count(new LambdaQueryWrapper<AssessTransformerParam>()
                .eq(AssessTransformerParam::getAssessId, updateQuery.getAssessId())
                .ne(AssessTransformerParam::getTransId,updateQuery.getTransId())
                .eq(AssessTransformerParam::getTransName, updateQuery.getTransName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.TRANS_NAME_REPETITION);
        }
        if (entity != null) {
            BeanUtil.copyProperties(updateQuery,entity);
            this.updateById(entity);
        }
    }
}