package com.zhongshu.card.server.core.service.paySetting;

import com.github.microservice.net.ResultContent;
import com.github.microservice.types.payment.PaymentType;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectMainPaySettingDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectOrgPaySettingInfoDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectPaySettingDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.paySetting.ProjectOrgPaySettingInfo;
import com.zhongshu.card.server.core.domain.paySetting.ProjectPaySetting;
import com.zhongshu.card.server.core.service.base.SuperService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构、项目支付配置判断
 *
 * @author TRX
 * @date 2024/11/19
 */
@Slf4j
@Service
public class OrgPaySettingJudgmentService extends SuperService {

    @Autowired
    private ProjectOrgPaySettingInfoDao projectPaySettingInfoDao;

    @Autowired
    private ProjectMainPaySettingDao projectMainPaySettingDao;

    @Autowired
    private ProjectPaySettingDao projectPaySettingDao;

    @Autowired
    private OrganizationDao organizationDao;

    /**
     * 设备 绑定机构判定
     *
     * @param projectOid
     * @param oid
     * @return
     */
    public ResultContent checkOrgPaySetting(String projectOid, String oid) {
        // 检查项目上是否设置了支付产品
        List<ProjectPaySetting> list = projectPaySettingDao.findByProjectOid(projectOid);
        if (ObjectUtils.isEmpty(list)) {
            return ResultContent.buildFail("项目上未配置支付产品");
        }
        List<ProjectPaySetting> enableList = list.stream().filter(it -> {
            return it.getState() == DataState.Enable;
        }).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(enableList)) {
            return ResultContent.buildFail("项目上未启用支付产品");
        }
        Organization organization = organizationDao.findTopByOid(oid);
        if (ObjectUtils.isEmpty(organization)) {
            return ResultContent.buildFail(String.format("机构不存在"));
        }
        // 检查机构
        for (ProjectPaySetting paySetting : enableList) {
            if (paySetting.getChannelType() != PaymentType.WeChat) {
                ProjectOrgPaySettingInfo projectOrgPaySettingInfo = projectPaySettingInfoDao.findTopByBelongOigAndProjectOidAndChannelType(oid, projectOid, paySetting.getChannelType());
                if (ObjectUtils.isEmpty(projectOrgPaySettingInfo)) {
                    return ResultContent.buildFail(String.format("【%s】支付设备未配置%s", organization.getName(), paySetting.getChannelType().getRemark()));
                }
                if (projectOrgPaySettingInfo.getIsDefault() != null && projectOrgPaySettingInfo.getIsDefault()) {
                    return ResultContent.buildFail("不能绑定主账户");
                }
            }
        }
        return ResultContent.buildSuccess();
    }

}
