package org.springblade.modules.log.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.log.excel.LogYysdExcel;
import org.springblade.modules.log.pojo.entity.LogYysdEntity;
import org.springblade.modules.log.pojo.vo.LogYysdVO;
import org.springblade.modules.log.service.ILogYysdService;
import org.springblade.modules.log.wrapper.LogYysdWrapper;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.RoleConstant;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 应用商店日志 控制器
 *
 * @author BladeX
 * @since 2024-10-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/logYysd")
@Tag(name = "应用商店日志", description = "应用商店日志接口")
public class LogYysdController extends BladeController {

	private final ILogYysdService logYysdService;

	/**
	 * 应用商店日志 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入logYysd")
	public R<LogYysdVO> detail(LogYysdEntity logYysd) {
		LogYysdEntity detail = logYysdService.getOne(Condition.getQueryWrapper(logYysd));
		return R.data(LogYysdWrapper.build().entityVO(detail));
	}

	/**
	 * 应用商店日志 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入logYysd")
	public R<IPage<LogYysdVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> logYysd, Query query) {
		IPage<LogYysdEntity> pages = logYysdService.page(Condition.getPage(query), Condition.getQueryWrapper(logYysd, LogYysdEntity.class));
		return R.data(LogYysdWrapper.build().pageVO(pages));
	}


	/**
	 * 应用商店日志 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入logYysd")
	public R<IPage<LogYysdVO>> page(LogYysdVO logYysd, Query query) {
		IPage<LogYysdVO> pages = logYysdService.selectLogYysdPage(Condition.getPage(query), logYysd);
		return R.data(pages);
	}

	/**
	 * 应用商店日志 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入logYysd")
	public R save(@Valid @RequestBody LogYysdEntity logYysd) {
		return R.status(logYysdService.save(logYysd));
	}

	/**
	 * 应用商店日志 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入logYysd")
	public R update(@Valid @RequestBody LogYysdEntity logYysd) {
		return R.status(logYysdService.updateById(logYysd));
	}

	/**
	 * 应用商店日志 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入logYysd")
	public R submit(@Valid @RequestBody LogYysdEntity logYysd) {
		return R.status(logYysdService.saveOrUpdate(logYysd));
	}

	/**
	 * 应用商店日志 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description  = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(logYysdService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-logYysd")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出数据", description  = "传入logYysd")
	public void exportLogYysd(@Parameter(hidden = true) @RequestParam Map<String, Object> logYysd, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<LogYysdEntity> queryWrapper = Condition.getQueryWrapper(logYysd, LogYysdEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(LogYysdEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(LogYysdEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<LogYysdExcel> list = logYysdService.exportLogYysd(queryWrapper);
		ExcelUtil.export(response, "应用商店日志数据" + DateUtil.time(), "应用商店日志数据表", list, LogYysdExcel.class);
	}

}
