/**
 * 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.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.AccidentRecordExcel;
import org.springblade.transport.excel.AccidentRecordExportExcel;
import org.springblade.transport.excel.AccidentRecordImporter;
import org.springblade.transport.pojo.entity.AccidentRecord;
import org.springblade.transport.pojo.vo.AccidentRecordVO;
import org.springblade.transport.service.IAccidentRecordService;
import org.springblade.transport.wrapper.AccidentRecordWrapper;
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 = "accident_record")
@RequestMapping("/accident-record")
@Tag(name = "事故记录", description = "事故记录")
public class AccidentRecordController 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 IAccidentRecordService accidentRecordService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入accidentRecord")
	public R<AccidentRecordVO> detail(AccidentRecord accidentRecord) {
		AccidentRecord detail = accidentRecordService.getOne(Condition.getQueryWrapper(accidentRecord));
		return R.data(AccidentRecordWrapper.build().entityVO(detail));
	}

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

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

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

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

	/**
	 * 导出事故记录
	 */
	@GetMapping("/export-accident-record")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导出事故记录")
	public void exportAccidentRecord(AccidentRecordVO accidentRecord,
									 @RequestParam(required = false) String ids,
									 HttpServletResponse response) {
		List<AccidentRecordExportExcel> list = accidentRecordService.exportAccidentRecord(buildExportQuery(accidentRecord, ids));
		ExcelUtil.export(response, "事故记录" + DateUtil.time(), "事故记录表", list, AccidentRecordExportExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("/export-template")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导出模板")
	public void exportTemplate(HttpServletResponse response) {
		List<AccidentRecordExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "事故记录模板", "事故记录表", list, AccidentRecordExcel.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<AccidentRecord> buildExportQuery(AccidentRecordVO accidentRecord, String ids) {
		LambdaQueryWrapper<AccidentRecord> queryWrapper = Wrappers.<AccidentRecord>lambdaQuery()
			.eq(AccidentRecord::getIsDeleted, 0)
			.orderByDesc(AccidentRecord::getCreateTime);
		if (Func.isNotEmpty(ids)) {
			queryWrapper.in(AccidentRecord::getId, Func.toLongList(ids));
		}
		if (Func.isNotEmpty(accidentRecord.getCreateDept())) {
			queryWrapper.eq(AccidentRecord::getCreateDept, accidentRecord.getCreateDept());
		}
		if (Func.isNotEmpty(accidentRecord.getVehicleType())) {
			queryWrapper.eq(AccidentRecord::getVehicleType, accidentRecord.getVehicleType());
		}
		if (Func.isNotEmpty(accidentRecord.getVehicleNo())) {
			queryWrapper.like(AccidentRecord::getVehicleNo, accidentRecord.getVehicleNo());
		}
		if (Func.isNotEmpty(accidentRecord.getAccidentNature())) {
			queryWrapper.eq(AccidentRecord::getAccidentNature, accidentRecord.getAccidentNature());
		}
		if (Func.isNotEmpty(accidentRecord.getAccidentResponsibility())) {
			queryWrapper.eq(AccidentRecord::getAccidentResponsibility, accidentRecord.getAccidentResponsibility());
		}
		if (Func.isNotEmpty(accidentRecord.getAccidentReasonDamage())) {
			queryWrapper.like(AccidentRecord::getAccidentReasonDamage, accidentRecord.getAccidentReasonDamage());
		}
		if (Func.isNotEmpty(accidentRecord.getAccidentAssessmentDateStart())) {
			queryWrapper.ge(AccidentRecord::getAccidentDate, accidentRecord.getAccidentAssessmentDateStart());
		}
		if (Func.isNotEmpty(accidentRecord.getAccidentAssessmentDateEnd())) {
			queryWrapper.le(AccidentRecord::getAccidentDate, accidentRecord.getAccidentAssessmentDateEnd());
		}
		if (Func.isNotEmpty(accidentRecord.getCreateTimeStart())) {
			queryWrapper.ge(AccidentRecord::getCreateTime, accidentRecord.getCreateTimeStart());
		}
		if (Func.isNotEmpty(accidentRecord.getCreateTimeEnd())) {
			queryWrapper.le(AccidentRecord::getCreateTime, accidentRecord.getCreateTimeEnd());
		}
		return queryWrapper;
	}

}
