package cn.iocoder.yudao.module.member.service.userexpand;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.userexpand.vo.UserExpandPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userexpand.vo.UserExpandSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.userexpand.UserExpandDO;
import cn.iocoder.yudao.module.member.dal.mysql.userexpand.UserExpandMapper;
import cn.iocoder.yudao.module.member.util.MemberConstants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_EXPAND_NOT_EXISTS;

/**
 * 用户拓展属性 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class UserExpandServiceImpl implements UserExpandService {

    @Resource
    private UserExpandMapper userExpandMapper;

    @Override
    public Long createUserExpand(UserExpandSaveReqVO createReqVO) {
        // 插入
        UserExpandDO userExpand = BeanUtils.toBean(createReqVO, UserExpandDO.class);
        userExpandMapper.insert(userExpand);
        // 返回
        return userExpand.getId();
    }

    @Override
    public void updateUserExpand(UserExpandSaveReqVO updateReqVO) {
        // 校验存在
        validateUserExpandExists(updateReqVO.getId());
        // 更新
        UserExpandDO updateObj = BeanUtils.toBean(updateReqVO, UserExpandDO.class);
        userExpandMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserExpand(Long id) {
        // 校验存在
        validateUserExpandExists(id);
        // 删除
        userExpandMapper.deleteById(id);
    }

    private void validateUserExpandExists(Long id) {
        if (userExpandMapper.selectById(id) == null) {
            throw exception(USER_EXPAND_NOT_EXISTS);
        }
    }

    @Override
    public UserExpandDO getUserExpand(Long id) {
        return userExpandMapper.selectById(id);
    }

    @Override
    public PageResult<UserExpandDO> getUserExpandPage(UserExpandPageReqVO pageReqVO) {
        return userExpandMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateUserExpandByuserId(Long userId, AppMemberUserUpdateReqVO reqVO) {
        UserExpandDO userExpandDO = userExpandMapper.selectOne(Wrappers.<UserExpandDO>lambdaQuery()
                .eq(UserExpandDO::getUserId, userId)
                .last(MemberConstants.LIMIT_ONE));
        if(userExpandDO == null){
            UserExpandDO add = new UserExpandDO();
            BeanUtil.copyProperties(reqVO,add);
            add.setUserId(userId);
            userExpandMapper.insert(add);
        }else {
            BeanUtil.copyProperties(reqVO,userExpandDO);
            userExpandMapper.updateById(userExpandDO);
        }
    }

    @Override
    public UserExpandDO getUserExpandByuserId(Long userId) {
        UserExpandDO userExpandDO = userExpandMapper.selectOne(Wrappers.<UserExpandDO>lambdaQuery()
                .eq(UserExpandDO::getUserId, userId)
                .last(MemberConstants.LIMIT_ONE));
        return userExpandDO;
    }
}