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 lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.SysIndustrySkill;
import org.dromara.system.domain.bo.SysIndustrySkillBo;
import org.dromara.system.domain.vo.SysIndustrySkillVo;
import org.dromara.system.mapper.SysIndustrySkillMapper;
import org.dromara.system.service.ISysIndustrySkillService;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class SysIndustrySkillServiceImpl implements ISysIndustrySkillService {

    private final SysIndustrySkillMapper baseMapper;

    @Override
    public List<SysIndustrySkillVo> queryList(SysIndustrySkillBo bo) {
        LambdaQueryWrapper<SysIndustrySkill> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSkillName()), SysIndustrySkill::getSkillName, bo.getSkillName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysIndustrySkill::getStatus, bo.getStatus());
        lqw.eq(bo.getIndustryId() != null, SysIndustrySkill::getIndustryId, bo.getIndustryId());
        lqw.orderByAsc(SysIndustrySkill::getOrderNum);


        List<SysIndustrySkill> list = baseMapper.selectList(lqw);

        return BeanUtil.copyToList(list, SysIndustrySkillVo.class);
    }

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

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

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

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

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

}
