package com.xjrsoft.module.student.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.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.R;
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.student.dto.AddStudentConsumeDateDto;
import com.xjrsoft.module.student.dto.StudentConsumeDatePageDto;
import com.xjrsoft.module.student.dto.UpdateStudentConsumeDateDto;
import com.xjrsoft.module.student.entity.StudentConsumeDate;
import com.xjrsoft.module.student.service.IStudentConsumeDateService;
import com.xjrsoft.module.student.vo.StudentConsumeDatePageVo;
import com.xjrsoft.module.student.vo.StudentConsumeDateVo;
import com.xjrsoft.module.system.entity.DictionaryDetail;
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 org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
* @title: 学生消费数据
* @Author dzx
* @Date: 2024-08-26
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/studentConsumeDate")
@Api(value = "/student"  + "/studentConsumeDate",tags = "学生消费数据代码")
@AllArgsConstructor
public class StudentConsumeDateController {


    private final IStudentConsumeDateService studentConsumeDateService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生消费数据列表(分页)")
    @SaCheckPermission("studentconsumedate:detail")
    public RT<PageOutput<StudentConsumeDatePageVo>> page(@Valid StudentConsumeDatePageDto dto) {

        IPage<StudentConsumeDatePageVo> page = studentConsumeDateService.selectJoinListPage(ConventPage.getPage(dto), StudentConsumeDatePageVo.class,
                MPJWrappers.<StudentConsumeDate>lambdaJoin()
                        .orderByDesc(StudentConsumeDate::getId)
                        .eq(ObjectUtil.isNotNull(dto.getUserId()), StudentConsumeDate::getStudentId, dto.getUserId())
                        .eq(StrUtil.isNotEmpty(dto.getConsumeType()), StudentConsumeDate::getConsumeType, dto.getConsumeType())
                        .between(ObjectUtil.isNotNull(dto.getStartDate()) && ObjectUtil.isNotNull(dto.getEndDate()), StudentConsumeDate::getConsumeTime, dto.getStartDate(), dto.getEndDate())
                        .select(StudentConsumeDate.class, x -> VoToColumnUtil.fieldsToColumns(StudentConsumeDatePageVo.class).contains(x.getProperty()))
                        .select(StudentConsumeDate::getId)
                        .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, StudentConsumeDate::getConsumeType, ext -> ext.selectAs(DictionaryDetail::getName, StudentConsumeDatePageVo::getConsumeTypeCn))
        );
        PageOutput<StudentConsumeDatePageVo> pageOutput = ConventPage.getPageOutput(page, StudentConsumeDatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生消费数据信息")
    @SaCheckPermission("studentconsumedate:detail")
    public RT<StudentConsumeDateVo> info(@RequestParam Long id){
        StudentConsumeDate studentConsumeDate = studentConsumeDateService.getById(id);
        if (studentConsumeDate == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(studentConsumeDate, StudentConsumeDateVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生消费数据")
    @SaCheckPermission("studentconsumedate:add")
    public RT<Boolean> add(@Valid @RequestBody AddStudentConsumeDateDto dto){
        StudentConsumeDate studentConsumeDate = BeanUtil.toBean(dto, StudentConsumeDate.class);
        boolean isSuccess = studentConsumeDateService.save(studentConsumeDate);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生消费数据")
    @SaCheckPermission("studentconsumedate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateStudentConsumeDateDto dto){

        StudentConsumeDate studentConsumeDate = BeanUtil.toBean(dto, StudentConsumeDate.class);
        return RT.ok(studentConsumeDateService.updateById(studentConsumeDate));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生消费数据")
    @SaCheckPermission("studentconsumedate:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(studentConsumeDateService.removeBatchByIds(ids));

    }

    @PostMapping("/excel-import")
    @ApiOperation(value = "excel消费记录导入")
    @SaCheckPermission("coursetable:excelimport")
    public RT<Boolean> excelImport(@RequestParam("file") MultipartFile file) throws IOException {
        String result = studentConsumeDateService.excelImport(file);
        if (result.length() > 0) {
            throw new MyException(result);
        }
        return RT.ok("全部成功", true);
    }
}