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.EquipmentLedgerExcel;
import org.springblade.transport.pojo.entity.EquipmentLedger;
import org.springblade.transport.pojo.vo.EquipmentLedgerVO;
import org.springblade.transport.service.IEquipmentLedgerService;
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 = "equipment_ledger")
@RequestMapping("/equipment-ledger")
@Tag(name = "设备台账", description = "设备台账")
public class EquipmentLedgerController 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 IEquipmentLedgerService equipmentLedgerService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入设备台账ID")
	public R<EquipmentLedger> detail(@RequestParam Long id) {
		return R.data(equipmentLedgerService.getById(id));
	}

	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页")
	public R<IPage<EquipmentLedgerVO>> list(EquipmentLedgerVO equipmentLedger, Query query) {
		return R.data(equipmentLedgerService.selectEquipmentLedgerPage(Condition.getPage(normalizeQuery(query)), equipmentLedger));
	}

	@GetMapping("/next-equipment-code")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "获取下一个设备编号")
	public R<String> nextEquipmentCode() {
		return R.data(equipmentLedgerService.nextEquipmentCode());
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增或修改")
	public R submit(@Valid @RequestBody EquipmentLedger equipmentLedger) {
		return R.status(equipmentLedgerService.submit(equipmentLedger));
	}

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

	@PostMapping("/import-equipment-ledger")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导入设备台账")
	public R importEquipmentLedger(MultipartFile file, HttpServletResponse response) {
		List<EquipmentLedgerExcel> failureList = equipmentLedgerService.importEquipmentLedger(ExcelUtil.read(file, EquipmentLedgerExcel.class));
		if (Func.isNotEmpty(failureList)) {
			org.springblade.common.excel.ImportFailureExcelUtil.export(response, "设备台账导入失败明细" + DateUtil.time(), "导入失败明细", failureList, EquipmentLedgerExcel.class);
			return null;
		}
		return R.success("操作成功");
	}

	@GetMapping("/export-equipment-ledger")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导出设备台账")
	public void exportEquipmentLedger(EquipmentLedgerVO equipmentLedger, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<EquipmentLedgerExcel> list = equipmentLedgerService.exportEquipmentLedger(buildExportQuery(equipmentLedger, ids));
		ExcelUtil.export(response, "设备台账" + DateUtil.time(), "设备台账表", list, EquipmentLedgerExcel.class);
	}

	@GetMapping("/export-template")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出模板")
	public void exportTemplate(HttpServletResponse response) {
		ExcelUtil.export(response, "设备台账模板", "设备台账表", new ArrayList<>(), EquipmentLedgerExcel.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<EquipmentLedger> buildExportQuery(EquipmentLedgerVO equipmentLedger, String ids) {
		LambdaQueryWrapper<EquipmentLedger> queryWrapper = Wrappers.<EquipmentLedger>lambdaQuery()
			.eq(EquipmentLedger::getIsDeleted, 0)
			.orderByDesc(EquipmentLedger::getCreateTime);
		if (Func.isNotEmpty(ids)) queryWrapper.in(EquipmentLedger::getId, Func.toLongList(ids));
		if (Func.isNotEmpty(equipmentLedger.getVehicleNo())) queryWrapper.like(EquipmentLedger::getVehicleNo, equipmentLedger.getVehicleNo());
		if (Func.isNotEmpty(equipmentLedger.getVehicleType())) queryWrapper.eq(EquipmentLedger::getVehicleType, equipmentLedger.getVehicleType());
		if (Func.isNotEmpty(equipmentLedger.getEquipmentCode())) queryWrapper.like(EquipmentLedger::getEquipmentCode, equipmentLedger.getEquipmentCode());
		if (Func.isNotEmpty(equipmentLedger.getCreateDept())) queryWrapper.eq(EquipmentLedger::getCreateDept, equipmentLedger.getCreateDept());
		return queryWrapper;
	}
}
