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.AssessDicShockLoadMapper;
import com.njcn.advance.pojo.param.assess.AssessDicShockLoadQuery;
import com.njcn.advance.pojo.po.assess.AssessDicShockLoad;
import com.njcn.advance.pojo.po.assess.AssessDicTransformer;
import com.njcn.advance.service.assess.IAssessDicShockLoadService;
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 AssessDicShockLoadServiceImpl extends ServiceImpl<AssessDicShockLoadMapper, AssessDicShockLoad> implements IAssessDicShockLoadService {

    @Override
    public AssessDicShockLoad getByInpactloadtypeId(String inpactloadtypeId) {
        return this.getOne(Wrappers.<AssessDicShockLoad>lambdaQuery()
                .eq(AssessDicShockLoad::getInpactloadtypeId, inpactloadtypeId));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssessDicShockLoadQuery.UpdateAssessDicShockLoadQuery updateQuery) {
        AssessDicShockLoad entity = this.getByInpactloadtypeId(updateQuery.getInpactloadtypeId());
        if (entity != null) {
            int count = this.count(new LambdaQueryWrapper<AssessDicShockLoad>()
                    .ne(AssessDicShockLoad::getInpactloadtypeId, updateQuery.getInpactloadtypeId())
                    .eq(AssessDicShockLoad::getInpactloadtypeName, updateQuery.getInpactloadtypeName())
            );
            if (count > 0) {
                throw new BusinessException(AdvanceResponseEnum.SHOCKLOAD_NAME_REPETITION);
            }
            BeanUtil.copyProperties(updateQuery,entity);
            this.updateById(entity);
        }
    }
}