package org.springblade.modules.application.history.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.application.history.excel.ApplicationHistoryExcel;
import org.springblade.modules.application.history.pojo.entity.ApplicationHistoryEntity;
import org.springblade.modules.application.history.pojo.vo.ApplicationHistoryVO;
import org.springblade.modules.application.history.service.IApplicationHistoryService;
import org.springblade.modules.application.history.wrapper.ApplicationHistoryWrapper;
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.BladeConstant;
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-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-history/history")
@Tag(name = "应用历史", description = "应用历史接口")
public class ApplicationHistoryController extends BladeController {

	private final IApplicationHistoryService applicationHistoryService;

	/**
	 * 应用历史 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入history")
	public R<ApplicationHistoryVO> detail(ApplicationHistoryEntity history) {
		ApplicationHistoryEntity detail = applicationHistoryService.getOne(Condition.getQueryWrapper(history));
		return R.data(ApplicationHistoryWrapper.build().entityVO(detail));
	}

	/**
	 * 应用历史 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入history")
	public R<IPage<ApplicationHistoryVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> history, Query query) {
		IPage<ApplicationHistoryEntity> pages = applicationHistoryService.page(Condition.getPage(query), Condition.getQueryWrapper(history, ApplicationHistoryEntity.class));
		return R.data(ApplicationHistoryWrapper.build().pageVO(pages));
	}

	/**
	 * 应用历史 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入history")
	public R<IPage<ApplicationHistoryVO>> page(ApplicationHistoryVO history, Query query) {
		IPage<ApplicationHistoryVO> pages = applicationHistoryService.selectHistoryPage(Condition.getPage(query), history);
		return R.data(pages);
	}

	/**
	 * 应用历史 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入history")
	public R save(@Valid @RequestBody ApplicationHistoryEntity history) {
		return R.status(applicationHistoryService.save(history));
	}

	/**
	 * 应用历史 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入history")
	public R update(@Valid @RequestBody ApplicationHistoryEntity history) {
		return R.status(applicationHistoryService.updateById(history));
	}

	/**
	 * 应用历史 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入history")
	public R submit(@Valid @RequestBody ApplicationHistoryEntity history) {
		return R.status(applicationHistoryService.saveOrUpdate(history));
	}

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

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-history")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入history")
	public void exportHistory(@Parameter(hidden = true) @RequestParam Map<String, Object> history, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ApplicationHistoryEntity> queryWrapper = Condition.getQueryWrapper(history, ApplicationHistoryEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(History::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(ApplicationHistoryEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ApplicationHistoryExcel> list = applicationHistoryService.exportHistory(queryWrapper);
		ExcelUtil.export(response, "应用历史数据" + DateUtil.time(), "应用历史数据表", list, ApplicationHistoryExcel.class);
	}

}
