package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.feign.user.IUserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

/**
 * 同步用户
 */
@Slf4j
@Service("SyncUserTask")
@ConditionalOnExpression("'${spring.profiles.active}'!='develop'")
public class SyncUserTaskImpl implements ITaskExcuteService {

    @Autowired
    private UserService userService;

    @Autowired
    private IUserFeign userFeign;

    @Override
    public void excuteTask(ITask task) throws AppException {
        userService.refreshUser();
        log.info("同步用户");
/*        UserPdu userPdu = new UserPdu();
        userPdu.setPage(1);
        userPdu.setSize(-1);
        Rest<RespData<List<UserPdu>>> list = userFeign.list(userPdu);
        userService.updateUserList(list.getData().getData());*/

        //resourceService.updateUserList();

     /*   UserPdu userPdu = new UserPdu();
        userPdu.setPage(1);
        userPdu.setSize(-1);
        Rest<RespData<List<UserPdu>>> list = userFeign.list(userPdu);

*//*        List<UserPdu> firstList = list.getData().getData();

        List<UserEntity> secondList = userService.find(new UserQuery());
        //交叉对比后，多的新增 少的删除
        List<UserEntity> diffList = secondList.parallelStream()
                .filter(item -> !firstList.parallelStream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId()))
                .collect(Collectors.toList());*//*

        //log.info("diffList:{}",JSON.toJSONString(diffList));

        //更新本地用户信息，并且更新用户站点树
        list.getData().getData().forEach(user -> {
            //查询用户是否存在，
            //UserEntity userEntity = userService.get(user.getId());
            UserEntity userEntity =userService.selectOne(new UserQuery().loginName(user.getLoginName()));
            if (ObjectUtils.isEmpty(userEntity)) {
                //新增
                UserEntity entity = new UserEntity();
                entity.initAttrValue();
                BeanUtils.copyProperties(user, entity, BeanUtil.getNullPropertyNames(user));
                userService.save(entity);
                Context context = new Context();

                context.setUser(entity);
                new Thread(new SyncTreeSiteThread(siteService, context)).start();
            }
        });*/

    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }


}
