package com.fzm.mall.server.admin.user_center.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.fzm.mall.server.admin.config.Redis;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.home_page.enums.status.MerchantRegisterEnum;
import com.fzm.mall.server.admin.home_page.model.Merchant;
import com.fzm.mall.server.admin.user_center.entity.dto.UserLockDTO;
import com.fzm.mall.server.admin.user_center.entity.vo.UserVO;
import com.fzm.mall.server.admin.user_center.enums.status.UserLockStatusEnum;
import com.fzm.mall.server.admin.user_center.mapper.UserMapper;
import com.fzm.mall.server.admin.user_center.model.User;
import com.fzm.mall.server.admin.user_center.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wangp
 * @date 2021/3/22 14:33
 * @description 用户信息表 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final Redis redis;

    @Override
    public IPage<UserVO> page(String uid, String account, Integer pageNum, Integer pageSize) {
        IPage<UserVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.page(page, uid, account);
    }

    @Override
    public void lock(UserLockDTO lockDTO) {
        Integer lock = lockDTO.getLock();
        String uid = lockDTO.getUid();

        //用户所定或解锁
        UpdateWrapper<User> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(User::getStatus, lock)
                .eq(User::getUid, uid);
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }

        //用户锁定后，token立即无效
        if (lock.equals(UserLockStatusEnum.LOCKING.getStatus())) {
            redis.del(uid + MALLGlobalConfig.MALL_LOGIN_SUFFIX);
        }
    }

    @Override
    public User queryByPhone(String phone) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(User::getPhone, phone)
                .eq(User::getStatus, UserLockStatusEnum.NORMAL.getStatus());
        return getOne(qw);
    }

    @Override
    public User queryByUid(String uid) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(User::getUid, uid)
                .eq(User::getStatus, UserLockStatusEnum.NORMAL.getStatus());
        return getOne(qw);
    }

    @Override
    public String getAllUserLinkPhone() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getStatus, UserLockStatusEnum.NORMAL.getStatus());

        JSONArray arr = new JSONArray();
        list(qw).forEach(v -> arr.add(v.getPhone()));
        return arr.toJSONString();
    }

    @Override
    public User queryByAddress(String address) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getAddress, address);
        return getOne(qw);
    }
}
