/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xiaoniu.subsystem.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.common.core.result.R;
import com.xiaoniu.common.core.utils.BeanUtil;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.subsystem.system.domain.Document;
import com.xiaoniu.subsystem.system.domain.dto.DocumentDTO;
import com.xiaoniu.subsystem.system.domain.dto.DocumentPageDTO;
import com.xiaoniu.subsystem.system.domain.vo.DocumentVO;
import com.xiaoniu.subsystem.system.service.IDocumentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUnitId;
import static com.xiaoniu.common.security.utils.SecurityUtils.getUserId;

/**
 * 文件管理表 控制器
 *
 * @author sst
 * @since 2024-11-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/document")
@Tag(name = "数据接收管理表", description = "数据接收管理表接口")
public class DocumentController  {

	private IDocumentService documentService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入document")
	public R<DocumentVO> detail(DocumentDTO dto) {
		Document detail = documentService.getOne(new QueryWrapper<>(dto));
		return R.data(BeanUtil.copy(detail, DocumentVO.class));
	}

	/**
	 * 分页 文件管理表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入document")
	public R<IPage<DocumentVO>> page(DocumentPageDTO dto) {
		IPage<Document> pages = documentService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
				new LambdaQueryWrapper<Document>()
						.like(StringUtils.isNotBlank(dto.getDocumentName()),Document::getDocumentName,
								dto.getDocumentName())
						.orderByDesc(Document::getCreateTime));
		return R.data(BeanUtil.copy(pages, DocumentVO.class));
	}

	/**
	 * 新增 文件管理表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入document")
	public R save(@Valid @RequestBody DocumentDTO dto) {
		Document one = documentService.getOne(new LambdaQueryWrapper<Document>()
				.eq(Document::getFlightName, dto.getFlightName())
				.eq(Document::getFlightId, dto.getFlightId())
				.eq(Document::getDocumentCode, dto.getDocumentCode()));
		if (null != one) {
			return R.fail("已存在上报单位名称：" + dto.getFlightName() +
					",编码：" + dto.getDocumentCode() +
					"的文件，请核对");
		}
		return R.status(documentService.save(
				dto.setCreateDept(getUnitId()).setCreateUser(getUserId()).setCreateTime(new Date())));
	}

	/**
	 * 修改 文件管理表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入document")
	public R update(@Valid @RequestBody DocumentDTO dto) {
		Document one = documentService.getOne(new LambdaQueryWrapper<Document>()
				.ne(Document::getDocumentId, dto.getDocumentId())
				.eq(Document::getFlightName, dto.getFlightName())
				.eq(Document::getFlightId, dto.getFlightId())
				.eq(Document::getDocumentCode, dto.getDocumentCode()));
		if (null != one) {
			return R.fail("已存在上报单位名称：" + dto.getFlightName() +
					",编码：" + dto.getDocumentCode() +
					"的文件，请核对");
		}
		return R.status(documentService.updateById(
				dto.setUpdateUser(getUserId()).setUpdateTime(new Date())));
	}

	// /**
	//  * 新增或修改 文件管理表
	//  */
	// @PostMapping("/submit")
	// @ApiOperationSupport(order = 6)
	// @Operation(summary = "新增或修改", description = "传入document")
	// public R submit(@Valid @RequestBody Document document) {
	// 	return R.status(documentService.saveOrUpdate(document));
	// }


	/**
	 * 删除 文件管理表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String documentIds) {
		return R.status(documentService.removeByIds(Arrays.stream(documentIds.split(",")).toList()));
	}

	
}
