package com.xjrsoft.module.textbook.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.textbook.dto.*;
import com.xjrsoft.module.textbook.entity.TextbookStudentClaim;
import com.xjrsoft.module.textbook.service.ITextbookStudentClaimService;
import com.xjrsoft.module.textbook.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 学生教材认领记录
* @Author szs
* @Date: 2023-12-26
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/textbookStudentClaim")
@Api(value = "/textbook"  + "/textbookStudentClaim",tags = "学生教材认领记录代码")
@AllArgsConstructor
public class TextbookStudentClaimController {


    private final ITextbookStudentClaimService textbookStudentClaimService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生教材认领记录列表(分页)")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<PageOutput<TextbookStudentClaimPageVo>> page(@Valid TextbookStudentClaimPageDto dto){
        LambdaQueryWrapper<TextbookStudentClaim> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(TextbookStudentClaim::getId)
                .select(TextbookStudentClaim.class,x -> VoToColumnUtil.fieldsToColumns(TextbookStudentClaimPageVo.class).contains(x.getProperty()));
        IPage<TextbookStudentClaim> page = textbookStudentClaimService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<TextbookStudentClaimPageVo> pageOutput = ConventPage.getPageOutput(page, TextbookStudentClaimPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/student-confirm-list")
    @ApiOperation(value="学生教材领取确定页面")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<TextbookClaimStudentConfirmVo> studentConfirmList(@Valid TextbookClaimStudentConfirmDto dto){
        TextbookClaimStudentConfirmVo textbookClaimStudentConfirmVo = textbookStudentClaimService.getStudentConfirmList(dto);
        return RT.ok(textbookClaimStudentConfirmVo);
    }

    @GetMapping(value = "/student-semester-list")
    @ApiOperation(value="学生教材领取学期页面")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<List<TextbookStudentSemesterVo>> studentSemesterList(){
        List<TextbookStudentSemesterVo> textbookStudentSemesterVoList = textbookStudentClaimService.getStudentSemesterList();
        return RT.ok(textbookStudentSemesterVoList);
    }

    @GetMapping(value = "/teacher-check-byClass-list")
    @ApiOperation(value="教师教材领取按班级查看页面")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<PageOutput<TeacherCheckByclassVo>> teacherCheckByClassList(@Valid TeacherCheckByclassDto dto){
        IPage<TeacherCheckByclassVo> page = textbookStudentClaimService.getTeacherCheckByclassList(dto);
        PageOutput<TeacherCheckByclassVo> pageOutput = ConventPage.getPageOutput(page, TeacherCheckByclassVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/teacher-check-byStu-list")
    @ApiOperation(value="教师教材领取按学生查看页面")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<PageOutput<TeacherCheckByStuVo>> teacherCheckByStuList(@Valid TeacherCheckByStuDto dto){
        IPage<TeacherCheckByStuVo> page = textbookStudentClaimService.getTeacherCheckByStuList(dto);
        PageOutput<TeacherCheckByStuVo> pageOutput = ConventPage.getPageOutput(page, TeacherCheckByStuVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/teacher-check-stu-claim-list")
    @ApiOperation(value="教师查看一本教材的学生领取情况页面")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<List<TeacherCheckStuClaimVo>> teacherCheckStuClaimList(@Valid TeacherCheckStuClaimDto dto){
        List<TeacherCheckStuClaimVo> teacherCheckStuClaimList = textbookStudentClaimService.getTeacherCheckStuClaimList(dto);
        return RT.ok(teacherCheckStuClaimList);
    }

    @GetMapping(value = "/teacher-get-stu-noClaim-list")
    @ApiOperation(value="班主任获取班上某个学生没有认领的教材记录")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<List<TextbookClaimVO>> teacherGetStuNoClaimList(@RequestParam Long studentUserId){
        List<TextbookClaimVO> textbookClaimVOList = textbookStudentClaimService.getTeacherGetStuNoClaimList(studentUserId);
        return RT.ok(textbookClaimVOList);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生教材认领记录信息")
    @SaCheckPermission("textbookstudentclaim:detail")
    public RT<TextbookStudentClaimVo> info(@RequestParam Long id){
        TextbookStudentClaim textbookStudentClaim = textbookStudentClaimService.getById(id);
        if (textbookStudentClaim == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(textbookStudentClaim, TextbookStudentClaimVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生教材认领记录")
    @SaCheckPermission("textbookstudentclaim:add")
    public RT<Boolean> add(@Valid @RequestBody AddTextbookStudentClaimDto dto){
        TextbookStudentClaim textbookStudentClaim = BeanUtil.toBean(dto, TextbookStudentClaim.class);
        boolean isSuccess = textbookStudentClaimService.save(textbookStudentClaim);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生教材认领记录")
    @SaCheckPermission("textbookstudentclaim:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTextbookStudentClaimDto dto){

        TextbookStudentClaim textbookStudentClaim = BeanUtil.toBean(dto, TextbookStudentClaim.class);
        return RT.ok(textbookStudentClaimService.updateById(textbookStudentClaim));

    }

    @PutMapping("/confirm")
    @ApiOperation(value = "学生自己确认已经领取")
    @SaCheckPermission("textbookstudentclaim:edit")
    public RT<Boolean> confirm(@Valid @RequestBody List<Long> textbookStudentClaimIds){
        return RT.ok(textbookStudentClaimService.updateByIds(textbookStudentClaimIds));
    }

    @PutMapping("/teacher-confirm")
    @ApiOperation(value = "教师帮助学生确认已经领取")
    @SaCheckPermission("textbookstudentclaim:edit")
    public RT<Boolean> teacherConfirm(@Valid @RequestBody List<TeacherConfirmDto> teacherConfirmDtoList){
        for (TeacherConfirmDto dto : teacherConfirmDtoList) {
            TextbookStudentClaim textbookStudentClaim = BeanUtil.toBean(dto, TextbookStudentClaim.class);
            textbookStudentClaim.setId(dto.getTextbookStudentClaimId());
            textbookStudentClaim.setIsClaim(1);
            textbookStudentClaim.setRemark(dto.getRemark());
            textbookStudentClaim.setModifyUserId(StpUtil.getLoginIdAsLong());
            textbookStudentClaim.setModifyDate(new Date());
            textbookStudentClaimService.updateById(textbookStudentClaim);
        }
        return RT.ok(true);
    }

    @DeleteMapping
    @ApiOperation(value = "删除学生教材认领记录")
    @SaCheckPermission("textbookstudentclaim:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(textbookStudentClaimService.removeBatchByIds(ids));

    }

}