package com.gxwebsoft.glt.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.glt.service.GltUserTicketService;
import com.gxwebsoft.glt.entity.GltUserTicket;
import com.gxwebsoft.glt.param.GltUserTicketParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    @Operation(summary = "分页查询我的水票")
    @GetMapping("/page")
    public ApiResult<PageResult<GltUserTicket>> page(GltUserTicketParam param) {
        // 使用关联查询
        return success(gltUserTicketService.pageRel(param));
    }

    @Operation(summary = "可用水票总数")
    @GetMapping("/my-total")
    public ApiResult<?> myTotal() {
        Integer userId = getLoginUserId();
        if (userId == null) {
            return fail("未登录");
        }
        Integer tenantId = getTenantId();
        if (tenantId == null) {
            return fail("租户信息缺失");
        }
        Integer availableQty = gltUserTicketService.sumAvailableQtyByUserId(userId, tenantId);
        Map<String, Object> data = new HashMap<>();
        data.put("userId", userId);
        // 兼容旧字段：totalQty 表示“可用水票总数”
        data.put("totalQty", availableQty);
        data.put("availableQty", availableQty);
        return success(data);
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicket:list')")
    @Operation(summary = "查询全部我的水票")
    @GetMapping()
    public ApiResult<List<GltUserTicket>> list(GltUserTicketParam param) {
        // 使用关联查询
        return success(gltUserTicketService.listRel(param));
    }

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

    @PreAuthorize("hasAuthority('glt:gltUserTicket:save')")
    @OperationLog
    @Operation(summary = "添加我的水票")
    @PostMapping()
    public ApiResult<?> save(@RequestBody GltUserTicket gltUserTicket) {
        if (gltUserTicketService.save(gltUserTicket)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicket:update')")
    @OperationLog
    @Operation(summary = "修改我的水票")
    @PutMapping()
    public ApiResult<?> update(@RequestBody GltUserTicket gltUserTicket) {
        if (gltUserTicketService.updateById(gltUserTicket)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
