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.EmpExamDo;
import com.rongwei.trainingcommon.sys.service.EmpExamService;
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/exam")
@Slf4j
public class EmpExamController {
    @Autowired
    private EmpExamService empExamService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody QueryPar<EmpExamDo> params){
        try {
            Wrapper<EmpExamDo> entity = new QueryWrapper(params.getParameter());
            List<EmpExamDo> list = empExamService.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 {
            EmpExamDo empExam = empExamService.getById(id);
            return R.ok(empExam);
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.queryError();
        }
    }

    /**
     * 保存or更新
     */
    @PostMapping("/saveOrUpdate")
    public R add(@RequestBody EmpExamDo empExam){
        try{
            empExamService.saveOrUpdate(empExam);
            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{
            empExamService.removeByIds(ids);
            return R.deleteOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.deleteError();
        }
    }

}
