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.InternshipsBo;
import org.dromara.ygpassport.domain.dto.WxInternshipsDto;
import org.dromara.ygpassport.domain.vo.InternshipsVo;
import org.dromara.ygpassport.service.IInternshipsService;
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/wxInternships")
public class WxInternshipsController extends BaseController {

    private final IInternshipsService internshipsService;

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

        List<InternshipsVo> internshipsVoList = internshipsService.queryListByStuNo(stuNo);

        List<WxInternshipsDto> wxInternshipsDtoList = new ArrayList<>();
        for(InternshipsVo internshipsVo : internshipsVoList){
            WxInternshipsDto wxInternshipsDto = new WxInternshipsDto(internshipsVo);
            wxInternshipsDtoList.add(wxInternshipsDto);
        }

        return R.ok(wxInternshipsDtoList);
    }

    /**
     * 新增实习经历
     *
     * @param jsonObject json中包含一个参数：internshipsBo（实习经历bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        InternshipsBo bo = jsonObject.get("internshipsBo", InternshipsBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(internshipsService.insertByBo(bo));
    }

    /**
     * 修改实习经历
     *
     * @param jsonObject json中包含一个参数：internshipsBo（实习经历bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody JSONObject jsonObject){
        InternshipsBo bo = jsonObject.get("internshipsBo", InternshipsBo.class);

        return toAjax(internshipsService.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(internshipsService.deleteWithValidByIds(List.of(ids), true));
    }
}
