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 com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.MPJWrappers;
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.AddBaseStudentPostDto;
import com.xjrsoft.module.student.dto.BaseStudentPostListDto;
import com.xjrsoft.module.student.dto.BaseStudentPostPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentPostDto;
import com.xjrsoft.module.student.entity.BaseStudentPost;
import com.xjrsoft.module.student.service.IBaseStudentPostService;
import com.xjrsoft.module.student.vo.BaseStudentPostListVo;
import com.xjrsoft.module.student.vo.BaseStudentPostPageVo;
import com.xjrsoft.module.student.vo.BaseStudentPostVo;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
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.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 学生职务设置
* @Author dzx
* @Date: 2023-11-13
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/basestudentpost")
@Api(value = "/student"  + "/basestudentpost",tags = "学生职务设置代码")
@AllArgsConstructor
public class BaseStudentPostController {


    private final IBaseStudentPostService baseStudentPostService;

    @GetMapping(value = "/list")
    @ApiOperation(value="学生职务设置列表(不分页)")
    @SaCheckPermission("basestudentpost:detail")
    public RT<List<BaseStudentPostListVo>> list(@Valid BaseStudentPostListDto dto){

        LambdaQueryWrapper<BaseStudentPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(BaseStudentPost::getId)
                .eq(ObjectUtil.isNotNull(dto.getLevel()), BaseStudentPost::getLevel, dto.getLevel())
                .eq(BaseStudentPost::getStatus,1)
                .orderByDesc(BaseStudentPost::getId)
                .select(BaseStudentPost.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentPostListVo.class).contains(x.getProperty()));

        List<BaseStudentPost> list = baseStudentPostService.list(queryWrapper);
        List<BaseStudentPostListVo> listVos = BeanUtil.copyToList(list, BaseStudentPostListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="学生职务设置列表(分页)")
    @SaCheckPermission("basestudentpost:detail")
    public RT<PageOutput<BaseStudentPostPageVo>> page(@Valid BaseStudentPostPageDto dto){

        IPage<BaseStudentPostPageVo> page = baseStudentPostService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentPostPageVo.class,
                MPJWrappers.<BaseStudentPost>lambdaJoin()
                        .orderByAsc(BaseStudentPost::getSortCode)

                .selectAs(DictionaryDetail::getName, BaseStudentPostPageVo::getLevelCn)
                .select(BaseStudentPost.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentPostPageVo.class).contains(x.getProperty()))
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, BaseStudentPost::getLevel)
                .select(BaseStudentPost::getId)
                .eq(ObjectUtil.isNotNull(dto.getLevel()), BaseStudentPost::getLevel, dto.getLevel())
        );
        PageOutput<BaseStudentPostPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentPostPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生职务设置信息")
    @SaCheckPermission("basestudentpost:detail")
    public RT<BaseStudentPostVo> info(@RequestParam Long id){
        BaseStudentPost baseStudentPost = baseStudentPostService.getById(id);
        if (baseStudentPost == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentPost, BaseStudentPostVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生职务设置")
    @SaCheckPermission("basestudentpost:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentPostDto dto){
        BaseStudentPost baseStudentPost = BeanUtil.toBean(dto, BaseStudentPost.class);
        boolean isSuccess = baseStudentPostService.save(baseStudentPost);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生职务设置")
    @SaCheckPermission("basestudentpost:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentPostDto dto){

        BaseStudentPost baseStudentPost = BeanUtil.toBean(dto, BaseStudentPost.class);
        return RT.ok(baseStudentPostService.updateById(baseStudentPost));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生职务设置")
    @SaCheckPermission("basestudentpost:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseStudentPostService.deleteLogicallyBatchByIds(ids));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid BaseStudentPostPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<BaseStudentPostPageVo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<BaseStudentPostPageVo>) page(dto).getData()).getList();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, BaseStudentPostPageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

        return RT.fileStream(bot.toByteArray(), "BaseStudentPost" + ExcelTypeEnum.XLSX.getValue());
    }

}