package com.rongwei.savecheck.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rongwe.scentity.domian.CheckTemplateDo;
import com.rongwe.scentity.domian.CheckTemplateItemsDo;
import com.rongwei.rwcommon.base.QueryPar;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.CheckItemContentService;
import com.rongwei.sfcommon.sys.service.CheckTemplateItemsService;
import com.rongwei.sfcommon.sys.service.CheckTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
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("/checkTemplate")
@Slf4j
public class CheckTemplateController {
    @Autowired
    private CheckTemplateService checkTemplateService;
    @Autowired
    private CheckTemplateItemsService checkTemplateItemsService;
    @Autowired
    private CheckItemContentService checkItemContentService;

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

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

    }

    @PostMapping("/updateStatusByHiId")
    public R updateStatusByHiId(@RequestBody Map<String, Object> map) {
        try {
            checkTemplateService.updateStatusByHiId(map);
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

    @PostMapping("/templateItemsDeleteResolve")
    public R templateItemsDeleteResolve(@RequestBody List<String> ids) {
        try {
            checkTemplateItemsService.templateItemsDeleteResolve(ids);
            return R.ok();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

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

    /**
     * 有任务的模板不能删除
     */
    @PostMapping("/hasTaskDelete")
    public R hasTaskDelete(@RequestBody List<String> ids) {
        try {
            checkTemplateService.removeByIds(ids);
            return R.deleteOk();
        } catch (Exception e) {
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.deleteError();
        }
    }

    /**
     * 点检任务生成
     * 定时
     *
     * @return
     */
    @Scheduled(cron = "0 5 0 * * ?")
    @PostMapping("/point/check/task")
    public R pointCheckCreate() {
        checkTemplateService.pointCheckCreate(null);
        return R.ok();
    }

    /**
     * 点检任务生成
     * 非定时
     *
     * @param map
     * @return
     */
    @PostMapping("/pointCheckCreate")
    public R pointCheckCreate(@RequestBody(required = false) Map<String, Object> map) {
        log.info("生成点检任务参数为:{}", map);
        try {
            checkTemplateService.pointCheckCreate(map);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StringUtils.spliceErrorMsg(e), e.fillInStackTrace());
            return R.error();
        }
    }

    /**
     * 根据模板生成检查任务
     *
     * @param id   安全检查模板id
     * @param mode 是否忽略检查项的频次 0:不忽略 1：忽略
     * @return
     */
    @PostMapping("/create/check/task/{id}/{mode}")
    public R createCheckPlan(@PathVariable(name = "id") String id,
                             @PathVariable(name = "mode") Boolean mode) {
        log.info("开始根据模板:{}创建检查任务", id);
        return checkTemplateService.createCheckPlanByTempId(id, mode);
    }






    /**
     * 查询检查内容是否存在被未删除的模板引用
     * 如未存在则直接删除，否则弹框告知
     */
    @PostMapping("/existTemplateItem")
    public R getItemByContentId(@RequestBody List<String> ids) {
        try {
            List<CheckTemplateItemsDo> list = checkTemplateItemsService.getItemByContentId(ids);
            if (list == null || list.isEmpty()) {
                checkItemContentService.removeByIds(ids);
                return R.ok("canDelete");
            } else {
                return R.ok("notDelete");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

}
