/*
 * 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.Source;
import com.tofly.base.entity.Sourcedic;
import com.tofly.base.service.SourceService;
import com.tofly.base.service.SourcedicService;
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("/sourcedic")
@Api(tags="系统资源配置字典表接口")
public class SourcedicController {

    private final  SourcedicService sourcedicService;
    
    private final SourceService sourceService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param sourcedic 系统资源配置字典表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getSourcedicPage(Page page, @ModelAttribute Sourcedic sourcedic) {
      return ResultRespone.success(sourcedicService.page(page, Wrappers.query(sourcedic)));
    }


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

     /*
     * 新增系统资源配置字典表
     * @param sourcedic 系统资源配置字典表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增系统资源配置字典表" )
    @ApiOperation(value = "新增系统资源配置字典表")
    @PostMapping
    public ResultRespone save(@RequestBody Sourcedic sourcedic) {
      return ResultRespone.success(sourcedicService.save(sourcedic));
    }

     /*
     * 修改系统资源配置字典表
     * @param sourcedic 系统资源配置字典表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改系统资源配置字典表" )
    @ApiOperation(value = "修改系统资源配置字典表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Sourcedic sourcedic) {
      return ResultRespone.success(sourcedicService.updateById(sourcedic));
    }

    /*
     * 通过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(sourcedicService.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<Source> sourceList=sourceService.list(Wrappers.<Source>lambdaQuery()
                    .eq(Source::getPid,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(sourcedicService.removeByIds(list));
        }else {
            return ResultRespone.failed("还存在资源在使用该字典，不能删除："+sb.toString());
        }
    }
}
