package com.mosty.bkzx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.model.dto.bkzx.TbBkDbBkDto;
import com.mosty.base.model.dto.bkzx.TbBkDbInsertDto;
import com.mosty.base.model.dto.bkzx.TbBkDbSearchDto;
import com.mosty.base.model.dto.bkzx.TbBkDbUpDto;
import com.mosty.base.model.entity.bkzx.TbBkDb;
import com.mosty.bkzx.service.TbBkDbService;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "布控库管理服务")
@RestController
@AllArgsConstructor
@RequestMapping("/tbBkDb")
public class TbBkDbController {
    private final TbBkDbService tbBkDbService;

    /**
     * 新建布控数据库信息
     *
     * @return
     */
    @ApiOperation("新建布控库")
    @JwtSysUser
    @Log(title = "新建各市布控数据库", businessType = BusinessType.INSERT)
    @PostMapping("/builderDb")
    public ResponseResult<?> builderDb(@RequestBody TbBkDbInsertDto dto) {
        if (null == dto) {
            throw new BusinessException("新建布控数据库信息无效！");
        }
        dto.checkData();
        tbBkDbService.builderDb(dto);
        return ResponseResult.success("创建布控数据库成功！");
    }

    @ApiOperation("删除布控数据库")
    @JwtSysUser
    @Log(title = "布控数据库删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public ResponseResult<?> delDb(@RequestBody List<Long> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            throw new BusinessException("删除布控数据库记录集合无效！");
        }
        tbBkDbService.delDb(keys);
        return ResponseResult.success();
    }

    @ApiOperation("布控数据库分页查询")
    @JwtSysUser
    @GetMapping("/pagelist")
    public ResponseResult<IPage<TbBkDb>> getPageList(TbBkDbSearchDto repar) {
        if (null == repar) {
            throw new BusinessException("布控数据库分页查询参数无效！");
        }
        return ResponseResult.success(tbBkDbService.getPageList(repar));
    }

    @ApiOperation("建库的区域选择")
    @JwtSysUser
    @GetMapping("/arelist")
    public ResponseResult<List<Map>> getAreList() {
        return ResponseResult.success(tbBkDbService.getAreList());
    }

    @ApiOperation("布控数据详情信息")
    @JwtSysUser
    @GetMapping("/detail")
    public ResponseResult<TbBkDb> detail(@RequestParam(name = "id", required = true) String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("数据库主键数据无效！");
        }
        return ResponseResult.success(tbBkDbService.getDetail(id));
    }

    @ApiOperation("对大华数据库进行布控")
    @JwtSysUser
    @PostMapping("/bkdb")
    public ResponseResult<?> bKDb(@RequestBody TbBkDbBkDto reqPar) {
        if (null == reqPar || null == reqPar.getId() || reqPar.getBkKssj() == null || reqPar.getBkJssj() == null) {
            throw new BusinessException("布控数据库参数异常，请核查 ！");
        }
        return ResponseResult.success(tbBkDbService.bKDb(reqPar));
    }

    @ApiOperation("对数据库布控进行撤销布控操作")
    @JwtSysUser
    @PostMapping("/unbkdb")
    public ResponseResult<?> unBkDb(@RequestBody List<Long> id) {
        if (CollectionUtils.isEmpty(id)) {
            throw new BusinessException("撤销布控数据库信息无效！");
        }
        return ResponseResult.success(tbBkDbService.unBkDb(id));
    }

    @ApiOperation("编辑数据库基础信息")
    @JwtSysUser
    @PostMapping("/modify")
    public ResponseResult<?> modify(@RequestBody TbBkDbUpDto upDto) {
        if (null == upDto || StringUtils.isEmpty(upDto.getId())) {
            throw new BusinessException("修改参数无效！");
        }
        return ResponseResult.success(tbBkDbService.modify(upDto));
    }
}
