/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.yhyj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.yhyj.entity.YhyjEntity;
import org.springblade.modules.yhyj.vo.YhyjVO;
import org.springblade.modules.yhyj.excel.YhyjExcel;
import org.springblade.modules.yhyj.wrapper.YhyjWrapper;
import org.springblade.modules.yhyj.service.IYhyjService;
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 springfox.documentation.annotations.ApiIgnore;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 消防检查——隐患移交三方 控制器
 *
 * @author BladeX
 * @since 2024-04-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-yhyj/yhyj")
@Api(value = "消防检查——隐患移交三方", tags = "消防检查——隐患移交三方接口")
public class YhyjController extends BladeController {

	private final IYhyjService yhyjService;

	/**
	 * 消防检查——隐患移交三方 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入yhyj")
	public R<YhyjVO> detail(YhyjEntity yhyj) {
		YhyjEntity detail = yhyjService.getOne(Condition.getQueryWrapper(yhyj));
		return R.data(YhyjWrapper.build().entityVO(detail));
	}
	/**
	 * 消防检查——隐患移交三方 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入yhyj")
	public R<IPage<YhyjVO>> list(@ApiIgnore @RequestParam Map<String, Object> yhyj, Query query) {
		IPage<YhyjEntity> pages = yhyjService.page(Condition.getPage(query), Condition.getQueryWrapper(yhyj, YhyjEntity.class));
		return R.data(YhyjWrapper.build().pageVO(pages));
	}

	/**
	 * 消防检查——隐患移交三方 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入yhyj")
	public R<IPage<YhyjVO>> page(YhyjVO yhyj, Query query) {
		yhyj.setDeptId(AuthUtil.getDeptId());
		IPage<YhyjVO> pages = yhyjService.selectYhyjPage(Condition.getPage(query), yhyj);
		return R.data(pages);
	}

	/**
	 * 消防检查——隐患移交三方 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入yhyj")
	public R save(@Valid @RequestBody YhyjEntity yhyj) {
		return R.status(yhyjService.save(yhyj));
	}

	/**
	 * 消防检查——隐患移交三方 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入yhyj")
	public R update(@Valid @RequestBody YhyjEntity yhyj) {
		return R.status(yhyjService.updateById(yhyj));
	}

	/**
	 * 消防检查——隐患移交三方 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入yhyj")
	public R submit(@Valid @RequestBody YhyjEntity yhyj) {
		return R.status(yhyjService.saveOrUpdate(yhyj));
	}

	/**
	 * 消防检查——隐患移交三方 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(yhyjService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-yhyj")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入yhyj")
	public void exportYhyj(@ApiIgnore @RequestParam Map<String, Object> yhyj, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<YhyjEntity> queryWrapper = Condition.getQueryWrapper(yhyj, YhyjEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Yhyj::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(YhyjEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<YhyjExcel> list = yhyjService.exportYhyj(queryWrapper);
		ExcelUtil.export(response, "消防检查——隐患移交三方数据" + DateUtil.time(), "消防检查——隐患移交三方数据表", list, YhyjExcel.class);
	}

	/**
	 * 根据id查询隐患移交的result
	 */
	@GetMapping("getResultById")
	public R getResultById(Long id) {
		YhyjEntity yhyj = yhyjService.getById(id);
		return R.data(yhyj.getResult());
	}
}
