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.CareerSkillBo;
import org.dromara.ygpassport.domain.dto.WxCareerSkillDto;
import org.dromara.ygpassport.domain.vo.CareerSkillVo;
import org.dromara.ygpassport.service.ICareerSkillService;
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/wxCareerSkill")
public class WxCareerSkillController extends BaseController {

    private final ICareerSkillService careerSkillService;

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

        List<CareerSkillVo> careerSkillBoList = careerSkillService.queryListByStuNo(stuNo);

        List<WxCareerSkillDto> wxCareerSkillDtoList = new ArrayList<>();
        for(CareerSkillVo careerSkillVo : careerSkillBoList){
            WxCareerSkillDto wxCareerSkillDto = new WxCareerSkillDto(careerSkillVo);
            wxCareerSkillDtoList.add(wxCareerSkillDto);
        }

        return R.ok(wxCareerSkillDtoList);
    }

    /**
     * 添加职业技能
     *
     * @param jsonObject json中包含一个参数：careerSkillBo（职业技能bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        CareerSkillBo bo = jsonObject.get("careerSkillBo", CareerSkillBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(careerSkillService.insertByBo(bo));
    }

    /**
     * 修改职业技能
     *
     * @param jsonObject json中包含一个参数：careerSkillBo（职业技能bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody JSONObject jsonObject){
        CareerSkillBo bo = jsonObject.get("wxCareerSkillDto", CareerSkillBo.class);

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