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 cn.hutool.core.util.StrUtil;
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.AddBaseStudentScholarshipCategoryDto;
import com.xjrsoft.module.student.dto.BaseStudentScholarshipCategoryPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentScholarshipCategoryDto;
import com.xjrsoft.module.student.entity.BaseStudentScholarshipCategory;
import com.xjrsoft.module.student.service.IBaseStudentScholarshipCategoryService;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipCategoryPageVo;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipCategoryVo;
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 dzx
* @Date: 2023-11-23
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentScholarshipCategory")
@Api(value = "/student"  + "/baseStudentScholarshipCategory",tags = "奖学金类别代码")
@AllArgsConstructor
public class BaseStudentScholarshipCategoryController {


    private final IBaseStudentScholarshipCategoryService baseStudentScholarshipCategoryService;

    @GetMapping(value = "/page")
    @ApiOperation(value="奖学金类别列表(分页)")
    @SaCheckPermission("basestudentscholarshipcategory:detail")
    public RT<PageOutput<BaseStudentScholarshipCategoryPageVo>> page(@Valid BaseStudentScholarshipCategoryPageDto dto){
        IPage<BaseStudentScholarshipCategoryPageVo> page = baseStudentScholarshipCategoryService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentScholarshipCategoryPageVo.class,
            MPJWrappers.<BaseStudentScholarshipCategory>lambdaJoin()
            .like(StrUtil.isNotEmpty(dto.getScholarshipName()), BaseStudentScholarshipCategory::getName, dto.getScholarshipName())
            .like(StrUtil.isNotEmpty(dto.getScholarshipSource()), DictionaryDetail::getName, dto.getScholarshipSource())
            .eq(ObjectUtil.isNotNull(dto.getScholarshipLevel()), BaseStudentScholarshipCategory::getScholarshipLevel, dto.getScholarshipLevel())
            .select(BaseStudentScholarshipCategory::getId)
            .selectAs(DictionaryDetail::getName, BaseStudentScholarshipCategoryPageVo::getScholarshipSourceCn)
            .select(BaseStudentScholarshipCategory.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentScholarshipCategoryPageVo.class).contains(x.getProperty()))
            .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, BaseStudentScholarshipCategory::getScholarshipSource)
        );

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询奖学金类别信息")
    @SaCheckPermission("basestudentscholarshipcategory:detail")
    public RT<BaseStudentScholarshipCategoryVo> info(@RequestParam Long id){
        BaseStudentScholarshipCategory baseStudentScholarshipCategory = baseStudentScholarshipCategoryService.getByIdDeep(id);
        if (baseStudentScholarshipCategory == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentScholarshipCategory, BaseStudentScholarshipCategoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增奖学金类别")
    @SaCheckPermission("basestudentscholarshipcategory:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentScholarshipCategoryDto dto){
        BaseStudentScholarshipCategory baseStudentScholarshipCategory = BeanUtil.toBean(dto, BaseStudentScholarshipCategory.class);
        boolean isSuccess = baseStudentScholarshipCategoryService.add(baseStudentScholarshipCategory);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改奖学金类别")
    @SaCheckPermission("basestudentscholarshipcategory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentScholarshipCategoryDto dto){

        BaseStudentScholarshipCategory baseStudentScholarshipCategory = BeanUtil.toBean(dto, BaseStudentScholarshipCategory.class);
        return RT.ok(baseStudentScholarshipCategoryService.update(baseStudentScholarshipCategory));

    }

    @DeleteMapping
    @ApiOperation(value = "删除奖学金类别")
    @SaCheckPermission("basestudentscholarshipcategory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseStudentScholarshipCategoryService.delete(ids));

    }

}