package ink.wgink.login.app.service.appdeviceuser.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.app.dao.appdeviceuser.IAppDeviceUserDao;
import ink.wgink.login.app.pojo.dtos.appdeviceuser.AppDeviceUserDTO;
import ink.wgink.login.app.pojo.pos.appdeviceuser.AppDeviceUserPO;
import ink.wgink.login.app.service.appdeviceuser.IAppDeviceUserService;
import ink.wgink.login.base.manager.ConfigManager;
import ink.wgink.util.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AppDeviceUserServiceImpl
 * @Description: 设备用户
 * @Author: wanggeng
 * @Date: 2021/4/7 4:21 下午
 * @Version: 1.0
 */
@Service
public class AppDeviceUserServiceImpl extends DefaultBaseService implements IAppDeviceUserService {

    @Autowired
    private IAppDeviceUserDao appDeviceUserDao;

    @Override
    public void save(String deviceNo, String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("deviceNo", deviceNo);
        params.put("userId", userId);
        params.put("gmtCreate", DateUtil.getTime());
        appDeviceUserDao.save(params);
    }

    @Override
    public List<AppDeviceUserDTO> list(Map<String, Object> params) {
        return appDeviceUserDao.list(params);
    }

    @Override
    public List<AppDeviceUserDTO> listByUserId(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        return list(params);
    }

    @Override
    public List<AppDeviceUserPO> listPO(Map<String, Object> params) {
        return appDeviceUserDao.listPO(params);
    }

    @Override
    public List<AppDeviceUserPO> listPOByUserId(String userId) {
        Map<String, Object> params = getHashMap(2);
        params.put("userId", userId);
        return listPO(params);
    }

    @Override
    public boolean canSign(String userId, String deviceNo) {
        Object appDeviceCountObject = ConfigManager.getInstance().getConfig().get("appDeviceCount");
        if (Objects.isNull(appDeviceCountObject)) {
            return true;
        }
        Integer appDeviceCount = Integer.parseInt(appDeviceCountObject.toString());
        if (appDeviceCount == 0) {
            return true;
        }
        List<AppDeviceUserPO> appDeviceUserPOs = listPOByUserId(userId);
        boolean canLogin = false;
        if (!appDeviceUserPOs.isEmpty()) {
            for (AppDeviceUserPO appDeviceUserPO : appDeviceUserPOs) {
                if (StringUtils.equals(deviceNo, appDeviceUserPO.getDeviceNo())) {
                    canLogin = true;
                    break;
                }
            }
        }
        if (!canLogin && appDeviceUserPOs.size() < appDeviceCount) {
            LOG.debug("绑定设备");
            save(deviceNo, userId);
            canLogin = true;
        }
        return canLogin;
    }

}
