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 lombok.extern.slf4j.Slf4j;
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.RyInfoExcel;
import org.springblade.swzdr.pojo.entity.RyInfoEntity;
import org.springblade.swzdr.pojo.vo.RyInfoVO;
import org.springblade.swzdr.pojo.vo.SwzdrDjVO;
import org.springblade.swzdr.service.IRyInfoService;
import org.springblade.swzdr.wrapper.RyInfoWrapper;
import org.springframework.beans.BeanUtils;
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-12
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blade-ryInfo/ryInfo")
@Tag(name = "人员_基本信息", description = "人员_基本信息接口")
public class SwzdrRyInfoController extends BladeController {

	private final IRyInfoService ryInfoService;

	/**
	 * 人员_基本信息 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入ryInfo")
	public R<SwzdrDjVO> detail(Long id) {
		try {
			RyInfoEntity ryInfoEntity = ryInfoService.getById(id);
			return R.data(ryInfoService.getByUserId(ryInfoEntity));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return R.fail("查询失败，请联系管理员");
		}
	}

	/**
	 * 人员_基本信息 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入ryInfo")
	public R<IPage<RyInfoVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> ryInfo, Query query) {
		IPage<RyInfoEntity> pages = ryInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(ryInfo, RyInfoEntity.class));
		return R.data(RyInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 人员_基本信息 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入ryInfo")
	public R<IPage<RyInfoVO>> page(@RequestParam Map<String, String> params, Query query) {
		params.put("userId", AuthUtil.getUserId().toString());
		params.put("deptId", AuthUtil.getDeptId().split(",")[0]);
		IPage<RyInfoVO> pages = ryInfoService.selectRyInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@Transactional
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入SwzdrDjVO")
	public R save(@Valid @RequestBody SwzdrDjVO swzdrDjVO) {
		// 保存人员基本信息
		RyInfoEntity ryInfoEntity = new RyInfoEntity();
		BeanUtils.copyProperties(swzdrDjVO, ryInfoEntity);
		ryInfoService.saveOrUpdate(ryInfoEntity);
		// 保存人员其他信息
		swzdrDjVO.setId(ryInfoEntity.getId());
		return R.status(ryInfoService.swzdrDj(swzdrDjVO));
	}

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

	/**
	 *
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入ryInfo")
	public R update(@Valid @RequestBody RyInfoEntity ryInfo) {
		return R.status(ryInfoService.updateById(ryInfo));
	}

	/**
	 *
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入ryInfo")
	public R submit(@Valid @RequestBody RyInfoEntity ryInfo) {
		return R.status(ryInfoService.saveOrUpdate(ryInfo));
	}

	/**
	 *
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-ryInfo")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入ryInfo")
	public void exportRyInfo(@Parameter(hidden = true) @RequestParam Map<String, Object> ryInfo, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RyInfoEntity> queryWrapper = Condition.getQueryWrapper(ryInfo, RyInfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(RyInfo::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(RyInfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RyInfoExcel> list = ryInfoService.exportRyInfo(queryWrapper);
		ExcelUtil.export(response, "人员_基本信息数据" + DateUtil.time(), "人员_基本信息数据表", list, RyInfoExcel.class);
	}

}
