package jnpf.cultivate.v2.controller.web.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.CultivateCoverCategoryService;
import jnpf.cultivate.service.CultivateCoverInfoService;
import jnpf.cultivate.service.CultivateExamSettingService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.po.exam.FtbCultivateExamSettingEntity;
import jnpf.model.cultivate.v2.common.vo.V2BaseIdNameVo;
import jnpf.model.cultivate.v2.exam.req.V2ExamSettingReq;
import jnpf.model.cultivate.v2.exam.req.V2QueryCoverReq;
import jnpf.model.cultivate.v2.exam.req.V2SaveCoverCategoryReq;
import jnpf.model.cultivate.v2.exam.req.V2SaveCoverReq;
import jnpf.model.cultivate.v2.exam.vo.V2CoverVo;
import jnpf.model.cultivate.v2.exam.vo.V2TreeCoverVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * web通用模块
 *
 * @author shangyi
 */
@RestController
@RequestMapping("/v2/web/common")
public class V2CommonController {

    @Resource
    private CultivateCoverCategoryService coverCategoryService;

    @Resource
    private CultivateCoverInfoService coverInfoService;

    @Resource
    private CultivateExamSettingService examSettingService;


    /**
     * 封面分类列表
     * 封面分类类型： 0-自定义，1-系统内置  不传入-就查询所有
     * @return
     */
    @PostMapping("/cover/category/list")
    public ActionResult<List<V2BaseIdNameVo>> categoryList(@RequestParam(value = "isSystem", required = false) Integer isSystem) {
        return ActionResult.success(coverCategoryService.getCategoryList(isSystem));
    }

    /**
     * 添加/编辑封面分类
     *
     * @param req
     * @return
     */
    @PostMapping("/cover/category/save")
    public ActionResult<String> add(@RequestBody @Valid V2SaveCoverCategoryReq req) {
        return ActionResult.success(coverCategoryService.saveCategory(req));
    }

    /**
     * 删除封面分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/cover/category/del/{id}")
    public ActionResult<Void> coverCategoryDel(@PathVariable("id") String id) {
        coverCategoryService.delCategory(id);
        return ActionResult.success();
    }

    /**
     * 添加封面
     *
     * @param req
     * @return
     */
    @PostMapping("/cover/save")
    public ActionResult<Void> add(@RequestBody @Valid V2SaveCoverReq req) {
        coverInfoService.saveCover(req);
        return ActionResult.success();
    }

    /**
     * 删除封面
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/cover/del")
    public ActionResult<Void> coverDel(@RequestBody List<String> ids) {
        coverInfoService.removeByIds(ids);
        return ActionResult.success();
    }

    /**
     * 获取封面列表
     *
     * @param req
     * @return
     */
    @PostMapping("/cover/page")
    public ActionResult<PageListVO<V2CoverVo>> coverList(@RequestBody @Valid V2QueryCoverReq req) {
        return ActionResult.success(CultivatePage.coverPageList(coverInfoService.getPageList(req)));
    }


    /**
     * 获取所有封面,两级结构
     *
     * @return
     */
    @GetMapping("/cover/treeCover")
    public ActionResult<List<V2TreeCoverVo>> coverTypeAll() {
        return ActionResult.success(coverCategoryService.treeCover());
    }

    /**
     * 增加/修改考试配置
     *
     * @param req
     * @return
     */
    @PostMapping("/examSetting/save")
    public ActionResult<Void> saveExamSetting(@RequestBody @Valid V2ExamSettingReq req) {
        examSettingService.saveExamSetting(req);
        return ActionResult.success();
    }

    /**
     * 查询全局考试配置
     *
     * @return 全局考试配置
     */
    @GetMapping("/examSetting/queryGlobalExamSetting")
    public ActionResult<FtbCultivateExamSettingEntity> queryGlobalExamSetting() {
        return ActionResult.success(examSettingService.getById("1"));
    }


}
