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.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddBaseStudentBehaviorProjectDto;
import com.xjrsoft.module.student.dto.BaseStudentBehaviorProjectPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentBehaviorProjectDto;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorProject;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorCategoryService;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorProjectService;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorProjectPageVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorProjectTreeVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorProjectVo;
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 javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @title: 学生操行分考核项目
 * @Author fanxp
 * @Date: 2023-11-15
 * @Version 1.0
 */
@RestController
@RequestMapping("/student" + "/basestudentbehaviorproject")
@Api(value = "/student" + "/basestudentbehaviorproject", tags = "学生操行分考核项目代码")
@AllArgsConstructor
public class BaseStudentBehaviorProjectController {

    private final IBaseStudentBehaviorCategoryService baseStudentBehaviorCategoryService;

    private final IBaseStudentBehaviorProjectService baseStudentBehaviorProjectService;

    @GetMapping(value = "/tree")
    @ApiOperation(value = "学生操行分考核项目列表(树)")
    @SaCheckPermission("basestudentbehaviorproject:detail")
    public RT<List<BaseStudentBehaviorProjectTreeVo>> tree() {

        List<BaseStudentBehaviorProjectTreeVo> voList = new ArrayList<>();
        baseStudentBehaviorCategoryService.list().forEach((node) -> {
            voList.add(new BaseStudentBehaviorProjectTreeVo(){{
                setId(node.getId());
                setName(node.getName());
            }});
        });


        baseStudentBehaviorProjectService.list().forEach((node)->{
            voList.add(new BaseStudentBehaviorProjectTreeVo(){{
                setId(node.getId());
                setName(node.getName());
                setParentId(node.getBaseStudentBehaviorCategoryId());
            }});
        });
        List<BaseStudentBehaviorProjectTreeVo> treeVoList = TreeUtil.build(voList);

        return RT.ok(treeVoList);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "学生操行分考核项目列表(分页)")
    @SaCheckPermission("basestudentbehaviorproject:detail")
    public RT<PageOutput<BaseStudentBehaviorProjectPageVo>> page(@Valid BaseStudentBehaviorProjectPageDto dto) {

        MPJLambdaWrapper<BaseStudentBehaviorProject> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(BaseStudentBehaviorProject.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentBehaviorProjectPageVo.class).contains(x.getProperty()))
                .selectAs(DictionaryDetail::getName,BaseStudentBehaviorProjectPageVo::getScoreTypeCN)
                .selectAs(BaseStudentBehaviorProject::getId,BaseStudentBehaviorProjectPageVo::getId)
                .orderByDesc(BaseStudentBehaviorProject::getId)
                .eq(ObjectUtil.isNotNull(dto.getBaseStudentBehaviorCategoryId()), BaseStudentBehaviorProject::getBaseStudentBehaviorCategoryId, dto.getBaseStudentBehaviorCategoryId())
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,BaseStudentBehaviorProject::getScoreType);
        IPage<BaseStudentBehaviorProjectPageVo> page = baseStudentBehaviorProjectService.selectJoinListPage(ConventPage.getPage(dto),BaseStudentBehaviorProjectPageVo.class, queryWrapper);
        PageOutput<BaseStudentBehaviorProjectPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentBehaviorProjectPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询学生操行分考核项目信息")
    @SaCheckPermission("basestudentbehaviorproject:detail")
    public RT<BaseStudentBehaviorProjectVo> info(@RequestParam Long id) {
        BaseStudentBehaviorProject baseStudentBehaviorProject = baseStudentBehaviorProjectService.getById(id);
        if (baseStudentBehaviorProject == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentBehaviorProject, BaseStudentBehaviorProjectVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生操行分考核项目")
    @SaCheckPermission("basestudentbehaviorproject:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentBehaviorProjectDto dto) {
        BaseStudentBehaviorProject baseStudentBehaviorProject = BeanUtil.toBean(dto, BaseStudentBehaviorProject.class);
        boolean isSuccess = baseStudentBehaviorProjectService.save(baseStudentBehaviorProject);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生操行分考核项目")
    @SaCheckPermission("basestudentbehaviorproject:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentBehaviorProjectDto dto) {

        BaseStudentBehaviorProject baseStudentBehaviorProject = BeanUtil.toBean(dto, BaseStudentBehaviorProject.class);
        return RT.ok(baseStudentBehaviorProjectService.updateById(baseStudentBehaviorProject));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生操行分考核项目")
    @SaCheckPermission("basestudentbehaviorproject:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
        return RT.ok(baseStudentBehaviorProjectService.deleteDeepByIds(ids));

    }

}