package com.jeequan.jeepay.agent.ctrl.anon;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.SysClientVersion;
import com.jeequan.jeepay.service.impl.SysClientVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 版本管理
 *
 * @author xiaoyu
 * @date 2021/12/31 11:11
 */
@Slf4j
@RestController
@RequestMapping("/api/anon/clientVersion")
public class ClientVersionController extends CommonCtrl {

	@Autowired private SysClientVersionService sysClientVersionService;

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 11:28
	 * @describe: 版本信息
	 */
	@RequestMapping(value = "/versionInfo", method = RequestMethod.GET)
	public ApiRes versionInfo() {
		// 版本号
		String versionNumber = getValStringRequired("versionNumber");
		SysClientVersion dbClientVersion = sysClientVersionService.getOne(
				SysClientVersion.gw()
				.eq(SysClientVersion::getClientType, SysClientVersion.CLIENT_TYPE_AGENT)
				.eq(SysClientVersion::getVersionSerialNumber, versionNumber)
		);

		//未查询到当前版本信息,  查询最新版本作为 需[强制更新] 的版本
		if(dbClientVersion == null){
			List<SysClientVersion> newVersionList = sysClientVersionService.page(
					new Page<>(1, 1),
					new QueryWrapper<SysClientVersion>().lambda().eq(SysClientVersion:: getClientType, SysClientVersion.CLIENT_TYPE_AGENT).orderByDesc(SysClientVersion:: getVersionId)
			).getRecords();
			if(newVersionList != null && !newVersionList.isEmpty()){
				SysClientVersion newVersion = newVersionList.get(0);
				newVersion.setForceUpdate(CS.YES);
				return ApiRes.ok(newVersion);
			}else{
				//未查询到 任何版本信息
				return ApiRes.ok(new JSONObject());
			}
		}

		// 查询到当前版本信息， 需查询当前版本之后的最新版本信息
		List<SysClientVersion> newVersionList = sysClientVersionService.page(
				new Page<>(1, 1),
				new QueryWrapper<SysClientVersion>().lambda()
						.eq(SysClientVersion:: getClientType, SysClientVersion.CLIENT_TYPE_AGENT)
						.gt(SysClientVersion:: getVersionId, dbClientVersion.getVersionId())
						.orderByDesc(SysClientVersion:: getVersionId)
		).getRecords();

		if(newVersionList == null || newVersionList.isEmpty()){
			//未查询到 任何版本信息
			return ApiRes.ok(new JSONObject());
		}
		//最新版本信息
		SysClientVersion newVersion = newVersionList.get(0);

		//如果当前版本前，包含强制更新， 则更改当前版本为强制更新版本
		long count = sysClientVersionService.count(
				new QueryWrapper<SysClientVersion>().lambda()
						.eq(SysClientVersion:: getClientType, SysClientVersion.CLIENT_TYPE_AGENT)
						.eq(SysClientVersion:: getForceUpdate, CS.YES)
						.gt(SysClientVersion:: getVersionId, dbClientVersion.getVersionId())
		);
		if(count > 0){
			newVersion.setForceUpdate(CS.YES);
		}
		return ApiRes.ok(newVersion);
	}

	/**
	 * @author: yr
	 * @date: 2023/02/10 16:20
	 * @describe: 服务商App获取最新版下载url
	 */
	@RequestMapping(value = "/downloadUrl", method = RequestMethod.GET)
	public ApiRes downloadUrl() {
		LambdaQueryWrapper<SysClientVersion> wrapper = SysClientVersion.gw();
		wrapper.eq(SysClientVersion::getClientType, SysClientVersion.CLIENT_TYPE_AGENT);
		if (sysClientVersionService.count(wrapper) == 0){
			return ApiRes.ok();
		}
		wrapper.orderByDesc(SysClientVersion::getVersionId);
		wrapper.last("limit 1");
		return ApiRes.ok(sysClientVersionService.getOne(wrapper).getDownloadUrl());
	}

}
