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.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.safecommon.utils.JSCommonUtils;
import com.rongwei.training.domain.EmpExamDetailDo;
import com.rongwei.training.domain.EmpExamDo;
import com.rongwei.training.vo.EmpExamDetailVo;
import com.rongwei.trainingcommon.sys.service.EmpExamDetailService;
import com.rongwei.trainingcommon.sys.service.EmpExamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
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/detail")
@Slf4j
public class EmpExamDetailController {
    @Autowired
    private EmpExamDetailService empExamDetailService;
    @Autowired
    private EmpExamService empExamService;

    @PostMapping("/saveEmpExam")
    public R saveEmpExam(@RequestBody EmpExamDetailVo empExamDetailVo) {
        try {
            EmpExamDetailDo empExamDetail = new EmpExamDetailDo();
            BeanUtils.copyProperties(empExamDetail, empExamDetailVo);
            empExamDetail.setId(SecurityUtil.getUUID());
            String factoryId = JSCommonUtils.getCurrentUserFactoryId(null);
            empExamDetail.setTenantid(factoryId);
            empExamDetailService.save(empExamDetail);
            // 更新考试状态
            EmpExamDo empExamDo = empExamService.getById(empExamDetail.getEmpexamid());
            empExamDo.setExamstatus(empExamDetailVo.getExamstatus());
            empExamService.updateById(empExamDo);
            return R.saveOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }
    }

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

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

}
