package com.jeesharp.business.modules.marketing.memberTenant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.marketing.memberTenant.entity.MemberTenant;
import com.jeesharp.business.modules.marketing.memberTenant.enums.MemberTenantBindStatusEnum;
import com.jeesharp.business.modules.marketing.memberTenant.enums.MemberTenantRoleEnum;
import com.jeesharp.business.modules.marketing.memberTenant.mapper.MemberTenantMapper;
import com.jeesharp.business.modules.marketing.memberTenant.service.MemberTenantService;
import com.jeesharp.business.modules.marketing.memberTenant.vo.MemberTenantQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 会员-店铺租户关系领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Service
public class MemberTenantServiceImpl extends CrudService<MemberTenantMapper, MemberTenant, MemberTenantQueryVO>
        implements MemberTenantService {

    @Override
    public MemberTenant createMemberTenant(MemberTenant memberTenant) {
        if (StrUtil.isNotBlank(memberTenant.getId())) {
            throw new BizErrorException("新增会员-店铺关系不应预置主键");
        }
        if (memberTenant.getBindStatus() == null) {
            memberTenant.setBindStatus(MemberTenantBindStatusEnum.BOUND.getValue());
        }
        if (StrUtil.isBlank(memberTenant.getMemberRole())) {
            memberTenant.setMemberRole(MemberTenantRoleEnum.CUSTOMER.getValue());
        }
        getBaseMapper().insert(memberTenant);
        return memberTenant;
    }

    @Override
    public void updateMemberTenant(MemberTenant memberTenant) {
        if (StrUtil.isBlank(memberTenant.getId())) {
            throw new BizErrorException("更新会员-店铺关系主键不能为空");
        }
        if (get(memberTenant.getId()) == null) {
            throw new BizErrorException("会员-店铺关系不存在");
        }
        getBaseMapper().update(memberTenant);
    }

    @Override
    public MemberTenant getByMemberAndTenant(String memberId, String tenantId) {
        if (StrUtil.isBlank(memberId) || StrUtil.isBlank(tenantId)) {
            return null;
        }
        return getBaseMapper().getByMemberAndTenant(memberId, tenantId);
    }

    @Override
    public List<MemberTenant> listBoundByMemberId(String memberId) {
        MemberTenantQueryVO query = new MemberTenantQueryVO();
        query.setMemberId(memberId);
        query.setBindStatus(MemberTenantBindStatusEnum.BOUND.getValue());
        return findList(query);
    }

    @Override
    public List<MemberTenant> listClerkTenantsByMemberId(String memberId) {
        MemberTenantQueryVO query = new MemberTenantQueryVO();
        query.setMemberId(memberId);
        query.setBindStatus(MemberTenantBindStatusEnum.BOUND.getValue());
        query.setMemberRoleIn(Arrays.asList(
                MemberTenantRoleEnum.CLERK.getValue(),
                MemberTenantRoleEnum.MANAGER.getValue()));
        return findList(query);
    }

    @Override
    public void assignMemberRole(String id, String memberRole) {
        if (StrUtil.isBlank(id)) {
            throw new BizErrorException("关系主键不能为空");
        }
        if (MemberTenantRoleEnum.fromValue(memberRole) == null) {
            throw new BizErrorException("店铺角色无效");
        }
        MemberTenant entity = get(id);
        if (entity == null) {
            throw new BizErrorException("店铺会员关系不存在");
        }
        entity.setMemberRole(memberRole);
        getBaseMapper().update(entity);
    }
}
