package com.ruoyi.web.modules.account.support;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.modules.account.constant.AccountManageConstants;
import com.ruoyi.web.modules.account.domain.AccountManageRow;
import com.ruoyi.web.modules.account.dto.AccountPasswordUpdateDTO;
import com.ruoyi.web.modules.account.dto.AccountProfileUpdateDTO;
import com.ruoyi.web.modules.account.mapper.AccountManageMapper;
import com.ruoyi.web.modules.store.domain.BizMerchantAccount;
import com.ruoyi.web.modules.store.mapper.BizMerchantAccountMapper;

/**
 * 账号管理校验与展示辅助
 */
@Component
public class AccountManageSupport
{
    @Autowired
    private AccountManageMapper accountManageMapper;

    @Autowired
    private BizMerchantAccountMapper merchantAccountMapper;

    @Autowired
    private ISysUserService sysUserService;

    public String resolveAccountName(String nickName, String userName)
    {
        if (StringUtils.isNotEmpty(nickName))
        {
            return nickName;
        }
        return userName;
    }

    public boolean isMerchantAdmin(AccountManageRow row)
    {
        return row != null && row.getMerchantId() != null;
    }

    public boolean isMerchantAdmin(Long userId)
    {
        return merchantAccountMapper.selectMerchantAdminByUserId(userId) != null;
    }

    public String resolveMobile(AccountManageRow row)
    {
        if (isMerchantAdmin(row))
        {
            return row.getUserName();
        }
        if (StringUtils.isNotEmpty(row.getPhonenumber()))
        {
            return row.getPhonenumber();
        }
        return row.getUserName();
    }

    public String resolveMobile(SysUser user, boolean merchantAdmin)
    {
        if (merchantAdmin)
        {
            return user.getUserName();
        }
        if (StringUtils.isNotEmpty(user.getPhonenumber()))
        {
            return user.getPhonenumber();
        }
        return user.getUserName();
    }

    public BizMerchantAccount findMerchantAdminBinding(Long userId)
    {
        return merchantAccountMapper.selectMerchantAdminByUserId(userId);
    }

    public AccountManageRow requireAccountRow(Long userId)
    {
        AccountManageRow row = accountManageMapper.selectByUserId(userId);
        if (row == null)
        {
            throw new ServiceException(AccountManageConstants.MSG_NOT_FOUND);
        }
        return row;
    }

    public SysUser requireSysUser(Long userId)
    {
        SysUser user = sysUserService.selectUserById(userId);
        if (user == null || !"0".equals(user.getDelFlag()))
        {
            throw new ServiceException(AccountManageConstants.MSG_NOT_FOUND);
        }
        return user;
    }

    public void validateProfileUpdate(AccountProfileUpdateDTO dto)
    {
        if (dto == null || dto.getUserId() == null)
        {
            throw new ServiceException(AccountManageConstants.MSG_NOT_FOUND);
        }
        if (StringUtils.isEmpty(dto.getAccountName()))
        {
            throw new ServiceException(AccountManageConstants.MSG_ACCOUNT_NAME_REQUIRED);
        }
        if (StringUtils.isEmpty(dto.getMobile()))
        {
            throw new ServiceException(AccountManageConstants.MSG_MOBILE_REQUIRED);
        }
        validateMobile(dto.getMobile());
        validateEmail(dto.getEmail());
    }

    public void validateMobile(String mobile)
    {
        if (StringUtils.isEmpty(mobile) || !mobile.matches("^1\\d{10}$"))
        {
            throw new ServiceException(AccountManageConstants.MSG_MOBILE_INVALID);
        }
    }

    public void validateEmail(String email)
    {
        if (StringUtils.isEmpty(email))
        {
            return;
        }
        if (!email.matches("^[\\w.-]+@[\\w.-]+\\.\\w+$"))
        {
            throw new ServiceException(AccountManageConstants.MSG_EMAIL_INVALID);
        }
    }

    public void validatePasswordUpdate(AccountPasswordUpdateDTO dto)
    {
        if (dto == null || dto.getUserId() == null)
        {
            throw new ServiceException(AccountManageConstants.MSG_NOT_FOUND);
        }
        if (StringUtils.isEmpty(dto.getPassword()))
        {
            throw new ServiceException(AccountManageConstants.MSG_PASSWORD_REQUIRED);
        }
        if (StringUtils.isEmpty(dto.getConfirmPassword())
                || !dto.getPassword().equals(dto.getConfirmPassword()))
        {
            throw new ServiceException(AccountManageConstants.MSG_PASSWORD_MISMATCH);
        }
        if (dto.getPassword().length() < AccountManageConstants.MIN_PASSWORD_LENGTH)
        {
            throw new ServiceException(AccountManageConstants.MSG_PASSWORD_SHORT);
        }
    }

    public void assertDeletable(Long userId, Long currentUserId)
    {
        requireAccountRow(userId);
        if (userId.equals(currentUserId))
        {
            throw new ServiceException(AccountManageConstants.MSG_CANNOT_DELETE_SELF);
        }
        if (AccountManageConstants.BUILTIN_ADMIN_USER_ID.equals(userId))
        {
            throw new ServiceException(AccountManageConstants.MSG_CANNOT_DELETE_BUILTIN);
        }
        AccountManageRow row = accountManageMapper.selectByUserId(userId);
        if (isMerchantAdmin(row) && row.getShopCount() != null && row.getShopCount() > 0)
        {
            throw new ServiceException(AccountManageConstants.MSG_HAS_SHOPS);
        }
    }
}
