/**
 * 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.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.CurrencyExcel;
import org.springblade.system.excel.CurrencyImporter;
import org.springblade.system.pojo.entity.Currency;
import org.springblade.system.pojo.vo.CurrencyVO;
import org.springblade.system.service.ICurrencyService;
import org.springblade.system.wrapper.CurrencyWrapper;
import org.springframework.format.annotation.DateTimeFormat;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 币种汇率 控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@PreAuth(menu = "currency")
@RequestMapping("/currency")
@Tag(name = "币种汇率", description = "币种汇率")
public class CurrencyController extends BladeController {

	private final ICurrencyService currencyService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入currency")
	public R<CurrencyVO> detail(Currency currency) {
		Currency detail = currencyService.getOne(Condition.getQueryWrapper(currency));
		return R.data(CurrencyWrapper.build().entityVO(detail));
	}

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

	/**
	 * 有效汇率查询
	 */
	@GetMapping("/rate")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "有效汇率查询", description = "根据币种编码和业务日期查询有效汇率")
	public R<CurrencyVO> rate(@Parameter(description = "币种编码", required = true) @RequestParam String code,
							  @Parameter(description = "业务日期") @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(required = false) LocalDate businessDate) {
		return R.data(currencyService.getEffectiveRate(code, businessDate));
	}

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

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

	/**
	 * 启用或停用
	 */
	@PostMapping("/status")
	@ApiOperationSupport(order = 6)
	@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(currencyService.changeStatus(id, status));
	}

	/**
	 * 导入币种汇率
	 */
	@PostMapping("/import-currency")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导入币种汇率", description = "传入excel")
	public R importCurrency(MultipartFile file, HttpServletResponse response) {
		List<CurrencyExcel> failureList = currencyService.importCurrency(ExcelUtil.read(file, CurrencyExcel.class));
		if (Func.isNotEmpty(failureList)) {
			org.springblade.common.excel.ImportFailureExcelUtil.export(response, "币种汇率导入失败明细" + DateUtil.time(), "导入失败明细", failureList, CurrencyExcel.class);
			return null;
		}
		return R.success("操作成功");
	}

	/**
	 * 导出币种汇率
	 */
	@GetMapping("/export-currency")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出币种汇率")
	public void exportCurrency(CurrencyVO currency,
							   @RequestParam(required = false) String ids,
							   HttpServletResponse response) {
		List<CurrencyExcel> list = currencyService.exportCurrency(buildExportQuery(currency, ids));
		ExcelUtil.export(response, "币种汇率" + DateUtil.time(), "币种汇率表", list, CurrencyExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("/export-template")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出模板")
	public void exportTemplate(HttpServletResponse response) {
		List<CurrencyExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "币种汇率模板", "币种汇率表", list, CurrencyExcel.class);
	}

	private LambdaQueryWrapper<Currency> buildExportQuery(CurrencyVO currency, String ids) {
		LambdaQueryWrapper<Currency> queryWrapper = Wrappers.<Currency>lambdaQuery()
			.eq(Currency::getIsDeleted, 0)
			.orderByAsc(Currency::getCode)
			.orderByDesc(Currency::getEffectiveDate);
		if (Func.isNotEmpty(ids)) {
			queryWrapper.in(Currency::getId, Func.toLongList(ids));
		}
		if (Func.isNotEmpty(currency.getCode())) {
			queryWrapper.like(Currency::getCode, currency.getCode());
		}
		if (Func.isNotEmpty(currency.getName())) {
			queryWrapper.like(Currency::getName, currency.getName());
		}
		if (Func.isNotEmpty(currency.getStatus())) {
			queryWrapper.eq(Currency::getStatus, currency.getStatus());
		}
		if (Func.isNotEmpty(currency.getExchangeRate())) {
			queryWrapper.eq(Currency::getExchangeRate, currency.getExchangeRate());
		}
		if (Func.isNotEmpty(currency.getDataSource())) {
			queryWrapper.eq(Currency::getDataSource, currency.getDataSource());
		}
		if (Func.isNotEmpty(currency.getEffectiveDateStart())) {
			queryWrapper.ge(Currency::getEffectiveDate, currency.getEffectiveDateStart());
		}
		if (Func.isNotEmpty(currency.getEffectiveDateEnd())) {
			queryWrapper.le(Currency::getEffectiveDate, currency.getEffectiveDateEnd());
		}
		if (Func.isNotEmpty(currency.getExpiryDateStart())) {
			queryWrapper.ge(Currency::getExpiryDate, currency.getExpiryDateStart());
		}
		if (Func.isNotEmpty(currency.getExpiryDateEnd())) {
			queryWrapper.le(Currency::getExpiryDate, currency.getExpiryDateEnd());
		}
		if (Func.isNotEmpty(currency.getUpdateTimeStart())) {
			queryWrapper.ge(Currency::getUpdateTime, currency.getUpdateTimeStart());
		}
		if (Func.isNotEmpty(currency.getUpdateTimeEnd())) {
			queryWrapper.le(Currency::getUpdateTime, currency.getUpdateTimeEnd());
		}
		return queryWrapper;
	}

}
