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

import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.zhongshu.card.client.model.org.OrganizationRelationSearch;
import com.zhongshu.card.client.model.school.ShopToSchoolModel;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.school.SchoolBingShopService;
import com.zhongshu.card.client.type.OrganizationRelationType;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.org.OrganizationRelationDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.org.OrganizationRelation;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.org.OrganizationServiceImpl;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * 学校绑定商户的管理服务
 *
 * @author TRX
 * @date 2024/6/11
 */
@Slf4j
@Service
public class SchoolBingShopServiceImpl extends SuperService implements SchoolBingShopService {

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    OrganizationRelationDao organizationRelationDao;

    @Autowired
    OrganizationServiceImpl organizationService;

    /**
     * 商户绑定学校
     *
     * @param shopOid
     * @return
     */
    @Override
    public ResultContent schoolBingShop(String shopOid) {
        OrganizationRelation relation = null;
        String oid = getCurrentOid();
        // 学校信息
        Organization mainOrganization = organizationDao.findTopByOid(oid);
        if (mainOrganization.getAuthType() != AuthType.School) {
            return ResultContent.buildFail(String.format("当前机构不属于学校类型"));
        }
        // 商户信息
        Organization relOrganization = organizationDao.findTopByOid(shopOid);
        if (ObjectUtils.isEmpty(relOrganization)) {
            return ResultContent.buildFail(String.format("商户oid不存在：%s", shopOid));
        }
        relation = organizationRelationDao.findTopByMainOrganizationAndRelOrganizationAndRelationType(mainOrganization, relOrganization, OrganizationRelationType.ShopToSchool);
        if (ObjectUtils.isEmpty(relation)) {
            relation = new OrganizationRelation();
            initEntity(relation);
        }

        relation.setMainOrganization(mainOrganization);
        relation.setMainName(mainOrganization.getName());
        relation.setRelOrganization(relOrganization);
        relation.setRelName(relOrganization.getName());
        relation.setAuthType(mainOrganization.getAuthType());
        relation.setRelationType(OrganizationRelationType.ShopToSchool);
        organizationRelationDao.save(relation);
        return ResultContent.buildSuccess();
    }

    /**
     * 学校解绑商户信息
     *
     * @param shopOid
     * @return
     */
    @Override
    public ResultContent deleteSchoolBingShop(String shopOid) {
        String oid = getCurrentOid();
        // 学校信息
        Organization mainOrganization = organizationDao.findTopByOid(oid);
        if (mainOrganization.getAuthType() != AuthType.School) {
            return ResultContent.buildFail(String.format("当前机构不属于学校类型"));
        }
        // 商户信息
        Organization relOrganization = organizationDao.findTopByOid(shopOid);
        if (ObjectUtils.isEmpty(relOrganization)) {
            return ResultContent.buildFail(String.format("商户oid不存在：%s", shopOid));
        }
        OrganizationRelation relation = organizationRelationDao.findTopByMainOrganizationAndRelOrganizationAndRelationType(
                mainOrganization, relOrganization, OrganizationRelationType.ShopToSchool);
        if (ObjectUtils.isNotEmpty(relation)) {
            organizationRelationDao.delete(relation);
        }
        return ResultContent.buildSuccess();
    }

    /**
     * 得到学校商户信息
     *
     * @param shopOid
     * @return
     */
    public ResultContent<ShopToSchoolModel> getSchoolBingShop(String shopOid) {
        String oid = getCurrentOid();
        Organization mainOrganization = organizationDao.findTopByOid(oid);
        if (mainOrganization.getAuthType() != AuthType.School) {
            return ResultContent.buildFail(String.format("当前机构不属于学校类型"));
        }
        // 商户信息
        Organization relOrganization = organizationDao.findTopByOid(shopOid);
        if (ObjectUtils.isEmpty(relOrganization)) {
            return ResultContent.buildFail(String.format("商户oid不存在：%s", shopOid));
        }
        OrganizationRelation relation = organizationRelationDao.findTopByMainOrganizationAndRelOrganizationAndRelationType(
                mainOrganization, relOrganization, OrganizationRelationType.ShopToSchool);
        if (ObjectUtils.isEmpty(relation)) {
            return ResultContent.buildFail(String.format("未找到对应数据：%s", shopOid));
        }
        ShopToSchoolModel model = toModel(relation);
        return ResultContent.buildSuccess(model);
    }

    @Override
    public ResultContent<Page<ShopToSchoolModel>> page(OrganizationRelationSearch param, Pageable pageable) {
        String oid = getCurrentOid();
        Organization mainOrganization = organizationDao.findTopByOid(oid);
        if (ObjectUtils.isEmpty(mainOrganization)) {
            return ResultContent.buildFail(String.format("当前学校信息为空"));
        }
        param.setMainId(mainOrganization.getId());
        param.setRelationType(OrganizationRelationType.ShopToSchool);

        Page<OrganizationRelation> page = organizationRelationDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    public ShopToSchoolModel toModel(OrganizationRelation entity) {
        ShopToSchoolModel model = new ShopToSchoolModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
            // 统计设备的数量

        }
        return model;
    }

}
