package com.xjrsoft.module.attendance.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.attendance.dto.AddStudentAttendanceRecordDto;
import com.xjrsoft.module.attendance.dto.StudentAttendanceRecordClassPageDto;
import com.xjrsoft.module.attendance.dto.UpdateStudentAttendanceRecordDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.attendance.dto.StudentAttendanceRecordPageDto;
import com.xjrsoft.module.attendance.entity.StudentAttendanceRecord;
import com.xjrsoft.module.attendance.entity.TeacherAttendanceRecord;
import com.xjrsoft.module.attendance.service.IStudentAttendanceRecordService;
import com.xjrsoft.module.attendance.vo.StudentAttendanceRecordClassPageVo;
import com.xjrsoft.module.attendance.vo.StudentAttendanceRecordPageVo;

import com.xjrsoft.module.attendance.vo.StudentAttendanceRecordVo;
import com.xjrsoft.module.attendance.vo.TeacherAttendanceRecordPageVo;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.outint.entity.TeacherOutInRecord;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.teacher.entity.XjrUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @title: 学生考勤记录
* @Author dzx
* @Date: 2024-05-15
* @Version 1.0
*/
@RestController
@RequestMapping("/attendance" + "/studentAttendanceRecord")
@Api(value = "/attendance"  + "/studentAttendanceRecord",tags = "学生考勤记录代码")
@AllArgsConstructor
public class StudentAttendanceRecordController {


