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.organization.entity.UserDeptRelation;
import com.xjrsoft.module.outint.dto.AddTeacherOutInRecordDto;
import com.xjrsoft.module.outint.dto.TeacherOutInRecordPageDto;
import com.xjrsoft.module.outint.dto.UpdateTeacherOutInRecordDto;
import com.xjrsoft.module.outint.entity.StudentOutInRecord;
import com.xjrsoft.module.outint.entity.TeacherOutInRecord;
import com.xjrsoft.module.outint.service.ITeacherOutInRecordService;
import com.xjrsoft.module.outint.vo.StudentOutInRecordPageVo;
import com.xjrsoft.module.outint.vo.TeacherOutInRecordPageVo;
import com.xjrsoft.module.outint.vo.TeacherOutInRecordVo;
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.util.List;

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


    private final ITeacherOutInRecordService teacherOutInRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教师出入记录列表(分页)")
    @SaCheckPermission("teacheroutinrecord:detail")
    public RT<PageOutput<TeacherOutInRecordPageVo>> page(@Valid TeacherOutInRecordPageDto dto){

        MPJLambdaWrapper<TeacherOutInRecord> queryWrapper= MPJWrappers.<TeacherOutInRecord>lambdaJoin()
                .disableSubLogicDel()
                .orderByDesc(TeacherOutInRecord::getId)
                .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
                .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()), TeacherOutInRecord::getStatus, dto.getStatus())
                .eq(ObjectUtil.isNotNull(dto.getUserId())&&dto.getUserId()!=0, StudentOutInRecord::getUserId, dto.getUserId())
                .select(TeacherOutInRecord::getId)
                .select(TeacherOutInRecord.class,x -> VoToColumnUtil.fieldsToColumns(TeacherOutInRecordPageVo.class).contains(x.getProperty()))

                .leftJoin(XjrUser.class,XjrUser::getId,TeacherOutInRecord::getUserId,
                        ext->ext.selectAs(XjrUser::getCredentialNumber, TeacherOutInRecordPageVo::getIdentityCard)
                        .selectAs(XjrUser::getMobile,TeacherOutInRecordPageVo::getPhone)
                        .select(XjrUser::getName))

                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, XjrUser::getGender, ext->ext.selectAs(DictionaryDetail::getName, TeacherOutInRecordPageVo::getGender));

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教师出入记录信息")
    @SaCheckPermission("teacheroutinrecord:detail")
    public RT<TeacherOutInRecordVo> info(@RequestParam Long id){
        TeacherOutInRecord teacherOutInRecord = teacherOutInRecordService.getById(id);
        if (teacherOutInRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(teacherOutInRecord, TeacherOutInRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教师出入记录")
    @SaCheckPermission("teacheroutinrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddTeacherOutInRecordDto dto){
        TeacherOutInRecord teacherOutInRecord = BeanUtil.toBean(dto, TeacherOutInRecord.class);
        boolean isSuccess = teacherOutInRecordService.save(teacherOutInRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教师出入记录")
    @SaCheckPermission("teacheroutinrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTeacherOutInRecordDto dto){

        TeacherOutInRecord teacherOutInRecord = BeanUtil.toBean(dto, TeacherOutInRecord.class);
        return RT.ok(teacherOutInRecordService.updateById(teacherOutInRecord));

    }

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

    }

}