package jnpf.cultivate.controller.web.teaching;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.TeachingSkillService;
import jnpf.model.cultivate.dto.teaching.TeachingSkillAddDto;
import jnpf.model.cultivate.dto.teaching.TeachingSkillPageDto;
import jnpf.model.cultivate.dto.teaching.TeachingSkillSortDto;
import jnpf.model.cultivate.dto.teaching.TeachingSkillUpdateDto;
import jnpf.model.cultivate.vo.teaching.SkillInfoVo;
import jnpf.model.cultivate.vo.teaching.TeachingSkillVo;
import jnpf.util.FtbUtil;
import jnpf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 技能点配置管理
 */
@RestController
@RequestMapping("/teachingSkill")
public class TeachingSkillController {

    @Autowired
    private TeachingSkillService teachingSkillService;

    /**
     * 新增
     *
     * @param addDto 技能点DTO
     * @return 是否成功
     */
    @PostMapping("/add")
    public ActionResult<Boolean> add(@Valid @RequestBody TeachingSkillAddDto addDto) {

        if (StringUtil.isNotBlank(addDto.getCategoryId()) && "-1".equals(addDto.getCategoryId())) {
            addDto.setCategoryId(null);
        }
        return ActionResult.success(teachingSkillService.addData(addDto));
    }

    /**
     * 删除
     *
     * @param id 删除参数
     * @return 是否成功
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Boolean> delete(@PathVariable("id") String id) {
        return ActionResult.success(teachingSkillService.deleteData(id));
    }

    /**
     * 修改
     *
     * @param updateDto 技能点DTO
     * @return 是否成功
     */
    @PutMapping("/update")
    public ActionResult<Boolean> update(@Valid @RequestBody TeachingSkillUpdateDto updateDto) {

        if (StringUtil.isNotBlank(updateDto.getCategoryId()) && "-1".equals(updateDto.getCategoryId())) {
            updateDto.setCategoryId(null);
        }
        return ActionResult.success(teachingSkillService.updateData(updateDto));
    }

    /**
     * 查询
     *
     * @param id 查询参数
     * @return 技能点VO
     */
    @GetMapping("/get/{id}")
    public ActionResult<TeachingSkillVo> get(@PathVariable("id") String id) {
        return ActionResult.success(teachingSkillService.getData(id));
    }

    /**
     * 分页列表
     *
     * @param pageDto 分页查询参数
     * @return 技能点集合
     */
    @GetMapping("/pageList")
    public ActionResult<PageListVO<TeachingSkillVo>> pageList(@Valid TeachingSkillPageDto pageDto) {
        PageInfo<TeachingSkillVo> page = teachingSkillService.pageList(pageDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * 下拉列表
     *
     * @return 技能点集合
     */
    @GetMapping("/selectDataList")
    public ActionResult<List<TeachingSkillVo>> selectDataList() {
        return ActionResult.success(teachingSkillService.selectDataList());
    }

    /**
     * 排序
     *
     * @param sortDto 数据集合
     * @return 是否成功
     */
    @PutMapping("/sort")
    public ActionResult<Boolean> sort(@Valid @RequestBody TeachingSkillSortDto sortDto) {
        return ActionResult.success(teachingSkillService.sort(sortDto));
    }

    /**
     * 查询技能点[树形]
     * @return jnpf.base.ActionResult<java.util.List<jnpf.model.cultivate.vo.teaching.SkillInfoVo>>
     */
    @GetMapping(value = "/all-list")
    public ActionResult<List<SkillInfoVo>> getAllSkillList() {

        List<SkillInfoVo> list = teachingSkillService.getAllSkillList();
        return ActionResult.success(list);
    }
}