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.RyCgspExcel;
import org.springblade.swzdr.pojo.entity.RyCgspEntity;
import org.springblade.swzdr.pojo.vo.RyCgspVO;
import org.springblade.swzdr.service.IRyCgspService;
import org.springblade.swzdr.service.IRyInfoService;
import org.springblade.swzdr.wrapper.RyCgspWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 人员_撤管记录 控制器
 *
 * @author BladeX
 * @since 2024-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-ryCgsp/ryCgsp")
@Tag(name = "人员_撤管记录", description = "人员_撤管记录接口")
public class RyCgspController extends BladeController {

	private final IRyCgspService ryCgspService;

	private final IRyInfoService ryInfoService;

	/**
	 * 人员_撤管记录 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入ryCgsp")
	public R<RyCgspVO> detail(RyCgspEntity ryCgsp) {
		RyCgspEntity detail = ryCgspService.getOne(Condition.getQueryWrapper(ryCgsp));
		return R.data(RyCgspWrapper.build().entityVO(detail));
	}

	/**
	 * 人员_撤管记录 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入ryCgsp")
	public R<IPage<RyCgspVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> ryCgsp, Query query) {
		IPage<RyCgspEntity> pages = ryCgspService.page(Condition.getPage(query), Condition.getQueryWrapper(ryCgsp, RyCgspEntity.class));
		return R.data(RyCgspWrapper.build().pageVO(pages));
	}

	/**
	 * 人员_撤管记录 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入ryCgsp")
	public R<IPage<RyCgspVO>> page(@RequestParam Map<String, String> params, Query query) {
		params.put("userId", AuthUtil.getUserId().toString());
		params.put("deptId", AuthUtil.getDeptId().split(",")[0]);
		IPage<RyCgspVO> pages = ryCgspService.selectRyCgspPage(Condition.getPage(query), params);
		return R.data(pages);
	}

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

	/**
	 * 人员_撤管记录 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入ryCgsp")
	public R save(@Valid @RequestBody RyCgspEntity ryCgsp) {
		return R.status(ryCgspService.save(ryCgsp));
	}

	/**
	 * 人员_撤管记录 修改
	 */
	@Transactional
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入ryCgsp")
	public R update(@Valid @RequestBody RyCgspEntity ryCgsp) {
		ryCgspService.updateById(ryCgsp);
		if (ryCgsp.getSpjg() == 1) {
			// 撤管审批通过，将ry_info表数据删除
			ryInfoService.removeById(ryCgsp.getRyId());
		}
		return R.status(true);
	}

	/**
	 * 人员_撤管记录 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入ryCgsp")
	public R submit(@Valid @RequestBody RyCgspEntity ryCgsp) {
		return R.status(ryCgspService.saveOrUpdate(ryCgsp));
	}

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

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-ryCgsp")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入ryCgsp")
	public void exportRyCgsp(@Parameter(hidden = true) @RequestParam Map<String, Object> ryCgsp, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RyCgspEntity> queryWrapper = Condition.getQueryWrapper(ryCgsp, RyCgspEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(RyCgsp::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(RyCgspEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RyCgspExcel> list = ryCgspService.exportRyCgsp(queryWrapper);
		ExcelUtil.export(response, "人员_撤管记录数据" + DateUtil.time(), "人员_撤管记录数据表", list, RyCgspExcel.class);
	}

}
