package com.persagy.adm.server.custom.controller;

import cn.hutool.core.util.StrUtil;
import com.persagy.adm.server.auth.service.AdmAuthService;
import com.persagy.adm.server.custom.common.AdmRequest;
import com.persagy.adm.server.custom.datatx.DataTxHandler;
import com.persagy.adm.server.custom.dto.HasUpdateReq;
import com.persagy.adm.server.custom.entity.ConfigData;
import com.persagy.adm.server.custom.entity.Dict;
import com.persagy.adm.server.custom.entity.DownLoadData;
import com.persagy.adm.server.custom.entity.UploadRtn;
import com.persagy.adm.server.custom.service.ISyncApp;
import com.persagy.adm.server.custom.service.ServiceUtil;
import com.persagy.dmp.auth.domain.BossAuthUser;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * APP离线数据服务
 */
@RestController
@RequestMapping("/adm")
@Slf4j
public class AppController {

	@Autowired
	@Qualifier(ServiceUtil.SYNC_APP_IMPL)
	private ISyncApp syncApp;

	@Autowired
	private DataTxHandler dataTxHandler;

	@Autowired
	private AdmAuthService admAuthService;

	@RequestMapping("/clientId")
	public CommonResult<Map<String, Object>> clientId(@RequestBody AdmRequest req){
		HashMap<String, Object> rtn = new HashMap<>();
		rtn.put("clientId", syncApp.getClientId(req.getUserId()));
		return ResultHelper.single(rtn);
	}

	@RequestMapping("/dict")
	public CommonResult<Dict> dict(@RequestBody AdmRequest req){
		req.setUserId(admAuthService.loginInfo().getId());
		return ResultHelper.single(syncApp.downloadDict(req.getGroupCode(), req.getProjectId(), req.getUserId()));
	}

	@RequestMapping("/config")
	public CommonResult<ConfigData> config(@RequestBody AdmRequest req){
		req.setUserId(admAuthService.loginInfo().getId());
		return ResultHelper.single(syncApp.downloadConfig(req.getGroupCode(), req.getProjectId(), req.getUserId()));
	}

	@RequestMapping("/frame")
	public CommonResult<Map<String, Object>> frame(@RequestBody AdmRequest req){
		req.setUserId(admAuthService.loginInfo().getId());
		return ResultHelper.single(syncApp.downloadFrameData(req.getGroupCode(), req.getProjectId(), req.getUserId()));
	}

	@RequestMapping("/download")
	public CommonResult<DownLoadData> download(@RequestBody AdmRequest req){
		req.setUserId(admAuthService.loginInfo().getId());
		DownLoadData data;
		if(StrUtil.isNotBlank(req.getBuildingId())) {
			data = syncApp.downloadBuildingData(req.getGroupCode(), req.getProjectId(), req.getUserId(), req.getClientId(), req.getBuildingId(), req.getBdtpDownloadTs(), req.getAdmDownloadTs());
		}else {
			throw new BusinessException(ResponseCode.A0400.getCode(),"建筑ID不能为空");
		}
		return ResultHelper.single(data);
	}

	/**
	 * 下载对象关系数据
	 */
	@RequestMapping("/downloadObjRel")
	public CommonResult<DownLoadData> downloadObjRel(@RequestBody AdmRequest req){
		req.setUserId(admAuthService.loginInfo().getId());
		return ResultHelper.single(syncApp.downloadProjectData(req.getGroupCode(), req.getProjectId(), req.getUserId(), req.getClientId(), req.getBdtpDownloadTs(), req.getAdmDownloadTs()));
	}

	/**
	 * 上传离线数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/upload")
	public CommonResult<UploadRtn> upload(HttpServletRequest request){
		AdmRequest admRequest = dataTxHandler.handleRequest(request);
		BossAuthUser bossAuthUser = admAuthService.loginInfo();
		log.info("用户:{},上传数据:{}",bossAuthUser,admRequest);
		return ResultHelper.single(syncApp.uploadData(admRequest.getUploadData(), admRequest.getGroupCode(), admRequest.getProjectId(), admRequest.getUserId(), admRequest.getClientId()));
	}

	/**
	 * 是否有数据更新
	 */
	@PostMapping("/hasUpdate")
	public CommonResult<Boolean> hasUpdate(@RequestBody HasUpdateReq hasUpdateReq){
		return ResultHelper.single(syncApp.hasUpdate(hasUpdateReq));
	}



}
