/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.entity.Menu;
import com.tofly.base.entity.System;
import com.tofly.base.service.MenuService;
import com.tofly.base.service.SystemService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 子系统配置信息表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system")
@Api(tags="子系统管理")
public class SystemController {

    private final  SystemService systemService;

    private final MenuService menuService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param system 子系统配置信息表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getSystemPage(Page page, @ModelAttribute System system) {
      //return ResultRespone.success(systemService.page(page, Wrappers.query(system)));
        return ResultRespone.success(systemService.listSystemPage(page));
    }

    /**
     * 列表查询
     * @param system 子系统配置信息表
     * @return
     */
    @GetMapping("/list" )
    @ApiOperation(tags = {"用户管理","子系统管理"},value = "子系统列表")
    public ResultRespone getSystemPage(System system) {
        return ResultRespone.success(systemService.list(Wrappers.query(system)));
    }


    /**
     * 通过id查询子系统配置信息表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(systemService.getById(id));
    }

    /**
     * 新增子系统配置信息表
     * @param system 子系统配置信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增子系统配置信息表" )
    @ApiOperation(value = "新增子系统配置信息表")
    @PostMapping
    public ResultRespone save(@RequestBody System system) {
      return ResultRespone.success(systemService.save(system));
    }

    /**
     * 修改子系统配置信息表
     * @param system 子系统配置信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改子系统配置信息表" )
    @ApiOperation(value = "修改子系统配置信息表")
    @PutMapping
    public ResultRespone updateById(@RequestBody  System system) {
      return ResultRespone.success(systemService.updateById(system));
    }

    /**
     * 通过id删除子系统配置信息表
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除子系统配置信息表" )
    @ApiOperation(value = "通过ID删除子系统配置信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
        //非传统意义删除，修改禁用标志
        System system=new System();
        system.setId(id);
        system.setStatusFlag("0");
        return ResultRespone.success(systemService.updateById(system));
    }

    /**
     * 通过id删除菜单
     * @param ids id数组
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除子系统" )
    @ApiOperation(value = "通过ID删除子系统")
    @DeleteMapping("/removeByIds" )
    public ResultRespone removeByIds(@ApiParam(value = "通过ID拼接的字符串,以逗号隔开.如,'1,2,3,4,5,6,7",name = "ids",required = true) String ids) {
        String[] idss=ids.split(",");
        List<Long> list=new ArrayList();
        boolean flag=true;
        StringBuffer sb=new StringBuffer();
        for(String id:idss){
            //查询是否存在子菜单
            List<Menu> menuList=menuService.list(Wrappers.<Menu>lambdaQuery()
                    .eq(Menu::getSysId,Long.valueOf(id)));

            if(menuList.size()>0){
                flag=false;
                sb.append("id:").append(id).append(",对应子系统菜单还存在").append(menuList.size())
                        .append("条数据;");
            }
            list.add(Long.valueOf(id));
        }
        if(flag){

            return ResultRespone.success(systemService.removeByIds(list));
        }else {
            return ResultRespone.failed("还存在子系统菜单，不能删除："+sb.toString());
        }
    }

   }
