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.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.student.dto.AddWfCadreCandidateDto;
import com.xjrsoft.module.student.dto.UpdateWfCadreCandidateDto;
import com.xjrsoft.module.student.dto.WfCadreCandidatePageDto;
import com.xjrsoft.module.student.entity.WfCadreCandidate;
import com.xjrsoft.module.student.service.IWfCadreCandidateService;
import com.xjrsoft.module.student.vo.WfCadreCandidatePageVo;
import com.xjrsoft.module.student.vo.WfCadreCandidateVo;
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 szs
* @Date: 2023-12-20
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/wfCadreCandidate")
@Api(value = "/student"  + "/wfCadreCandidate",tags = "干部候选人代码")
@AllArgsConstructor
public class WfCadreCandidateController {


    private final IWfCadreCandidateService wfCadreCandidateService;

    @GetMapping(value = "/page")
    @ApiOperation(value="干部候选人列表(分页)")
    @SaCheckPermission("wfcadrecandidate:detail")
    public RT<PageOutput<WfCadreCandidatePageVo>> page(@Valid WfCadreCandidatePageDto dto){

        IPage<WfCadreCandidatePageVo> page = wfCadreCandidateService.getListPage(dto);
        PageOutput<WfCadreCandidatePageVo> pageOutput = ConventPage.getPageOutput(page, WfCadreCandidatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询干部候选人信息")
    @SaCheckPermission("wfcadrecandidate:detail")
    public RT<WfCadreCandidateVo> info(@RequestParam Long id){
        WfCadreCandidate wfCadreCandidate = wfCadreCandidateService.getById(id);
        if (wfCadreCandidate == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfCadreCandidate, WfCadreCandidateVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增干部候选人")
    @SaCheckPermission("wfcadrecandidate:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfCadreCandidateDto dto){
        WfCadreCandidate wfCadreCandidate = BeanUtil.toBean(dto, WfCadreCandidate.class);
        boolean isSuccess = wfCadreCandidateService.save(wfCadreCandidate);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改干部候选人")
    @SaCheckPermission("wfcadrecandidate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfCadreCandidateDto dto){

        WfCadreCandidate wfCadreCandidate = BeanUtil.toBean(dto, WfCadreCandidate.class);
        return RT.ok(wfCadreCandidateService.updateById(wfCadreCandidate));

    }

    @DeleteMapping
    @ApiOperation(value = "删除干部候选人")
    @SaCheckPermission("wfcadrecandidate:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfCadreCandidateService.removeBatchByIds(ids));

    }

}