package com.xjrsoft.module.classtime.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.conditions.query.QueryWrapper;
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.classtime.dto.AddClassTimeStatisticsSetDto;
import com.xjrsoft.module.classtime.dto.ClassTimeStatisticsSetPageDto;
import com.xjrsoft.module.classtime.dto.UpdateClassTimeStatisticsSetDto;
import com.xjrsoft.module.classtime.entity.ClassTimeStatisticsSet;
import com.xjrsoft.module.classtime.service.IClassTimeStatisticsSetService;
import com.xjrsoft.module.classtime.vo.ClassTimeStatisticsSetPageVo;
import com.xjrsoft.module.classtime.vo.ClassTimeStatisticsSetVo;
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.Date;
import java.util.List;

/**
* @title: 课时统计-设置
* @Author dzx
* @Date: 2024-09-26
* @Version 1.0
*/
@RestController
@RequestMapping("/classtime" + "/classTimeStatisticsSet")
@Api(value = "/classtime"  + "/classTimeStatisticsSet",tags = "课时统计-设置代码")
@AllArgsConstructor
public class ClassTimeStatisticsSetController {


    private final IClassTimeStatisticsSetService classTimeStatisticsSetService;

    @GetMapping(value = "/page")
    @ApiOperation(value="课时统计-设置列表(分页)")
    @SaCheckPermission("classtimestatisticsset:detail")
    public RT<PageOutput<ClassTimeStatisticsSetPageVo>> page(@Valid ClassTimeStatisticsSetPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询课时统计-设置信息")
    @SaCheckPermission("classtimestatisticsset:detail")
    public RT<ClassTimeStatisticsSetVo> info(@RequestParam Long id){
        ClassTimeStatisticsSet classTimeStatisticsSet = classTimeStatisticsSetService.getById(id);
        if (classTimeStatisticsSet == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(classTimeStatisticsSet, ClassTimeStatisticsSetVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增课时统计-设置")
    @SaCheckPermission("classtimestatisticsset:add")
    public RT<Boolean> add(@Valid @RequestBody AddClassTimeStatisticsSetDto dto){
        ClassTimeStatisticsSet classTimeStatisticsSet = BeanUtil.toBean(dto, ClassTimeStatisticsSet.class);
        classTimeStatisticsSet.setCreateDate(new Date());
        boolean isSuccess = classTimeStatisticsSetService.save(classTimeStatisticsSet);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改课时统计-设置")
    @SaCheckPermission("classtimestatisticsset:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateClassTimeStatisticsSetDto dto){
        ClassTimeStatisticsSet classTimeStatisticsSet = BeanUtil.toBean(dto, ClassTimeStatisticsSet.class);
        classTimeStatisticsSet.setModifyDate(new Date());
        return RT.ok(classTimeStatisticsSetService.updateById(classTimeStatisticsSet));

    }

    @DeleteMapping
    @ApiOperation(value = "删除课时统计-设置")
    @SaCheckPermission("classtimestatisticsset:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(classTimeStatisticsSetService.removeBatchByIds(ids));
    }

    @GetMapping(value = "/last-info")
    @ApiOperation(value="查询最新设置信息")
    @SaCheckPermission("classtimestatisticsset:detail")
    public RT<ClassTimeStatisticsSetVo> lastInfo(@RequestParam Integer category){
        List<ClassTimeStatisticsSet> list = classTimeStatisticsSetService.list(
                new QueryWrapper<ClassTimeStatisticsSet>().lambda()
                        .eq(ClassTimeStatisticsSet::getCategory, category)
                        .orderByDesc(ClassTimeStatisticsSet::getCreateDate)
        );
        if (list.isEmpty()) {
            return RT.ok();
        }
        ClassTimeStatisticsSet classTimeStatisticsSet = list.get(0);
        return RT.ok(BeanUtil.toBean(classTimeStatisticsSet, ClassTimeStatisticsSetVo.class));
    }

}