package com.gxwebsoft.bszx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.service.CmsArticleService;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.bszx.service.BszxBmService;
import com.gxwebsoft.bszx.entity.BszxBm;
import com.gxwebsoft.bszx.param.BszxBmParam;
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 com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 百色中学-报名记录控制器
 *
 * @author 科技小王子
 * @since 2025-03-06 22:50:25
 */
@Tag(name = "百色中学-报名记录管理")
@RestController
@RequestMapping("/api/bszx/bszx-bm")
public class BszxBmController extends BaseController {
    @Resource
    private BszxBmService bszxBmService;
    @Resource
    @Lazy
    private CmsArticleService cmsArticleService;

    @PreAuthorize("hasAuthority('bszx:bszxBm:list')")
    @Operation(summary = "分页查询百色中学-报名记录")
    @GetMapping("/page")
    public ApiResult<PageResult<BszxBm>> page(BszxBmParam param) {
        // 使用关联查询
        return success(bszxBmService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:list')")
    @Operation(summary = "查询全部百色中学-报名记录")
    @GetMapping()
    public ApiResult<List<BszxBm>> list(BszxBmParam param) {
        // 使用关联查询
        return success(bszxBmService.listRel(param));
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:list')")
    @Operation(summary = "根据id查询百色中学-报名记录")
    @GetMapping("/{id}")
    public ApiResult<BszxBm> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(bszxBmService.getByIdRel(id));
    }

    @OperationLog
    @Operation(summary = "申请报名生成邀请函")
    @PostMapping()
    public ApiResult<?> save(@RequestBody BszxBm bszxBm) {
      // 记录当前登录用户id
      User loginUser = getLoginUser();
      if (bszxBm.getName() == null) {
        return fail("请填写姓名");
      }
      if (loginUser != null) {
        bszxBm.setUserId(loginUser.getUserId());
        if (bszxBmService.count(new LambdaQueryWrapper<BszxBm>().eq(BszxBm::getUserId,loginUser.getUserId())) > 0) {
          return fail("您已经报名过了",null);
        }
        if (bszxBmService.save(bszxBm)) {
          cmsArticleService.saveInc(bszxBm.getFormId());
          return success("报名成功");
        }
      }
      return fail("添加失败");
    }

    @OperationLog
    @Operation(summary = "修改报名信息")
    @PutMapping()
    public ApiResult<?> update(@RequestBody BszxBm bszxBm) {
        final User loginUser = getLoginUser();
        if(loginUser == null){
          return fail("请先登录");
        }
        if (bszxBmService.updateById(bszxBm)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:remove')")
    @OperationLog
    @Operation(summary = "删除报名记录")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (bszxBmService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:save')")
    @OperationLog
    @Operation(summary = "批量添加百色中学-报名记录")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<BszxBm> list) {
        if (bszxBmService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:update')")
    @OperationLog
    @Operation(summary = "批量修改百色中学-报名记录")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<BszxBm> batchParam) {
        if (batchParam.update(bszxBmService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('bszx:bszxBm:remove')")
    @OperationLog
    @Operation(summary = "批量删除百色中学-报名记录")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (bszxBmService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

  @Operation(summary = "查询我的报名记录")
  @GetMapping("/myPage")
  public ApiResult<PageResult<BszxBm>> myPage(BszxBmParam param) {
    // 使用关联查询
    if (getLoginUser() != null) {
      param.setUserId(getLoginUserId());
      return success(bszxBmService.pageRel(param));
    }
    return fail("请先登录",null);
  }

  @Operation(summary = "获取海报地址")
  @GetMapping("/generatePoster")
  public ApiResult<?> generatePoster() throws Exception {
      if (getLoginUser() == null) {
        return fail("请先登录",null);
      }
    final BszxBm bm = bszxBmService.getOne(new LambdaQueryWrapper<BszxBm>().eq(BszxBm::getUserId, getLoginUser().getUserId()).last("limit 1"));
    return success("生成宣传海报",bszxBmService.generatePoster(bm));
  }

}
