package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
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.AddBaseStudentBursariesProjectDto;
import com.xjrsoft.module.student.dto.BaseStudentBursariesProjectPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentBursariesProjectDto;
import com.xjrsoft.module.student.entity.BaseStudentBursariesProject;
import com.xjrsoft.module.student.service.IBaseStudentBursariesProjectService;
import com.xjrsoft.module.student.vo.BaseStudentBursariesProjectPageVo;
import com.xjrsoft.module.student.vo.BaseStudentBursariesProjectVo;
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.List;

/**
* @title: 助学金项目
* @Author devil
* @Date: 2023-11-28
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentBursariesProject")
@Api(value = "/student"  + "/baseStudentBursariesProject",tags = "助学金项目代码")
@AllArgsConstructor
public class BaseStudentBursariesProjectController {


    private final IBaseStudentBursariesProjectService baseStudentBursariesProjectService;

    @GetMapping(value = "/page")
    @ApiOperation(value="助学金项目列表(分页)")
    @SaCheckPermission("basestudentbursariesproject:detail")
    public RT<PageOutput<BaseStudentBursariesProjectPageVo>> page(@Valid BaseStudentBursariesProjectPageDto dto){
        IPage<BaseStudentBursariesProjectPageVo> page = baseStudentBursariesProjectService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentBursariesProjectPageVo.class,
            MPJWrappers.<BaseStudentBursariesProject>lambdaJoin()
            .selectAs(DictionaryDetail::getName, BaseStudentBursariesProjectPageVo::getBursariesTypeCn)
            .selectAs(BaseStudentBursariesProject::getId, BaseStudentBursariesProjectPageVo::getId)
            .select(BaseStudentBursariesProject.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentBursariesProjectPageVo.class).contains(x.getProperty()))
            .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, BaseStudentBursariesProject::getBursariesType)
            .orderByAsc(BaseStudentBursariesProject::getSortCode)
        );

        PageOutput<BaseStudentBursariesProjectPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentBursariesProjectPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询助学金项目信息")
    @SaCheckPermission("basestudentbursariesproject:detail")
    public RT<BaseStudentBursariesProjectVo> info(@RequestParam Long id){
        BaseStudentBursariesProject baseStudentBursariesProject = baseStudentBursariesProjectService.getById(id);
        if (baseStudentBursariesProject == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentBursariesProject, BaseStudentBursariesProjectVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增助学金项目")
    @SaCheckPermission("basestudentbursariesproject:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentBursariesProjectDto dto){
        BaseStudentBursariesProject baseStudentBursariesProject = BeanUtil.toBean(dto, BaseStudentBursariesProject.class);
        boolean isSuccess = baseStudentBursariesProjectService.save(baseStudentBursariesProject);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改助学金项目")
    @SaCheckPermission("basestudentbursariesproject:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentBursariesProjectDto dto){

        BaseStudentBursariesProject baseStudentBursariesProject = BeanUtil.toBean(dto, BaseStudentBursariesProject.class);
        return RT.ok(baseStudentBursariesProjectService.updateById(baseStudentBursariesProject));

    }

    @DeleteMapping
    @ApiOperation(value = "删除助学金项目")
    @SaCheckPermission("basestudentbursariesproject:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseStudentBursariesProjectService.removeBatchByIds(ids));

    }

}