package com.izouma.nineth.web;

import com.izouma.nineth.domain.MetaSpatialInfo;
import com.izouma.nineth.dto.MetaRestResult;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.MetaSpatialInfoRepo;
import com.izouma.nineth.service.MetaSpatialInfoService;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/metaSpatialInfo")
@AllArgsConstructor
public class MetaSpatialInfoController extends BaseController {
    private MetaSpatialInfoService metaSpatialInfoService;
    private MetaSpatialInfoRepo metaSpatialInfoRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public MetaSpatialInfo save(@RequestBody MetaSpatialInfo record) {
        MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByRegionAndCoordinateAndDel(record.getRegion(), record.getCoordinate(), false);
        if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
            throw new BusinessException("当前区域内已经存在该坐标的空间");
        }
        if (record.getId() != null) {
            MetaSpatialInfo orig = metaSpatialInfoRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return metaSpatialInfoRepo.save(orig);
        }
        return metaSpatialInfoRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<MetaSpatialInfo> all(@RequestBody PageQuery pageQuery) {
        return metaSpatialInfoService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public MetaSpatialInfo get(@PathVariable Long id) {
        return metaSpatialInfoRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        metaSpatialInfoRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<MetaSpatialInfo> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/{userId}/find")
    public MetaRestResult<List<MetaSpatialInfo>> findByUserId(@PathVariable Long userId) {
        List<MetaSpatialInfo> metaSpatialInfos = metaSpatialInfoRepo.findAllByUserIdAndDel(userId, false);
        return MetaRestResult.returnSuccess(metaSpatialInfos);
    }

    @GetMapping("/onSale")
    public MetaRestResult<Integer> findOnSale() {
        int onSale = metaSpatialInfoRepo.countBySaleAndDel(true, false);
        return MetaRestResult.returnSuccess(onSale);
    }

    @GetMapping("/{id}/detail")
    public MetaRestResult<MetaSpatialInfo> detail(@PathVariable Long id) {
        MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findById(id).orElse(null);
        if (Objects.isNull(metaSpatialInfo)) {
            return MetaRestResult.returnError(String.format("没有id为：%S的空间信息", id));
        }
        return MetaRestResult.returnSuccess(metaSpatialInfo);
    }
}

