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.BqInfoExcel;
import org.springblade.modules.bq.pojo.entity.BqInfoEntity;
import org.springblade.modules.bq.pojo.entity.BqSpEntity;
import org.springblade.modules.bq.pojo.vo.BqInfoVO;
import org.springblade.modules.bq.pojo.vo.EjbqVO;
import org.springblade.modules.bq.pojo.vo.SjbqVO;
import org.springblade.modules.bq.pojo.vo.YjbqVO;
import org.springblade.modules.bq.service.IBqInfoService;
import org.springblade.modules.bq.service.IBqSpService;
import org.springblade.modules.bq.wrapper.BqInfoWrapper;
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("/bqInfo")
@Api(value = "标签表", tags = "标签表接口")
public class BqInfoController extends BladeController {

	private final IBqInfoService bqInfoService;

	private final IBqSpService bqSpService;

	/**
	 * 标签表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bqInfo")
	public R<BqInfoVO> detail(BqInfoEntity bqInfo) {
		BqInfoEntity detail = bqInfoService.getOne(Condition.getQueryWrapper(bqInfo));
		return R.data(BqInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 标签表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bqInfo")
	public R<IPage<BqInfoVO>> list(@RequestParam Map<String, Object> bqInfo, Query query) {
		IPage<BqInfoEntity> pages = bqInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(bqInfo, BqInfoEntity.class));
		return R.data(BqInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 标签表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bqInfo")
	public R page(@RequestParam Map<String, Object> params) {
		params.put("parentId", 0L);
		// 查询一级标签
		List<BqInfoVO> yjbqList = bqInfoService.selectBqInfoPage(params);
		for (BqInfoVO yjbqVO : yjbqList) {
			params.put("parentId", yjbqVO.getId());
			// 查询二级标签
			List<BqInfoVO> ejbqList = bqInfoService.selectBqInfoPage(params);
			for (BqInfoVO ejbqVO : ejbqList) {
				params.put("parentId", ejbqVO.getId());
				// 查询三级标签
				List<BqInfoVO> sjbqList = bqInfoService.selectBqInfoPage(params);
				ejbqVO.setChildren(sjbqList);
			}
			yjbqVO.setChildren(ejbqList);
		}
		return R.data(yjbqList);
	}

	/**
	 * 标签 新增
	 */
	@Transactional
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bqInfo")
	public R save(@Valid @RequestBody BqInfoEntity bqInfo) {
		// 默认审批通过
		bqInfo.setXzzt(1);
		bqInfoService.save(bqInfo);
		BqSpEntity bqSpEntity = new BqSpEntity();
		bqSpEntity.setBqId(bqInfo.getId());
		bqSpEntity.setSplx(1);
		// 默认审批通过
		bqSpEntity.setSpjg(1);
		return R.status(bqSpService.save(bqSpEntity));
	}

	/**
	 * 标签 新增
	 */
	@PostMapping("/saveBatch")
	public R saveBatch(@RequestBody List<YjbqVO> yjbqList) {
		for (YjbqVO yjbqVO : yjbqList) {
			// 一级标签
			BqInfoEntity yjbqEntity = new BqInfoEntity();
			yjbqEntity.setBqlx(1);
			yjbqEntity.setParentId(0L);
			yjbqEntity.setName(yjbqVO.getYjbq());
			yjbqEntity.setXzzt(1);
			yjbqEntity.setBqdj(1);
			bqInfoService.save(yjbqEntity);
			for (EjbqVO ejbqVO : yjbqVO.getEjList()) {
				// 二级标签
				BqInfoEntity ejbqEntity = new BqInfoEntity();
				ejbqEntity.setBqlx(1);
				ejbqEntity.setParentId(yjbqEntity.getId());
				ejbqEntity.setName(ejbqVO.getEjbq());
				ejbqEntity.setXzzt(1);
				ejbqEntity.setBqdj(2);
				bqInfoService.save(ejbqEntity);
				for (SjbqVO sjbqVO : ejbqVO.getSjList()) {
					// 三级标签
					BqInfoEntity sjbqEntity = new BqInfoEntity();
					sjbqEntity.setBqlx(1);
					sjbqEntity.setParentId(ejbqEntity.getId());
					sjbqEntity.setName(sjbqVO.getSjbq());
					sjbqEntity.setXzzt(1);
					sjbqEntity.setBqdj(3);
					bqInfoService.save(sjbqEntity);
				}
			}
		}
		return R.status(true);
	}

	/**
	 * 标签表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bqInfo")
	public R update(@Valid @RequestBody BqInfoEntity bqInfo) {
		return R.status(bqInfoService.updateById(bqInfo));
	}

	/**
	 * 标签表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bqInfo")
	public R submit(@Valid @RequestBody BqInfoEntity bqInfo) {
		return R.status(bqInfoService.saveOrUpdate(bqInfo));
	}

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

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

}
