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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.modules.account.domain.AccountManageQuery;
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.account.service.IAccountManageService;
import com.ruoyi.web.modules.account.service.IAccountProfileService;
import com.ruoyi.web.modules.account.support.AccountManageSupport;
import com.ruoyi.web.modules.account.util.MemberMobileUtils;
import com.ruoyi.web.modules.account.vo.AccountManageDetailVO;
import com.ruoyi.web.modules.account.vo.AccountManageListVO;
import com.ruoyi.web.modules.store.domain.BizMerchantAccount;
import com.ruoyi.web.modules.store.mapper.BizMerchantAccountMapper;

/**
 * 账号管理 Service 实现
 */
@Service
public class AccountManageServiceImpl implements IAccountManageService
{
    @Autowired
    private AccountManageMapper accountManageMapper;

    @Autowired
    private AccountManageSupport accountManageSupport;

    @Autowired
    private IAccountProfileService accountProfileService;

    @Autowired
    private BizMerchantAccountMapper merchantAccountMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<AccountManageListVO> selectAccountList(AccountManageQuery query)
    {
        if (query == null)
        {
            query = new AccountManageQuery();
        }
        List<AccountManageRow> rows = accountManageMapper.selectList(query);
        List<AccountManageListVO> result = new ArrayList<>();
        for (AccountManageRow row : rows)
        {
            result.add(toListVo(row, true));
        }
        return result;
    }

    @Override
    public AccountManageDetailVO selectAccountDetail(Long userId)
    {
        AccountManageRow row = accountManageSupport.requireAccountRow(userId);
        return toDetailVo(row);
    }

    @Override
    public Map<String, Object> updateProfile(AccountProfileUpdateDTO dto, String operator)
    {
        return accountProfileService.updateProfile(dto, operator);
    }

    @Override
    public void updatePassword(AccountPasswordUpdateDTO dto, String operator)
    {
        accountProfileService.updatePassword(dto, operator);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAccounts(Long[] userIds, Long currentUserId, String operator)
    {
        for (Long userId : userIds)
        {
            accountManageSupport.assertDeletable(userId, currentUserId);
        }
        for (Long userId : userIds)
        {
            BizMerchantAccount binding = accountManageSupport.findMerchantAdminBinding(userId);
            if (binding != null)
            {
                merchantAccountMapper.logicDeleteByUserId(userId, operator);
            }
            else
            {
                sysUserMapper.deleteUserById(userId);
            }
        }
    }

    private AccountManageListVO toListVo(AccountManageRow row, boolean maskMobile)
    {
        AccountManageListVO vo = new AccountManageListVO();
        vo.setUserId(row.getUserId());
        vo.setAccountName(accountManageSupport.resolveAccountName(row.getNickName(), row.getUserName()));
        String mobile = accountManageSupport.resolveMobile(row);
        vo.setMobile(maskMobile ? MemberMobileUtils.mask(mobile) : mobile);
        vo.setEmail(row.getEmail());
        vo.setShopCount(row.getShopCount());
        vo.setCreateTime(row.getCreateTime());
        return vo;
    }

    private AccountManageDetailVO toDetailVo(AccountManageRow row)
    {
        AccountManageDetailVO vo = new AccountManageDetailVO();
        vo.setUserId(row.getUserId());
        vo.setMerchantId(row.getMerchantId());
        vo.setAccountName(accountManageSupport.resolveAccountName(row.getNickName(), row.getUserName()));
        vo.setMobile(accountManageSupport.resolveMobile(row));
        vo.setEmail(row.getEmail());
        vo.setShopCount(row.getShopCount());
        vo.setCreateTime(row.getCreateTime());
        return vo;
    }
}
