package com.mortals.xhx.daemon.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
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.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.SendStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.pdu.user.UserPdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.common.utils.SendSmsTask;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import com.mortals.xhx.feign.user.IUserFeign;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendQuery;
import com.mortals.xhx.module.alarm.service.AlarmSmsSendService;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 同步用户,唯一标识为用户名。
 */
@Slf4j
@Service("SyncUserTask")
public class SyncUserTaskImpl implements ITaskExcuteService {

    @Autowired
    private IUserFeign userFeign;
    @Autowired
    private UserService userService;


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

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

        if (resp.getCode() == YesNoEnum.YES.getValue()) {
            //同步更新用户，以loginname为唯一标识，密码默认与用户相同
            resp.getData().getData().forEach(user -> {
                UserEntity tempEntity = userService.selectOne(new UserQuery().loginName(user.getLoginName()));
                if (ObjectUtils.isEmpty(tempEntity)) {
                    UserEntity userEntity = new UserEntity();
                    BeanUtils.copyProperties(user, userEntity, new String[]{"lastLoginTime","lastLoginAddress"});
                    userService.getUserDao().insert(userEntity);
                }
            });
        }

        log.info("syncUserResp:{}", JSON.toJSONString(resp));

        // return;
//        UserPdu userPdu = new UserPdu();
//        userPdu.setPage(1);
//        userPdu.setSize(-1);
//        String resp = userFeign.findAllUser(userPdu);
//        log.info("resp:{}",resp);
//        JSONObject jsonObject = JSONObject.parseObject(resp);
//        if (jsonObject.getInteger("code") == 1) {
//            List<UserPdu> userList = jsonObject.getJSONObject("data").getObject("data", new TypeReference<List<UserPdu>>() {
//            });
//
//            log.info("userList:{}", JSON.toJSONString(userList));
//
//        }

    }


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

    }


}
