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.ScoreplanBo;
import org.dromara.ygpassport.domain.dto.WxScoreplanDto;
import org.dromara.ygpassport.domain.vo.ScoreplanVo;
import org.dromara.ygpassport.service.IScoreplanService;
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/wxScoreplan")
public class WxScoreplanController extends BaseController {

    private final IScoreplanService scoreplanService;

    /**
     * 查询个人目标与成长计划列表
     *
     * @param jsonObject json中包含一个参数：stuNo（学号）
     *
     * @return R<List<WxScoreplanDto>>
     */
    @SaIgnore
    @PostMapping("/list")
    public R<List<WxScoreplanDto>> list(@RequestBody JSONObject jsonObject) {
        String stuNo = jsonObject.getStr("stuNo");

        List<ScoreplanVo> scoreplanVoList = scoreplanService.queryListByStuNo(stuNo);

        List<WxScoreplanDto> wxScoreplanDtoList = new ArrayList<>();

        for (ScoreplanVo scoreplanVo : scoreplanVoList) {
            WxScoreplanDto wxScoreplanDto = new WxScoreplanDto(scoreplanVo);
            wxScoreplanDtoList.add(wxScoreplanDto);
        }

        return R.ok(wxScoreplanDtoList);
    }


    /**
     * 新增个人目标与成长计划
     *
     * @param jsonObject json中包含一个参数：scoreplanBo（个人目标与成长计划bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        ScoreplanBo bo = jsonObject.get("scoreplanBo", ScoreplanBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(scoreplanService.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(scoreplanService.deleteWithValidByIds(List.of(ids), true));
    }
}
