package com.jeequan.jeepay.mgr.ctrl.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.SysClientVersion;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.SysClientVersionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

	@Autowired private SysClientVersionService sysClientVersionService;

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 11:15
	 * @describe: 版本管理列表查询
	 */
	@GetMapping
	@PreAuthorize("hasAuthority('ENT_CLIENT_VERSION_LIST')")
	public ApiRes list() {
		SysClientVersion clientVersion = getObject(SysClientVersion.class);
		LambdaQueryWrapper<SysClientVersion> wrapper = SysClientVersion.gw();
		if (StringUtils.isNotEmpty(clientVersion.getClientType())) {
			wrapper.eq(SysClientVersion::getClientType, clientVersion.getClientType());
		}
		if (StringUtils.isNotEmpty(clientVersion.getVersionSerialNumber())) {
			wrapper.eq(SysClientVersion::getVersionSerialNumber, clientVersion.getVersionSerialNumber());
		}
		wrapper.orderByDesc(SysClientVersion::getVersionId);

		IPage clientVersionList = sysClientVersionService.page(getIPage(), wrapper);

		return ApiRes.ok(clientVersionList);
	}

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 11:19
	 * @describe: 发布新版本
	 */
	@MethodLog(remark = "新建版本信息")
	@PostMapping
	@PreAuthorize("hasAuthority('ENT_CLIENT_VERSION_ADD')")
	public ApiRes add() {
		SysClientVersion clientVersion = getObject(SysClientVersion.class);

		// 查看当前客户端类型 & 版本序列号 是否已经存在
		long dbCount = sysClientVersionService.count(
				SysClientVersion.gw()
				.eq(SysClientVersion::getVersionSerialNumber, clientVersion.getVersionSerialNumber())
				.eq(SysClientVersion::getClientType, clientVersion.getClientType())
		);
		if (dbCount > 0) {
			throw new BizException("当前客户端已存在此版本序列号！");
		}
		boolean result = sysClientVersionService.save(clientVersion);
		if (!result) {
			return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_CREATE);
		}

		return ApiRes.ok();
	}

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 16:50
	 * @describe: 编辑信息
	 */
	@MethodLog(remark = "编辑信息")
	@PreAuthorize("hasAuthority('ENT_CLIENT_VERSION_EDIT')")
	@RequestMapping(value="/{recordId}", method = RequestMethod.PUT)
	public ApiRes update(@PathVariable("recordId") Integer recordId) {
		SysClientVersion clientVersion = getObject(SysClientVersion.class);
		//根据 客户端类型 & 版本序列号 查询是否存在
		SysClientVersion dbRecord = sysClientVersionService.getOne(new QueryWrapper<SysClientVersion>().lambda()
				.eq(SysClientVersion::getClientType, clientVersion.getClientType())
				.eq(SysClientVersion::getVersionSerialNumber, clientVersion.getVersionSerialNumber())
		);
		if(dbRecord != null && !dbRecord.getVersionId().equals(recordId)){
			throw new BizException("当前客户端类型已存在该版本序列号！");
		}
		clientVersion.setVersionId(recordId);
		boolean result = sysClientVersionService.updateById(clientVersion);
		if (!result) {
			return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
		}
		return ApiRes.ok();
	}

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 11:28
	 * @describe: 查看详情
	 */
	@GetMapping("/{recordId}")
	@PreAuthorize("hasAuthority('ENT_CLIENT_VERSION_VIEW')")
	public ApiRes detail(@PathVariable("recordId") Long recordId) {
		SysClientVersion clientVersion = sysClientVersionService.getById(recordId);
		return ApiRes.ok(clientVersion);
	}

	/**
	 * @author: xiaoyu
	 * @date: 2021/12/31 11:30
	 * @describe: 删除信息
	 */
	@MethodLog(remark = "删除客户端版本")
	@DeleteMapping("/{recordId}")
	@PreAuthorize("hasAuthority('ENT_CLIENT_VERSION_DEL')")
	public ApiRes delete(@PathVariable("recordId") Long recordId) {

		boolean result = sysClientVersionService.removeById(recordId);
		if (!result) {
			return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_DELETE);
		}
		return ApiRes.ok();
	}


}
