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.CheckItemsPartsDo;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.CheckItemsPartsService;
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-08-04 18:39:10
 */
@RestController
@RequestMapping("checkItemsParts")
@Slf4j
public class CheckItemsPartsController {
    @Autowired
    private CheckItemsPartsService checkItemsPartsService;

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

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

    /**
     * 定时检查设备配件是否过期
     * 如过期则发送邮件提醒以及生成隐患跟踪任务
     *
     * @return
     */
    @PostMapping("/partExpireWarn")
    public R partExpireWarn(@RequestBody Map<String, Object> map) {
        try {
            checkItemsPartsService.partExpireWarn();
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

}
