/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.transport.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.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.transport.excel.OilElectricRecordExcel;
import org.springblade.transport.excel.OilElectricRecordExportExcel;
import org.springblade.transport.excel.OilElectricRecordImporter;
import org.springblade.transport.pojo.entity.OilElectricRecord;
import org.springblade.transport.pojo.vo.OilElectricRecordVO;
import org.springblade.transport.service.IOilElectricRecordService;
import org.springblade.transport.wrapper.OilElectricRecordWrapper;
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
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "oil_electric_record")
@RequestMapping("/oil-electric-record")
@Tag(name = "油电记录", description = "油电记录")
public class OilElectricRecordController extends BladeController {

	private static final int DEFAULT_CURRENT = 1;
	private static final int DEFAULT_SIZE = 10;
	private static final int MAX_SIZE = 100;

	private final IOilElectricRecordService oilElectricRecordService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入oilElectricRecord")
	public R<OilElectricRecordVO> detail(OilElectricRecord oilElectricRecord) {
		OilElectricRecord detail = oilElectricRecordService.getOne(Condition.getQueryWrapper(oilElectricRecord));
		return R.data(OilElectricRecordWrapper.build().entityVO(detail));
	}

	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入oilElectricRecord")
	public R<IPage<OilElectricRecordVO>> list(OilElectricRecordVO oilElectricRecord, Query query) {
		IPage<OilElectricRecordVO> pages = oilElectricRecordService.selectOilElectricRecordPage(Condition.getPage(normalizeQuery(query)), oilElectricRecord);
		return R.data(pages);
	}

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

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

	@PostMapping("/import-oil-electric-record")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "导入油电记录", description = "传入excel")
	public R importOilElectricRecord(MultipartFile file, HttpServletResponse response) {
		List<OilElectricRecordExcel> failureList = oilElectricRecordService.importOilElectricRecord(ExcelUtil.read(file, OilElectricRecordExcel.class));
		if (Func.isNotEmpty(failureList)) {
			org.springblade.common.excel.ImportFailureExcelUtil.export(response, "油电记录导入失败明细" + DateUtil.time(), "导入失败明细", failureList, OilElectricRecordExcel.class);
			return null;
		}
		return R.success("操作成功");
	}

	@GetMapping("/export-oil-electric-record")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导出油电记录")
	public void exportOilElectricRecord(OilElectricRecordVO oilElectricRecord,
										@RequestParam(required = false) String ids,
										HttpServletResponse response) {
		List<OilElectricRecordExportExcel> list = oilElectricRecordService.exportOilElectricRecord(buildExportQuery(oilElectricRecord, ids));
		ExcelUtil.export(response, "油电记录" + DateUtil.time(), "油电记录表", list, OilElectricRecordExportExcel.class);
	}

	@GetMapping("/export-template")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导出模板")
	public void exportTemplate(HttpServletResponse response) {
		List<OilElectricRecordExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "油电记录模板", "油电记录表", list, OilElectricRecordExcel.class);
	}

	private Query normalizeQuery(Query query) {
		if (query == null) {
			query = new Query();
		}
		if (query.getCurrent() == null || query.getCurrent() < DEFAULT_CURRENT) {
			query.setCurrent(DEFAULT_CURRENT);
		}
		if (query.getSize() == null || query.getSize() <= 0) {
			query.setSize(DEFAULT_SIZE);
		}
		if (query.getSize() > MAX_SIZE) {
			query.setSize(MAX_SIZE);
		}
		return query;
	}

	private LambdaQueryWrapper<OilElectricRecord> buildExportQuery(OilElectricRecordVO oilElectricRecord, String ids) {
		LambdaQueryWrapper<OilElectricRecord> queryWrapper = Wrappers.<OilElectricRecord>lambdaQuery()
			.eq(OilElectricRecord::getIsDeleted, 0)
			.orderByDesc(OilElectricRecord::getTransactionTime)
			.orderByDesc(OilElectricRecord::getCreateTime);
		if (Func.isNotEmpty(ids)) {
			queryWrapper.in(OilElectricRecord::getId, Func.toLongList(ids));
		}
		if (Func.isNotEmpty(oilElectricRecord.getCreateDept())) {
			queryWrapper.eq(OilElectricRecord::getCreateDept, oilElectricRecord.getCreateDept());
		}
		if (Func.isNotEmpty(oilElectricRecord.getVehicleType())) {
			queryWrapper.eq(OilElectricRecord::getVehicleType, oilElectricRecord.getVehicleType());
		}
		if (Func.isNotEmpty(oilElectricRecord.getFeeType())) {
			queryWrapper.eq(OilElectricRecord::getFeeType, oilElectricRecord.getFeeType());
		}
		if (Func.isNotEmpty(oilElectricRecord.getVehicleNo())) {
			queryWrapper.like(OilElectricRecord::getVehicleNo, oilElectricRecord.getVehicleNo());
		}
		if (Func.isNotEmpty(oilElectricRecord.getTransactionTimeStart())) {
			queryWrapper.ge(OilElectricRecord::getTransactionTime, oilElectricRecord.getTransactionTimeStart());
		}
		if (Func.isNotEmpty(oilElectricRecord.getTransactionTimeEnd())) {
			queryWrapper.le(OilElectricRecord::getTransactionTime, oilElectricRecord.getTransactionTimeEnd());
		}
		if (Func.isNotEmpty(oilElectricRecord.getTransactionAmountStart())) {
			queryWrapper.ge(OilElectricRecord::getTransactionAmount, oilElectricRecord.getTransactionAmountStart());
		}
		if (Func.isNotEmpty(oilElectricRecord.getTransactionAmountEnd())) {
			queryWrapper.le(OilElectricRecord::getTransactionAmount, oilElectricRecord.getTransactionAmountEnd());
		}
		if (Func.isNotEmpty(oilElectricRecord.getCreateTimeStart())) {
			queryWrapper.ge(OilElectricRecord::getCreateTime, oilElectricRecord.getCreateTimeStart());
		}
		if (Func.isNotEmpty(oilElectricRecord.getCreateTimeEnd())) {
			queryWrapper.le(OilElectricRecord::getCreateTime, oilElectricRecord.getCreateTimeEnd());
		}
		return queryWrapper;
	}

}
