package com.ecep.contract.ds.other.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ecep.contract.cloud.u8.YongYouU8Service;
import com.ecep.contract.ds.other.model.CloudYu;
import com.ecep.contract.vo.CloudYuVo;

@RestController
@RequestMapping("/cloud/yu")
public class CloudYuController {
    @Autowired
    private YongYouU8Service yongYouU8Service;

    @RequestMapping("/{id:\\d+}")
    public CloudYuVo findById(@PathVariable Integer id) {
        return yongYouU8Service.findById(id);
    }

    @RequestMapping("/list")
    public Page<CloudYuVo> list(
            Map<String, Object> params,
            @RequestParam(defaultValue = "0", name = "page") int pageNumber,
            @RequestParam(defaultValue = "10", name = "size") int pageSize) {
        Sort sort = Sort.by(Sort.Order.desc("id"));
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);
        return yongYouU8Service.findAll((Specification<CloudYu>) null, pageable).map(CloudYu::toVo);
    }

    @PostMapping("/save")
    public CloudYu save(CloudYuVo cloudYuVo) {
        CloudYu cloudYu = yongYouU8Service.getById(cloudYuVo.getId());
        yongYouU8Service.updateByVo(cloudYu, cloudYuVo);
        return yongYouU8Service.save(cloudYu);
    }

    @PostMapping("/delete/{id:\\d+}")
    public void delete(@PathVariable Integer id) {
        if (id == null) {
            return;
        }
        CloudYu cloudYu = yongYouU8Service.getById(id);
        yongYouU8Service.delete(cloudYu);
    }
}