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.ArchivesGoBo;
import org.dromara.ygpassport.domain.dto.WxArchivesGoDto;
import org.dromara.ygpassport.domain.vo.ArchivesGoVo;
import org.dromara.ygpassport.service.IArchivesGoService;
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/wxArchivesGo")
public class WxArchivesGoController extends BaseController {

    private final IArchivesGoService archivesGoService;

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

        List<ArchivesGoVo> archivesGoVoList = archivesGoService.queryListByStuNo(stuNo);

        List<WxArchivesGoDto> wxArchivesGoDtoList = new ArrayList<>();

        for (ArchivesGoVo archivesGoVo : archivesGoVoList) {
            WxArchivesGoDto wxArchivesGoDto = new WxArchivesGoDto(archivesGoVo);
            wxArchivesGoDtoList.add(wxArchivesGoDto);
        }

        return R.ok(wxArchivesGoDtoList);
    }

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