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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.AddBaseStudentAssessmentItemDto;
import com.xjrsoft.module.student.dto.BaseStudentAssessmentItemPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentAssessmentItemDto;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentItem;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentItemService;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentItemPageVo;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentItemVo;
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.Date;
import java.util.List;

/**
* @title: 考核项目表
* @Author dzx
* @Date: 2024-03-05
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentAssessmentItem")
@Api(value = "/student"  + "/baseStudentAssessmentItem",tags = "考核项目表代码")
@AllArgsConstructor
public class BaseStudentAssessmentItemController {


    private final IBaseStudentAssessmentItemService baseStudentAssessmentItemService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考核项目表列表(分页)")
    @SaCheckPermission("basestudentassessmentitem:detail")
    public RT<PageOutput<BaseStudentAssessmentItemPageVo>> page(@Valid BaseStudentAssessmentItemPageDto dto){

        LambdaQueryWrapper<BaseStudentAssessmentItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(BaseStudentAssessmentItem::getId)
                .select(BaseStudentAssessmentItem.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentAssessmentItemPageVo.class).contains(x.getProperty()))
                .eq(dto.getBaseStudentAssessmentCategoryId() != null, BaseStudentAssessmentItem::getBaseStudentAssessmentCategoryId, dto.getBaseStudentAssessmentCategoryId());
        IPage<BaseStudentAssessmentItem> page = baseStudentAssessmentItemService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<BaseStudentAssessmentItemPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentAssessmentItemPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考核项目表信息")
    @SaCheckPermission("basestudentassessmentitem:detail")
    public RT<BaseStudentAssessmentItemVo> info(@RequestParam Long id){
        BaseStudentAssessmentItem baseStudentAssessmentItem = baseStudentAssessmentItemService.getById(id);
        if (baseStudentAssessmentItem == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentAssessmentItem, BaseStudentAssessmentItemVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增考核项目表")
    @SaCheckPermission("basestudentassessmentitem:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentAssessmentItemDto dto){
        BaseStudentAssessmentItem baseStudentAssessmentItem = BeanUtil.toBean(dto, BaseStudentAssessmentItem.class);
        baseStudentAssessmentItem.setCreateUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentItem.setCreateDate(new Date());
        boolean isSuccess = baseStudentAssessmentItemService.save(baseStudentAssessmentItem);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考核项目表")
    @SaCheckPermission("basestudentassessmentitem:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentAssessmentItemDto dto){
        BaseStudentAssessmentItem baseStudentAssessmentItem = BeanUtil.toBean(dto, BaseStudentAssessmentItem.class);
        baseStudentAssessmentItem.setModifyUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentItem.setModifyDate(new Date());
        return RT.ok(baseStudentAssessmentItemService.updateById(baseStudentAssessmentItem));

    }

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

    }

}