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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.bind.v2.model.core.ID;
import com.tofly.base.dto.DepartmentSouceDTO;
import com.tofly.base.entity.Departmentsource;
import com.tofly.base.service.DepartmentsourceService;
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.Comparator;
import java.util.Date;
import java.util.Map;

/**
 * 单位资源配置关联信息表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@AllArgsConstructor
@RequestMapping("/departmentsource")
@Api(tags = "单位资源配置关联信息表接口")
public class DepartmentsourceController {

    private final DepartmentsourceService departmentsourceService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param departmentsource 单位资源配置关联信息表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getDepartmentsourcePage(Page page, @ModelAttribute Departmentsource departmentsource) {
        return ResultRespone.success(departmentsourceService.page(page, Wrappers.query(departmentsource)));
    }


    /**
     * 资源列表
     *
     * @param departmentsource 单位资源配置关联信息表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(tags = {"部门管理"}, value = "单位资源列表")
    public ResultRespone getDepartmentsourcePage(@ModelAttribute Departmentsource departmentsource) {
        return ResultRespone.success(departmentsourceService.list(Wrappers.query(departmentsource)).stream().sorted(Comparator.comparing(Departmentsource::getCreateTime, Comparator.reverseOrder()).thenComparing(Departmentsource::getId, Comparator.reverseOrder())));
    }

    /**
     * 通过id查询单位资源配置关联信息表
     *
     * @param departmentId id
     * @return ResultRespone
     */
    @GetMapping("/{departmentId}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("departmentId") Long departmentId) {
        return ResultRespone.success(departmentsourceService.selectByDepartMent(departmentId));
    }

    /**
     * 新增单位资源配置关联信息表
     *
     * @param departmentsource 单位资源配置关联信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增单位资源配置关联信息表")
    @ApiOperation(value = "新增单位资源配置关联信息表")
    @PostMapping
    public ResultRespone save(@RequestBody Departmentsource departmentsource) {
        return ResultRespone.success(departmentsourceService.save(departmentsource));
    }

//    /**
//     * 修改单位资源配置关联信息表
//     * @param departmentsource 单位资源配置关联信息表
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="修改单位资源配置关联信息表" )
//    @ApiOperation(value = "修改单位资源配置关联信息表")
//    @PutMapping
//    public ResultRespone updateById(@RequestBody Departmentsource departmentsource) {
//      return ResultRespone.success(departmentsourceService.updateById(departmentsource));
//    }

    /**
     * 通过id删除单位资源配置关联信息表
     *
     * @param departmentId id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id删除单位资源配置关联信息表")
    @ApiOperation(value = "通过ID删除单位资源配置关联信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{departmentId}")
    public ResultRespone removeById(@PathVariable Long departmentId) {
        return ResultRespone.success(departmentsourceService.removeById(departmentId));
    }


    /**
     * 修改单位资源配置关联信息表
     *
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改单位资源配置关联信息表")
    @ApiOperation(tags = {"部门管理"}, value = "修改单位资源配置关联信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sourceIds", value = "资源ID，多个使用逗号(,)隔开", dataType = "String")
    })
    @PutMapping
    public ResultRespone updateById(@ApiParam(hidden = true) @RequestBody Map map) {
        return ResultRespone.success(
                departmentsourceService.updateDepartmentsource(Long.valueOf((String) (map.get("departmentId"))), (String) map.get("sourceIds")));
    }

    /**
     * 地图数据服务挂接
     *
     * @return
     */
    @ToFlyAppLog(title = "地图数据服务挂接")
    @ApiOperation(value = "地图数据服务挂接")
    @PostMapping("departmentConnectionSource")
    public ResultRespone departmentConnectionSource(@RequestBody DepartmentSouceDTO departmentSouceDTO) {
        Departmentsource departmentsource = new Departmentsource();
        departmentsource.setSourceId(departmentSouceDTO.getSourceId());
        departmentsource.setDepartmentId(departmentSouceDTO.getDepartmentId());
        departmentsource.setUserId(departmentSouceDTO.getUserId());
        departmentsource.setCreateTime(new Date());
        return ResultRespone.success(departmentsourceService.save(departmentsource));
    }
}
