package org.dromara.ygpassport.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.ygpassport.domain.bo.VolunteeringBo;
import org.dromara.ygpassport.domain.dto.WxVolunteeringDto;
import org.dromara.ygpassport.domain.vo.VolunteeringVo;
import org.dromara.ygpassport.service.IVolunteeringService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 移动端-第二课堂-志愿服务
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ygpassport/wxVolunteering")
public class WxVolunteeringController extends BaseController {

    private final IVolunteeringService volunteeringService;

    /**
     * 查询志愿服务
     *
     * @param jsonObject json中包含一个参数：stuNo（学号）
     *
     * @return R<WxVolunteeringDto>
     */
    @SaIgnore
    @PostMapping("/getInfo")
    public R<WxVolunteeringDto> getInfo(@RequestBody JSONObject jsonObject){
        String stuNo = jsonObject.getStr("stuNo"); // 获取学号

        List<VolunteeringVo> volunteeringVoList = volunteeringService.queryListByStuNo(stuNo);

        WxVolunteeringDto wxVolunteeringDto = new WxVolunteeringDto();
        wxVolunteeringDto.setVolunteeringVoList(volunteeringVoList);

        return R.ok(wxVolunteeringDto);
    }

    /**
     * 新增志愿服务
     *
     * @param jsonObject json中包含一个参数：volunteeringBo（志愿服务bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        VolunteeringBo bo = jsonObject.get("volunteeringBo", VolunteeringBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(volunteeringService.insertByBo(bo));
    }

    /**
     * 修改志愿服务
     *
     * @param jsonObject json中包含一个参数：volunteeringBo（志愿服务bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody JSONObject jsonObject){
        VolunteeringBo bo = jsonObject.get("volunteeringBo", VolunteeringBo.class);
        return toAjax(volunteeringService.updateByBo(bo));
    }

    /**
     * 删除志愿服务
     *
     * @param jsonObject json中包含一个参数：ids（需要删除的id数组）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/remove")
    public R<Void> remove(@RequestBody JSONObject jsonObject){
        Long[] ids = jsonObject.get("ids", Long[].class);
        return toAjax(volunteeringService.deleteWithValidByIds(List.of(ids), true));
    }
}
