package com.community.huiju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TaUserMapper;
import com.community.huiju.dao.TaUserVerifyMapper;
import com.community.huiju.dao.UserMapper;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TaUserVerify;
import com.community.huiju.model.User;
import com.community.huiju.service.ITaUserVerifyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审核 业务实现
 *
 * @author weiximei
 */
@Service
public class TaUserVerifyServiceImpl extends ServiceImpl<TaUserVerifyMapper, TaUserVerify> implements ITaUserVerifyService {

    @Autowired
    private TaUserVerifyMapper taUserVerifyMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseBean selectAll(String phone, String userName, String idCard, Integer pageNum, Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();

        Page<TaUserVerify> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        IPage<TaUserVerify> userVerifyIPage = taUserVerifyMapper.selectAllByPhoneAndUserNameAndIdCard(page, phone, userName, idCard);
        responseBean.addSuccess(userVerifyIPage);

        return responseBean;
    }

    @Override
    public ResponseBean auditUser(UserElement userElement, Integer userVerifyId, Boolean verifyStatus) {
        ResponseBean responseBean = new ResponseBean();

        TaUserVerify userVerify = taUserVerifyMapper.selectById(userVerifyId);
        if (null == userVerify) {
            responseBean.addError("审核数据不存在！");
            return responseBean;
        }

        if (!Constant.TO_AUDIT.equals(userVerify.getVerifyStatus())) {
            responseBean.addError("审核状态错误！");
            return responseBean;
        }

        User user = userMapper.selectById(userElement.getId());
        String status = verifyStatus == true ? Constant.APPROVED : Constant.REJECTED;
        userVerify.setVerifyStatus(status);
        userVerify.setVerifyName(user.getUserName());

        taUserVerifyMapper.updateById(userVerify);
        responseBean.addSuccess("操作成功！");
        return responseBean;
    }
}
