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.GoalBo;
import org.dromara.ygpassport.domain.dto.WxGoalDto;
import org.dromara.ygpassport.domain.vo.GoalVo;
import org.dromara.ygpassport.service.IGoalService;
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;

/**
 * 移动端-b目标记录
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ygpassport/WxGoal")
public class WxGoalController extends BaseController {

    private final IGoalService goalService;

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

        List<GoalVo> goalVoList = goalService.queryByBugByStuNo(stuNo);

        List<WxGoalDto> wxGoalDtoList = new ArrayList<>();

        for (GoalVo goalVo : goalVoList) {
            WxGoalDto wxGoalDto = new WxGoalDto(goalVo);
            wxGoalDtoList.add(wxGoalDto);
        }

        return R.ok(wxGoalDtoList);
    }

    /**
     * 添加目标记录
     *
     * @param jsonObject json中包含一个参数：goalBo（目标记录bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        GoalBo bo = jsonObject.get("goalBo", GoalBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(goalService.insertByBo(bo));
    }

    /**
     * 修改目标记录
     *
     * @param jsonObject json中包含一个参数：goalBo（问题记录bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody JSONObject jsonObject){
        GoalBo bo = jsonObject.get("goalBo", GoalBo.class);

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