package com.cool.store.service.close.impl;

import com.cool.store.dao.CloseStoreAccountInfoDAO;
import com.cool.store.dao.CloseStoreInfoDAO;
import com.cool.store.dao.CloseStoreStageInfoDAO;
import com.cool.store.entity.CloseStoreAccountInfoDO;
import com.cool.store.entity.CloseStoreInfoDO;
import com.cool.store.enums.close.*;
import com.cool.store.service.close.AccountCloseFactory;
import com.cool.store.service.close.AccountCloseStrategy;
import com.cool.store.service.close.CloseStoreAccountService;
import com.cool.store.service.close.CloseStoreService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.vo.close.account.CloseStoreAccountVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 闭店账号关闭 服务实现类
 * </p>
 *
 * @author wangff
 * @since 2025/10/8
 */
@Service
@RequiredArgsConstructor
public class CloseStoreAccountServiceImpl implements CloseStoreAccountService {
    private final CloseStoreAccountInfoDAO closeStoreAccountInfoDAO;
    private final AccountCloseFactory accountCloseFactory;
    private final CloseStoreStageInfoDAO closeStoreStageInfoDAO;
    private final CloseStoreService closeStoreService;
    private final CloseStoreInfoDAO closeStoreInfoDAO;

    @Override
    public List<CloseStoreAccountVO> getAccountList(Long closeStoreId) {
        List<CloseStoreAccountInfoDO> list = closeStoreAccountInfoDAO.getByCloseStoreId(closeStoreId);
        return BeanUtil.toList(list, CloseStoreAccountVO.class);
    }

    @Override
    public Boolean closeAccount(Long id) {
        CloseStoreAccountInfoDO accountInfo = closeStoreAccountInfoDAO.getById(id);
        if (Objects.nonNull(accountInfo)) {
            closeStoreStageInfoDAO.verifyStage(accountInfo.getCloseStoreId(), CloseStoreStageEnum.CLOSE_STORE_STAGE_2, CloseStoreSubStageEnum.CLOSE_STORE_SUB_STAGE_20, CloseStoreSubStageStatusEnum.CLOSE_STORE_SUB_STAGE_STATUS_25);
            if (AccountCloseTypeEnum.APPLY_CLOSE.getType().equals(accountInfo.getCloseType())) {
                CloseStoreInfoDO closeStoreInfoDO = closeStoreInfoDAO.getById(accountInfo.getCloseStoreId());
                // 申请关闭
                AccountCloseStrategy accountClose = accountCloseFactory.getByCode(accountInfo.getSystemCode());
                if (Objects.nonNull(accountClose) && accountClose.accountClose(closeStoreInfoDO.getStoreId())) {
                    closeStoreAccountInfoDAO.updateStatus(id, CloseStoreAccountStatusEnum.CLOSED.getStatus());
                    verifyAndUpdateStage(accountInfo.getCloseStoreId());
                    return true;
                }
            } else {
                // 直接关闭
                closeStoreAccountInfoDAO.updateStatus(id, CloseStoreAccountStatusEnum.CLOSED.getStatus());
                verifyAndUpdateStage(accountInfo.getCloseStoreId());
                return true;
            }
        }
        return false;
    }

    @Override
    public void verifyAndUpdateStage(Long closeStoreId) {
        if (!closeStoreAccountInfoDAO.existNotCloseAccount(closeStoreId)) {
            // 所有账号已关闭，校验当前阶段
            CloseStoreStageEnum currentStage = CloseStoreStageEnum.CLOSE_STORE_STAGE_2;
            CloseStoreSubStageEnum currentSubStage = CloseStoreSubStageEnum.CLOSE_STORE_SUB_STAGE_20;
            CloseStoreSubStageStatusEnum currentExpectedSubStageStatus = CloseStoreSubStageStatusEnum.CLOSE_STORE_SUB_STAGE_STATUS_25;
            closeStoreStageInfoDAO.verifyStage(closeStoreId, currentStage, currentSubStage, currentExpectedSubStageStatus);
            // 流程流转
            closeStoreService.closeStoreProcessFlow(closeStoreId, currentSubStage);
        }
    }
}
