package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ZsWorkInfoBo;
import com.ruoyi.system.domain.vo.ZsWorkInfoVo;
import com.ruoyi.system.domain.ZsWorkInfo;
import com.ruoyi.system.mapper.ZsWorkInfoMapper;
import com.ruoyi.system.service.IZsWorkInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 职位管理Service业务层处理
 *
 * @author wq
 * @date 2025-06-24
 */
@RequiredArgsConstructor
@Service
public class ZsWorkInfoServiceImpl implements IZsWorkInfoService {

    private final ZsWorkInfoMapper baseMapper;

    /**
     * 查询职位管理
     */
    @Override
    public ZsWorkInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询职位管理列表
     */
    @Override
    public TableDataInfo<ZsWorkInfoVo> queryPageList(ZsWorkInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZsWorkInfo> lqw = buildQueryWrapper(bo);
        Page<ZsWorkInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询职位管理列表
     */
    @Override
    public List<ZsWorkInfoVo> queryList(ZsWorkInfoBo bo) {
        LambdaQueryWrapper<ZsWorkInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsWorkInfo> buildQueryWrapper(ZsWorkInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsWorkInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZsWorkInfo::getName, bo.getName());
        lqw.eq(bo.getLocation() != null, ZsWorkInfo::getLocation, bo.getLocation());
        lqw.eq(bo.getWebSite() != null, ZsWorkInfo::getWebSite, bo.getWebSite());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDisabled()), ZsWorkInfo::getIsDisabled, bo.getIsDisabled());
        return lqw;
    }

    /**
     * 新增职位管理
     */
    @Override
    public Boolean insertByBo(ZsWorkInfoBo bo) {
        ZsWorkInfo add = BeanUtil.toBean(bo, ZsWorkInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改职位管理
     */
    @Override
    public Boolean updateByBo(ZsWorkInfoBo bo) {
        ZsWorkInfo update = BeanUtil.toBean(bo, ZsWorkInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZsWorkInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除职位管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
