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.AssessCapacitorParamMapper;
import com.njcn.advance.pojo.param.assess.AssessCapacitorParamQuery;
import com.njcn.advance.pojo.po.assess.AssessCapacitorParam;
import com.njcn.advance.pojo.po.assess.AssessDicCableType;
import com.njcn.advance.pojo.po.assess.AssessUser;
import com.njcn.advance.service.assess.IAssessCapacitorParamService;
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 AssessCapacitorParamServiceImpl extends ServiceImpl<AssessCapacitorParamMapper, AssessCapacitorParam>
        implements IAssessCapacitorParamService {

    @Override
    public AssessCapacitorParam getByCapacitorId(String capacitorId) {
        return this.getOne(Wrappers.<AssessCapacitorParam>lambdaQuery()
                .eq(AssessCapacitorParam::getCapacitorId, capacitorId));
    }

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessCapacitorParamQuery.UpdateAssessCapacitorParamQuery updateQuery) {
        AssessCapacitorParam entity = this.getByCapacitorId(updateQuery.getCapacitorId());
        if (entity != null) {
            int count = this.count(new LambdaQueryWrapper<AssessCapacitorParam>()
                    .eq(AssessCapacitorParam::getAssessId, updateQuery.getAssessId())
                    .ne(AssessCapacitorParam::getCapacitorId, updateQuery.getCapacitorId())
                    .eq(AssessCapacitorParam::getCapacitorName, updateQuery.getCapacitorName())
            );
            if (count > 0) {
                throw new BusinessException(AdvanceResponseEnum.CAPACITOR_NAME_REPETITION);
            }
            AssessCapacitorParam pojo = new AssessCapacitorParam();
            BeanUtil.copyProperties(updateQuery,pojo);
            this.updateById(pojo);
        }
    }
}