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

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.AssessWindLoadParamMapper;
import com.njcn.advance.pojo.param.assess.AssessWindLoadParamQuery;
import com.njcn.advance.pojo.po.assess.AssessWindLoadParam;
import com.njcn.advance.service.assess.IAssessWindLoadParamService;
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 AssessWindLoadParamServiceImpl extends ServiceImpl<AssessWindLoadParamMapper, AssessWindLoadParam>
        implements IAssessWindLoadParamService {

    @Override
    public AssessWindLoadParam getByWindgeneratorId(String windgeneratorId) {
        return this.getOne(Wrappers.<AssessWindLoadParam>lambdaQuery()
                .eq(AssessWindLoadParam::getWindgeneratorId, windgeneratorId));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AssessWindLoadParamQuery param) {
        AssessWindLoadParam entity = new AssessWindLoadParam();
        int count = this.count(new LambdaQueryWrapper<AssessWindLoadParam>()
                .eq(AssessWindLoadParam::getAssessId, param.getAssessId())
                .eq(AssessWindLoadParam::getWindgeneratorName, param.getWindgeneratorName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.WIND_NAME_REPETITION);
        }
        // 拷贝参数到实体类（实际项目中可使用BeanUtils.copyProperties）
        entity.setAssessId(param.getAssessId());
        entity.setWindgeneratorName(param.getWindgeneratorName());
        entity.setFlickerCoeff(param.getFlickerCoeff());
        entity.setS(param.getS());
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessWindLoadParamQuery.UpdateAssessWindLoadParamQuery updateQuery) {
        AssessWindLoadParam entity = this.getByWindgeneratorId(updateQuery.getWindgeneratorId());
        if (entity != null) {
            int count = this.count(new LambdaQueryWrapper<AssessWindLoadParam>()
                    .eq(AssessWindLoadParam::getAssessId, updateQuery.getAssessId())
                    .ne(AssessWindLoadParam::getWindgeneratorId,updateQuery.getWindgeneratorId())
                    .eq(AssessWindLoadParam::getWindgeneratorName, updateQuery.getWindgeneratorName())
            );
            if (count > 0) {
                throw new BusinessException(AdvanceResponseEnum.WIND_NAME_REPETITION);
            }
            // 拷贝参数到实体类（实际项目中可使用BeanUtils.copyProperties）
            entity.setAssessId(updateQuery.getAssessId());
            entity.setWindgeneratorName(updateQuery.getWindgeneratorName());
            entity.setFlickerCoeff(updateQuery.getFlickerCoeff());
            entity.setS(updateQuery.getS());
            this.updateById(entity);
        }
    }
}