package com.rongwei.training.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rongwei.rwcommon.base.QueryPar;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.training.domain.EmpCourseDo;
import com.rongwei.trainingcommon.sys.service.EmpCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工课程记录表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-09-07 17:59:22
 */
@RestController
@RequestMapping("emp/course")
@Slf4j
public class EmpCourseController {
    @Autowired
    private EmpCourseService empCourseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody QueryPar<EmpCourseDo> params) {
        try {
            Wrapper<EmpCourseDo> entity = new QueryWrapper(params.getParameter());
            List<EmpCourseDo> list = empCourseService.list(entity);
            return R.ok(list);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.queryError();
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        try {
            EmpCourseDo empCourse = empCourseService.getById(id);
            return R.ok(empCourse);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.queryError();
        }
    }

    /**
     * 保存or更新
     */
    @PostMapping("/saveOrUpdate")
    public R add(@RequestBody EmpCourseDo empCourse) {
        try {
            empCourseService.saveOrUpdate(empCourse);
            return R.saveOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }

    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody List<String> ids) {
        try {
            empCourseService.removeByIds(ids);
            return R.deleteOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.deleteError();
        }
    }

}
