package com.uas.account.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.account.core.Logger;
import com.uas.account.dao.UserDao;
import com.uas.account.dao.UserSpaceDao;
import com.uas.account.entity.*;
import com.uas.sso.common.util.HttpUtil;
import com.uas.sso.common.util.HttpUtil.ResponseWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysDataToMallServiceImpl implements SysDataToMallService {

	/**
	 * 应用都默认是mall
	 */
	private static final String appId = "mall";

	@Autowired
	private AppService appService;

	private Logger logger = Logger.getLogger();

	@Autowired
	private UserService userService;

	@Autowired
    private UserDao userDao;

	@Autowired
    private UserSpaceDao userSpaceDao;

	@Override
	public void user(User user) {
		App app = appService.findOne(appId);
		String url = app.getBackUserUrl();
		if (null != url) {
			JSONObject formData = JSON.parseObject(JSON.toJSONString(user));
            ResponseWrap res = null;// 5分钟超时
            try {
                res = HttpUtil.doPost(url, formData, 300000);
                if (!res.isSuccess())
                    logger.error("同步用户信息到商城失败", user.getDialectUID() + "，msg" + res.getContent());
			    else
                logger.info("同步用户信息到商城", user.getDialectUID());
            } catch (Exception e) {
                userDao.saveFailed(new UserFailed(user, user, e));
            }

		}
	}

    @Override
	public void UserSpace(com.uas.account.entity.UserSpace userSpace, UserSpaceDetail detail) {
		App app = appService.findOne(appId);
		String url = app.getBackSpaceUrl();
		if (null != url) {
			JSONObject formData = JSON.parseObject(JSON.toJSONString(userSpace));
			formData.putAll(JSON.parseObject(JSON.toJSONString(detail)));
            ResponseWrap res = null;// 5分钟超时
            try {
                res = HttpUtil.doPost(url, formData, 300000);
                if (!res.isSuccess())
                    logger.error("同步企业信息到商城失败", userSpace.getDialectUID() + "，msg" + res.getContent());
                else
                    logger.info("同步企业信息到商城", userSpace.getDialectUID());
            } catch (Exception e) {
                userSpaceDao.saveFailed(new UserSpaceFailed(userSpace, userSpace, e));
            }
		}
	}

	@Override
	public void ChangeAdmin(ChangeAdminDetail detail) {
		App app = appService.findOne(appId);
		String url = app.getBackChangeAdminUrl();
		if (null != url) {
			JSONObject formData = JSON.parseObject(JSON.toJSONString(detail));
			try {
				ResponseWrap res = HttpUtil.doPost(url, formData, 300000);
				if (!res.isSuccess())
					logger.error("同步企业信息到商城失败", detail.getEnUu() + "，msg" + res.getContent());
				else
					logger.info("同步企业信息到商城", detail.getEnUu());
			} catch (Exception e) {
                e.printStackTrace();
			}

		}
	}

	@Override
	public void changepwd(User user, String password) {
		App app = appService.findOne(appId);
		if (password.length() > 32) {// 已经加密的不需要再进行加密处理
			user.setPassword(password);
		} else {
			String pwd = userService.getEncryPassword(app.getEncryFormat(), password, user.getDialectUID());
			user.setPassword(pwd);
		}
		try {
			user(user);
			logger.info("修改密码同步商城", user.getDialectUID());
		} catch (Exception e) {
			logger.error("修改密码同步商城失败", user.getDialectUID());
		}
	}

    @Override
    public void reMoveUser(User user) {
        App app = appService.findOne(appId);
        String url = app.getBackUserUrl();
        if (null != url) {
            url = url + "/removerUser";
            JSONObject formData = JSON.parseObject(JSON.toJSONString(user));
            try {
                ResponseWrap res = HttpUtil.doPost(url, formData, 300000);
                if (!res.isSuccess())
                    logger.error("删除用户同步到商城失败", user.getDialectUID() + "," + user.getSpaceDialectUID() + "，msg" + res.getContent());
                else
                    logger.info("删除用户同步到商城", user.getDialectUID() + "," + user.getSpaceDialectUID());
            } catch (Exception e) {
            }

        }
    }

}
