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.AddTeacherAttendanceRecordDto;
import com.xjrsoft.module.attendance.dto.UpdateTeacherAttendanceRecordDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.attendance.dto.TeacherAttendanceRecordPageDto;
import com.xjrsoft.module.attendance.entity.TeacherAttendanceRecord;
import com.xjrsoft.module.attendance.service.ITeacherAttendanceRecordService;
import com.xjrsoft.module.attendance.vo.TeacherAttendanceRecordPageVo;

import com.xjrsoft.module.attendance.vo.TeacherAttendanceRecordVo;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.outint.entity.TeacherOutInRecord;
import com.xjrsoft.module.outint.vo.TeacherOutInRecordPageVo;
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" + "/teacherAttendanceRecord")
@Api(value = "/attendance"  + "/teacherAttendanceRecord",tags = "教师考勤记录代码")
@AllArgsConstructor
public class TeacherAttendanceRecordController {


    private final ITeacherAttendanceRecordService teacherAttendanceRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教师考勤记录列表(分页)")
    @SaCheckPermission("teacherattendancerecord:detail")
    public RT<PageOutput<TeacherAttendanceRecordPageVo>> page(@Valid TeacherAttendanceRecordPageDto dto){

        MPJLambdaWrapper<TeacherAttendanceRecord> queryWrapper =  MPJWrappers.<TeacherAttendanceRecord>lambdaJoin()
                .orderByDesc(TeacherAttendanceRecord::getId)
                .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
                .ge(ObjectUtil.isNotNull(dto.getStartTime()), TeacherAttendanceRecord::getCreateDate,dto.getStartTime()+" 00:00:00")
                .le(ObjectUtil.isNotNull(dto.getStartTime()),TeacherAttendanceRecord::getCreateDate,dto.getStartTime()+" 23:59:59")
                .eq(ObjectUtil.isNotNull(dto.getTimeInterval())&&dto.getTimeInterval()!=0, TeacherAttendanceRecord::getTimeInterval, dto.getTimeInterval())
                .eq(StrUtil.isNotBlank(dto.getAttendanceStatus()), TeacherAttendanceRecord::getAttendanceStatus,dto.getAttendanceStatus())
                .eq(ObjectUtil.isNotNull(dto.getAttendanceMode())&&dto.getAttendanceMode()!=0, TeacherAttendanceRecord::getAttendanceMode, dto.getAttendanceMode())
                .like(StrUtil.isNotBlank(dto.getCarNumber()), TeacherAttendanceRecord::getCarNumber,dto.getCarNumber())
                .select(TeacherAttendanceRecord::getId)
                .select(TeacherAttendanceRecord.class,x -> VoToColumnUtil.fieldsToColumns(TeacherAttendanceRecordPageVo.class).contains(x.getProperty()))
                .select(XjrUser::getName,XjrUser::getMobile,XjrUser::getCredentialNumber)
                .leftJoin(XjrUser.class,XjrUser::getId,TeacherOutInRecord::getUserId)
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, TeacherAttendanceRecord::getAttendanceStatus, ext->ext.selectAs(DictionaryDetail::getName, TeacherAttendanceRecordPageVo::getAttendanceStatusCn))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, XjrUser::getGender, ext->ext.selectAs(DictionaryDetail::getName, TeacherAttendanceRecordPageVo::getGender))
                ;

        if(ObjectUtil.isNotNull(dto.getDeptId())&&dto.getDeptId()!=0){
            queryWrapper.leftJoin(UserDeptRelation.class,UserDeptRelation::getUserId,XjrUser::getId)
                    .eq(UserDeptRelation::getDeptId,dto.getDeptId());
        }
        IPage<TeacherAttendanceRecordPageVo> page = teacherAttendanceRecordService.selectJoinListPage(ConventPage.getPage(dto), TeacherAttendanceRecordPageVo.class,queryWrapper);
        PageOutput<TeacherAttendanceRecordPageVo> pageOutput = ConventPage.getPageOutput(page, TeacherAttendanceRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教师考勤记录信息")
    @SaCheckPermission("teacherattendancerecord:detail")
    public RT<TeacherAttendanceRecordVo> info(@RequestParam Long id){
        TeacherAttendanceRecord teacherAttendanceRecord = teacherAttendanceRecordService.getById(id);
        if (teacherAttendanceRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(teacherAttendanceRecord, TeacherAttendanceRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教师考勤记录")
    @SaCheckPermission("teacherattendancerecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddTeacherAttendanceRecordDto dto){
        TeacherAttendanceRecord teacherAttendanceRecord = BeanUtil.toBean(dto, TeacherAttendanceRecord.class);
        boolean isSuccess = teacherAttendanceRecordService.save(teacherAttendanceRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教师考勤记录")
    @SaCheckPermission("teacherattendancerecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTeacherAttendanceRecordDto dto){

        TeacherAttendanceRecord teacherAttendanceRecord = BeanUtil.toBean(dto, TeacherAttendanceRecord.class);
        return RT.ok(teacherAttendanceRecordService.updateById(teacherAttendanceRecord));

    }

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

    }

}