/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.basicdata.rest;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.inputJson.CurrentPage;
import com.youchain.basicdata.domain.Area;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.service.AreaService;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.dto.AreaDto;
import com.youchain.basicdata.service.dto.PointQueryCriteria;
import com.youchain.config.FileProperties;
import com.youchain.exception.handler.ApiResult;
import com.youchain.modules.system.domain.Dept;
import com.youchain.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

/**
 * @author liuxue
 * @website https://eladmin.vip
 * @date 2023-07-26
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "ponit管理")
@Slf4j
@RequestMapping("/api/point")
public class PointController {

    private final PointService pointService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('point:list')")
    public void exportPoint(HttpServletResponse response, PointQueryCriteria criteria) throws IOException {
        pointService.download(pointService.queryAll(criteria), response);
    }

    @Log("导入点位")
    @ApiOperation("导入点位")
    @PostMapping(value = "/import_point")
    @PreAuthorize("@el.check('point:importPoint')")
    public ResponseEntity<Object> importPoint(@RequestParam("file") MultipartFile multipartFile) {
        try {
            String result =pointService.importPoint(multipartFile);
            return successRequest(result,null);
        } catch (Exception e) {
            return badRequest("导入失败：" + e.getMessage());
        }

    }

    @GetMapping
    @ApiOperation("查询point")
    public ResponseEntity<Object> queryPoint(PointQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(pointService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/queryPointAll")
    @ApiOperation("查询point")
    public ResponseEntity<Object> queryPointAll(PointQueryCriteria criteria) {
        return new ResponseEntity<>(pointService.queryAll(criteria), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增point")
    @ApiOperation("新增point")
    @PreAuthorize("@el.check('point:add')")
    public ResponseEntity<Object> createPoint(@Validated @RequestBody Point resources) {
        resources.setDept(UserUtils.getDept());
        return new ResponseEntity<>(pointService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改point")
    @ApiOperation("修改point")
    @PreAuthorize("@el.check('point:edit')")
    public ResponseEntity<Object> updatePoint(@Validated @RequestBody Point resources) {
        pointService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除point")
    @ApiOperation("删除point")
    @PreAuthorize("@el.check('point:del')")
    public ResponseEntity<Object> deletePoint(@RequestBody Long[] ids) {
        pointService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    private ResponseEntity<Object> badRequest(String message) {
        return new ResponseEntity<>(ApiResult.fail(BAD_REQUEST.value(), message, ""), HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<Object> successRequest(String message, Object object) {
        return new ResponseEntity<>(ApiResult.fail(OK.value(), message, object), HttpStatus.OK);
    }

}
