package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.utils.ClassUtil;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.device.DeviceModelDto;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.device.DeviceModelSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.device.DeviceModelVo;
import cn.hanghui.outapi.platform.tenant.entity.po.DeviceModel;
import cn.hanghui.outapi.platform.tenant.mapper.DeviceModelMapper;
import cn.hanghui.outapi.platform.tenant.service.DeviceModelService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备类型 服务实现类
 * </p>
 *
 * @author wxg
 * @since 2023-12-27
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceModelServiceImpl implements DeviceModelService {

    private final DeviceModelMapper deviceModelMapper;

    @Override
    public Boolean edit(DeviceModelDto dto) {
        Long deviceModelId = dto.getDeviceModelId();
        if(deviceModelId == null){
            DeviceModel deviceModel = BeanUtil.copyProperties(dto, DeviceModel.class);
            deviceModelMapper.insert(deviceModel);
        }else {
            DeviceModel base = deviceModelMapper.selectById(deviceModelId);
            ClassUtil.throwBusinessException(ObjectUtil.isNull(base),"设备型号不存在");
            BeanUtil.copyProperties(dto,base);
            deviceModelMapper.updateById(base);
        }
        return true;
    }

    @Override
    public List<DeviceModelVo> deviceModelList() {
        LambdaQueryWrapper<DeviceModel> wrapper = new LambdaQueryWrapper<>();
        //查询的基础条件
        wrapper.eq(DeviceModel::getIsDeleted,Boolean.FALSE);
        wrapper.orderByDesc(DeviceModel::getId);
        List<DeviceModel> deviceModelList = deviceModelMapper.selectList(wrapper);

        return deviceModelList.stream().map(p->{
            DeviceModelVo item = BeanUtil.copyProperties(p,DeviceModelVo.class);
            item.setDeviceModelId(p.getId());
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<DeviceModelVo> deviceModelPage(DeviceModelSearch dto) {
        Page<DeviceModel> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<DeviceModel> wrapper = this.getWrapper(dto);
        IPage<DeviceModel> pageList = deviceModelMapper.selectPage(page, wrapper);
        return pageList.convert(t -> {
            DeviceModelVo item = BeanUtil.copyProperties(t, DeviceModelVo.class);
            item.setDeviceModelId(t.getId());
            return item;
        });
    }
    private LambdaQueryWrapper<DeviceModel> getWrapper(DeviceModelSearch dto) {
        LambdaQueryWrapper<DeviceModel> wrapper = new LambdaQueryWrapper<>();
        //查询的传参条件
        wrapper.eq(ObjectUtil.isNotNull(dto.getDeviceModelId()), DeviceModel::getId,dto.getDeviceModelId());
        //查询的基础条件
        wrapper.eq(DeviceModel::getIsDeleted,Boolean.FALSE);
        wrapper.orderByDesc(DeviceModel::getId);
        return wrapper;
    }
    @Override
    public Boolean deviceModelDelete(Long deviceModelId) {
        DeviceModel deviceModel = deviceModelMapper.selectById(deviceModelId);
        ClassUtil.throwBusinessException(ObjectUtil.isNull(deviceModel),"设备型号不存在");
        ClassUtil.throwBusinessException(deviceModel.getIsDeleted(),"设备型号已删除");
        deviceModel.setIsDeleted(Boolean.TRUE);
        deviceModelMapper.updateById(deviceModel);
        return Boolean.TRUE;
    }
}
