package org.jeecg.modules.warning.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.warning.entity.AConfigure;
import org.jeecg.modules.warning.entity.ATemplate;
import org.jeecg.modules.warning.entity.ATrack;
import org.jeecg.modules.warning.model.chartTemplateTreeModel;
import org.jeecg.modules.warning.service.IAConfigureService;
import org.jeecg.modules.warning.service.IATemplateService;
import org.jeecg.modules.warning.service.IATrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 模板 前端控制器
 * <p>
 *
 * @Author:
 */
@RestController
@RequestMapping("/warning/template")
@Slf4j
public class ATemplateController {

    @Autowired
    private IATemplateService iaTemplateService;
    @Autowired
    private IATrackService iaTrackService;
    @Autowired
    private IAConfigureService iaConfigureService;

    /**
     * 获取模板树
     *
     * @return
     */
    @RequestMapping(value = "/getChartTreeList", method = RequestMethod.GET)
    public Result<List<chartTemplateTreeModel>> getChartTreeList(String id) {
        Result<List<chartTemplateTreeModel>> result = new Result<>();
        result.setResult(iaTemplateService.getMyChartTreeList(id));
        return result;
    }

    /**
     * 获取模板
     *
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Result<List<ATemplate>> getList(ATemplate aTemplate, HttpServletRequest req) {
        Result<List<ATemplate>> result = new Result<List<ATemplate>>();
        // 20211218 TODO: do cas login
        LoginUser user = new LoginUser(); //        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<ATemplate> queryWrapper = QueryGenerator.initQueryWrapper(aTemplate, req.getParameterMap());
//        queryWrapper.eq("CREATE_BY", user.getId());
        queryWrapper.eq("is_default", "Y");
        queryWrapper.orderByDesc("create_time");
        List<ATemplate> templateList = iaTemplateService.list(queryWrapper);
        result.setSuccess(true);
        result.setResult(templateList);
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<ATemplate> add(@RequestBody ATemplate aTemplate) {
        Result<ATemplate> result = new Result<ATemplate>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if(user==null){
            aTemplate.setCreateBy("1");
        }else{
            aTemplate.setCreateBy(user.getId());
        }

        aTemplate.setCreateTime(new Date());
        try {
            iaTemplateService.save(aTemplate);
            result.success("保存成功！");
            result.setResult(aTemplate);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<ATemplate> edit(@RequestBody ATemplate aTemplate) {
        Result<ATemplate> result = new Result<ATemplate>();
        ATemplate aTemplate1 = iaTemplateService.getById(aTemplate.getId());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            aTemplate.setCreateBy("1");
        }else{
            aTemplate.setCreateBy(user.getId());
        }

        if (aTemplate1 == null) {
            result.error500("未找到对应实体");
        } else {
            aTemplate.setUpdateBy(user.getId());
            aTemplate.setUpdateTime(new Date());
            boolean ok = iaTemplateService.updateById(aTemplate);
            if (ok) {
                result.success("编辑成功!");
            }
        }
        return result;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @Transactional
    public Result<ATemplate> delete(@RequestParam(name = "id", required = true) String id) {
        Result<ATemplate> result = new Result<ATemplate>();
        boolean ok = iaTemplateService.removeById(id);
        Map<String, Object> delMap = new HashMap<>();
        delMap.put("TEMPLATE_ID", id);
        iaTrackService.removeByMap(delMap);
        iaConfigureService.removeByMap(delMap);
        if (ok) {
            result.success("删除成功!");
        } else {
            result.error500("删除失败!");
        }
        return result;
    }

}
