/*
 *    Copyright (c) 2018-2025, lengleng 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 yifu4cloud.com 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: lengleng (wangiegie@gmail.com)
 */

package com.yifu.cloud.plus.v1.yifu.archives.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TGzArea;
import com.yifu.cloud.plus.v1.yifu.archives.service.TGzAreaService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TGzAreaSearchVo;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 瓜子特殊字典
 *
 * @author hgw
 * @date 2026-02-03 14:16:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tgzarea")
@Tag(name = "瓜子特殊字典管理")
public class TGzAreaController {

	private final TGzAreaService tGzAreaService;


	/**
	 * 简单分页查询
	 *
	 * @param page    分页对象
	 * @param tGzArea 瓜子特殊字典
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TGzArea>> getTGzAreaPage(Page<TGzArea> page, TGzArea tGzArea) {
		return new R<>(tGzAreaService.getTGzAreaPage(page, tGzArea));
	}

	/**
	 * 不分页查询
	 *
	 * @param tGzArea 瓜子特殊字典
	 * @return
	 */
	@Operation(summary = "不分页查询", description = "不分页查询")
	@PostMapping("/noPage")
	public R<List<TGzArea>> getTGzAreaList(@RequestBody TGzArea tGzArea) {
		return R.ok(tGzAreaService.getTGzAreaList(tGzArea));
	}

	/**
	 * 通过id查询瓜子特殊字典
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询：hasPermission('archives_tgzarea_get')")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('archives_tgzarea_get')")
	public R<TGzArea> getById(@PathVariable("id") String id) {
		return R.ok(tGzAreaService.getById(id));
	}

	/**
	 * 新增瓜子特殊字典
	 *
	 * @param tGzArea 瓜子特殊字典
	 * @return R
	 */
	@Operation(summary = "新增瓜子特殊字典", description = "新增瓜子特殊字典：hasPermission('archives_tgzarea_add')")
	@SysLog("新增瓜子特殊字典")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('archives_tgzarea_add')")
	public R<String> save(@RequestBody TGzArea tGzArea) {
		R<String> judgeR = this.judgeCore(tGzArea);
		if (judgeR != null) return judgeR;
		tGzAreaService.save(tGzArea);
		return R.ok();
	}

	private R<String> judgeCore(TGzArea tGzArea) {
		if (Common.isEmpty(tGzArea.getAreaCode())) {
			return R.failed("请填写皖信地址编码");
		}
		if (Common.isEmpty(tGzArea.getGzCode())) {
			return R.failed("请填写瓜子地址编码");
		}
		long count = tGzAreaService.getTGzAreaCountByCode(tGzArea.getAreaCode(), tGzArea.getId());
		if (count > 0) {
			return R.failed("该皖信地址编码已存在!");
		}
		return null;
	}

	/**
	 * 修改瓜子特殊字典
	 *
	 * @param tGzArea 瓜子特殊字典
	 * @return R
	 */
	@Operation(summary = "修改瓜子特殊字典", description = "修改瓜子特殊字典：hasPermission('archives_tgzarea_edit')")
	@SysLog("修改瓜子特殊字典")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('archives_tgzarea_edit')")
	public R<String> updateById(@RequestBody TGzArea tGzArea) {
		R<String> judgeR = this.judgeCore(tGzArea);
		if (judgeR != null) return judgeR;
		tGzAreaService.updateById(tGzArea);
		return R.ok();
	}

	/**
	 * 通过id删除瓜子特殊字典
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除瓜子特殊字典", description = "通过id删除瓜子特殊字典：hasPermission('archives_tgzarea_del')")
	@SysLog("通过id删除瓜子特殊字典")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('archives_tgzarea_del')")
	public R<Boolean> removeById(@PathVariable String id) {
		return R.ok(tGzAreaService.removeById(id));
	}


	/**
	 * 瓜子特殊字典 批量导出
	 *
	 * @author hgw
	 * @date 2026-02-03 14:16:10
	 **/
	@Operation(description = "导出瓜子特殊字典")
	@PostMapping("/export")
	public void export(HttpServletResponse response, @RequestBody TGzAreaSearchVo searchVo) {
		tGzAreaService.listExport(response, searchVo);
	}
}
