package com.gxwebsoft.glt.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.glt.entity.GltUserTicketRelease;
import com.gxwebsoft.glt.param.GltUserTicketReleaseParam;
import com.gxwebsoft.glt.service.GltUserTicketReleaseService;
import com.gxwebsoft.glt.service.impl.GltUserTicketAutoReleaseServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 水票释放控制器
 *
 * @author 科技小王子
 * @since 2026-02-03 18:55:55
 */
@Tag(name = "水票释放管理")
@RestController
@RequestMapping("/api/glt/glt-user-ticket-release")
public class GltUserTicketReleaseController extends BaseController {
    @Resource
    private GltUserTicketReleaseService gltUserTicketReleaseService;

    @Resource
    private GltUserTicketAutoReleaseServiceImpl gltUserTicketAutoReleaseService;

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:list')")
    @Operation(summary = "分页查询水票释放")
    @GetMapping("/page")
    public ApiResult<PageResult<GltUserTicketRelease>> page(GltUserTicketReleaseParam param) {
        // 使用关联查询
        return success(gltUserTicketReleaseService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:list')")
    @Operation(summary = "查询全部水票释放")
    @GetMapping()
    public ApiResult<List<GltUserTicketRelease>> list(GltUserTicketReleaseParam param) {
        // 使用关联查询
        return success(gltUserTicketReleaseService.listRel(param));
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:list')")
    @Operation(summary = "根据id查询水票释放")
    @GetMapping("/{id}")
    public ApiResult<GltUserTicketRelease> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(gltUserTicketReleaseService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:save')")
    @OperationLog
    @Operation(summary = "添加水票释放")
    @PostMapping()
    public ApiResult<?> save(@RequestBody GltUserTicketRelease gltUserTicketRelease) {
        // 记录当前登录用户id
         User loginUser = getLoginUser();
         if (loginUser != null) {
         gltUserTicketRelease.setUserId(loginUser.getUserId());
         }
        if (gltUserTicketReleaseService.save(gltUserTicketRelease)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:update')")
    @OperationLog
    @Operation(summary = "修改水票释放")
    @PutMapping()
    public ApiResult<?> update(@RequestBody GltUserTicketRelease gltUserTicketRelease) {
        if (gltUserTicketReleaseService.updateById(gltUserTicketRelease)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:remove')")
    @OperationLog
    @Operation(summary = "删除水票释放")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (gltUserTicketReleaseService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:save')")
    @OperationLog
    @Operation(summary = "批量添加水票释放")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<GltUserTicketRelease> list) {
        if (gltUserTicketReleaseService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:update')")
    @OperationLog
    @Operation(summary = "批量修改水票释放")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<GltUserTicketRelease> batchParam) {
        if (batchParam.update(gltUserTicketReleaseService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketRelease:remove')")
    @OperationLog
    @Operation(summary = "批量删除水票释放")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (gltUserTicketReleaseService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "水票释放测试")
    @PostMapping("/releaseTask")
    public ApiResult<?> releaseTask() {
        gltUserTicketAutoReleaseService.releaseTask();
        return success(true);
    }

}
