package org.jeecg.modules.subwayNetwork.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.PageSearch;
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.subwayNetwork.dto.LightRailQueryDTO;
import org.jeecg.modules.subwayNetwork.entity.LightRail;
import org.jeecg.modules.subwayNetwork.service.ILightRailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.subwayNetwork.vo.LightRailQueryVO;
import org.jeecg.modules.subwayNetwork.vo.SectionStationMapVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 线路车站-轻轨线路
 * @Author: jeecg-boot
 * @Date: 2023-06-14
 * @Version: V1.0
 */
@Api(tags = "线路车站-轻轨线路")
@RestController
@RequestMapping("/subwayNetwork/lightRail")
@Slf4j
public class LightRailController extends JeecgController<LightRail, ILightRailService> {
    @Autowired
    private ILightRailService lightRailService;

    @AutoLog(value = "线路车站-轻轨线路-分页列表查询")
    @ApiOperation(value = "线路车站-轻轨线路-分页列表查询", notes = "线路车站-轻轨线路-分页列表查询")
    @PostMapping(value = "/list")
    public Result<IPage<LightRailQueryVO>> queryPageList(@RequestBody PageSearch<LightRailQueryDTO> dto) {

        IPage<LightRailQueryVO> pageList = lightRailService.queryPageList(dto);
        return Result.OK(pageList);
    }


    @AutoLog(value = "线路车站-轻轨线路-编辑")
    @ApiOperation(value = "线路车站-轻轨线路-编辑", notes = "线路车站-轻轨线路-编辑")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody LightRail lightRail) {
        //获取当前用户
        lightRail.setUpdateTime(new Date());
        if (ObjectUtil.isEmpty(lightRail.getId())) {
            lightRail.setCreateTime(new Date());
            lightRailService.save(lightRail);
        } else {
            lightRailService.updateById(lightRail);
        }

        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "线路车站-轻轨线路-通过id删除")
    @ApiOperation(value = "线路车站-轻轨线路-通过id删除", notes = "线路车站-轻轨线路-通过id删除")
    @GetMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        lightRailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "线路车站-轻轨线路-批量删除")
    @ApiOperation(value = "线路车站-轻轨线路-批量删除", notes = "线路车站-轻轨线路-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.lightRailService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "线路车站-轻轨线路-通过id查询")
    @ApiOperation(value = "线路车站-轻轨线路-通过id查询", notes = "线路车站-轻轨线路-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LightRail> queryById(@RequestParam(name = "id", required = true) String id) {
        LightRail lightRail = lightRailService.getById(id);
        if (lightRail == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lightRail);
    }

    @AutoLog(value = "线路车站-轻轨线路-获取全部线路")
    @ApiOperation(value = "线路车站-轻轨线路-获取全部线路", notes = "线路车站-轻轨线路-获取全部线路")
    @GetMapping(value = "/listAll")
    public Result<List<Map<String, Object>>> listAll() {
        List<LightRail> pageList = lightRailService.list();

        List<Map<String, Object>> list = new ArrayList<>();
        for (LightRail lightRail : pageList) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", lightRail.getRailLineName());
            map.put("value", lightRail.getId() );
            list.add(map);
        }
        return Result.OK(list);
    }


    @AutoLog(value = "线路车站-线别管理-获取区间车站映射")
    @ApiOperation(value = "线路车站-线别管理-获取区间车站映射", notes = "线路车站-线别管理-获取区间车站映射")
    @GetMapping(value = "/getSectionStation")
    public Result<List<SectionStationMapVO>> getSectionStation() {
        return Result.OK(lightRailService.getSectionStation());
    }
}
