package cn.superdesk.uniorder.dubbo.consumer.sys;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.organisation.SysCompany;
import cn.superdesk.uniorder.dao.entity.sys.SysUser;
import cn.superdesk.uniorder.dao.entity.sys.SysUserOrderSourcetypeKey;
import cn.superdesk.uniorder.dto.param.system.SysUserQueryCriteria;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionSysUser;
import cn.superdesk.uniorder.dto.vo._superdeskapp.jwt.SupperdeskAppTokenUser;
import cn.superdesk.uniorder.dto.vo.sys.SysUserView;
import cn.superdesk.uniorder.dto.vo.sys.SysUserVo;
import cn.superdesk.uniorder.dubbo.facade.sys.DubboSysUserService;
import cn.superdesk.uniorder.dubbo.facade.unified.DubboUnifiedUserlogService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2020/11/10 上午10:01
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/11/10 上午10:01
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysUserDubboConsumerService {


	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboSysUserService        dubboSysUserService;
	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboUnifiedUserlogService dubboUnifiedUserlogService;

	/**
	 * 统一订单 后台登陆
	 *
	 * @param username
	 * @param password
	 * @return
	 * @throws UnifiedOrderRuntimeException
	 */
	public SysUserVo userLoginBySupperDeskUniOrderBackEndFacade(String username, String password) throws UnifiedOrderRuntimeException {
		return dubboSysUserService.userLoginBySupperDeskUniOrderBackEndFacade(username, password);
	}

	/**
	 * 服务之窗 后台登陆
	 *
	 * @param userToken
	 * @return
	 */
//    public SysUserVo userLoginByToken(TokenUserBySuperdeskApp userToken) {
//
//        return dubboSysUserService.userLoginByTokenFacade(userToken);
//    }
	public SysUserVo userLoginBySupperDeskUniServiceTokenFacade(SupperdeskAppTokenUser userToken) {

		return dubboSysUserService.userLoginBySupperDeskUniServiceTokenFacade(userToken);
	}

	/**
	 * 自家商城 供应商端 后台登陆
	 *
	 * @param conditions
	 * @return
	 */
	public SysUserVo userLoginBySupplier(Map<String, Object> conditions) {

		//		param
//		{
//			"conditions" : {
//				"saleId" : 1,
//				"saleName" : "前台优选"
//			}
//		}

		log.info("{}", AnJsonUtils.toJson(conditions));

		return dubboSysUserService.userLoginBySupperDeskUniMallSupplierFacade(conditions);
	}

	public R<List<SysUserView>> findAllPage(SysUserQueryCriteria queryCriteria) {

		return dubboSysUserService.findAllPage(queryCriteria);
	}

	public SysUser add(SysUser sysUser, List<SysUserOrderSourcetypeKey> sysUserOrderSourcetypeKeys, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		sysUser.setCreateTime(DateUtil.date());
		sysUser.setCreateUserId(loginSysUser.getSysUserId());

		sysUser.setUpdateTime(DateUtil.date());
		sysUser.setUpdateUserId(loginSysUser.getSysUserId());

		return dubboSysUserService.add(sysUser, sysUserOrderSourcetypeKeys);
	}

	public void edit(SysUser user, List<SysUserOrderSourcetypeKey> sysUserOrderSourcetypeKeys, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		user.setUpdateTime(DateUtil.date());
		user.setUpdateUserId(loginSysUser.getSysUserId());

		dubboSysUserService.edit(user, sysUserOrderSourcetypeKeys);
	}

	@Deprecated
	public void delete(Long id) {

		dubboSysUserService.delete(id);
	}

	public int opPhysicsDelete(Long id, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboSysUserService.opPhysicsDelete(id, loginSysUser);
	}

	public int opLogicalDelete(Long id, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboSysUserService.opLogicalDelete(id, loginSysUser);
	}


	public SysCompany getSysCompany(Long id) {

		return dubboSysUserService.getSysCompany(id);
	}

	public List<SelectOptionSysUser> findAll(Map<String, Object> params) {

		return dubboSysUserService.findAll(params);
	}

	public List<SysUser> findBySysUserIds(List<Long> sysUserIds) {
		return dubboSysUserService.findBySysUserIds(sysUserIds);
	}


	public void createLog(Long userId, String outIp, String localIp) {


		String remarks = "";
		dubboUnifiedUserlogService.createLog(DateUtil.date(), "登陆", localIp, outIp, remarks, userId);
	}


	/****************************************** 工具方法 sysUserIds 推导 start ******************************************/

	public List<String> convertSysUserIds2FullName(List<Long> sysUserIds) {
		return dubboSysUserService.convertSysUserIds2FullName(sysUserIds);
	}

	public Map<Long, String> convertSysUserIds2MapSysUserId2FullName(List<Long> sysUserIds) {
		return dubboSysUserService.convertSysUserIds2MapSysUserId2FullName(sysUserIds);
	}

	public Map<Long, SysUser> convertSysUserIds2MapSysUserId2Entity(List<Long> sysUserIds) {
		return dubboSysUserService.convertSysUserIds2MapSysUserId2Entity(sysUserIds);
	}

	/****************************************** 工具方法 sysUserIds 推导   end ******************************************/

}
