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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.mapper.carrycapacity.CarryCapacityDevicePOMapper;
import com.njcn.advance.pojo.carrycapacity.param.CarryCapacityDeviceParam;
import com.njcn.advance.pojo.carrycapacity.po.CarryCapacityDevicePO;
import com.njcn.advance.service.carrycapacity.CarryCapacityDevicePOService;
import com.njcn.advance.utils.CheckStringUtil;
import com.njcn.common.pojo.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * Description:
 * Date: 2024/3/19  16:36【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CarryCapacityDevicePOServiceImpl extends ServiceImpl<CarryCapacityDevicePOMapper, CarryCapacityDevicePO> implements CarryCapacityDevicePOService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDevice(CarryCapacityDeviceParam.CarryCapacityDeviceUpdateParam deviceParam) {
        if(StringUtils.isBlank(deviceParam.getDevName())){
            throw new BusinessException("干扰源设备名称不能为空");
        }
        checkName(deviceParam,true);
        return this.lambdaUpdate().eq(CarryCapacityDevicePO::getDevId, deviceParam.getDevId())
                .set(CarryCapacityDevicePO::getDevName, deviceParam.getDevName())
                .set(CarryCapacityDevicePO::getDevScale, deviceParam.getDevScale())
                .set(CarryCapacityDevicePO::getProtocolCapacity, deviceParam.getProtocolCapacity())
                .update();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CarryCapacityDeviceParam capacityDeviceParam) {
        if(StringUtils.isBlank(capacityDeviceParam.getDevName())){
            throw new BusinessException("干扰源设备名称不能为空");
        }
        checkName(capacityDeviceParam,false);

        CarryCapacityDevicePO carryCapacityDevice = new CarryCapacityDevicePO();
        BeanUtils.copyProperties(capacityDeviceParam,carryCapacityDevice);
        return this.save(carryCapacityDevice);
    }

    /**
     * 检查名称是否已存在
     *
     * @return 结果
     */
    private void checkName(CarryCapacityDeviceParam carryCapacityDeviceParam, boolean isUpdate) {
        if(carryCapacityDeviceParam.getDevName().length()>32){
            throw new BusinessException("超过最大长度");

        }
        CheckStringUtil.checkName(carryCapacityDeviceParam.getDevName());

        LambdaQueryWrapper<CarryCapacityDevicePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件组合：where state = 1 and name = ?
        lambdaQueryWrapper.eq(CarryCapacityDevicePO::getUserId,carryCapacityDeviceParam.getUserId()).eq(CarryCapacityDevicePO::getDevName, carryCapacityDeviceParam.getDevName());

        //and id <> ?
        if (isUpdate) {
            if (carryCapacityDeviceParam instanceof CarryCapacityDeviceParam.CarryCapacityDeviceUpdateParam) {
                lambdaQueryWrapper.ne(CarryCapacityDevicePO::getDevId, ((CarryCapacityDeviceParam.CarryCapacityDeviceUpdateParam) carryCapacityDeviceParam).getDevId());
            }
        }

        //若存在条件数据抛出异常
        int count = this.getBaseMapper().selectCount(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("干扰源设备名称已存在");
        }

    }
}
