package com.fuyuanshen.app.service.impl;

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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.fuyuanshen.app.domain.bo.AppPersonnelInfoRecordsBo;
import com.fuyuanshen.app.domain.vo.AppPersonnelInfoRecordsVo;
import com.fuyuanshen.app.domain.AppPersonnelInfoRecords;
import com.fuyuanshen.app.mapper.AppPersonnelInfoRecordsMapper;
import com.fuyuanshen.app.service.IAppPersonnelInfoRecordsService;

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

/**
 * 人员信息登记记录Service业务层处理
 *
 * @author CYT
 * @date 2025-08-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppPersonnelInfoRecordsServiceImpl implements IAppPersonnelInfoRecordsService {

    private final AppPersonnelInfoRecordsMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<AppPersonnelInfoRecords> buildQueryWrapper(AppPersonnelInfoRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppPersonnelInfoRecords> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppPersonnelInfoRecords::getId);
        lqw.eq(bo.getDeviceId() != null, AppPersonnelInfoRecords::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getPersonnelId() != null, AppPersonnelInfoRecords::getPersonnelId, bo.getPersonnelId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), AppPersonnelInfoRecords::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), AppPersonnelInfoRecords::getPosition, bo.getPosition());
        lqw.like(StringUtils.isNotBlank(bo.getUnitName()), AppPersonnelInfoRecords::getUnitName, bo.getUnitName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), AppPersonnelInfoRecords::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSendMsg()), AppPersonnelInfoRecords::getSendMsg, bo.getSendMsg());
        return lqw;
    }

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

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

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

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