/**
 * 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.Edition;
import com.xiaoniu.subsystem.system.domain.dto.EditionDTO;
import com.xiaoniu.subsystem.system.domain.dto.EditionPageDTO;
import com.xiaoniu.subsystem.system.domain.vo.DocumentVO;
import com.xiaoniu.subsystem.system.domain.vo.EditionVO;
import com.xiaoniu.subsystem.system.service.IEditionService;
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.Collections;
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("/edition")
@Tag(name = "版本管理表", description = "版本管理表接口")
public class EditionController  {

	private IEditionService editionService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入edition")
	public R<EditionVO> detail(EditionDTO dto) {
		Edition detail = editionService.getOne(new QueryWrapper<>(dto));
		return R.data(BeanUtil.copy(detail, EditionVO.class));
	}

	/**
	 * 分页 版本管理表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入edition")
	public R<IPage<EditionVO>> page(EditionPageDTO dto) {
		IPage<Edition> pages = editionService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
				new LambdaQueryWrapper<Edition>()
						.like(StringUtils.isNotBlank(dto.getEditionName()),Edition::getEditionName,
								dto.getEditionName())
						.orderByDesc(Edition::getCreateTime));
		return R.data(BeanUtil.copy(pages, EditionVO.class));
	}

	/**
	 * 新增 版本管理表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入edition")
	public R save(@Valid @RequestBody EditionDTO dto) {
		return R.status(editionService.save(dto
				.setCreateDept(getUnitId()).setCreateUser(getUserId()).setCreateTime(new Date())));
	}

	/**
	 * 修改 版本管理表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入edition")
	public R update(@Valid @RequestBody EditionDTO dto) {
		return R.status(editionService.updateById(
				dto.setUpdateUser(getUserId()).setUpdateTime(new Date())));
	}

	// /**
	//  * 新增或修改 版本管理表
	//  */
	// @PostMapping("/submit")
	// @ApiOperationSupport(order = 6)
	// @Operation(summary = "新增或修改", description = "传入edition")
	// public R submit(@Valid @RequestBody Edition edition) {
	// 	return R.status(editionService.saveOrUpdate(edition));
	// }


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

	
}
