package org.dromara.ygpassport.controller;

import java.util.Arrays;
import java.util.List;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONObject;
import com.github.yulichang.toolkit.MPJWrappers;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysOss;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysFileUploadService;
import org.dromara.ygpassport.domain.StudentAlbum;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.ygpassport.domain.vo.StudentAlbumVo;
import org.dromara.ygpassport.domain.bo.StudentAlbumBo;
import org.dromara.ygpassport.service.IStudentAlbumService;

/**
 * 学生相册
 *
 * @author Lion Li
 * @date 2024-09-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/studentAlbum")
public class StudentAlbumController extends BaseController {

    private final IStudentAlbumService studentAlbumService;

    private final ISysFileUploadService fileUploadService;

    /**
     * 根据学号和类型查询学生相册列表（类型可为空）
     */
    @SaIgnore
    @PostMapping("/list")
    public R<List<StudentAlbumVo>> list(@RequestBody JSONObject jsonObject) {
        String stuNo = jsonObject.getStr("stuNo");
        Long type = jsonObject.getLong("type");
        // 查询相册列表
        List<StudentAlbumVo> studentAlbumVoList = studentAlbumService.selectJoinList(StudentAlbumVo.class,
            MPJWrappers.<StudentAlbum>lambdaJoin()
                .selectAll(StudentAlbum.class)
                .eq(StringUtils.isNotBlank(stuNo), StudentAlbum::getStuNo, stuNo)
                .eq(type != null, StudentAlbum::getType, type));
        // 将相片信息封装
        for (StudentAlbumVo studentAlbumVo : studentAlbumVoList){
            List<SysOssVo> list = fileUploadService.listByIds(Arrays.asList(studentAlbumVo.getPhotoId()));
            studentAlbumVo.setOss(list.get(0));
        }

        return R.ok(studentAlbumVoList);
    }

    /**
     * 查询学生相册列表
     */
    @SaCheckPermission("ygpassport:studentAlbum:list")
    @GetMapping("/allList")
    public TableDataInfo<StudentAlbumVo> allList(StudentAlbumBo bo, PageQuery pageQuery) {
        return studentAlbumService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取学生相册详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<StudentAlbumVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(studentAlbumService.queryById(id));
    }

    /**
     * 新增学生相册-类型(0个人相册，1志愿服务）
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StudentAlbumBo bo) {
        return toAjax(studentAlbumService.insertByBo(bo));
    }

    /**
     * 修改学生相册
     */
    @SaIgnore
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentAlbumBo bo) {
        return toAjax(studentAlbumService.updateByBo(bo));
    }

}
