package com.CUST.brain.web.controller.business;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.dao.domain.business.TalentBasic;
import com.CUST.brain.service.TalentBasicService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 人才基本信息管理
 */
@RestController
@RequestMapping("/business/talent")
@Api(tags = "人才基本信息管理")
public class TalentBasicController {

    @Resource
    private TalentBasicService talentBasicService;

    @GetMapping("/list")
    @ApiOperation("分页查询人才列表")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<Page<TalentBasic>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("姓名") @RequestParam(required = false) String name,
            @ApiParam("单位ID") @RequestParam(required = false) String unitId,
            @ApiParam("职称") @RequestParam(required = false) String title,
            @ApiParam("分类定级") @RequestParam(required = false) String classifyGrade) {
        
        Page<TalentBasic> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TalentBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, TalentBasic::getName, name)
                .eq(unitId != null, TalentBasic::getUnitId, unitId)
                .eq(title != null, TalentBasic::getTitle, title)
                .eq(classifyGrade != null, TalentBasic::getClassifyGrade, classifyGrade)
                .eq(TalentBasic::getFlag, 0)
                .orderByDesc(TalentBasic::getCreateTime);
        
        return CommonResult.success(talentBasicService.page(page, wrapper));
    }

    @GetMapping("/name/{name}")
    @ApiOperation("根据姓名查询人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<List<TalentBasic>> getByName(@PathVariable String name) {
        return CommonResult.success(talentBasicService.getByName(name));
    }

    @GetMapping("/unit/{unitId}")
    @ApiOperation("根据单位ID查询人才")
    @PreAuthorize("hasAuthority('business:talent:query')")
    public CommonResult<List<TalentBasic>> getByUnitId(@PathVariable String unitId) {
        return CommonResult.success(talentBasicService.getByUnitId(unitId));
    }

    @GetMapping("/title/{title}")
    @ApiOperation("根据职称查询人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<List<TalentBasic>> getByTitle(@PathVariable String title) {
        return CommonResult.success(talentBasicService.getByTitle(title));
    }

    @GetMapping("/grade/{classifyGrade}")
    @ApiOperation("根据分类定级查询人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<List<TalentBasic>> getByClassifyGrade(@PathVariable String classifyGrade) {
        return CommonResult.success(talentBasicService.getByClassifyGrade(classifyGrade));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取人才详情")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<TalentBasic> getById(@PathVariable String id) {
        return CommonResult.success(talentBasicService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<Boolean> add(@Valid @RequestBody TalentBasic talent) {
        return CommonResult.success(talentBasicService.save(talent));
    }

    @PutMapping
    @ApiOperation("修改人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<Boolean> update(@Valid @RequestBody TalentBasic talent) {
        return CommonResult.success(talentBasicService.updateTalent(talent));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除人才")
    @PreAuthorize("hasAuthority('business:talent')")
    public CommonResult<Boolean> delete(@PathVariable String id) {
        return CommonResult.success(talentBasicService.removeById(id));
    }
} 