package com.casic.missiles.invoke.sync.handler;

import com.casic.missiles.core.common.service.IExpandComponentService;
import com.casic.missiles.modular.sync.config.SyncBizConfig;
import com.casic.missiles.modular.sync.handler.base.AbstractProcessHandler;
import com.casic.missiles.modular.system.model.Dept;
import com.casic.missiles.modular.system.model.DeptSync;
import com.casic.missiles.modular.system.model.User;
import com.casic.missiles.modular.system.model.UserSync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 基础数据转换实现
 */
@Component
public class ReceiveHandler extends AbstractProcessHandler<UserSync, DeptSync> {

    @Autowired
    IExpandComponentService service;

    /**
     * 业务接收用户数据处理为系统user数据
     *
     * @param userDto
     * @return
     */
    @Override
    public User processUser(User user, UserSync userDto) {
        //增量同步
        user.setDeptid(Long.valueOf(userDto.getOrganizationCode()));

        return user;
    }

    @Override
    public SyncBizConfig getSyncGlobalConfig() {
        SyncBizConfig syncGlobalConfig = new SyncBizConfig();
        syncGlobalConfig.setCreateDefaultRole(false);
        return syncGlobalConfig;
    }

    /**
     * 业务接收组织数据处理为系统dept数据
     *
     * @param deptDto
     * @return
     */
    @Override
    public Dept processDept(Dept dept, DeptSync deptDto) {
        dept.setIsParent(deptDto.getIsSuper());
        dept.setId(Long.valueOf(deptDto.getOrgaCode()));
        dept.setPid(Long.valueOf(deptDto.getSuperCode()));
        return dept;
    }

    @Override
    public void afterProcessUser(List<String> targetIds) {
        //处理数据用户角色对应关系 更新同步用户
        // CommonPermissionFactory.me().processUserData();
    }

    @Override
    public void afterProcessDept(List<String> targetIds) {
        service.reloadSyncDeptPids();
    }
}
