package cn.cecep.talroad.controller.app.version;

import cn.cecep.talroad.common.core.constant.HttpStatus;
import cn.cecep.talroad.common.core.utils.bean.BeanUtils;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResult;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.domain.AppVersionAddressManagement;
import cn.cecep.talroad.service.IAppVersionAddressManagementService;
import cn.cecep.talroad.vo.AppVersionAddressManagementVo;
import cn.cecep.talroad.vo.query.AppVersionAddressManagementQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;

 /**
 * @Description: app版本管理表
 * @Author: code-generate
 * @Date:   2023-07-06
 * @Version: V1.0
 */
@Api(tags = "app版本管理表")
@RestController
@RequestMapping("/appVersionAddressManagement")
public class AppVersionAddressManagementController  extends BasesController {
	@Autowired
	private IAppVersionAddressManagementService appVersionAddressManagementService;

    /**
	 * 查询app版本管理表列表
	 */
	@ApiOperation(value = "查询app版本管理表列表")
	@GetMapping("/list")
	public AjaxResults<IPage<AppVersionAddressManagement>> list(AppVersionAddressManagementQuery appVersionAddressManagement) {
		Page<AppVersionAddressManagement> page = new Page<>(appVersionAddressManagement.getPageNum(), appVersionAddressManagement.getPageSize());
		IPage<AppVersionAddressManagement> list = appVersionAddressManagementService.page(page, AppVersionAddressManagementQuery.createLambdaQueryWrapper(appVersionAddressManagement));
		return AjaxResults.success(list);
	}


	/**
	 * 获取app版本管理表详细信息
	 */
	@ApiOperation(value = "获取app版本管理表详细信息")
	@GetMapping(value = "/{id}")
	public AjaxResults<AppVersionAddressManagement> getInfo(@PathVariable("id") String id) {
		return AjaxResults.success(appVersionAddressManagementService.getById(id));
	}

	/**
	 * 新增app版本管理表
	 */
	@ApiOperation(value = "新增app版本管理表")
	@Log(title = "app版本管理表", businessType = BusinessType.INSERT)
	@PostMapping(value = "/add")
	public AjaxResults<Boolean> add(@RequestBody AppVersionAddressManagementVo appVersionAddressManagementVo) {
		AppVersionAddressManagement appVersionAddressManagement = new AppVersionAddressManagement();
		BeanUtils.copyBeanProp(appVersionAddressManagementVo, appVersionAddressManagement);
		return appVersionAddressManagementService.save(appVersionAddressManagement) ? AjaxResults.success() : AjaxResults.error(300001, "新增失败");
	}

	/**
	 * 修改app版本管理表
	 */
	@ApiOperation(value = "修改app版本管理表")
	@Log(title = "app版本管理表", businessType = BusinessType.UPDATE)
	@PostMapping(value = "/edit")
	public AjaxResults<Boolean> edit(@RequestBody AppVersionAddressManagementVo appVersionAddressManagementVo) {
		AppVersionAddressManagement appVersionAddressManagement = appVersionAddressManagementService.getById(appVersionAddressManagementVo.getId());
		if (ObjectUtil.isNull(appVersionAddressManagement)) {
			return AjaxResults.error(300002, "修改失败");
		}
		// 赋值
		BeanUtil.copyProperties(appVersionAddressManagementVo, appVersionAddressManagement);
		return appVersionAddressManagementService.saveOrUpdate(appVersionAddressManagement) ? AjaxResults.success() :
				AjaxResults.error(300003, "修改失败");
	}

	/**
	 * 删除app版本管理表
	 */
	@ApiOperation(value = "删除app版本管理表")
	@Log(title = "app版本管理表", businessType = BusinessType.DELETE)
	@PostMapping("/{ids}")
	public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
		return appVersionAddressManagementService.removeByIds(Arrays.asList(ids)) ? AjaxResults.success() :
				AjaxResults.error(300004, "删除失败");
	}


	 /**
	  *  根据ID 查询 版本地址管理 详细信息
	  * @param
	  * @return
	  */
	 @GetMapping(value = "/findUpToDateVersionAddress")
	 @ApiOperation(value = "查询最新的版本")
	 @ApiImplicitParams({
			 @ApiImplicitParam(name = "isDebug", value = "是否生产 0: 测试 1: 生产 ", dataType = "Integer"),
	 })
	 public AjaxResults<AppVersionAddressManagement> findUpToDateVersionAddress(Integer isDebug, Integer sourceIsCoerceUpdate) {

		 AppVersionAddressManagement versionAddress = appVersionAddressManagementService.findUpToDateVersionAddress(isDebug);
		 if (ObjectUtil.isNotNull(versionAddress)) {
			 return AjaxResults.success(versionAddress);
		 } else {
			 return AjaxResults.error(HttpStatus.NO_CONTENT,"您当前已是最新版本");
		 }
	 }

	 /**
	  *  根据app 访问地址 产生二维码地址 返回
	  * @param
	  * @return 需要时再开启
	  */
	 @GetMapping(value = "/generateAppAddressQrCode")
	 @ApiOperation(value = "根据app 访问地址 产生二维码地址返回")
	 @ApiImplicitParams({
			 @ApiImplicitParam(name = "url", value = "文件地址", dataType = "String"),
			 @ApiImplicitParam(name = "logUrl", value = "内嵌log地址", dataType = "String"),
			 @ApiImplicitParam(name = "ipOrigin", value = "ip和端口", dataType = "String"),
			 @ApiImplicitParam(name = "isDebug", value = "是否生产 0: 测试 1: 生产 ", dataType = "Integer"),
	 })
	 @ApiOperationSupport(author = "张培志")
	 public AjaxResult generateAppAddressQrCode(String url, String logUrl, Integer isDebug, String ipOrigin) throws Exception {
		 return AjaxResult.success(appVersionAddressManagementService.generateAppAddressQrCode(url, logUrl, isDebug, ipOrigin));
	 }
}
