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.application.device.ApplicationTypeDto;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.application.device.ApplicationTypeSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.application.device.ApplicationTypeVo;
import cn.hanghui.outapi.platform.tenant.entity.po.ApplicationType;
import cn.hanghui.outapi.platform.tenant.mapper.ApplicationTypeMapper;
import cn.hanghui.outapi.platform.tenant.service.ApplicationTypeService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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 ApplicationTypeServiceImpl implements ApplicationTypeService {
    private final ApplicationTypeMapper applicationTypeMapper;

    @Override
    public Boolean edit(ApplicationTypeDto dto) {
        Long applicationTypeId = dto.getApplicationTypeId();
        if(applicationTypeId == null){
            ApplicationType applicationType = BeanUtil.copyProperties(dto, ApplicationType.class);
            applicationTypeMapper.insert(applicationType);
        }else {
            ApplicationType base = applicationTypeMapper.selectById(applicationTypeId);
            ClassUtil.throwBusinessException(ObjectUtil.isNull(base),"应用类型不存在");
            BeanUtil.copyProperties(dto,base);
            applicationTypeMapper.updateById(base);
        }
        return true;
    }

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

        return applicationTypeList.stream().map(p->{
            ApplicationTypeVo item = BeanUtil.copyProperties(p,ApplicationTypeVo.class);
            item.setApplicationTypeId(p.getId());
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<ApplicationTypeVo> applicationTypePage(ApplicationTypeSearch dto) {
        Page<ApplicationType> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<ApplicationType> wrapper = this.getWrapper(dto);
        IPage<ApplicationType> pageList = applicationTypeMapper.selectPage(page, wrapper);
        return pageList.convert(t -> {
            ApplicationTypeVo item = BeanUtil.copyProperties(t, ApplicationTypeVo.class);
            item.setApplicationTypeId(t.getId());
            return item;
        });
    }
    private LambdaQueryWrapper<ApplicationType> getWrapper(ApplicationTypeSearch dto) {
        LambdaQueryWrapper<ApplicationType> wrapper = new LambdaQueryWrapper<>();
        //查询的传参条件
        wrapper.eq(ObjectUtil.isNotNull(dto.getApplicationTypeId()), ApplicationType::getId,dto.getApplicationTypeId());
        //查询的基础条件
        wrapper.eq(ApplicationType::getIsDeleted,Boolean.FALSE);
        wrapper.orderByDesc(ApplicationType::getId);
        return wrapper;
    }
    @Override
    public Boolean applicationTypeDelete(Long applicationTypeId) {
        ApplicationType applicationType = applicationTypeMapper.selectById(applicationTypeId);
        ClassUtil.throwBusinessException(ObjectUtil.isNull(applicationType),"应用类型不存在");
        ClassUtil.throwBusinessException(applicationType.getIsDeleted(),"应用类型已删除");
        applicationType.setIsDeleted(Boolean.TRUE);
        applicationTypeMapper.updateById(applicationType);
        return Boolean.TRUE;
    }
}
