package com.xjrsoft.module.teacher.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.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.GenderDictionaryEnum;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.attendance.entity.StudentAttendanceRecord;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.organization.service.IUserDeptRelationService;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.service.IDictionarydetailService;
import com.xjrsoft.module.teacher.dto.AddXjrUserDto;
import com.xjrsoft.module.teacher.dto.UpdateXjrUserDto;
import com.xjrsoft.module.teacher.dto.XjrUserPageDto;
import com.xjrsoft.module.teacher.entity.BaseTeacher;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.service.ITeacherbaseManagerService;
import com.xjrsoft.module.teacher.vo.XjrUserPageVo;
import com.xjrsoft.module.teacher.vo.XjrUserVo;
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;
import java.util.stream.Collectors;

/**
* @title: 教职工基本信息管理
* @Author 管理员
* @Date: 2023-08-08
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/teacherbasemanager")
@Api(value = "/teacher"  + "/teacherbasemanager",tags = "教职工基本信息管理代码")
@AllArgsConstructor
public class TeacherbaseManagerController {


    private final ITeacherbaseManagerService teacherbaseManagerService;
    private final IDictionarydetailService dictionarydetailService;
    private final IUserDeptRelationService userDeptRelationService;

    @GetMapping(value = "/page")
    @ApiOperation(value="XjrUser列表(分页)")
    @SaCheckPermission("teacherbasemanager:detail")
    public R page(@Valid XjrUserPageDto dto){

        MPJLambdaWrapper<XjrUser> queryWrapper = MPJWrappers.<XjrUser>lambdaJoin()
                .disableSubLogicDel()
                .like(StrUtil.isNotBlank(dto.getUserName()),XjrUser::getUserName,dto.getUserName())
                .like(StrUtil.isNotBlank(dto.getMobile()),XjrUser::getMobile,dto.getMobile())
                .like(StrUtil.isNotBlank(dto.getName()),XjrUser::getName,dto.getName())
                .like(StrUtil.isNotBlank(dto.getEmail()),XjrUser::getEmail,dto.getEmail())

                .orderByDesc(XjrUser::getId)
                .select(XjrUser::getId)
                .select(XjrUser.class,x -> VoToColumnUtil.fieldsToColumns(XjrUserPageVo.class).contains(x.getProperty()))
                .innerJoin(BaseTeacher.class,BaseTeacher::getUserId,XjrUser::getId)
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,BaseTeacher::getJobState,ext->ext.selectAs(DictionaryDetail::getName, XjrUserPageVo::getJobState))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,XjrUser::getCredentialType,ext->ext.selectAs(DictionaryDetail::getName, XjrUserPageVo::getCredentialType))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,BaseTeacher::getEmployWay,ext->ext.selectAs(DictionaryDetail::getName, XjrUserPageVo::getEmployWay))

                .selectAsClass(BaseTeacher.class, XjrUserPageVo.class);

        if(ObjectUtil.isAllNotEmpty(dto.getDepartmentId())&&dto.getDepartmentId()!=0){
            queryWrapper
                    .eq(UserDeptRelation::getDeptId, dto.getDepartmentId())
                    .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, XjrUser::getId);
        }

        IPage<XjrUserPageVo> page = teacherbaseManagerService.selectJoinListPage(ConventPage.getPage(dto),XjrUserPageVo.class, queryWrapper);

        PageOutput<XjrUserPageVo> pageOutput = ConventPage.getPageOutput(page, XjrUserPageVo.class);

        for (XjrUserPageVo record : pageOutput.getList()) {
            record.setGenderCn(GenderDictionaryEnum.getValue(record.getGender()));
        }

        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询XjrUser信息")
    @SaCheckPermission("teacherbasemanager:detail")
    public R info(@RequestParam Long id){
        XjrUser xjrUser = teacherbaseManagerService.getByIdDeep(id);
        if (xjrUser == null) {
           return R.error("找不到此数据！");
        }
        XjrUserVo userVo = BeanUtil.toBean(xjrUser, XjrUserVo.class);
        userVo.setGenderCn(GenderDictionaryEnum.getValue(userVo.getGender()));

        List<Long> deptIds = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
                        .eq(UserDeptRelation::getUserId, userVo.getId()))
                .stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
        String allDeptIdStr = StrUtil.join(StringPool.COMMA, deptIds);
        userVo.setDepartmentIds(allDeptIdStr);

        return R.ok(userVo);
    }


    @PostMapping
    @ApiOperation(value = "新增XjrUser")
    @SaCheckPermission("teacherbasemanager:add")
    public R add(@Valid @RequestBody AddXjrUserDto dto){
        return R.ok(teacherbaseManagerService.add(dto));

    }

    @PutMapping
    @ApiOperation(value = "修改XjrUser")
    @SaCheckPermission("teacherbasemanager:edit")
    public R update(@Valid @RequestBody UpdateXjrUserDto dto){

        return R.ok(teacherbaseManagerService.update(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("teacherbasemanager:delete")
    public R delete(@Valid @RequestBody List<Long> ids){
        return R.ok(teacherbaseManagerService.delete(ids));

    }

}