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


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.assess.AssessLoadParamMapper;
import com.njcn.advance.mapper.assess.AssessUserMapper;
import com.njcn.advance.pojo.param.assess.AssessLoadParamQuery;
import com.njcn.advance.pojo.po.assess.AssessCapacitorParam;
import com.njcn.advance.pojo.po.assess.AssessLoadParam;
import com.njcn.advance.service.assess.IAssessLoadParamService;
import com.njcn.common.pojo.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 评估用户负荷参数表 服务实现类
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@Service
@RequiredArgsConstructor
public class AssessLoadParamServiceImpl extends ServiceImpl<AssessLoadParamMapper, AssessLoadParam> implements IAssessLoadParamService {

    private final AssessUserMapper assessUserMapper;

    @Override
    public AssessLoadParam getByLoadId(String loadId) {
        return this.getById(loadId);
    }

    @Override
    public List<AssessLoadParam> listByAssessId(String assessId) {
        LambdaQueryWrapper<AssessLoadParam> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AssessLoadParam::getAssessId,assessId);
        List<AssessLoadParam> result = this.list(lambdaQueryWrapper);
        return  result;
    }

    @Override
    public boolean add(AssessLoadParamQuery assessLoadParam) {
        AssessLoadParam pojo = new AssessLoadParam();
        BeanUtils.copyProperties(assessLoadParam,pojo);
        int count = this.count(new LambdaQueryWrapper<AssessLoadParam>()
                .eq(AssessLoadParam::getAssessId, assessLoadParam.getAssessId())
                .eq(AssessLoadParam::getLoadName, assessLoadParam.getLoadName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.LOAD_NAME_REPETITION);
        }
        this.save(pojo);
        return true;
    }

    @Override
    public boolean update(AssessLoadParamQuery.UpdateAssessLoadParamQuery updateQuery) {
        AssessLoadParam pojo = new AssessLoadParam();
        int count = this.count(new LambdaQueryWrapper<AssessLoadParam>()
                .eq(AssessLoadParam::getAssessId, updateQuery.getAssessId())
                .ne(AssessLoadParam::getLoadId, updateQuery.getLoadId())
                .eq(AssessLoadParam::getLoadName, updateQuery.getLoadName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.LOAD_NAME_REPETITION);
        }
        BeanUtils.copyProperties(updateQuery,pojo);
        this.updateById(pojo);
        return true;
    }
}
