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.ForeignLanguageSkill;
import org.dromara.ygpassport.domain.bo.ForeignLanguageSkillBo;
import org.dromara.ygpassport.domain.dto.WxForeignLanguageSkillDto;
import org.dromara.ygpassport.service.IForeignLanguageSkillService;
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/wxForeignLanguageSkill")
public class WxForeignLanguageSkillController extends BaseController {

    private final IForeignLanguageSkillService foreignLanguageSkillService;

    /**
     * 查询外语能力水平列表
     *
     * @param jsonObject json中包含一个参数：stuNo（学号）
     *
     * @return R<WxForeignLanguageSkillDto>
     */
    @SaIgnore
    @PostMapping("/list")
    public R<List<WxForeignLanguageSkillDto>> list(@RequestBody JSONObject jsonObject){
        String stuNo = jsonObject.getStr("stuNo");

        List<ForeignLanguageSkill> foreignLanguageSkills = foreignLanguageSkillService.queryListByStuNo(stuNo);

        List<WxForeignLanguageSkillDto> wxForeignLanguageSkillDtoList = new ArrayList<>();
        for(ForeignLanguageSkill foreignLanguageSkill : foreignLanguageSkills){
            WxForeignLanguageSkillDto wxForeignLanguageSkillDto = new WxForeignLanguageSkillDto(foreignLanguageSkill);
            wxForeignLanguageSkillDtoList.add(wxForeignLanguageSkillDto);
        }

        return R.ok(wxForeignLanguageSkillDtoList);
    }

    /**
     * 新增外语能力水平
     *
     * @param jsonObject json中包含一个参数：foreignLanguageSkillBo（外语能力水平bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        ForeignLanguageSkillBo bo = jsonObject.get("foreignLanguageSkillBo", ForeignLanguageSkillBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(foreignLanguageSkillService.insertByBo(bo));
    }

    /**
     * 修改外语能力水平
     *
     * @param jsonObject json中包含一个参数：foreignLanguageSkillBo（外语能力水平bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody JSONObject jsonObject){
        ForeignLanguageSkillBo bo = jsonObject.get("foreignLanguageSkillBo", ForeignLanguageSkillBo.class);

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