package com.xjrsoft.module.teacher.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.teacher.dto.AddAttendanceConfigDto;
import com.xjrsoft.module.teacher.dto.AttendanceConfigPageDto;
import com.xjrsoft.module.teacher.dto.UpdateAttendanceConfigDto;
import com.xjrsoft.module.teacher.entity.AttendanceConfig;
import com.xjrsoft.module.teacher.service.IAttendanceConfigService;
import com.xjrsoft.module.teacher.vo.AttendanceConfigPageVo;
import com.xjrsoft.module.teacher.vo.AttendanceConfigVo;
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 szs
* @Date: 2023-12-18
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/attendanceConfig")
@Api(value = "/teacher"  + "/attendanceConfig",tags = "考勤-配置代码")
@AllArgsConstructor
public class AttendanceConfigController {


    private final IAttendanceConfigService attendanceConfigService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考勤-配置列表(分页)")
    @SaCheckPermission("attendanceconfig:detail")
    public RT<PageOutput<AttendanceConfigPageVo>> page(@Valid AttendanceConfigPageDto dto){

        LambdaQueryWrapper<AttendanceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(AttendanceConfig::getId)
                .select(AttendanceConfig.class,x -> VoToColumnUtil.fieldsToColumns(AttendanceConfigPageVo.class).contains(x.getProperty()));
        IPage<AttendanceConfig> page = attendanceConfigService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AttendanceConfigPageVo> pageOutput = ConventPage.getPageOutput(page, AttendanceConfigPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考勤-配置信息")
    @SaCheckPermission("attendanceconfig:detail")
    public RT<AttendanceConfigVo> info(@RequestParam Long id){
        AttendanceConfig attendanceConfig = attendanceConfigService.getById(id);
        if (attendanceConfig == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(attendanceConfig, AttendanceConfigVo.class));
    }

    @GetMapping(value = "/get-present-config")
    @ApiOperation(value="获取当前使用的配置信息")
    @SaCheckPermission("attendanceconfig:detail")
    public RT<AttendanceConfigVo> getPresentConfig(){
        LambdaQueryWrapper<AttendanceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(AttendanceConfig::getId)
                .select(AttendanceConfig.class,x -> VoToColumnUtil.fieldsToColumns(AttendanceConfigPageVo.class).contains(x.getProperty()))
                .eq(AttendanceConfig::getDeleteMark,0)
                .eq(AttendanceConfig::getEnabledMark,1);
        List<AttendanceConfig> attendanceConfigList = attendanceConfigService.list(queryWrapper);

        if (attendanceConfigList == null || !(attendanceConfigList.size() > 0)) {
            return RT.error("找不到打卡配置数据！");
        }
        return RT.ok(BeanUtil.toBean(attendanceConfigList.get(0), AttendanceConfigVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增考勤-配置")
    @SaCheckPermission("attendanceconfig:add")
    public RT<Boolean> add(@Valid @RequestBody AddAttendanceConfigDto dto){
        AttendanceConfig attendanceConfig = BeanUtil.toBean(dto, AttendanceConfig.class);
        boolean isSuccess = attendanceConfigService.save(attendanceConfig);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考勤-配置")
    @SaCheckPermission("attendanceconfig:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAttendanceConfigDto dto){

        AttendanceConfig attendanceConfig = BeanUtil.toBean(dto, AttendanceConfig.class);
        return RT.ok(attendanceConfigService.updateById(attendanceConfig));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考勤-配置")
    @SaCheckPermission("attendanceconfig:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(attendanceConfigService.removeBatchByIds(ids));

    }

}