package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.interfaces.MPJBaseJoin;
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.TreeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddBaseStudentAssessmentProjectDto;
import com.xjrsoft.module.student.dto.BaseStudentAssessmentProjectListDto;
import com.xjrsoft.module.student.dto.BaseStudentAssessmentProjectPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentAssessmentProjectDto;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentItem;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentProject;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentCategoryService;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentItemService;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentProjectService;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentProjectListVo;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentProjectPageVo;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentProjectTreeVo;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentProjectVo;
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.Date;
import java.util.List;

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

    private final IBaseStudentAssessmentCategoryService baseStudentAssessmentCategoryService;

    private final IBaseStudentAssessmentItemService baseStudentAssessmentItemService;

    private final IBaseStudentAssessmentProjectService baseStudentAssessmentProjectService;


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

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

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

        return RT.ok(treeVoList);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "学生考核项目列表(分页)")
    @SaCheckPermission("basestudentassessmentproject:detail")
    public RT<PageOutput<BaseStudentAssessmentProjectPageVo>> page(@Valid BaseStudentAssessmentProjectPageDto dto) {

        MPJBaseJoin<BaseStudentAssessmentProject> queryWrapper = MPJWrappers.<BaseStudentAssessmentProject>lambdaJoin()
                .disableSubLogicDel()
                .orderByDesc(BaseStudentAssessmentProject::getSortCode)
                .eq(ObjectUtil.isNotEmpty(dto.getBaseStudentAssessmentCategoryId()), BaseStudentAssessmentProject::getBaseStudentAssessmentCategoryId, dto.getBaseStudentAssessmentCategoryId())
                .select(BaseStudentAssessmentProject.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentAssessmentProjectPageVo.class).contains(x.getProperty()))
                .leftJoin(BaseStudentAssessmentItem.class, BaseStudentAssessmentItem::getId, BaseStudentAssessmentProject::getBaseStudentAssessmentCategoryId)
                .select(BaseStudentAssessmentProject::getId)
                .selectAs(BaseStudentAssessmentItem::getName,BaseStudentAssessmentProjectPageVo::getBaseStudentAssessmentCategoryName);

        IPage<BaseStudentAssessmentProjectPageVo> page = baseStudentAssessmentProjectService.selectJoinListPage(ConventPage.getPage(dto),BaseStudentAssessmentProjectPageVo.class,queryWrapper);
        PageOutput<BaseStudentAssessmentProjectPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentAssessmentProjectPageVo.class);
        return RT.ok(pageOutput);
    }


    @GetMapping(value = "/list")
    @ApiOperation(value = "学生考核项目列表(不分页)")
    @SaCheckPermission("basestudentassessmentproject:detail")
    public RT<List<BaseStudentAssessmentProjectListVo>> list(@Valid BaseStudentAssessmentProjectListDto dto) {

        LambdaQueryWrapper<BaseStudentAssessmentProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(BaseStudentAssessmentProject::getId)
                .eq(ObjectUtil.isNotEmpty(dto.getBaseStudentAssessmentCategoryId()), BaseStudentAssessmentProject::getBaseStudentAssessmentCategoryId, dto.getBaseStudentAssessmentCategoryId())
                .select(BaseStudentAssessmentProject.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentAssessmentProjectListVo.class).contains(x.getProperty()));

        List<BaseStudentAssessmentProject> list = baseStudentAssessmentProjectService.list(queryWrapper);
        List<BaseStudentAssessmentProjectListVo> listVos = BeanUtil.copyToList(list, BaseStudentAssessmentProjectListVo.class);
        return RT.ok(listVos);
    }

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


    @PostMapping
    @ApiOperation(value = "新增学生考核项目")
    @SaCheckPermission("basestudentassessmentproject:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentAssessmentProjectDto dto) {
        BaseStudentAssessmentProject baseStudentAssessmentProject = BeanUtil.toBean(dto, BaseStudentAssessmentProject.class);
        QueryWrapper<BaseStudentAssessmentProject> queryWrapperSortcode = new QueryWrapper<>();
        queryWrapperSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        BaseStudentAssessmentProject b = baseStudentAssessmentProjectService.getOne(queryWrapperSortcode);
        baseStudentAssessmentProject.setSortCode(b.getSortCode() + 1);
        baseStudentAssessmentProject.setCreateUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentProject.setCreateDate(new Date());
        baseStudentAssessmentProject.setDepartmentId(0L);
        baseStudentAssessmentProject.setPointsDeductedMax(dto.getBasicPoints());
        baseStudentAssessmentProject.setPointsDeductedMin(dto.getBasicPoints());
        baseStudentAssessmentProject.setAddPointsMax(dto.getBasicPoints());
        baseStudentAssessmentProject.setAddPointsMin(dto.getBasicPoints());
        baseStudentAssessmentProject.setBehaviorScore(0D);
        baseStudentAssessmentProject.setBaseStudentBehaviorProjectId(0L);
        boolean isSuccess = baseStudentAssessmentProjectService.save(baseStudentAssessmentProject);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生考核项目")
    @SaCheckPermission("basestudentassessmentproject:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentAssessmentProjectDto dto) {
        BaseStudentAssessmentProject baseStudentAssessmentProject = BeanUtil.toBean(dto, BaseStudentAssessmentProject.class);
        baseStudentAssessmentProject.setModifyUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentProject.setModifyDate(new Date());
        return RT.ok(baseStudentAssessmentProjectService.updateById(baseStudentAssessmentProject));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学生考核项目")
    @SaCheckPermission("basestudentassessmentproject:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
        return RT.ok(baseStudentAssessmentProjectService.removeBatchByIds(ids));

    }

}