package org.springblade.modules.bq.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 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.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.bq.excel.BqSpExcel;
import org.springblade.modules.bq.pojo.entity.BqSpEntity;
import org.springblade.modules.bq.pojo.vo.BqSpVO;
import org.springblade.modules.bq.service.IBqInfoService;
import org.springblade.modules.bq.service.IBqSpService;
import org.springblade.modules.bq.wrapper.BqSpWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 标签_审批 控制器
 *
 * @author wth
 * @since 2024-11-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/bqSp")
@Api(value = "标签_审批", tags = "标签_审批接口")
public class BqSpController extends BladeController {

	private final IBqSpService bqSpService;

	private final IBqInfoService bqInfoService;

	/**
	 * 标签_审批 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bqSp")
	public R<BqSpVO> detail(BqSpEntity bqSp) {
		BqSpEntity detail = bqSpService.getOne(Condition.getQueryWrapper(bqSp));
		return R.data(BqSpWrapper.build().entityVO(detail));
	}

	/**
	 * 标签_审批 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bqSp")
	public R<IPage<BqSpVO>> list(@RequestParam Map<String, Object> bqSp, Query query) {
		IPage<BqSpEntity> pages = bqSpService.page(Condition.getPage(query), Condition.getQueryWrapper(bqSp, BqSpEntity.class));
		return R.data(BqSpWrapper.build().pageVO(pages));
	}

	/**
	 * 标签_审批 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bqSp")
	public R<IPage<BqSpVO>> page(BqSpVO bqSp, Query query) {
		IPage<BqSpVO> pages = bqSpService.selectBqSpPage(Condition.getPage(query), bqSp);
		return R.data(pages);
	}

	/**
	 * 标签_删除审批
	 */
	@Transactional
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bqSp")
	public R save(@Valid @RequestBody BqSpEntity bqSp) {
		// 默认审批通过  平台端需删除
		bqSp.setSpjg(1);
		bqSpService.save(bqSp);
		// 默认审批通过  平台端需删除
		return R.status(bqInfoService.removeById(bqSp.getBqId()));
	}

	/**
	 * 标签_审批 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bqSp")
	public R update(@Valid @RequestBody BqSpEntity bqSp) {
		return R.status(bqSpService.updateById(bqSp));
	}

	/**
	 * 标签_审批 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bqSp")
	public R submit(@Valid @RequestBody BqSpEntity bqSp) {
		return R.status(bqSpService.saveOrUpdate(bqSp));
	}

	/**
	 * 标签_审批 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@RequestParam String ids) {
		return R.status(bqSpService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-bqSp")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导出数据", notes = "传入bqSp")
	public void exportBqSp(@RequestParam Map<String, Object> bqSp, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<BqSpEntity> queryWrapper = Condition.getQueryWrapper(bqSp, BqSpEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(BqSpEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(BqSpEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<BqSpExcel> list = bqSpService.exportBqSp(queryWrapper);
		ExcelUtil.export(response, "标签_审批数据" + DateUtil.time(), "标签_审批数据表", list, BqSpExcel.class);
	}

}
