package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.TreeUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.service.IBaseClassService;
import com.xjrsoft.module.base.service.IBaseGradeService;
import com.xjrsoft.module.student.dto.PbVXssfdetailPageDto;
import com.xjrsoft.module.student.dto.PbVXsxxsfytbExcelDto;
import com.xjrsoft.module.student.dto.PbVXsxxsfytbPageDto;
import com.xjrsoft.module.student.dto.PersonalInfoDto;
import com.xjrsoft.module.student.dto.PersonalPortraitFeeInformationDto;
import com.xjrsoft.module.student.service.IPbVXssfdetailService;
import com.xjrsoft.module.student.service.IPbVXsxxsfytbService;
import com.xjrsoft.module.student.vo.BaseClassQfCountVo;
import com.xjrsoft.module.student.vo.BaseClassTreeVo;
import com.xjrsoft.module.student.vo.FeeDetailListVo;
import com.xjrsoft.module.student.vo.PbVXssfdetailPageVo;
import com.xjrsoft.module.student.vo.PbVXsxxsfytbExcelVo;
import com.xjrsoft.module.student.vo.PbVXsxxsfytbPageVo;
import com.xjrsoft.module.student.vo.PersonalPortraitFeeInformationVo;
import com.xjrsoft.module.student.vo.PersonalPortraitPersonalInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
* @title: 学生职务设置
* @Author dzx
* @Date: 2023-11-13
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/consumption")
@Api(value = "/student"  + "/consumption",tags = "学生消费管理")
@AllArgsConstructor
public class ConsumptionController {
    private final IPbVXssfdetailService pbVXssfdetailService;

    private final IPbVXsxxsfytbService pbVXsxxsfytbService;
    private final IBaseGradeService baseGradeService;
    private final IBaseClassService baseClassService;

    @GetMapping(value = "/tree")
    @ApiOperation(value = "学生在读毕业年级班级树")
    @SaCheckPermission("studentmanager:detail")
    public RT<List<BaseClassTreeVo>> tree() {
        List<BaseClassTreeVo> voList = new ArrayList<>();

        List<BaseClassQfCountVo> qfCount = pbVXsxxsfytbService.getClassQfCount();
        Map<Long, Integer> qfCountMap = new HashMap<>();
        for (BaseClassQfCountVo qfCountVo : qfCount) {
            qfCountMap.put(qfCountVo.getId(), qfCountVo.getCount());
        }

        voList.add(new BaseClassTreeVo(){{
            setId(6L);
            setName("年级");
        }});

        baseGradeService.list().forEach((node) -> {
            if(node.getStatus() == 1){
                voList.add(new BaseClassTreeVo(){{
                    setId(node.getId());
                    setName(node.getName());
                    setParentId(6L);
                }});
            }
        });

        baseClassService.list(new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())).forEach((node)->{
            voList.add(new BaseClassTreeVo(){{
                setId(node.getId());
                setName(node.getName());
                setParentId(node.getGradeId());
                setStatus(0);
                if(qfCountMap.get(node.getId()) != null && qfCountMap.get(node.getId()) > 0){
                    setStatus(1);
                }
            }});
        });

        List<BaseClassTreeVo> treeVoList = TreeUtil.build(voList);

        return RT.ok(treeVoList);
    }

    @GetMapping(value = "/personal-info")
    @ApiOperation(value="学生个人信息")
    @SaCheckPermission("consumption:detail")
    public RT<PersonalPortraitPersonalInfoVo> personalInfo(@Valid PersonalInfoDto dto){
        PersonalPortraitPersonalInfoVo info = pbVXssfdetailService.getPersonalInfo(dto);
        return RT.ok(info);
    }

    @GetMapping(value = "/detail-page")
    @ApiOperation(value="学生收费明细分页查询")
    @SaCheckPermission("consumption:detail")
    public RT<PageOutput<PbVXssfdetailPageVo>> detailPage(@Valid PbVXssfdetailPageDto dto){
        Page<PbVXssfdetailPageVo> page = pbVXssfdetailService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<PbVXssfdetailPageVo> pageOutput = ConventPage.getPageOutput(page, PbVXssfdetailPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/pb_vx_sxxsfytbPage")
    @ApiOperation(value="学生消费管理页分页查询")
    @SaCheckPermission("consumption:detail")
    public RT<PageOutput<PbVXsxxsfytbPageVo>> pbVXsxxsfytbPage(@Valid PbVXsxxsfytbPageDto dto){
        IPage<PbVXsxxsfytbPageVo> page = pbVXsxxsfytbService.getPage(dto);
        PageOutput<PbVXsxxsfytbPageVo> pageOutput = ConventPage.getPageOutput(page, PbVXsxxsfytbPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/cost-information")
    @ApiOperation(value="费用信息")
    @SaCheckPermission("consumption:detail")
    public RT<PersonalPortraitFeeInformationVo> costInformation(@Valid PersonalPortraitFeeInformationDto dto){

        return RT.ok(pbVXsxxsfytbService.listCostInformation(dto));
    }

    @PostMapping("/export-query")
    @ApiOperation(value = "有参导出")
    public ResponseEntity<byte[]> exportData(@RequestBody PbVXsxxsfytbExcelDto dto) {
        List<PbVXsxxsfytbExcelVo> dataList = pbVXsxxsfytbService.getList(dto);
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, PbVXsxxsfytbExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

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

    @GetMapping(value = "/fee-detail")
    @ApiOperation(value="学生缴费的详情")
    @SaCheckPermission("consumption:detail")
    public RT<List<FeeDetailListVo>> feeDetail(@RequestParam String studentcode){
        return RT.ok(pbVXsxxsfytbService.getFeeDetail(studentcode));
    }
}