package com.ichangzuo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ichangzuo.common.base.BasePageQuery;
import com.ichangzuo.core.security.util.SecurityUtils;
import com.ichangzuo.system.mapper.RealNameMapper;
import com.ichangzuo.system.model.entity.Message;
import com.ichangzuo.system.model.entity.RealNameInfo;
import com.ichangzuo.system.model.query.MessagePageQuery;
import com.ichangzuo.system.model.query.RealNameInfoPageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RealNameServiceImpl extends ServiceImpl<RealNameMapper, RealNameInfo> {
    @Autowired
    private RealNameMapper realNameMapper;

    public RealNameInfo getCurrentRealName() {
        Long userId = SecurityUtils.getUserId();

        return getOne(new LambdaQueryWrapper<RealNameInfo>()
                .eq(RealNameInfo::getUserId, userId)
        );
    }

    public boolean setVerifyRealName(Long uid, int state){
        return update(new LambdaUpdateWrapper<RealNameInfo>()
                .eq(RealNameInfo::getUserId, uid)
                .set(RealNameInfo::getIsName, state));
    }

    public IPage<RealNameInfo> listPage(RealNameInfoPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<RealNameInfo> page = new Page<>(pageNum, pageSize);
        Integer isName = queryParams.getIsName();

        LambdaQueryWrapper<RealNameInfo> lqw = new LambdaQueryWrapper<RealNameInfo>()
                .isNotNull(RealNameInfo::getIdentityCard)
                .orderByAsc(RealNameInfo::getUserId);
        if(isName>=1 && isName<=3){
            lqw.eq(RealNameInfo::getIsName, isName);
        }
        else{
            lqw.orderByDesc(RealNameInfo::getIsName);
        }

        return realNameMapper.selectPage(page, lqw);
    }
}
