package com.rongwei.savecheck.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rongwe.scentity.vo.AspAccidentReportVo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.AccidentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 事故表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-07-17 09:33:23
 */
@RestController
@RequestMapping("accident")
@Slf4j
public class AccidentController {
    @Autowired
    private AccidentService accidentService;

    /**
     * 生成事故表编号
     *
     * @param map
     * @return
     */
    @PostMapping("/code")
    public R code(@RequestBody Map<String, Object> map) {
        try {
            accidentService.getCode(map);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

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

    /**
     * 事故表word导出
     *
     * @param map
     * @return
     */
    @PostMapping("/word")
    public R word(@RequestBody Map<String, Object> map) {
        try {
            R r = accidentService.word(map);
            return R.ok(r);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

    /**
     * 定时任务生成每月的工伤月度报表
     */
    @PostMapping("/report")
    public R report() {
        try {
            return accidentService.report();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

    /**
     * 查询工伤率报表
     */
    @PostMapping("/selectReport")
    public R selectReport(@RequestParam(required = true) Integer year,
                          @RequestParam(required = false) String tenantId) {
        try {
            List<AspAccidentReportVo> r = accidentService.selectReport(year, tenantId);
            IPage<AspAccidentReportVo> res = new Page<>();
            res.setRecords(r);
            return R.ok(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }
}
