/*
 * 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.Departmentsource;
import com.tofly.base.entity.Source;
import com.tofly.base.service.DepartmentsourceService;
import com.tofly.base.service.SourceService;
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("/source")
@Api(tags="资源管理")
public class SourceController {

    private final  SourceService sourceService;

    private final DepartmentsourceService departmentsourceService;


    /**
    * 分页查询
    * @param page 分页对象
    * @param source 系统资源配置信息表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getSourcePage(Page page, @ModelAttribute Source source) {
      return ResultRespone.success(sourceService.page(page, Wrappers.query(source)));
    }

    /**
     * 列表查询
     * @param source 系统资源配置信息表
     * @return
     */
    @GetMapping("/list" )
    @ApiOperation(tags = {"部门管理","资源管理"},value = "资源列表")
    public ResultRespone getSourcePage(@ModelAttribute Source source) {
        return ResultRespone.success(sourceService.list(Wrappers.query(source)));
    }


    /**
     * 通过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(sourceService.getById(id));
    }

    /**
     * 新增系统资源配置信息表
     * @param source 系统资源配置信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增系统资源配置信息表" )
    @ApiOperation(value = "新增系统资源配置信息表")
    @PostMapping
    public ResultRespone save(@RequestBody Source source) {
      return ResultRespone.success(sourceService.save(source));
    }

    /**
     * 修改系统资源配置信息表
     * @param source 系统资源配置信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改系统资源配置信息表" )
    @ApiOperation(value = "修改系统资源配置信息表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Source source) {
      return ResultRespone.success(sourceService.updateById(source));
    }

    /**
     * 通过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) {
      return ResultRespone.success(sourceService.removeById(id));
    }

    /**
     * 通过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<Departmentsource> sourceList=departmentsourceService.list(Wrappers.<Departmentsource>lambdaQuery()
                    .eq(Departmentsource::getSourceId,Long.valueOf(id)));

            if(sourceList.size()>0){
                flag=false;
                sb.append("id:").append(id).append(",该资源还存在").append(sourceList.size())
                        .append("个单位正在使用;");
            }
            list.add(Long.valueOf(id));
        }
        if(flag){
            return ResultRespone.success(sourceService.removeByIds(list));
        }else {
            return ResultRespone.failed("还存在单位在使用该资源，不能删除："+sb.toString());
        }
    }
}
