package org.springblade.swzdr.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.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
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.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.swzdr.excel.RyDjclExcel;
import org.springblade.swzdr.pojo.entity.RyDjclEntity;
import org.springblade.swzdr.pojo.vo.RyDjclVO;
import org.springblade.swzdr.service.IRyDjclService;
import org.springblade.swzdr.wrapper.RyDjclWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 人员_打击处理 控制器
 *
 * @author BladeX
 * @since 2024-09-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-ryDjcl/ryDjcl")
@Tag(name = "人员_打击处理", description = "人员_打击处理接口")
public class RyDjclController extends BladeController {

	private final IRyDjclService ryDjclService;

	/**
	 * 人员_打击处理 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入ryDjcl")
	public R<RyDjclVO> detail(RyDjclEntity ryDjcl) {
		RyDjclEntity detail = ryDjclService.getOne(Condition.getQueryWrapper(ryDjcl));
		return R.data(RyDjclWrapper.build().entityVO(detail));
	}
	/**
	 * 人员_打击处理 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入ryDjcl")
	public R<IPage<RyDjclVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> ryDjcl, Query query) {
		IPage<RyDjclEntity> pages = ryDjclService.page(Condition.getPage(query), Condition.getQueryWrapper(ryDjcl, RyDjclEntity.class));
		return R.data(RyDjclWrapper.build().pageVO(pages));
	}

	/**
	 * 人员_打击处理 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入ryDjcl")
	public R<IPage<RyDjclVO>> page(@RequestParam Map<String, String> params, Query query) {
		params.put("userId", AuthUtil.getUserId().toString());
		params.put("deptId", AuthUtil.getDeptId().split(",")[0]);
		IPage<RyDjclVO> pages = ryDjclService.selectRyDjclPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 人员_撤管记录 根据人员id查询
	 */
	@GetMapping("/getListByRyId")
	public R getListByRyId(Long ryId) {
		List<RyDjclVO> djclList = ryDjclService.getListByRyId(ryId);
		return R.data(djclList);
	}

	/**
	 * 人员_打击处理 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入ryDjcl")
	public R save(@Valid @RequestBody RyDjclEntity ryDjcl) {
		return R.status(ryDjclService.save(ryDjcl));
	}

	/**
	 * 人员_打击处理 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入ryDjcl")
	public R update(@Valid @RequestBody RyDjclEntity ryDjcl) {
		return R.status(ryDjclService.updateById(ryDjcl));
	}

	/**
	 * 人员_打击处理 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入ryDjcl")
	public R submit(@Valid @RequestBody RyDjclEntity ryDjcl) {
		return R.status(ryDjclService.saveOrUpdate(ryDjcl));
	}

	/**
	 * 人员_打击处理 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description  = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return R.status(ryDjclService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-ryDjcl")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入ryDjcl")
	public void exportRyDjcl(@Parameter(hidden = true) @RequestParam Map<String, Object> ryDjcl, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RyDjclEntity> queryWrapper = Condition.getQueryWrapper(ryDjcl, RyDjclEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(RyDjcl::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(RyDjclEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RyDjclExcel> list = ryDjclService.exportRyDjcl(queryWrapper);
		ExcelUtil.export(response, "人员_打击处理数据" + DateUtil.time(), "人员_打击处理数据表", list, RyDjclExcel.class);
	}

}
