package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.AppUserIdDTO;
import org.springblade.modules.app.common.query.AppUserIdQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.AppUserIdDO;
import org.springblade.modules.app.mapper.AppUserIdMapper;
import org.springblade.modules.app.service.IAppUserIdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * app用户标识表 服务实现类
 * </p>
 *
 * @author huyinuo
 * @since 2022-01-17
 */
@Service
public class AppUserIdServiceImpl extends ServiceImpl<AppUserIdMapper, AppUserIdDO> implements IAppUserIdService {

    @Override
    public List<AppUserIdDTO> listAppUserIdByUserId(AppUserIdQuery appUserIdQuery) {
        Validator.notNull(appUserIdQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<AppUserIdDO> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(appUserIdQuery.getUserId()), DbField.USER_ID, appUserIdQuery.getUserId());
        wrapper.eq(ObjectUtils.isNotEmpty(appUserIdQuery.getClientId()), DbField.CLIENT_ID, appUserIdQuery.getClientId());
        List<AppUserIdDO> appUserIdDOList = list(wrapper);
        return ModelConvertUtil.copyListTo(appUserIdDOList, AppUserIdDTO.class);
    }

    @Override
    public Long addAppUserId(AppUserIdDTO appUserIdDTO) {
        Validator.notNull(appUserIdDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        AppUserIdDO appUserIdDO = ModelConvertUtil.copyTo(appUserIdDTO, AppUserIdDO.class);
        Validator.isTrue(save(appUserIdDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return appUserIdDO.getId();
    }

    @Override
    public Boolean updateAppUserId(AppUserIdDTO appUserIdDTO) {
        Validator.notNull(appUserIdDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        AppUserIdDO appUserIdDO = ModelConvertUtil.copyTo(appUserIdDTO, AppUserIdDO.class);
        Validator.isTrue(updateById(appUserIdDO), ErrorCode.UPDATE_DATA_ERROR);
        return true;
    }

    @Override
    public AppUserIdDTO getAppUserIdDTO(AppUserIdQuery appUserIdQuery) {
        Validator.notNull(appUserIdQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(appUserIdQuery.getUserId(), ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(appUserIdQuery.getClientId(), ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<AppUserIdDO> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(appUserIdQuery.getUserId()), DbField.USER_ID, appUserIdQuery.getUserId());
        wrapper.eq(ObjectUtils.isNotEmpty(appUserIdQuery.getClientId()), DbField.CLIENT_ID, appUserIdQuery.getClientId());
        AppUserIdDO appUserIdDO = getOne(wrapper);
        return ModelConvertUtil.copyTo(appUserIdDO, AppUserIdDTO.class);
    }
}
