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.ScholarshipBo;
import org.dromara.ygpassport.domain.dto.WxScholarshipDto;
import org.dromara.ygpassport.domain.vo.ScholarshipVo;
import org.dromara.ygpassport.service.IScholarshipService;
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.ArrayList;
import java.util.List;

/**
 * 移动端-个人成长-奖学金情况
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ygpassport/wxScholarship")
public class WxScholarshipController extends BaseController {

    private final IScholarshipService scholarshipService;

    /**
     * 查询奖学金情况列表
     *
     * @param jsonObject json中包含一个参数：stuNo（学号）
     *
     * @return R<List<WxScholarshipDto>>
     */
    @SaIgnore
    @PostMapping("/list")
    public R<List<WxScholarshipDto>> list(@RequestBody JSONObject jsonObject) {
        String stuNo = jsonObject.getStr("stuNo");

        List<ScholarshipVo> scholarshipVoList = scholarshipService.queryListByStuNo(stuNo);

        List<WxScholarshipDto> wxScholarshipDtoList = new ArrayList<>();

        for (ScholarshipVo scholarshipVo : scholarshipVoList) {
            WxScholarshipDto wxScholarshipDto = new WxScholarshipDto(scholarshipVo);
            wxScholarshipDtoList.add(wxScholarshipDto);
        }

        return R.ok(wxScholarshipDtoList);
    }

    /**
     * 新增获奖经历
     *
     * @param jsonObject json中包含一个参数：scholarshipBo（获奖经历bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        ScholarshipBo bo = jsonObject.get("scholarshipBo", ScholarshipBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(scholarshipService.insertByBo(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(scholarshipService.deleteWithValidByIds(List.of(ids), true));
    }

}
