package org.springblade.modules.nh.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.nh.pojo.entity.AlarmCodeEntity;
import org.springblade.modules.nh.pojo.vo.AlarmCodeVO;
import org.springblade.modules.nh.excel.AlarmCodeExcel;
import org.springblade.modules.nh.wrapper.AlarmCodeWrapper;
import org.springblade.modules.nh.service.IAlarmCodeService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 故障代码 控制器
 *
 * @author AlexWang
 * @since 2024-06-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("nh/alarmCode")
@Tag(name = "故障代码", description = "故障代码接口")
public class AlarmCodeController extends BladeController {

	private final IAlarmCodeService alarmCodeService;

	/**
	 * 故障代码 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入alarmCode")
	public R<AlarmCodeVO> detail(AlarmCodeEntity alarmCode) {
		AlarmCodeEntity detail = alarmCodeService.getOne(Condition.getQueryWrapper(alarmCode));
		return R.data(AlarmCodeWrapper.build().entityVO(detail));
	}
	/**
	 * 故障代码 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入alarmCode")
	public R<IPage<AlarmCodeVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> alarmCode, Query query) {
		IPage<AlarmCodeEntity> pages = alarmCodeService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmCode, AlarmCodeEntity.class));
		return R.data(AlarmCodeWrapper.build().pageVO(pages));
	}

	/**
	 * 故障代码 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入alarmCode")
	public R<IPage<AlarmCodeVO>> page(AlarmCodeVO alarmCode, Query query) {
		IPage<AlarmCodeVO> pages = alarmCodeService.selectAlarmCodePage(Condition.getPage(query), alarmCode);
		return R.data(pages);
	}

	/**
	 * 故障代码 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入alarmCode")
	public R save(@Valid @RequestBody AlarmCodeEntity alarmCode) {
		return R.status(alarmCodeService.save(alarmCode));
	}

	/**
	 * 故障代码 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入alarmCode")
	public R update(@Valid @RequestBody AlarmCodeEntity alarmCode) {
		return R.status(alarmCodeService.updateById(alarmCode));
	}

	/**
	 * 故障代码 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入alarmCode")
	public R submit(@Valid @RequestBody AlarmCodeEntity alarmCode) {
		return R.status(alarmCodeService.saveOrUpdate(alarmCode));
	}

	/**
	 * 故障代码 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除", description  = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return R.status(alarmCodeService.removeByIds(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-alarmCode")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入alarmCode")
	public void exportAlarmCode(@Parameter(hidden = true) @RequestParam Map<String, Object> alarmCode, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<AlarmCodeEntity> queryWrapper = Condition.getQueryWrapper(alarmCode, AlarmCodeEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(AlarmCode::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(AlarmCodeEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<AlarmCodeExcel> list = alarmCodeService.exportAlarmCode(queryWrapper);
		ExcelUtil.export(response, "故障代码数据" + DateUtil.time(), "故障代码数据表", list, AlarmCodeExcel.class);
	}

}
