package com.kingtom.shengtai.app.base.session;

import java.util.List;

import com.kingtom.kirin.api.org.model.OrgPersonDTO;
import com.kingtom.kirin.app.affair.model.AffairInfo;
import com.kingtom.kirin.app.affair.model.AffairInfoDO;
import com.kingtom.kirin.app.affair.model.AffairMember;
import com.kingtom.kirin.app.affair.service.IAffairService;
import com.kingtom.kirin.app.auth.service.IAuthService;
import com.kingtom.kirin.app.org.OrgConst;
import com.kingtom.kirin.app.org.model.OrgUnit;
import com.kingtom.kirin.app.org.service.IOrgMemberService;
import com.kingtom.kirin.app.security.base.ISessionDataLoader;
import com.kingtom.kirin.app.security.base.SessionData;
import com.kingtom.kirin.app.security.service.ISecurityContainerService;
import com.kingtom.kirin.core.common.CommonConst;
import com.kingtom.shengtai.app.affair.service.SupplierJoinHandler;
import com.kingtom.shengtai.app.affair.service.UserJoinOrgHandler;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 应用模块名称</p>
 * 代码描述</p>
 * Copyright: Copyright (C) 2023 , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司</p>
 *
 * @author wany
 * @since 2023/12/26
 */
@Component
public class SessionLoader implements ISessionDataLoader{

    @Autowired
    private ISecurityContainerService container;

    @Autowired
    private IAuthService authService;

    @Autowired
    private IOrgMemberService orgMemberService;

    @Autowired
    private IAffairService affairService;

    @PostConstruct
    public void init(){
        container.registerSessionLoader(this);
    }

    @Override
    public int getOrder(){
        return CommonConst.INT_100;
    }

    @Override
    public void onLoad(SessionData data){
        OrgPersonDTO orgPersonDTO = data.getAttr(OrgConst.SESSION_ATTR_ORG_PERSON);
        OrgUnit orgUnit = data.getAttr(OrgConst.SESSION_ATTR_ORG_UNIT);
        fillBusinessRole(data, orgPersonDTO, orgUnit);
        fillAffairState(data);
    }

    private void fillAffairState(SessionData data){
        AffairInfoDO userJoinOrgAffair = affairService.findByTypeAndSourceId(UserJoinOrgHandler.NAME, data.getUserId());
        AffairInfoDO supplierJoinAffair =
                affairService.findByTypeAndSourceId(SupplierJoinHandler.NAME, data.getUserId());
        AffairInfoDO affairInfoDO = null;
        if(userJoinOrgAffair.getInfo() != null && supplierJoinAffair.getInfo() != null){
            affairInfoDO =
                    userJoinOrgAffair.getInfo().getGmtCreate().compareTo(supplierJoinAffair.getInfo().getGmtCreate()) >
                            0 ? userJoinOrgAffair : supplierJoinAffair;
        } else if(userJoinOrgAffair.getInfo() != null){
            affairInfoDO = userJoinOrgAffair;
        } else{
            affairInfoDO = supplierJoinAffair;
        }

        AffairInfo info = affairInfoDO.getInfo();
        List<AffairMember> members = affairInfoDO.getMembers();
        if(affairInfoDO.getInfo() == null){
            //未提交审核
            data.putAttr("auditState", 0);
            return;
        }
        //增加待办信息id
        data.putAttr("affairId", info.getId());
        data.putAttr("affairType", info.getType());
        if(CommonConst.StateEnum.Enable.getKey().equals(info.getState())){
            //已提交未审核
            data.putAttr("auditState", 1);
        } else if(CommonConst.StateEnum.Disable.getKey().equals(info.getState())){
            AffairMember member =
                    members.stream().filter(e -> CommonConst.StateEnum.Disable.getKey().equals(e.getState()))
                            .findFirst().orElse(null);
            if(member == null){
                //未提交审核
                data.putAttr("auditState", 0);
            } else{
                if(CommonConst.BooleanEnum.True.getKey().equals(member.getResult())){
                    data.putAttr("auditState", 2);
                } else{
                    data.putAttr("auditState", 3);
                }
            }

        }
    }

    private void fillBusinessRole(SessionData data, OrgPersonDTO orgPersonDTO, OrgUnit orgUnit){
        //默认未注册角色
        data.putAttr("BusinessRole", null);
        String personId = orgPersonDTO == null ? null : orgPersonDTO.getId();
        if(StringUtils.isNotEmpty(personId)){
            boolean orgMember = orgMemberService.isOrgMember(orgUnit.getId(), personId);
            if(orgMember){
                //组织成员
                data.putAttr("BusinessRole", "orgMember");
                return;
            }
        }
        boolean userHasRole = authService.findUserHasRole("supplier", data.getUserId(), data.getSysId());
        if(userHasRole){
            //供应商
            data.putAttr("BusinessRole", "supplier");
        }
    }

}
