package org.dromara.work.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.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 org.springframework.stereotype.Service;
import org.dromara.work.domain.bo.TpUserAuthBo;
import org.dromara.work.domain.vo.TpUserAuthVo;
import org.dromara.work.domain.TpUserAuth;
import org.dromara.work.mapper.TpUserAuthMapper;
import org.dromara.work.service.ITpUserAuthService;

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

/**
 * 用户认证Service业务层处理
 *
 * @author Maosw
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class TpUserAuthServiceImpl extends MPJBaseServiceImpl<TpUserAuthMapper, TpUserAuth> implements ITpUserAuthService {

    private final TpUserAuthMapper baseMapper;

    /**
     * 查询用户认证
     *
     * @param id 主键
     * @return 用户认证
     */
    @Override
    public TpUserAuthVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户认证列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户认证分页列表
     */
    @Override
    public TableDataInfo<TpUserAuthVo> queryPageList(TpUserAuthBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TpUserAuth> lqw = buildQueryWrapper(bo);
        Page<TpUserAuthVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户认证列表
     *
     * @param bo 查询条件
     * @return 用户认证列表
     */
    @Override
    public List<TpUserAuthVo> queryList(TpUserAuthBo bo) {
        LambdaQueryWrapper<TpUserAuth> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TpUserAuth> buildQueryWrapper(TpUserAuthBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TpUserAuth> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TpUserAuth::getId);
        lqw.eq(bo.getUserId() != null, TpUserAuth::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), TpUserAuth::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), TpUserAuth::getIdCard, bo.getIdCard());
        lqw.eq(bo.getSex() != null, TpUserAuth::getSex, bo.getSex());
        lqw.eq(bo.getProfessionStartYear() != null, TpUserAuth::getProfessionStartYear, bo.getProfessionStartYear());
        lqw.eq(bo.getWorkType() != null, TpUserAuth::getWorkType, bo.getWorkType());
        lqw.eq(StringUtils.isNotBlank(bo.getRegion()), TpUserAuth::getRegion, bo.getRegion());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardFront()), TpUserAuth::getIdCardFront, bo.getIdCardFront());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardBack()), TpUserAuth::getIdCardBack, bo.getIdCardBack());
        lqw.eq(bo.getType() != null, TpUserAuth::getType, bo.getType());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TpUserAuth::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增用户认证
     *
     * @param bo 用户认证
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpUserAuthBo bo) {
        TpUserAuth add = MapstructUtils.convert(bo, TpUserAuth.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户认证
     *
     * @param bo 用户认证
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpUserAuthBo bo) {
        TpUserAuth update = MapstructUtils.convert(bo, TpUserAuth.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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