package org.springblade.modules.externalLog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.externalLog.entity.ExternalLogEntity;
import org.springblade.modules.externalLog.excel.ExternalLogExcel;
import org.springblade.modules.externalLog.service.IExternalLogService;
import org.springblade.modules.externalLog.vo.ExternalLogVO;
import org.springblade.modules.externalLog.wrapper.ExternalLogWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 对外日志 控制器
 *
 * @author BladeX
 * @since 2024-01-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-extemalLog/extemalLog")
@Api(value = "对外日志", tags = "对外日志接口")
public class ExternalLogController extends BladeController {

	private final IExternalLogService extemalLogService;

	/**
	 * 对外日志 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入extemalLog")
	public R<ExternalLogVO> detail(ExternalLogEntity extemalLog) {
		ExternalLogEntity detail = extemalLogService.getOne(Condition.getQueryWrapper(extemalLog));
		return R.data(ExternalLogWrapper.build().entityVO(detail));
	}

	/**
	 * 对外日志 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入extemalLog")
	public R<IPage<ExternalLogVO>> list(@ApiIgnore @RequestParam Map<String, Object> extemalLog, Query query) {
		IPage<ExternalLogEntity> pages = extemalLogService.page(Condition.getPage(query), Condition.getQueryWrapper(extemalLog, ExternalLogEntity.class));
		return R.data(ExternalLogWrapper.build().pageVO(pages));
	}

	/**
	 * 对外日志 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入extemalLog")
	public R<IPage<ExternalLogVO>> page(ExternalLogVO extemalLog, Query query) {
		IPage<ExternalLogVO> pages = extemalLogService.selectExtemalLogPage(Condition.getPage(query), extemalLog);
		return R.data(pages);
	}

	/**
	 * 对外日志 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入extemalLog")
	public R save(@Valid @RequestBody ExternalLogEntity extemalLog) {
		return R.status(extemalLogService.save(extemalLog));
	}

	/**
	 * 对外日志 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入extemalLog")
	public R update(@Valid @RequestBody ExternalLogEntity extemalLog) {
		return R.status(extemalLogService.updateById(extemalLog));
	}

	/**
	 * 对外日志 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入extemalLog")
	public R submit(@Valid @RequestBody ExternalLogEntity extemalLog) {
		return R.status(extemalLogService.saveOrUpdate(extemalLog));
	}

	/**
	 * 对外日志 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(extemalLogService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-extemalLog")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入extemalLog")
	public void exportExtemalLog(@ApiIgnore @RequestParam Map<String, Object> extemalLog, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ExternalLogEntity> queryWrapper = Condition.getQueryWrapper(extemalLog, ExternalLogEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(ExtemalLog::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(ExternalLogEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ExternalLogExcel> list = extemalLogService.exportExtemalLog(queryWrapper);
		ExcelUtil.export(response, "对外日志数据" + DateUtil.time(), "对外日志数据表", list, ExternalLogExcel.class);
	}

}
