/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.dictBszn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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.dictBszn.excel.DictBsznExcel;
import org.springblade.modules.dictBszn.pojo.entity.DictBsznEntity;
import org.springblade.modules.dictBszn.pojo.vo.BsznVO;
import org.springblade.modules.dictBszn.pojo.vo.DictBsznVO;
import org.springblade.modules.dictBszn.service.IDictBsznService;
import org.springframework.web.bind.annotation.*;

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

/**
 *  控制器
 *
 * @author BladeX
 * @since 2024-10-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dictBszn")
public class DictBsznController extends BladeController {

	private final IDictBsznService dictBsznService;

	/**
	 *  详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	public R<DictBsznEntity> detail(DictBsznEntity dictBszn) {
		DictBsznEntity detail = dictBsznService.getOne(Condition.getQueryWrapper(dictBszn));
		return R.data(detail);
	}

	/**
	 *  分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	public R<IPage<DictBsznEntity>> list(@RequestParam Map<String, Object> dictBszn, Query query) {
		IPage<DictBsznEntity> pages = dictBsznService.page(Condition.getPage(query), Condition.getQueryWrapper(dictBszn, DictBsznEntity.class));
		return R.data(pages);
	}


	/**
	 *  自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	public R<IPage<DictBsznVO>> page(DictBsznVO dictBszn, Query query) {
		IPage<DictBsznVO> pages = dictBsznService.selectDictBsznPage(Condition.getPage(query), dictBszn);
		return R.data(pages);
	}

	/**
	 *  新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	public R save(@Valid @RequestBody DictBsznEntity dictBszn) {
		return R.status(dictBsznService.save(dictBszn));
	}

	/**
	 *  修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	public R update(@Valid @RequestBody DictBsznEntity dictBszn) {
		return R.status(dictBsznService.updateById(dictBszn));
	}

	/**
	 *  新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	public R submit(@Valid @RequestBody DictBsznEntity dictBszn) {
		return R.status(dictBsznService.saveOrUpdate(dictBszn));
	}

	/**
	 *  删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	public R remove(@RequestParam String ids) {
		return R.status(dictBsznService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-dictBszn")
	@ApiOperationSupport(order = 8)
	public void exportDictBszn(@RequestParam Map<String, Object> dictBszn, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<DictBsznEntity> queryWrapper = Condition.getQueryWrapper(dictBszn, DictBsznEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(DictBsznEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(DictBsznEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<DictBsznExcel> list = dictBsznService.exportDictBszn(queryWrapper);
		ExcelUtil.export(response, "数据" + DateUtil.time(), "数据表", list, DictBsznExcel.class);
	}

	/**
	 * 根据事项名称模糊搜索
	 */
	@GetMapping("selectListByMc")
	public R selectListByMc(String dictValue) {
		return R.data(dictBsznService.selectListByMc(dictValue));
	}

	/**
	 * 根据id查看详情
	 */
	@GetMapping("detailById")
	public R detailById(Long id) {
		return R.data(dictBsznService.detailById(id));
	}

	/**
	 * 办事指南列表
	 */
	@GetMapping("bsznList")
	public R bsznList() {
		List<BsznVO> topList = dictBsznService.selectTop();
		for (BsznVO top:topList) {
			List<BsznVO> oneList = dictBsznService.selectOneList(top.value);
			top.setChildren(oneList);
			for (BsznVO one:oneList) {
				List<BsznVO> twoList = dictBsznService.selectOneList(one.value);
				one.setChildren(twoList);
				for (BsznVO two:twoList) {
					List<BsznVO> threeList = dictBsznService.selectOneList(two.value);
					two.setChildren(threeList);
				}
			}
		}
		return R.data(topList);
	}
}
