package com.leromro.core.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.leromro.common.core.controller.BaseController;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.core.domain.R;
import com.leromro.common.core.page.TableDataInfo;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.domain.VolunteerInfo;
import com.leromro.core.domain.dto.VolunteerInfoDTO;
import com.leromro.core.domain.vo.VolunteerInfoDetailVO;
import com.leromro.core.domain.vo.VolunteerInfoVO;
import com.leromro.core.service.IVolunteerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 志愿者信息
 * @author lr
 * @since 2025-04-07
 */
@Api(tags = "志愿者注册以及查询")
@RestController
@RequestMapping("/core/volunteer/info")
public class VolunteerInfoController extends BaseController {
    @Autowired
    private IVolunteerInfoService volunteerInfoService;
    /**
     * 新增志愿者信息
     * 已测试
     */
    @ApiOperation( value = "新增志愿者信息",notes = "传入路径参数 大分类，传入对象参数志愿者信息")
    @PostMapping("/newVolunteerInfo/{serviceCategory}")
    public AjaxResult newVolunteerInfoByuserId(@RequestBody VolunteerInfoDTO dto, @PathVariable Long serviceCategory){
        volunteerInfoService.newVolunteerInfoByuserId(dto,serviceCategory);
        return AjaxResult.success();
    }
    /**
     * 修改志愿者信息
     * 未测试
     */
//    @ApiOperation(value = "修改志愿者信息" , notes = "传入志愿者修改后的对象")
//    @PutMapping("/updateInfo")
//    public AjaxResult updateVolunteerInfo(@RequestBody VolunteerInfoVO volunteerInfoVO){
//        boolean b = volunteerInfoService.updateById(volunteerInfoVO);
//        return AjaxResult.success();
//    }
    /**
     * 志愿者信息回显
     * 已测试
     */
    @ApiOperation(value = "志愿者信息回显" , notes = "传入大分类，返回大分类中志愿者已有信息")
    @GetMapping("/volunteerInfo")
    public R<VolunteerInfo> selectVolunteerInfo(Long serviceCategory){
        Long userId = SecurityUtils.getUserId();
        VolunteerInfo info = volunteerInfoService.selectByUserID(serviceCategory,userId);
        return R.ok(info);
    }
    /**
     * 查询志愿者信息列表
     * 已测试
     */
    @ApiOperation(value = "(后台)查询志愿者信息列表",notes = "传入进行分类的字段，根据分类字段查询，返回List 集合")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "appStatus", value = "审批传1，管理传2", required = true)
    })
    @GetMapping("/web/list")
    public TableDataInfo<VolunteerInfo> webList(VolunteerInfoDTO dto)
    {
        startPage();
        List<VolunteerInfo> list = volunteerInfoService.webList(dto);
        return getDataTable(list);
    }

    /**
     * 获取志愿者信息列表
     * 已测试
     */
    @ApiOperation(value = "查询志愿者信息列表",notes = "传入进行分类的字段，根据分类字段查询，返回List 集合")
    @GetMapping ("/list")
    public TableDataInfo<VolunteerInfoVO> list( VolunteerInfo volunteerInfo)
    {
        startPage();
        List<VolunteerInfoVO> list = volunteerInfoService.selectVolunteerInfoList(volunteerInfo);
        return getDataTable(list);
    }

    /**
     * 获取志愿者信息详细信息
     * 已测试
     */
    @ApiOperation( value = "获取志愿者信息详细信息",notes = "传入志愿者id,以及分类的id， 返回志愿者的信息")
    @PostMapping(value = "/getDetails")
    public R<VolunteerInfoDetailVO> getInfo(@RequestBody VolunteerInfo volunteerInfo)
    {

        return R.ok(volunteerInfoService.selectLVolunteerInfoByVolunteerId(volunteerInfo));
    }

    /**
     * 志愿者审批
     */
    @ApiOperation(value = "志愿者审批",notes = "传入当前行主键volunteerInfoId，审批状态appStatus:2通过，3拒绝,驳回原因rejectReason,状态为3则必填")
    @PostMapping("/web/approval")
    public AjaxResult approval(@RequestBody VolunteerInfo volunteerInfo){
        if ("3".equals(volunteerInfo.getAppStatus()) && StrUtil.isBlank(volunteerInfo.getRejectReason())){
            return AjaxResult.warn("请填写驳回原因");
        }
        volunteerInfoService.update(new LambdaUpdateWrapper<VolunteerInfo>()
                .eq(VolunteerInfo::getVolunteerInfoId,volunteerInfo.getVolunteerInfoId())
                .set(VolunteerInfo::getAppStatus,volunteerInfo.getAppStatus())
                .set(StrUtil.isNotBlank(volunteerInfo.getRejectReason()),VolunteerInfo::getRejectReason,volunteerInfo.getRejectReason()));
        return AjaxResult.success();
    }

  /*  @ApiOperation("用户已注册志愿者查询")
    @GetMapping("/havenRegister")
    public Void selectUserHavenRegister(){
        Long userId = SecurityUtils.getUserId();
        volunteerInfoService.selectHavenRegisterByUserID(userId);
    }*/
}
