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

import com.ruoyi.bst.storeStaff.domain.StoreStaff;
import com.ruoyi.bst.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.bst.storeStaff.domain.StoreStaffVO;
import com.ruoyi.bst.storeStaff.service.StoreStaffConverter;
import com.ruoyi.bst.storeStaff.service.StoreStaffService;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreStaffConverterImpl implements StoreStaffConverter {

    @Autowired
    private StoreStaffService storeStaffService;
    @Autowired
    private UserService userService;


    @Override
    public StoreStaff toPoByCreate(StoreStaffVO data) {
        if (data == null) {
            return null;
        }

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

        StoreStaff po = new StoreStaffQuery();
        po.setStoreId(data.getStoreId());
        po.setUserId(user.getUserId());
        po.setCreateId(data.getCreateId());
        po.setRemark(data.getRemark());
        po.setPermissions(data.getPermissions());
        return po;
    }

    @Override
    public StoreStaff toPoByUpdate(StoreStaffVO data) {
        if (data == null) {
            return null;
        }

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

        StoreStaff po = new StoreStaffQuery();
        po.setId(data.getId());
        po.setStoreId(data.getStoreId());
        po.setUserId(user.getUserId());
        po.setCreateId(data.getCreateId());
        po.setRemark(data.getRemark());
        po.setPermissions(data.getPermissions());
        return po;
    }

    private UserVO getUser(String phone) {
        UserVO user = userService.selectUserByUserName(phone);
        if (user == null) {
            user = userService.registerUser(phone, null, null);
        }
        return user;
    }
}
