package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.BaseBuild;
import com.usky.iot.domain.BaseBuildFacilityRelate;
import com.usky.iot.domain.BaseBuildUnit;
import com.usky.iot.domain.DmpDeviceInfo;
import com.usky.iot.mapper.BaseBuildUnitMapper;
import com.usky.iot.service.BaseBuildUnitService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2026-02-03
 */
@Service
public class BaseBuildUnitServiceImpl extends AbstractCrudService<BaseBuildUnitMapper, BaseBuildUnit> implements BaseBuildUnitService {
    @Override
    public void add(List<BaseBuildUnit> baseBuildUnits){
        if(CollectionUtils.isNotEmpty(baseBuildUnits)){
            for(int i=0;i<baseBuildUnits.size();i++){
                BaseBuildUnit baseBuildUnit = baseBuildUnits.get(i);
                baseBuildUnit.setCreateBy(SecurityUtils.getUsername());
                baseBuildUnit.setCreateTime(LocalDateTime.now());
                baseBuildUnit.setTenantId(SecurityUtils.getTenantId());
                this.save(baseBuildUnit);
            }
        }else {
            throw new BusinessException("提交内容不能为空");
        }
    }

    @Override
    public void update(List<BaseBuildUnit> baseBuildUnits){
        if(CollectionUtils.isNotEmpty(baseBuildUnits)){
            this.remove(1);
            for(int i=0;i<baseBuildUnits.size();i++){
                BaseBuildUnit baseBuildUnit = baseBuildUnits.get(i);
                baseBuildUnit.setUpdateBy(SecurityUtils.getUsername());
                baseBuildUnit.setUpdateTime(LocalDateTime.now());
                baseBuildUnit.setTenantId(SecurityUtils.getTenantId());
                this.save(baseBuildUnit);
            }
        }else {
            throw new BusinessException("提交内容不能为空");
        }


    }

    @Override
    public void remove(Integer id){
        LambdaQueryWrapper<BaseBuildUnit> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseBuildUnit::getTenantId,SecurityUtils.getTenantId());
        this.remove(queryWrapper);

    }

    @Override
    public List<BaseBuildUnit> baseBuildUnitList(Integer id,Integer buildId){
        LambdaQueryWrapper<BaseBuildUnit> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(!ObjectUtils.isEmpty(id),BaseBuildUnit::getId,id)
                .eq(BaseBuildUnit::getBuildId,buildId)
                .eq(BaseBuildUnit::getTenantId,SecurityUtils.getTenantId())
                .orderByDesc(BaseBuildUnit::getId);
        List<BaseBuildUnit> list = this.list(queryWrapper);
        return list;
    }
}
