package com.ruoyi.bst.areaJoin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.areaJoin.domain.AreaJoin;
import com.ruoyi.bst.areaJoin.domain.AreaJoinVO;
import com.ruoyi.bst.areaJoin.service.AreaJoinConverter;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.system.user.service.UserService;

@Service
public class AreaJoinConverterImpl implements AreaJoinConverter {

    @Autowired
    private UserService userService;

    @Override
    public AreaJoin toPoByCreate(AreaJoinVO data) {
        if (data == null) {
            return null;
        }

        UserVO user = this.getUser(data.getUserPhone());
        ServiceUtil.assertion(user == null, "账号为%s的用户不存在", data.getUserPhone());

        AreaJoin po = new AreaJoin();
        po.setAreaId(data.getAreaId());
        po.setType(data.getType());
        po.setUserId(user.getUserId());
        po.setPoint(data.getPoint());
        po.setCreateId(data.getCreateId());
        po.setRemark(data.getRemark());
        po.setPermissions(data.getPermissions());
        return po;
    }

    @Override
    public AreaJoin toPoByUpdate(AreaJoinVO data) {
        if (data == null) {
            return null;
        }

        UserVO user = this.getUser(data.getUserPhone());
        ServiceUtil.assertion(user == null, "账号为%s的用户不存在", data.getUserPhone());

        AreaJoin po = new AreaJoin();
        po.setId(data.getId());
        po.setAreaId(data.getAreaId());
        po.setType(data.getType());
        po.setUserId(user.getUserId());
        po.setPoint(data.getPoint());
        po.setCreateId(data.getCreateId());
        po.setRemark(data.getRemark());
        po.setPermissions(data.getPermissions());
        return po;
    }

    private UserVO getUser(String phone) {
        return userService.getOrCreateByPhone(phone);
    }
}