package org.dromara.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.work.domain.TpStaff;
import org.dromara.work.domain.bo.TpStaffBo;
import org.dromara.work.domain.vo.TpStaffVo;
import org.dromara.work.mapper.TpStaffMapper;
import org.dromara.work.service.ITpStaffService;
import org.springframework.stereotype.Service;

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

/**
 * 人员Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpStaffServiceImpl implements ITpStaffService {

    private final TpStaffMapper baseMapper;

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

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

    /**
     * 查询符合条件的人员列表
     *
     * @param bo 查询条件
     * @return 人员列表
     */
    @Override
    public List<TpStaffVo> queryList(TpStaffBo bo) {
        LambdaQueryWrapper<TpStaff> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TpStaff> buildQueryWrapper(TpStaffBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TpStaff> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), TpStaff::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), TpStaff::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TpStaff::getName, bo.getName());
        lqw.eq(bo.getCid() != null, TpStaff::getCid, bo.getCid());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TpStaff::getPhone, bo.getPhone());
        lqw.eq(bo.getYear() != null, TpStaff::getYear, bo.getYear());
        lqw.eq(bo.getWork() != null, TpStaff::getWork, bo.getWork());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TpStaff::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), TpStaff::getPassword, bo.getPassword());
        lqw.eq(bo.getIdentity() != null, TpStaff::getIdentity, bo.getIdentity());
        lqw.eq(bo.getRid() != null, TpStaff::getRid, bo.getRid());
        lqw.eq(bo.getState() != null, TpStaff::getState, bo.getState());
        lqw.eq(bo.getIsDel() != null, TpStaff::getIsDel, bo.getIsDel());
        lqw.eq(bo.getAddTime() != null, TpStaff::getAddTime, bo.getAddTime());
        lqw.eq(bo.getYue() != null, TpStaff::getYue, bo.getYue());
        lqw.eq(bo.getUpType() != null, TpStaff::getUpType, bo.getUpType());
        lqw.eq(bo.getUrid() != null, TpStaff::getUrid, bo.getUrid());
        return lqw;
    }

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

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

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

    /**
     * 校验并批量删除人员信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
