package com.ruoyi.info.nursing.service.impl;

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

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.nursing.domain.NursingCertificate;
import com.ruoyi.info.nursing.domain.bo.NursingCertificateBo;
import com.ruoyi.info.nursing.domain.vo.NursingCertificateVo;
import com.ruoyi.info.nursing.exception.NursingCertificateExceptionEnum;
import com.ruoyi.info.nursing.mapper.NursingCertificateMapper;
import com.ruoyi.info.nursing.service.INursingCertificateService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * 护工从业证书信息Service业务层处理
 *
 * @author baifc
 * @date 2025-09-29
 */
@RequiredArgsConstructor
@Service
public class NursingCertificateServiceImpl implements INursingCertificateService {

    private final NursingCertificateMapper baseMapper;

    /**
     * 查询护工从业证书信息分页
     *
     * @param bo 护工从业证书信息
     * @return 护工从业证书信息
     */
    @Override
    public TableDataInfo<NursingCertificateVo> queryPageList(NursingCertificateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NursingCertificate> lqw = buildQueryWrapper(bo);
        Page<NursingCertificateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询护工从业证书信息列表
     *
     * @param bo 护工从业证书信息
     * @return 护工从业证书信息
     */
    @Override
    public List<NursingCertificateVo> queryList(NursingCertificateBo bo) {
        LambdaQueryWrapper<NursingCertificate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NursingCertificate> buildQueryWrapper(NursingCertificateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NursingCertificate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCertificateName()), NursingCertificate::getCertificateName, bo.getCertificateName());
        lqw.eq(StringUtils.isNotBlank(bo.getPictureUrl()), NursingCertificate::getPictureUrl, bo.getPictureUrl());
        return lqw;
    }

    /**
     * 查询护工从业证书信息
     *
     * @param id 护工从业证书信息主键
     * @return 护工从业证书信息
     */
    @Override
    public NursingCertificateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情护工从业证书信息
     *
     * @param id 护工从业证书信息主键
     * @return 护工从业证书信息
     */
    @Override
    public NursingCertificate loadById(Long id, Boolean tw){
        NursingCertificate info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(NursingCertificateExceptionEnum.NursingCertificate_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增护工从业证书信息
     *
     * @param bo 护工从业证书信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(NursingCertificateBo bo) {
        NursingCertificate add = BeanUtil.toBean(bo, NursingCertificate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改护工从业证书信息
     *
     * @param bo 护工从业证书信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(NursingCertificateBo bo) {
        NursingCertificate nursingCertificate = baseMapper.selectById(bo.getId());
        NursingCertificate update = BeanCopyUtils.copy(bo, nursingCertificate);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(NursingCertificate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除护工从业证书信息
     *
     * @param ids 需要删除的护工从业证书信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<NursingCertificate> queryListByWorkerId(Long workerId) {
        LambdaQueryWrapper<NursingCertificate> lqw = Wrappers.lambdaQuery();
        lqw.eq(NursingCertificate::getWorkerId, workerId);
        return baseMapper.selectList(lqw);
    }

    @Override
    public void deleteByWorkerId(Long workerId) {
        List<NursingCertificate> list = this.queryListByWorkerId(workerId);
        baseMapper.deleteBatchIds(CollectionUtils.convertList(list, NursingCertificate::getId));
    }

}
