package com.ruoyi.web.modules.merchant.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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.MemberConstants;
import com.ruoyi.web.modules.account.domain.BizMember;
import com.ruoyi.web.modules.account.mapper.BizMemberMapper;
import com.ruoyi.web.modules.account.support.MemberSysUserSupport;
import com.ruoyi.web.modules.merchant.constant.MerchantBindConstants;
import com.ruoyi.web.modules.merchant.dto.MerchantCreateDTO;
import com.ruoyi.web.modules.merchant.mapper.MerchantBindMapper;
import com.ruoyi.web.modules.merchant.service.IMerchantAccountBindService;
import com.ruoyi.web.modules.store.constant.ShopConstants;
import com.ruoyi.web.modules.store.domain.BizMerchantAccount;
import com.ruoyi.web.modules.store.mapper.BizMerchantAccountMapper;

@Service
public class MerchantAccountBindServiceImpl implements IMerchantAccountBindService
{
    @Autowired
    private MerchantBindMapper merchantBindMapper;
    @Autowired
    private BizMerchantAccountMapper merchantAccountMapper;

    @Autowired
    private BizMemberMapper memberMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private MemberSysUserSupport memberSysUserSupport;

    @Override
    public void bindAccountOnMerchantCreate(Long merchantId, MerchantCreateDTO dto, String operator)
    {
        validateBindRequest(dto);
        if (MerchantBindConstants.BIND_SYS_USER.equals(dto.getBindType()))
        {
            bindFromSysUser(merchantId, dto.getBindUserId(), operator);
        }
        else if (MerchantBindConstants.BIND_MEMBER.equals(dto.getBindType()))
        {
            bindFromMember(merchantId, dto, operator);
        }
        else
        {
            throw new ServiceException(MerchantBindConstants.MSG_BIND_REQUIRED);
        }
    }

    private void bindFromSysUser(Long merchantId, Long userId, String operator)
    {
        if (userId == null)
        {
            throw new ServiceException(MerchantBindConstants.MSG_BIND_USER_REQUIRED);
        }
        SysUser user = sysUserService.selectUserById(userId);
        if (user == null)
        {
            throw new ServiceException(MerchantBindConstants.MSG_ADMIN_NOT_FOUND);
        }
        if (merchantBindMapper.countUserHasRole(userId, MerchantBindConstants.MERCHANT_ADMIN_ROLE_KEY) <= 0)
        {
            throw new ServiceException(MerchantBindConstants.MSG_ADMIN_NO_ROLE);
        }
        assertAccountNotBound(userId);
        BizMerchantAccount account = new BizMerchantAccount();
        account.setAccountId(userId);
        account.setMerchantId(merchantId);
        account.setDelFlag(ShopConstants.DEL_NORMAL);
        account.setCreateBy(operator);
        merchantAccountMapper.insert(account);
    }

    private void bindFromMember(Long merchantId, MerchantCreateDTO dto, String operator)
    {
        Long memberId = dto.getBindMemberId();
        if (memberId == null)
        {
            throw new ServiceException(MerchantBindConstants.MSG_BIND_MEMBER_REQUIRED);
        }
        BizMember member = memberMapper.selectById(memberId);
        if (member == null)
        {
            throw new ServiceException(MerchantBindConstants.MSG_MEMBER_NOT_FOUND);
        }
        SysUser user = sysUserService.selectUserById(memberId);
        if (user == null)
        {
            throw new ServiceException("会员账号不存在，无法绑定经营账号");
        }
        if (merchantBindMapper.countUserHasRole(memberId, MemberConstants.MEMBER_ROLE_KEY) <= 0)
        {
            throw new ServiceException("所选用户不是会员账号");
        }
        memberSysUserSupport.grantRoleIfAbsent(memberId, MerchantBindConstants.MERCHANT_ADMIN_ROLE_KEY);

        if (StringUtils.isEmpty(member.getMemberCode()))
        {
            throw new ServiceException("会员名称不能为空，无法作为经营账号登录名");
        }
        if (StringUtils.isEmpty(member.getNickName()))
        {
            throw new ServiceException("会员昵称不能为空，无法作为经营账号管理员姓名");
        }
        assertAccountNotBound(memberId);

        BizMerchantAccount account = new BizMerchantAccount();
        account.setAccountId(memberId);
        account.setMerchantId(merchantId);
        account.setDelFlag(ShopConstants.DEL_NORMAL);
        account.setCreateBy(operator);
        merchantAccountMapper.insert(account);
    }

    private void validateBindRequest(MerchantCreateDTO dto)
    {
        if (dto == null || StringUtils.isEmpty(dto.getBindType()))
        {
            throw new ServiceException(MerchantBindConstants.MSG_BIND_REQUIRED);
        }
    }

    private void assertAccountNotBound(Long accountId)
    {
        if (merchantAccountMapper.countByAccountId(accountId) > 0)
        {
            throw new ServiceException(MerchantBindConstants.MSG_ACCOUNT_ALREADY_BOUND);
        }
    }
}
