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.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.warning.entity.AConfigure;
import org.jeecg.modules.warning.entity.ATrack;
import org.jeecg.modules.warning.model.chartConfigureModel;
import org.jeecg.modules.warning.service.IAConfigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 配置 前端控制器
 * <p>
 *
 * @Author:
 */
@RestController
@RequestMapping("/warning/configure")
@Slf4j
public class AConfigureController {

    @Autowired
    private IAConfigureService iaConfigureService;

    /**
     * 新增曲线
     * @param aConfigure
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<AConfigure> add(@RequestBody AConfigure aConfigure)
    {
        Result<AConfigure> result = new Result<AConfigure>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<AConfigure> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("TRACK_ID",aConfigure.getTrackId());
        queryWrapper.orderByDesc("SORT_CODE");
        List<AConfigure> aConfigureList=iaConfigureService.list(queryWrapper);
        if (aConfigureList.size()>0){
            aConfigure.setSortCode(aConfigureList.get(0).getSortCode().add(new BigDecimal(8)));
        }else{
            aConfigure.setSortCode(new BigDecimal(2));
        }
        try {
            aConfigure.setCreateBy("1");
            aConfigure.setCreateTime(new Date());
            iaConfigureService.save(aConfigure);
            result.success("保存成功！");
            result.setResult(aConfigure);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return  result;
    }

    /**
     * 修改
     * @param aConfigure
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<AConfigure> edit(@RequestBody AConfigure aConfigure)
    {
        Result<AConfigure> result = new Result<AConfigure>();
        AConfigure aConfigure1=iaConfigureService.getById(aConfigure.getId());
        if(aConfigure1==null){
            result.error500("未找到对应实体");
        }else{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            aConfigure.setUpdateTime(new Date());
            aConfigure.setUpdateBy("1");
            boolean ok=iaConfigureService.updateById(aConfigure);
            if(ok) {
                result.success("编辑成功!");
            }
        }
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<AConfigure> delete (@RequestParam(name="id",required=true) String id){
        Result<AConfigure> result = new Result<AConfigure>();
        boolean ok = iaConfigureService.removeById(id);
        if(ok) {
            result.success("删除成功!");
        }else{
            result.error500("删除失败!");
        }
        return result;
    }

    /**
     * 数据源验重
     * @param id
     * @param dataSource
     * @return
     */
    @RequestMapping(value = "/doDuplicateCheck",method =RequestMethod.GET)
    public Result<Object> doDuplicateCheck(String trackId,String id, String dataSource)
    {
        QueryWrapper<AConfigure> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("TRACK_ID",trackId);
        List<AConfigure> list=iaConfigureService.list(queryWrapper);
        boolean duplicate=false;
        for (AConfigure item:list)
        {
            if (item.getDataSource().equals(dataSource)&&!item.getId().equals(id))
            {
                duplicate=true;
                break;
            }
        }
        if (!duplicate)
        {
            return Result.ok("该值可用！");
        }
        else
        {
            return Result.error("该值不可用，系统中已存在！");
        }
    }
}
