package com.gxwebsoft.glt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.glt.service.GltTicketTemplateService;
import com.gxwebsoft.glt.entity.GltTicketTemplate;
import com.gxwebsoft.glt.param.GltTicketTemplateParam;
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 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-ticket-template")
public class GltTicketTemplateController extends BaseController {
    @Resource
    private GltTicketTemplateService gltTicketTemplateService;

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

    @PreAuthorize("hasAuthority('glt:gltTicketTemplate:list')")
    @Operation(summary = "查询全部水票")
    @GetMapping()
    public ApiResult<List<GltTicketTemplate>> list(GltTicketTemplateParam param) {
        // 使用关联查询
        return success(gltTicketTemplateService.listRel(param));
    }

    @Operation(summary = "根据id查询水票")
    @GetMapping("/{id}")
    public ApiResult<GltTicketTemplate> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(gltTicketTemplateService.getByIdRel(id));
    }

    @Operation(summary = "根据商品ID查询水票")
    @GetMapping("/getByGoodsId/{goodsId}")
    public ApiResult<GltTicketTemplate> getByGoodsId(@PathVariable("goodsId") Integer goodsId) {
        GltTicketTemplate template = gltTicketTemplateService.getOne(
            new LambdaQueryWrapper<GltTicketTemplate>()
                .eq(GltTicketTemplate::getGoodsId, goodsId)
                .eq(GltTicketTemplate::getDeleted, 0)
                .orderByAsc(GltTicketTemplate::getSortNumber)
                .orderByDesc(GltTicketTemplate::getCreateTime)
                .last("limit 1")
        );
        return success(template);
    }



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

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

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

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

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

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

}
