/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.common.excel.ImportFailureExcelUtil;
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.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.excel.FeeItemExcel;
import org.springblade.system.excel.FeeItemImportFailureExcel;
import org.springblade.system.pojo.entity.FeeItem;
import org.springblade.system.pojo.vo.FeeItemVO;
import org.springblade.system.service.IFeeItemService;
import org.springblade.system.wrapper.FeeItemWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 费用项 控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@PreAuth(menu = "fee_item")
@RequestMapping("/fee-item")
@Tag(name = "费用项", description = "费用项")
public class FeeItemController extends BladeController {

	private final IFeeItemService feeItemService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入feeItem")
	public R<FeeItemVO> detail(FeeItem feeItem) {
		FeeItem detail = feeItemService.getOne(Condition.getQueryWrapper(feeItem));
		return R.data(FeeItemWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入feeItem")
	public R<IPage<FeeItemVO>> list(FeeItemVO feeItem, Query query) {
		IPage<FeeItemVO> pages = feeItemService.selectFeeItemPage(Condition.getPage(query), feeItem);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "新增或修改", description = "传入feeItem")
	public R submit(@Valid @RequestBody FeeItem feeItem) {
		return R.status(feeItemService.submit(feeItem));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(feeItemService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 启用或停用
	 */
	@PostMapping("/status")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "启用或停用", description = "传入id和status")
	public R status(@Parameter(description = "主键", required = true) @RequestParam Long id,
					@Parameter(description = "状态", required = true) @RequestParam Integer status) {
		return R.status(feeItemService.changeStatus(id, status));
	}

	/**
	 * 导入费用项
	 */
	@PostMapping("/import-fee-item")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导入费用项", description = "传入excel")
	public R importFeeItem(MultipartFile file, HttpServletResponse response) {
		List<FeeItemExcel> data = ExcelUtil.read(file, FeeItemExcel.class);
		List<FeeItemImportFailureExcel> failureList = feeItemService.importFeeItem(data);
		if (Func.isNotEmpty(failureList)) {
			ImportFailureExcelUtil.export(response, "费用项导入失败明细" + DateUtil.time(), "导入失败明细", failureList, FeeItemExcel.class);
			return null;
		}
		return R.success("导入数据成功");
	}

	/**
	 * 导出费用项
	 */
	@GetMapping("/export-fee-item")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导出费用项")
	public void exportFeeItem(FeeItemVO feeItem,
							  @RequestParam(required = false) String ids,
							  HttpServletResponse response) {
		List<FeeItemExcel> list = feeItemService.exportFeeItem(buildExportQuery(feeItem, ids));
		ExcelUtil.export(response, "费用项" + DateUtil.time(), "费用项表", list, FeeItemExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("/export-template")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出模板")
	public void exportTemplate(HttpServletResponse response) {
		List<FeeItemExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "费用项模板", "费用项导入模板", list, FeeItemExcel.class);
	}

	private LambdaQueryWrapper<FeeItem> buildExportQuery(FeeItemVO feeItem, String ids) {
		LambdaQueryWrapper<FeeItem> queryWrapper = Wrappers.<FeeItem>lambdaQuery()
			.eq(FeeItem::getIsDeleted, 0)
			.orderByDesc(FeeItem::getCreateTime);
		if (Func.isNotEmpty(ids)) {
			queryWrapper.in(FeeItem::getId, Func.toLongList(ids));
		}
		if (Func.isNotEmpty(feeItem.getFeeCategory())) {
			queryWrapper.eq(FeeItem::getFeeCategory, feeItem.getFeeCategory());
		}
		if (Func.isNotEmpty(feeItem.getName())) {
			queryWrapper.like(FeeItem::getName, feeItem.getName());
		}
		if (Func.isNotEmpty(feeItem.getEnglishName())) {
			queryWrapper.like(FeeItem::getEnglishName, feeItem.getEnglishName());
		}
		if (Func.isNotEmpty(feeItem.getStatus())) {
			queryWrapper.eq(FeeItem::getStatus, feeItem.getStatus());
		}
		if (Func.isNotEmpty(feeItem.getCreateDept())) {
			queryWrapper.eq(FeeItem::getCreateDept, feeItem.getCreateDept());
		}
		return queryWrapper;
	}

}
