package com.xjrsoft.module.outint.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.LocalDateTimeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.outint.dto.AddStudentOutInRecordDto;
import com.xjrsoft.module.outint.dto.StudentOutInRecordPageDto;
import com.xjrsoft.module.outint.dto.UpdateStudentOutInRecordDto;
import com.xjrsoft.module.outint.entity.StudentOutInRecord;
import com.xjrsoft.module.outint.service.IStudentOutInRecordService;
import com.xjrsoft.module.outint.vo.StudentOutInRecordPageVo;
import com.xjrsoft.module.outint.vo.StudentOutInRecordVo;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

/**
* @title: 学生出入记录
* @Author dzx
* @Date: 2024-05-14
* @Version 1.0
*/
@RestController
@RequestMapping("/outint" + "/studentOutInRecord")
@Api(value = "/outint"  + "/studentOutInRecord",tags = "学生出入记录代码")
@AllArgsConstructor
public class StudentOutInRecordController {


    private final IStudentOutInRecordService studentOutInRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生出入记录列表(分页)")
    @SaCheckPermission("studentoutinrecord:detail")
    public RT<PageOutput<StudentOutInRecordPageVo>> page(@Valid StudentOutInRecordPageDto dto){

        MPJLambdaWrapper<StudentOutInRecord> queryWrapper = MPJWrappers.<StudentOutInRecord>lambdaJoin()
                .disableSubLogicDel()
                    .orderByDesc(StudentOutInRecord::getId)
                .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
                .like(StrUtil.isNotBlank(dto.getIdentityCard()), XjrUser::getCredentialNumber,dto.getIdentityCard())
               .eq(StrUtil.isNotBlank(dto.getStduyStatus()), BaseStudentSchoolRoll::getStduyStatus,dto.getStduyStatus())
                .between((ObjectUtil.isNotNull(dto.getStartTime()) && ObjectUtil.isNotNull(dto.getStartTime())), StudentOutInRecord::getRecordTime, LocalDateTimeUtil.format(dto.getStartTime(),"yyyy-MM-dd")+" 00:00:00", LocalDateTimeUtil.format(dto.getStartTime(),"yyyy-MM-dd")+" 23:59:59")
                .eq(ObjectUtil.isNotEmpty(dto.getStatus()), StudentOutInRecord::getStatus, dto.getStatus())
                .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0, StudentOutInRecord::getClassId, dto.getClassId())
                .eq(ObjectUtil.isNotNull(dto.getUserId())&&dto.getUserId()!=0, StudentOutInRecord::getUserId, dto.getUserId())
                .select(StudentOutInRecord::getId)
                .select(StudentOutInRecord.class,x -> VoToColumnUtil.fieldsToColumns(StudentOutInRecordPageVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class,XjrUser::getId,StudentOutInRecord::getUserId,
                        ext->ext.selectAs(XjrUser::getCredentialNumber,StudentOutInRecordPageVo::getIdentityCard)
                                .selectAs(XjrUser::getMobile,StudentOutInRecordPageVo::getPhone)
                                .select(XjrUser::getName))
                .leftJoin(BaseClass.class,BaseClass::getId,StudentOutInRecord::getClassId,ext->ext.selectAs(BaseClass::getName,StudentOutInRecordPageVo::getClassCn))
                .leftJoin(XjrUser.class,XjrUser::getId,StudentOutInRecord::getTeacherId,ext->ext.selectAs(XjrUser::getName,StudentOutInRecordPageVo::getTeacherCn))
                .leftJoin(BaseStudentSchoolRoll.class,BaseStudentSchoolRoll::getUserId,StudentOutInRecord::getUserId)
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, XjrUser::getGender, ext->ext.selectAs(DictionaryDetail::getName, StudentOutInRecordPageVo::getGender))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, BaseStudentSchoolRoll::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StudentOutInRecordPageVo::getStduyStatusCn))
                ;
        IPage<StudentOutInRecordPageVo> page = studentOutInRecordService.selectJoinListPage(ConventPage.getPage(dto),StudentOutInRecordPageVo.class, queryWrapper);
        PageOutput<StudentOutInRecordPageVo> pageOutput = ConventPage.getPageOutput(page, StudentOutInRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生出入记录信息")
    @SaCheckPermission("studentoutinrecord:detail")
    public RT<StudentOutInRecordVo> info(@RequestParam Long id){
        StudentOutInRecord studentOutInRecord = studentOutInRecordService.getById(id);
        if (studentOutInRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(studentOutInRecord, StudentOutInRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生出入记录")
    @SaCheckPermission("studentoutinrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddStudentOutInRecordDto dto){
        StudentOutInRecord studentOutInRecord = BeanUtil.toBean(dto, StudentOutInRecord.class);
        boolean isSuccess = studentOutInRecordService.save(studentOutInRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生出入记录")
    @SaCheckPermission("studentoutinrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateStudentOutInRecordDto dto){

        StudentOutInRecord studentOutInRecord = BeanUtil.toBean(dto, StudentOutInRecord.class);
        return RT.ok(studentOutInRecordService.updateById(studentOutInRecord));

    }

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

    }

}