package org.dromara.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.SysIndustry;
import org.dromara.system.domain.bo.SysIndustryBo;
import org.dromara.system.domain.vo.SysIndustryVo;
import org.dromara.system.mapper.SysIndustryMapper;
import org.dromara.system.service.ISysIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysIndustryServiceImpl implements ISysIndustryService {
    @Autowired
    private SysIndustryMapper baseMapper;

    @Override
    public List<SysIndustryVo> queryList(SysIndustryBo bo) {
        LambdaQueryWrapper<SysIndustry> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getIndustryName()), SysIndustry::getIndustryName, bo.getIndustryName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysIndustry::getStatus, bo.getStatus());
        lqw.eq(bo.getParentId() != null, SysIndustry::getParentId, bo.getParentId());
        lqw.orderByAsc(SysIndustry::getOrderNum);

//        1.
        List<SysIndustry> list = baseMapper.selectList(lqw);

        // 2. 使用 BeanUtil 手动转成 VO 列表
        return BeanUtil.copyToList(list, SysIndustryVo.class);
    }

    @Override
    public List<SysIndustryVo> queryListIgnoreTenant(SysIndustryBo bo) {
        return TenantHelper.ignore(() -> queryList(bo));
    }

    @Override
    public SysIndustryVo queryById(Long industryId) {
        SysIndustry industry = baseMapper.selectById(industryId);
        // 2. 使用框架自带的 BeanUtil 手动转换
        return BeanUtil.toBean(industry, SysIndustryVo.class);
    }

    @Override
    public Boolean insertByBo(SysIndustryBo bo) {
        SysIndustry add = BeanUtil.toBean(bo, SysIndustry.class);
        return baseMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(SysIndustryBo bo) {
        SysIndustry update = BeanUtil.toBean(bo, SysIndustry.class);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteById(Long industryId) {
        return baseMapper.deleteById(industryId) > 0;
    }
}
