package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.BaseBuildFacility;
import com.usky.iot.domain.BaseBuildFacilityType;
import com.usky.iot.mapper.BaseBuildFacilityTypeMapper;
import com.usky.iot.service.BaseBuildFacilityService;
import com.usky.iot.service.BaseBuildFacilityTypeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.service.vo.BaseBuildFacilityTypeRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.Option;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 建筑设施类型 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-07-26
 */
@Service
public class BaseBuildFacilityTypeServiceImpl extends AbstractCrudService<BaseBuildFacilityTypeMapper, BaseBuildFacilityType> implements BaseBuildFacilityTypeService {
    @Autowired
    private BaseBuildFacilityService baseBuildFacilityService;

    @Override
    public void add(BaseBuildFacilityType baseBuildFacilityType){
        if(checkNameUnique(baseBuildFacilityType)){
            throw new BusinessException("新增建筑设施类型'"+baseBuildFacilityType.getTypeName()+"'失败，该建筑设施类型已存在");
        }
        baseBuildFacilityType.setCreateBy(SecurityUtils.getUsername());
        baseBuildFacilityType.setCreateTime(LocalDateTime.now());
        baseBuildFacilityType.setTenantId(SecurityUtils.getTenantId());
        this.save(baseBuildFacilityType);

    }

    @Override
    public void update(BaseBuildFacilityType baseBuildFacilityType){
        if(checkNameUnique(baseBuildFacilityType)){
            throw new BusinessException("修改建筑设施类型'"+baseBuildFacilityType.getTypeName()+"'失败，该建筑设施类型已存在");
        }
        baseBuildFacilityType.setUpdateBy(SecurityUtils.getUsername());
        baseBuildFacilityType.setUpdateTime(LocalDateTime.now());
        this.updateById(baseBuildFacilityType);
    }

    @Override
    public void remove(Integer id){
        BaseBuildFacilityType info = this.getById(id);
        Optional.ofNullable(info).orElseThrow(() -> new BusinessException("该建筑设备类型不存在"));

        LambdaQueryWrapper<BaseBuildFacility> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseBuildFacility::getFacilityType,info.getTypeCode())
                .eq(BaseBuildFacility::getDeleteFlag,0);
        int count = baseBuildFacilityService.count(queryWrapper);
        if(count > 0){
            throw new BusinessException("有建筑设施使用该建筑设施类型，不能删除");
        }

        this.removeById(id);
    }

    @Override
    public CommonPage<BaseBuildFacilityType> page(BaseBuildFacilityTypeRequestVO requestVO){
        IPage<BaseBuildFacilityType> page = new Page<>(requestVO.getCurrent(),requestVO.getSize());
        LambdaQueryWrapper<BaseBuildFacilityType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(requestVO.getTypeCode()),BaseBuildFacilityType::getTypeCode,requestVO.getTypeCode())
                .like(StringUtils.isNotBlank(requestVO.getTypeName()),BaseBuildFacilityType::getTypeName,requestVO.getTypeName())
                .orderByDesc(BaseBuildFacilityType::getId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getSize(),page.getCurrent());
    }

    @Override
    public boolean checkNameUnique(BaseBuildFacilityType baseBuildFacilityType){
        Integer id = null == baseBuildFacilityType.getId()?-1:baseBuildFacilityType.getId();
        LambdaQueryWrapper<BaseBuildFacilityType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseBuildFacilityType::getTypeName,baseBuildFacilityType.getTypeName());
        BaseBuildFacilityType one = this.getOne(queryWrapper);
        return null != one && !Objects.equals(one.getId(),id);
    }

}
