package com.usky.iot.controller.web;


import com.usky.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.iot.domain.BaseBuild;
import com.usky.iot.domain.BaseGgpFacility;
import com.usky.iot.domain.DmpDeviceInfo;
import com.usky.iot.service.BaseGgpFacilityService;
import com.usky.iot.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地理信息 前端控制器
 * </p>
 *
 * @author han
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/baseGgpFacility")
public class BaseGgpFacilityController {
    @Autowired
    private BaseGgpFacilityService baseGgpFacilityService;

    /**
     * 新增
     * @param baseGgpFacility
     * @return
     */
    @PostMapping("/add")
    public ApiResult<Void> add(@RequestBody BaseGgpFacility baseGgpFacility){
        baseGgpFacilityService.add(baseGgpFacility);
        return ApiResult.success();
    }

    /**
     * 修改
     * @param baseGgpFacility
     * @return
     */
    @PutMapping("/edit")
    public ApiResult<Void> edit(@RequestBody BaseGgpFacility baseGgpFacility){
        baseGgpFacilityService.update(baseGgpFacility);
        return ApiResult.success();
    }


    /**
     * 分页
     * @param baseGgpFacilityRequest
     * @return
     */
    @Log(title = "分页", businessType = BusinessType.OTHER)
    @PostMapping("/page")
    public ApiResult<CommonPage<BaseGgpFacility>> page(@RequestBody BaseGgpFacilityRequest baseGgpFacilityRequest)
    {
        return ApiResult.success(baseGgpFacilityService.page(baseGgpFacilityRequest));
    }


    /**
     * 设备关联设施信息查询
     * @param deviceId
     * @return
     */
    @GetMapping("/facilityDeviceList")
    public ApiResult<List<BaseFacilityDeviceVO>> facilityDeviceList(@RequestParam(value = "deviceId") String deviceId,
                                                                    @RequestParam(value = "facilityId",required = false) Integer facilityId)
    {
        return ApiResult.success(baseGgpFacilityService.facilityDeviceList(deviceId,facilityId));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> remove(@PathVariable("id") Integer id)
    {
        baseGgpFacilityService.remove(id);
        return ApiResult.success();
    }

    /**
     * 导出
     * @param baseGgpFacilityRequest
     * @param response
     * @throws IOException
     */
    @PostMapping("/export")
    public void export(@RequestBody BaseGgpFacilityRequest baseGgpFacilityRequest, HttpServletResponse response) throws IOException {
        List<BaseGgpFacilityExportVo> list = baseGgpFacilityService.recordLsitExport(baseGgpFacilityRequest);
        ExcelUtil<BaseGgpFacilityExportVo> util = new ExcelUtil<BaseGgpFacilityExportVo>(BaseGgpFacilityExportVo.class);
        util.exportExcel(response, list, "地理设施", "地理设施");
    }
    /**
     * 设施详情查询
     *
     * @param id  主键ID
     * @return
     */
    @GetMapping("facilityInfo")
    public ApiResult<List<BaseGgpFacility>> facilityInfo(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        return ApiResult.success(baseGgpFacilityService.facilityInfo(id));
    }

    /**
     * 关联设备查看分页
     * @param deviceId  设备ID
     * @param id  设施主键ID
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @Log(title = "关联设备查看分页", businessType = BusinessType.OTHER)
    @GetMapping("/deviceInfo")
    public ApiResult<CommonPage<DmpDeviceInfo>> deviceInfo(@RequestParam(value = "deviceId", required = false) String deviceId,
                                                           @RequestParam(value = "id") Integer id,
                                                           @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size)
    {
        return ApiResult.success(baseGgpFacilityService.deviceInfo(deviceId, id, current, size));
    }

    /**
     * 关联设备查看分页
     * @param deviceId  设备ID
     * @param deviceType  设备类型
     * @param id  设施主键ID
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @Log(title = "关联设备绑定分页", businessType = BusinessType.OTHER)
    @GetMapping("/deviceBindInfo")
    public ApiResult<CommonPage<Object>> deviceBindInfo(@RequestParam(value = "deviceType", required = false) String deviceType,
                                                        @RequestParam(value = "deviceId", required = false) String deviceId,
                                                           @RequestParam(value = "id") Integer id,
                                                           @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size)
    {
        return ApiResult.success(baseGgpFacilityService.deviceBindInfo(deviceType,deviceId, id, current, size));
    }

    /**
     * 关联建筑分页
     * @param buildNum  建筑编号
     * @param id  设施主键ID
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @Log(title = "关联建筑分页", businessType = BusinessType.OTHER)
    @GetMapping("/buildInfo")
    public ApiResult<CommonPage<BaseBuild>> buildInfo(@RequestParam(value = "buildNum", required = false) String buildNum,
                                                      @RequestParam(value = "id") Integer id,
                                                      @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                      @RequestParam(value = "size", required = false, defaultValue = "10") Integer size)
    {
        return ApiResult.success(baseGgpFacilityService.buildInfo(buildNum, id, current, size));
    }

    /**
     * 地理信息-撒点
     *
     * @param baseGgpFacilityVO
     * @return
     */
    @PostMapping("baseGgpFacilityList")
    public ApiResult<List<BaseGgpFacility>> baseGgpFacilityList(@RequestBody BaseGgpFacilityVO baseGgpFacilityVO) {
        return ApiResult.success(baseGgpFacilityService.baseGgpFacilityList(baseGgpFacilityVO));
    }

        /**
         * 设施弹窗查询
         *
         * @param id  主键ID
         * @return
         */
        @GetMapping("facilityPopInfo")
        public ApiResult<List<Object>> facilityPopInfo(@RequestParam(value = "id") Integer id) {
            return ApiResult.success(baseGgpFacilityService.facilityPopInfo(id));
        }

    /**
     * 地理设施-导入
     *
     * @param file 导入文件
     * @return
     * @throws Exception
     */
    @Log(title = "地理设施", businessType = BusinessType.IMPORT)
    @PostMapping("/facilityImport")
    public ApiResult<Void> facilityImport(@RequestParam("file") MultipartFile file){
        baseGgpFacilityService.facilityImport(file);
        return ApiResult.success();
    }

}

