package com.rongwei.savecheck.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rongwe.scentity.domian.PointCheckDo;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.PointCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rongwei.rwcommon.base.QueryPar;
import com.rongwei.rwcommon.base.R;
import lombok.extern.slf4j.Slf4j;

/**
 * 点检任务表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-07-17 09:33:23
 */
@RestController
@RequestMapping("pointCheck")
@Slf4j
public class PointCheckController {
    @Autowired
    private PointCheckService pointCheckService;

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

    /**
     * 保存or更新
     */
    @PostMapping("/saveOrUpdate")
    public R add(PointCheckDo pointCheck) {
        try {
            pointCheckService.saveOrUpdate(pointCheck);
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }

    }

    @PostMapping("/batchUpdate")
    public R batchUpdate(@RequestBody List<PointCheckDo> pointChecks) {
        try {
            pointCheckService.updateBatchById(pointChecks);
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }
    }

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

    /**
     * 点检保存生成隐患跟踪任务
     *
     * @param map
     * @return
     */
    @PostMapping("/createHiddenDangerTask")
    public R createHiddenDangerTask(@RequestBody Map<String, Object> map) {
        try {
            pointCheckService.createHiddenDangerTask(map);
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }
    }

}
