package com.yida.data.school.program.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.program.EduSmallProgram;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.program.SmallProgramSaveDTO;
import com.yida.data.school.dto.program.SmallProgramSelectPageDTO;
import com.yida.data.school.program.service.EduSmallProgramService;
import com.yida.data.school.vo.program.ListSmallProgramDeptVO;
import com.yida.data.school.vo.program.ProgramConfigDTO;
import com.yida.data.school.vo.program.SmallProgramInfoVO;
import com.yida.data.school.vo.program.SmallProgramSelectPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 小程序表 Controller
 *
 * @author ZYJ
 * @date 2021-12-10 13:44:31
 */
@Slf4j
@RestController
@RequestMapping("eduSmallProgram")
@Api(tags = "小程序后台api")
@RequiredArgsConstructor
public class EduSmallProgramController {

    private final EduSmallProgramService eduSmallProgramService;

    /**
     * 查询小程序列表数据
     *
     * @param smallProgramSelectPageDTO 小程序后台列表请求类
     * @return com.yida.data.common.core.common.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < com.yida.data.school.vo.program.SmallProgramSelectPageVO>>
     * @author ZYJ
     * @date 2021/12/10 14:03
     */
    @PostMapping(value = "listSmallProgramPage")
    @ApiOperation(value = "查询小程序列表数据")
    public ResultBean<IPage<EduSmallProgram>> listSmallProgramPage(@Valid SmallProgramSelectPageDTO smallProgramSelectPageDTO) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        IPage<EduSmallProgram> page = this.eduSmallProgramService.listSmallProgramPage(smallProgramSelectPageDTO,currentUser);
        return ResultBean.buildSuccess(page);
    }

    /**
     * 根据小程序id查询小程序详情
     *
     * @param smallProgramId 小程序id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.school.vo.program.SmallProgramInfoVO>
     * @author ZYJ
     * @date 2021/12/10 14:32
     */
    @GetMapping(value = "getSmallProgramInfo")
    @ApiOperation(value = "根据小程序id查询小程序详情")
    public ResultBean<SmallProgramInfoVO> getSmallProgramInfo(
            @ApiParam(value = "小程序id", required = true) @RequestParam Long smallProgramId) {
        SmallProgramInfoVO smallProgramInfoVO = this.eduSmallProgramService.getSmallProgramInfo(smallProgramId);
        return ResultBean.buildSuccess(smallProgramInfoVO);
    }

    /**
     * 保存小程序数据
     *
     * @param smallProgramSaveDTO 保存小程序请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/12/10 15:18
     */
    @PostMapping(value = "saveSmallProgram", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存小程序数据", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<String> saveSmallProgram(@Valid @RequestBody SmallProgramSaveDTO smallProgramSaveDTO) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        this.eduSmallProgramService.saveSmallProgram(smallProgramSaveDTO,currentUser);
        return ResultBean.buildSuccess();
    }

    /**
     * 批量删除小程序数据
     *
     * @param smallProgramIds 小程序id集合
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/12/10 17:49
     */
    @PostMapping(value = "deleteSmallProgramBatch", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量删除小程序数据", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<String> deleteSmallProgramBatch(@RequestBody List<Long> smallProgramIds) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        this.eduSmallProgramService.deleteSmallProgramBatch(smallProgramIds,currentUser);
        return ResultBean.buildSuccess();
    }

    @GetMapping(value = "listSmallProgramDept")
    @ApiOperation(value = "查询小程序所有展示栏目")
    public ResultBean<List<ListSmallProgramDeptVO>> listSmallProgramDept() {

        return ResultBean.buildSuccess(eduSmallProgramService.listSmallProgramDept());
    }

    @PostMapping(value = "setProgramConfig")
    @ApiOperation(value = "查询小程序所有展示栏目")
    public ResultBean setProgramConfig(@RequestBody ProgramConfigDTO config) {
        eduSmallProgramService.setProgramConfig(config,FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }
}
