package com.xjrsoft.module.student.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.TreeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseSemester;
import com.xjrsoft.module.base.service.IBaseSemesterService;
import com.xjrsoft.module.student.dto.AddPbSemesterConfigDto;
import com.xjrsoft.module.student.dto.UpdatePbSemesterConfigDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.student.dto.PbSemesterConfigPageDto;
import com.xjrsoft.module.student.entity.PbSemesterConfig;
import com.xjrsoft.module.student.service.IPbSemesterConfigService;
import com.xjrsoft.module.student.vo.BaseStudentTreeVo;
import com.xjrsoft.module.student.vo.PbSemesterConfigPageVo;

import com.xjrsoft.module.student.vo.PbSemesterConfigVo;
import com.xjrsoft.module.student.vo.SemesterYeatTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @title: 攀宝学期对应
* @Author zsz
* @Date: 2024-03-18
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/pbSemesterConfig")
@Api(value = "/student"  + "/pbSemesterConfig",tags = "攀宝学期对应代码")
@AllArgsConstructor
public class PbSemesterConfigController {

    private final IPbSemesterConfigService pbSemesterConfigService;

    private final IBaseSemesterService baseSemesterService;

    @GetMapping(value = "/tree")
    @ApiOperation(value = "年和学期树")
    @SaCheckPermission("pbsemesterconfig:detail")
    public RT<List<SemesterYeatTreeVo>> tree() {
        List<BaseSemester> baseSemesterList = baseSemesterService.list();

        baseSemesterList.stream().forEach(element -> {
            // 处理元素
            element.setModifyUserId(Long.parseLong(element.getName().substring(0,4)));
        });


        Map<Long, SemesterYeatTreeVo> yearMap = new HashMap<>();
        for (BaseSemester node : baseSemesterList) {
            Long year = node.getModifyUserId();
            if (!yearMap.containsKey(year)) {
                yearMap.put(year, new SemesterYeatTreeVo() {{
                    setId(year);
                    setName(year + "年");
                    setChildren(new ArrayList<>());
                }});
            }
            yearMap.get(year).getChildren().add(new SemesterYeatTreeVo() {{
                setId(node.getId());
                setName(node.getName());
                setParentId(year);
                setChildren(new ArrayList<>());
            }});
        }
        List<SemesterYeatTreeVo> voList = new ArrayList<>(yearMap.values());
        return RT.ok(voList);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="攀宝学期对应列表(分页)")
    @SaCheckPermission("pbsemesterconfig:detail")
    public RT<PageOutput<PbSemesterConfigPageVo>> page(@Valid PbSemesterConfigPageDto dto){

        LambdaQueryWrapper<PbSemesterConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(PbSemesterConfig::getId)
                .eq(dto.getBaseSemesterId() != null && dto.getBaseSemesterId() > 0, PbSemesterConfig::getBaseSemesterId, dto.getBaseSemesterId())
                .select(PbSemesterConfig.class,x -> VoToColumnUtil.fieldsToColumns(PbSemesterConfigPageVo.class).contains(x.getProperty()));
        IPage<PbSemesterConfig> page = pbSemesterConfigService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<PbSemesterConfigPageVo> pageOutput = ConventPage.getPageOutput(page, PbSemesterConfigPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询攀宝学期对应信息")
    @SaCheckPermission("pbsemesterconfig:detail")
    public RT<PbSemesterConfigVo> info(@RequestParam Long id){
        PbSemesterConfig pbSemesterConfig = pbSemesterConfigService.getById(id);
        if (pbSemesterConfig == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(pbSemesterConfig, PbSemesterConfigVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增攀宝学期对应")
    @SaCheckPermission("pbsemesterconfig:add")
    public RT<Boolean> add(@Valid @RequestBody AddPbSemesterConfigDto dto){
        PbSemesterConfig pbSemesterConfig = BeanUtil.toBean(dto, PbSemesterConfig.class);
        boolean isSuccess = pbSemesterConfigService.save(pbSemesterConfig);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改攀宝学期对应")
    @SaCheckPermission("pbsemesterconfig:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdatePbSemesterConfigDto dto){

        PbSemesterConfig pbSemesterConfig = BeanUtil.toBean(dto, PbSemesterConfig.class);
        return RT.ok(pbSemesterConfigService.updateById(pbSemesterConfig));

    }

    @DeleteMapping
    @ApiOperation(value = "删除攀宝学期对应")
    @SaCheckPermission("pbsemesterconfig:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(pbSemesterConfigService.removeBatchByIds(ids));

    }

}