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.CombatProjectBo;
import org.dromara.ygpassport.domain.dto.WxCombatProjectDto;
import org.dromara.ygpassport.domain.vo.CombatProjectVo;
import org.dromara.ygpassport.service.ICombatProjectService;
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/wxCombatProject")
public class  WxCombatProjectController extends BaseController {

    private final ICombatProjectService combatProjectService;

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

        List<CombatProjectVo> combatProjectBoList = combatProjectService.queryListByStuNo(stuNo);

        List<WxCombatProjectDto> wxCombatProjectDtoList = new ArrayList<>();
        for(CombatProjectVo combatProjectVo : combatProjectBoList){
            WxCombatProjectDto wxCombatProjectDto = new WxCombatProjectDto(combatProjectVo);
            wxCombatProjectDtoList.add(wxCombatProjectDto);
        }

        return R.ok(wxCombatProjectDtoList);
    }

    /**
     * 新增实战项目
     *
     * @param jsonObject json中包含一个参数：combatProjectBo（实战项目bo）
     *
     * @return R<Void>
     */
    @SaIgnore
    @PostMapping("/add")
    public R<Void> add(@RequestBody JSONObject jsonObject){
        CombatProjectBo bo = jsonObject.get("combatProjectBo", CombatProjectBo.class);
        bo.setCreateTime(DateUtils.getNowDate());
        return toAjax(combatProjectService.insertByBo(bo));
    }

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

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