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.ThemeCheckDo;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.ThemeCheckItemService;
import com.rongwei.sfcommon.sys.service.ThemeCheckService;
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("themeCheck")
@Slf4j
public class ThemeCheckController {
    @Autowired
    private ThemeCheckService themeCheckService;
    @Autowired
    private ThemeCheckItemService themeCheckItemService;

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

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

    /**
     * 日常检查新增保存生成检查任务发送邮件
     * @param themeCheckDo
     * @return
     */
    @PostMapping("/themeCheckSplit")
    public R themeCheckSplit(@RequestBody ThemeCheckDo themeCheckDo){
        try{
            themeCheckService.sendMailByCheckId(themeCheckDo.getId());
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.saveError();
        }
    }

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

    /**
     * 工段主管每天检查定时任务生成
     * @param map
     * @return
     */
    @PostMapping("/dayCheckCreateTask")
    public R dayCheckCreateTask(@RequestBody Map<String,Object> map){
        log.info("工段主管每天检查定时生成任务开始...");
        try{
            themeCheckService.dayCheckCreateTask();
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }



    /**
     * 每周四 向用户发送提醒邮件 安全观察
     * @param map
     * @return
     */
    @PostMapping("/weekChecksafeTask")
    public R weekChecksafeTask(@RequestBody Map<String,Object> map){
        log.info("每周四定时向用户发送提醒邮件...");
        try{
            themeCheckService.weekChecksafeTask();
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

}
