package org.springblade.modules.application.info.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.redis.cache.BladeRedis;
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.pojo.entity.ApplicationHistoryEntity;
import org.springblade.modules.application.history.service.IApplicationHistoryService;
import org.springblade.modules.application.info.excel.ApplicationInfoExcel;
import org.springblade.modules.application.info.pojo.entity.ApplicationInfoEntity;
import org.springblade.modules.application.info.pojo.vo.ApplicationInfoVO;
import org.springblade.modules.application.info.service.IApplicationInfoService;
import org.springblade.modules.application.info.wrapper.ApplicationInfoWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
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-info/info")
@Tag(name = "应用列表", description = "应用列表接口")
public class ApplicationInfoController extends BladeController {

	private final BladeRedis bladeRedis;

	private final IApplicationInfoService applicationInfoService;

	private final IApplicationHistoryService applicationHistoryService;

	/**
	 * 应用列表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入info")
	public R<ApplicationInfoVO> detail(ApplicationInfoEntity info) {
		ApplicationInfoEntity detail = applicationInfoService.getOne(Condition.getQueryWrapper(info));
		return R.data(ApplicationInfoWrapper.build().entityVO(detail));
	}
	/**
	 * 应用列表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入info")
	public R<IPage<ApplicationInfoVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> info, Query query) {
		IPage<ApplicationInfoEntity> pages = applicationInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(info, ApplicationInfoEntity.class));
		return R.data(ApplicationInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 应用列表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入info")
	public R<IPage<ApplicationInfoVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<ApplicationInfoVO> pages = applicationInfoService.selectInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 应用列表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入info")
	public R save(@Valid @RequestBody ApplicationInfoEntity info) {
		return R.status(applicationInfoService.save(info));
	}

	/**
	 * 应用列表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入info")
	public R update(@Valid @RequestBody ApplicationInfoEntity info) {
		return R.status(applicationInfoService.updateById(info));
	}

	/**
	 * 应用列表 新增或修改
	 */
	@Transactional
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入info")
	public R submit(@Valid @RequestBody ApplicationInfoEntity infoEntity) {
//		if (infoEntity.getId() != null) {
//			bladeRedis.del(infoEntity.getId().toString());
//		}
		applicationInfoService.saveOrUpdate(infoEntity);
		// 保存记录
		ApplicationHistoryEntity historyEntity = new ApplicationHistoryEntity();
		BeanUtils.copyProperties(infoEntity, historyEntity);
		applicationHistoryService.save(historyEntity);
		// 修改缓存
//		bladeRedis.set(infoEntity.getId().toString(), infoEntity.getJkdz());
		return R.status(true);
	}

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

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

}
