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.warning.entity.AConfigure;
import org.jeecg.modules.warning.entity.ATrack;
import org.jeecg.modules.warning.model.addTrackDto;
import org.jeecg.modules.warning.model.chartConfigureModel;
import org.jeecg.modules.warning.model.lineMoveDto;
import org.jeecg.modules.warning.service.IAConfigureService;
import org.jeecg.modules.warning.service.IATrackService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.beans.Transient;
import java.math.BigDecimal;
import java.util.*;


/**
 * <p>
 * 曲线道 前端控制器
 * <p>
 *
 * @Author:
 */
@RestController
@RequestMapping("/warning/track")
@Slf4j
public class ATrackController {

    @Autowired
    IATrackService iaTrackService;
    @Autowired
    private IAConfigureService iaConfigureService;

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

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

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

    /**
     * 曲线拖动排序
     * @param lineMoveDto
     * @return
     */
    @RequestMapping(value = "/moveLine", method = RequestMethod.POST)
    public Result<String> moveLine(@RequestBody lineMoveDto lineMoveDto){
        Result<String> result = new Result<String>();
        try{
            iaTrackService.moveLine(lineMoveDto);
            result.success("成功");
        }catch(Exception e){
            result.error500("保存失败");
        }
        return result;
    }
    @RequestMapping(value = "/addByDirection", method = RequestMethod.POST)
    public Result<String> addByDirection(@RequestBody addTrackDto addTrackDto){
        Result<String> result = new Result<String>();
        try{
            iaTrackService.addByDirection(addTrackDto);
            result.success("成功");
        }catch(Exception e){
            System.out.println(e);
            result.error500("保存失败");
        }
        return result;
    }
}