    private final IStudentAttendanceRecordService studentAttendanceRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生考勤记录列表(分页)")
    @SaCheckPermission("studentattendancerecord:detail")
    public RT<PageOutput<StudentAttendanceRecordPageVo>> page(@Valid StudentAttendanceRecordPageDto dto){

        MPJLambdaWrapper<StudentAttendanceRecord> queryWrapper =  MPJWrappers.<StudentAttendanceRecord>lambdaJoin()
                    .orderByDesc(StudentAttendanceRecord::getId)
                .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
                .like(StrUtil.isNotBlank(dto.getIdentityCard()), XjrUser::getCredentialNumber,dto.getIdentityCard())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()), StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 00:00:00")
                .le(ObjectUtil.isNotNull(dto.getStartTime()),StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 23:59:59")
                .eq(ObjectUtil.isNotNull(dto.getTimeInterval())&&dto.getTimeInterval()!=0, StudentAttendanceRecord::getTimeInterval, dto.getTimeInterval())
                .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0, StudentAttendanceRecord::getClassId, dto.getClassId())
                .eq(StrUtil.isNotBlank(dto.getAttendanceStatus()), StudentAttendanceRecord::getAttendanceStatus,dto.getAttendanceStatus())
                .eq(StrUtil.isNotBlank(dto.getStduyStatus()), StudentAttendanceRecord::getStduyStatus,dto.getStduyStatus())
                .select(StudentAttendanceRecord::getId)
                .select(XjrUser::getName,XjrUser::getMobile,XjrUser::getCredentialNumber)
                .select(StudentAttendanceRecord.class,x -> VoToColumnUtil.fieldsToColumns(StudentAttendanceRecordPageVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class,XjrUser::getId, TeacherOutInRecord::getUserId)
                .leftJoin(BaseClass.class,BaseClass::getId, StudentAttendanceRecord::getClassId,ext->ext.selectAs(BaseClass::getName,StudentAttendanceRecordPageVo::getClassCn))
                .leftJoin(XjrUser.class,XjrUser::getId, BaseClass::getTeacherId,ext->ext.selectAs(XjrUser::getName,StudentAttendanceRecordPageVo::getTeacherCn))

                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, StudentAttendanceRecord::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordPageVo::getStduyStatusCn))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, XjrUser::getGender, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordPageVo::getGender))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, StudentAttendanceRecord::getAttendanceStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordPageVo::getAttendanceStatusCn))
                ;

        IPage<StudentAttendanceRecordPageVo> page = studentAttendanceRecordService.selectJoinListPage(ConventPage.getPage(dto),StudentAttendanceRecordPageVo.class, queryWrapper);
        PageOutput<StudentAttendanceRecordPageVo> pageOutput = ConventPage.getPageOutput(page, StudentAttendanceRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/class-page")
    @ApiOperation(value="班级考勤记录列表(分页)")
    @SaCheckPermission("studentattendancerecord:detail")
    public RT<PageOutput<StudentAttendanceRecordClassPageVo>> classPage(@Valid StudentAttendanceRecordClassPageDto dto){

        MPJLambdaWrapper<StudentAttendanceRecord> queryWrapper = MPJWrappers.<StudentAttendanceRecord>lambdaJoin()
                .orderByDesc(StudentAttendanceRecord::getId)

                .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0, StudentAttendanceRecord::getClassId, dto.getClassId())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()), StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 00:00:00")
                .le(ObjectUtil.isNotNull(dto.getStartTime()),StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 23:59:59")
                .eq(ObjectUtil.isNotNull(dto.getTimeInterval())&&dto.getTimeInterval()!=0, StudentAttendanceRecord::getTimeInterval, dto.getTimeInterval())

                .select(StudentAttendanceRecord::getId)
                .select(StudentAttendanceRecord.class,x -> VoToColumnUtil.fieldsToColumns(StudentAttendanceRecordClassPageVo.class).contains(x.getProperty()))

                .leftJoin(BaseClass.class,BaseClass::getId, StudentAttendanceRecord::getClassId,ext->ext.selectAs(BaseClass::getName,StudentAttendanceRecordClassPageVo::getClassCn))
                .leftJoin(XjrUser.class,XjrUser::getId, BaseClass::getTeacherId,ext->ext.selectAs(XjrUser::getName,StudentAttendanceRecordClassPageVo::getTeacherCn))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, StudentAttendanceRecord::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordClassPageVo::getAttendanceStatusCn))
                ;
        IPage<StudentAttendanceRecordClassPageVo> page = studentAttendanceRecordService.selectJoinListPage(ConventPage.getPage(dto),StudentAttendanceRecordClassPageVo.class, queryWrapper);
        PageOutput<StudentAttendanceRecordClassPageVo> pageOutput = ConventPage.getPageOutput(page, StudentAttendanceRecordClassPageVo.class);
        return RT.ok(pageOutput);
    }
    @GetMapping(value = "/history-page")
    @ApiOperation(value="历史考勤记录列表(分页)")
    @SaCheckPermission("studentattendancerecord:detail")
    public RT<PageOutput<StudentAttendanceRecordClassPageVo>> historyPage(@Valid StudentAttendanceRecordClassPageDto dto){

        MPJLambdaWrapper<StudentAttendanceRecord> queryWrapper = MPJWrappers.<StudentAttendanceRecord>lambdaJoin()
                .orderByDesc(StudentAttendanceRecord::getId)

                .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0, StudentAttendanceRecord::getClassId, dto.getClassId())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()), StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 00:00:00")
                .le(ObjectUtil.isNotNull(dto.getStartTime()),StudentAttendanceRecord::getCreateDate,dto.getEndTime()+" 23:59:59")

                .select(StudentAttendanceRecord::getId)
                .select(StudentAttendanceRecord.class,x -> VoToColumnUtil.fieldsToColumns(StudentAttendanceRecordClassPageVo.class).contains(x.getProperty()))

                .leftJoin(BaseClass.class,BaseClass::getId, StudentAttendanceRecord::getClassId,ext->ext.selectAs(BaseClass::getName,StudentAttendanceRecordClassPageVo::getClassCn))
                .leftJoin(XjrUser.class,XjrUser::getId, BaseClass::getTeacherId,ext->ext.selectAs(XjrUser::getName,StudentAttendanceRecordClassPageVo::getTeacherCn))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, StudentAttendanceRecord::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordClassPageVo::getAttendanceStatusCn))
                ;
        IPage<StudentAttendanceRecordClassPageVo> page = studentAttendanceRecordService.selectJoinListPage(ConventPage.getPage(dto),StudentAttendanceRecordClassPageVo.class, queryWrapper);
        PageOutput<StudentAttendanceRecordClassPageVo> pageOutput = ConventPage.getPageOutput(page, StudentAttendanceRecordClassPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/leaving-school-page")
    @ApiOperation(value="离校统计列表(分页)")
    @SaCheckPermission("studentattendancerecord:detail")
    public RT<PageOutput<StudentAttendanceRecordClassPageVo>> leavingSchoolPage(@Valid StudentAttendanceRecordClassPageDto dto){

        MPJLambdaWrapper<StudentAttendanceRecord> queryWrapper = MPJWrappers.<StudentAttendanceRecord>lambdaJoin()
                .orderByDesc(StudentAttendanceRecord::getId)

                .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0, StudentAttendanceRecord::getClassId, dto.getClassId())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()), StudentAttendanceRecord::getCreateDate,dto.getStartTime()+" 00:00:00")
                .le(ObjectUtil.isNotNull(dto.getStartTime()),StudentAttendanceRecord::getCreateDate,dto.getEndTime()+" 23:59:59")
                .eq(ObjectUtil.isNotNull(dto.getTimeInterval())&&dto.getTimeInterval()!=0, StudentAttendanceRecord::getTimeInterval, dto.getTimeInterval())

                .select(StudentAttendanceRecord::getId)
                .select(StudentAttendanceRecord.class,x -> VoToColumnUtil.fieldsToColumns(StudentAttendanceRecordClassPageVo.class).contains(x.getProperty()))

                .leftJoin(BaseClass.class,BaseClass::getId, StudentAttendanceRecord::getClassId,ext->ext.selectAs(BaseClass::getName,StudentAttendanceRecordClassPageVo::getClassCn))
                .leftJoin(XjrUser.class,XjrUser::getId, BaseClass::getTeacherId,ext->ext.selectAs(XjrUser::getName,StudentAttendanceRecordClassPageVo::getTeacherCn))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, StudentAttendanceRecord::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentAttendanceRecordClassPageVo::getAttendanceStatusCn))
                ;
        IPage<StudentAttendanceRecordClassPageVo> page = studentAttendanceRecordService.selectJoinListPage(ConventPage.getPage(dto),StudentAttendanceRecordClassPageVo.class, queryWrapper);
        PageOutput<StudentAttendanceRecordClassPageVo> pageOutput = ConventPage.getPageOutput(page, StudentAttendanceRecordClassPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生考勤记录信息")
    @SaCheckPermission("studentattendancerecord:detail")
    public RT<StudentAttendanceRecordVo> info(@RequestParam Long id){
        StudentAttendanceRecord studentAttendanceRecord = studentAttendanceRecordService.getById(id);
        if (studentAttendanceRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(studentAttendanceRecord, StudentAttendanceRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生考勤记录")
    @SaCheckPermission("studentattendancerecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddStudentAttendanceRecordDto dto){
        StudentAttendanceRecord studentAttendanceRecord = BeanUtil.toBean(dto, StudentAttendanceRecord.class);
        boolean isSuccess = studentAttendanceRecordService.save(studentAttendanceRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生考勤记录")
    @SaCheckPermission("studentattendancerecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateStudentAttendanceRecordDto dto){

        StudentAttendanceRecord studentAttendanceRecord = BeanUtil.toBean(dto, StudentAttendanceRecord.class);
        return RT.ok(studentAttendanceRecordService.updateById(studentAttendanceRecord));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生考勤记录")
    @SaCheckPermission("studentattendancerecord:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(studentAttendanceRecordService.removeBatchByIds(ids));

    }

}