package com.fuyuanshen.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fuyuanshen.common.core.utils.MapstructUtils;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.mybatis.core.page.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 com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.dto.AppDeviceBo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.fuyuanshen.app.domain.bo.AppPersonnelInfoBo;
import com.fuyuanshen.app.domain.vo.AppPersonnelInfoVo;
import com.fuyuanshen.app.domain.AppPersonnelInfo;
import com.fuyuanshen.app.mapper.AppPersonnelInfoMapper;
import com.fuyuanshen.app.service.IAppPersonnelInfoService;

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

/**
 * 人员信息登记Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-02
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppPersonnelInfoServiceImpl implements IAppPersonnelInfoService {

    private final AppPersonnelInfoMapper baseMapper;

    /**
     * 查询人员信息登记
     *
     * @param id 主键
     * @return 人员信息登记
     */
    @Override
    public AppPersonnelInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询人员信息登记列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 人员信息登记分页列表
     */
    @Override
    public TableDataInfo<AppPersonnelInfoVo> queryPageList(AppPersonnelInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppPersonnelInfo> lqw = buildQueryWrapper(bo);
        Page<AppPersonnelInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的人员信息登记列表
     *
     * @param bo 查询条件
     * @return 人员信息登记列表
     */
    @Override
    public List<AppPersonnelInfoVo> queryList(AppPersonnelInfoBo bo) {
        LambdaQueryWrapper<AppPersonnelInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppPersonnelInfo> buildQueryWrapper(AppPersonnelInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppPersonnelInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, AppPersonnelInfo::getDeviceId, bo.getDeviceId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), AppPersonnelInfo::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getUnitName()), AppPersonnelInfo::getUnitName, bo.getUnitName());
        lqw.eq(StringUtils.isNotBlank(bo.getSendMsg()), AppPersonnelInfo::getSendMsg, bo.getSendMsg());
        return lqw;
    }

    /**
     * 新增人员信息登记
     *
     * @param bo 人员信息登记
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppPersonnelInfoBo bo) {
        AppPersonnelInfo add = MapstructUtils.convert(bo, AppPersonnelInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改人员信息登记
     *
     * @param bo 人员信息登记
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppPersonnelInfoBo bo) {
        AppPersonnelInfo update = MapstructUtils.convert(bo, AppPersonnelInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除人员信息登记信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    @Override
    public boolean registerPersonInfo(AppPersonnelInfoBo bo) {

        AppPersonnelInfo appPersonnelInfo = MapstructUtils.convert(bo, AppPersonnelInfo.class);

        return baseMapper.insertOrUpdate(appPersonnelInfo);
    }
}
