package com.gxwebsoft.hjm.controller;

import cn.hutool.core.util.ObjUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.hjm.entity.HjmCar;
import com.gxwebsoft.hjm.service.HjmCarService;
import com.gxwebsoft.hjm.service.HjmViolationService;
import com.gxwebsoft.hjm.entity.HjmViolation;
import com.gxwebsoft.hjm.param.HjmViolationParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 黄家明_违章记录控制器
 *
 * @author 科技小王子
 * @since 2025-06-20 13:48:43
 */
@Tag(name = "黄家明_违章记录管理")
@RestController
@RequestMapping("/api/hjm/hjm-violation")
public class HjmViolationController extends BaseController {
  @Resource
  private HjmViolationService hjmViolationService;
  @Resource
  private HjmCarService hjmCarService;

  @PreAuthorize("hasAuthority('hjm:hjmViolation:list')")
  @Operation(summary = "分页查询黄家明_违章记录")
  @GetMapping("/page")
  public ApiResult<PageResult<HjmViolation>> page(HjmViolationParam param) {
    // 使用关联查询
    return success(hjmViolationService.pageRel(param));
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:list')")
  @Operation(summary = "查询全部黄家明_违章记录")
  @GetMapping()
  public ApiResult<List<HjmViolation>> list(HjmViolationParam param) {
    // 使用关联查询
    return success(hjmViolationService.listRel(param));
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:list')")
  @Operation(summary = "根据id查询黄家明_违章记录")
  @GetMapping("/{id}")
  public ApiResult<HjmViolation> get(@PathVariable("id") Integer id) {
    // 使用关联查询
    return success(hjmViolationService.getByIdRel(id));
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:save')")
  @OperationLog
  @Operation(summary = "添加黄家明_违章记录")
  @PostMapping()
  public ApiResult<?> save(@RequestBody HjmViolation hjmViolation) {
    // 记录当前登录用户id
    User loginUser = getLoginUser();
    if (loginUser != null) {
      hjmViolation.setUserId(loginUser.getUserId());
    }
    final HjmCar car = hjmCarService.getByCode(hjmViolation.getCode());
    if (ObjUtil.isEmpty(car)) {
      return fail("车辆编号不存在");
    }
    if (hjmViolationService.save(hjmViolation)) {
      hjmViolationService.send(hjmViolation);
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:update')")
  @OperationLog
  @Operation(summary = "修改黄家明_违章记录")
  @PutMapping()
  public ApiResult<?> update(@RequestBody HjmViolation hjmViolation) {
    if (hjmViolationService.updateById(hjmViolation)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:remove')")
  @OperationLog
  @Operation(summary = "删除黄家明_违章记录")
  @DeleteMapping("/{id}")
  public ApiResult<?> remove(@PathVariable("id") Integer id) {
    if (hjmViolationService.removeById(id)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:save')")
  @OperationLog
  @Operation(summary = "批量添加黄家明_违章记录")
  @PostMapping("/batch")
  public ApiResult<?> saveBatch(@RequestBody List<HjmViolation> list) {
    if (hjmViolationService.saveBatch(list)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:update')")
  @OperationLog
  @Operation(summary = "批量修改黄家明_违章记录")
  @PutMapping("/batch")
  public ApiResult<?> removeBatch(@RequestBody BatchParam<HjmViolation> batchParam) {
    if (batchParam.update(hjmViolationService, "id")) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('hjm:hjmViolation:remove')")
  @OperationLog
  @Operation(summary = "批量删除黄家明_违章记录")
  @DeleteMapping("/batch")
  public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
    if (hjmViolationService.removeByIds(ids)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }

}
