package jnpf.cultivate.v2.controller.web.label;

import jnpf.base.ActionResult;
import jnpf.cultivate.v2.service.FtbCultivateLabelService;
import jnpf.model.cultivate.v2.label.dto.FtbCultivateLabelReq;
import jnpf.model.cultivate.v2.label.dto.FtbCultivateUpdateLabelReq;
import jnpf.model.cultivate.v2.label.vo.FtbCultivateLabelVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * web课程标签模块
 *
 * @author xgl
 */
@RestController
@RequestMapping("/v2/web/label")
public class FtbCultivateLabelController {

    @Autowired
    private FtbCultivateLabelService ftbCultivateLabelService;

    /**
     * 获取培训标签关联表列表
     * type  1-课程标签  2-鉴定表标签 3-带教技能点
     * source  类型：0-自定义，1-系统默认 ‘’-全部
     *
     * @return 返回标签列表
     */
    @GetMapping("/list")
    public ActionResult<List<FtbCultivateLabelVo>> getList(@RequestParam(value = "type") Integer type, @RequestParam(value = "source", defaultValue = "", required = false) String source) {
        if (null == type) {
            throw new RuntimeException("参数错误,请传入类型");
        }
        Integer searchSource = null;
        if (StringUtils.isNotEmpty(source)) {
            searchSource = Integer.parseInt(source);
        }
        return ActionResult.success(ftbCultivateLabelService.getList(type, searchSource));
    }


    /**
     * 新增标签
     *
     * @param req 请求参数
     * @return 返回添加的标签
     */
    @PostMapping
    public ActionResult<FtbCultivateLabelVo> add(@RequestBody @Valid FtbCultivateLabelReq req) {
        return ActionResult.success("新建成功", ftbCultivateLabelService.addData(req));
    }

    /**
     * 修改培训标签
     *
     * @param id  标签id
     * @param req 请求参数
     * @return 返回修改的标签
     */
    @PutMapping("/{id}")
    public ActionResult<FtbCultivateLabelVo> update(@PathVariable("id") String id, @RequestBody @Valid FtbCultivateUpdateLabelReq req) {
        return ActionResult.success("修改成功", ftbCultivateLabelService.updateData(id, req));
    }


    /**
     * 获取培训标签信息
     *
     * @param id 主键ID
     * @return 培训标签信息
     */
    @GetMapping("/get/{id}")
    public ActionResult<FtbCultivateLabelVo> getInfo(@PathVariable("id") String id) {
        return ActionResult.success(ftbCultivateLabelService.getInfo(id));
    }

    /**
     * 删除标签
     *
     * @param id 主键ID
     */
    @DeleteMapping("/{id}")
    public ActionResult<Void> delete(@PathVariable("id") String id) {
        ftbCultivateLabelService.deleteData(id);
        return ActionResult.success("删除成功");
    }
}