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.AssessShockLoadParamMapper;
import com.njcn.advance.pojo.param.assess.AssessShockLoadParamQuery;
import com.njcn.advance.pojo.po.assess.AssessLoadParam;
import com.njcn.advance.pojo.po.assess.AssessShockLoadParam;
import com.njcn.advance.service.assess.IAssessShockLoadParamService;
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 AssessShockLoadParamServiceImpl extends ServiceImpl<AssessShockLoadParamMapper, AssessShockLoadParam>
        implements IAssessShockLoadParamService {

    @Override
    public AssessShockLoadParam getByInpactloadId(String inpactloadId) {
        return this.getOne(Wrappers.<AssessShockLoadParam>lambdaQuery()
                .eq(AssessShockLoadParam::getInpactloadId, inpactloadId));
    }

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

    @Override
    public List<AssessShockLoadParam> listByTypeId(String inpactloadtypeId) {
        return this.list(Wrappers.<AssessShockLoadParam>lambdaQuery()
                .eq(AssessShockLoadParam::getInpactloadtypeId, inpactloadtypeId));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessShockLoadParamQuery.UpdateAssessShockLoadParamQuery updateQuery) {
        AssessShockLoadParam entity = this.getByInpactloadId(updateQuery.getInpactloadId());
        int count = this.count(new LambdaQueryWrapper<AssessShockLoadParam>()
                .eq(AssessShockLoadParam::getAssessId, updateQuery.getAssessId())
                .ne(AssessShockLoadParam::getInpactloadId,updateQuery.getInpactloadId())
                .eq(AssessShockLoadParam::getInpactloadtypeId, updateQuery.getInpactloadtypeId())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.INPACTLOADTYPE_NAME_REPETITION);
        }
        if (entity != null) {
            BeanUtil.copyProperties(updateQuery,entity);
            this.updateById(entity);
        }
    }
}