package com.zbkj.admin.controller;

import com.zbkj.common.model.home.Home;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.HomeRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.vo.HomeVo;
import com.zbkj.service.service.EbHomeService;
import com.zbkj.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("api/admin/floor/")
@Api(tags = "楼层")
public class HomeController {

    @Autowired
    private EbHomeService homeService;
    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @PreAuthorize("hasAuthority('admin:home:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<HomeVo>> getList(@Validated HomeRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(homeService.getAdminList(request, pageParamRequest)));
    }

    @PreAuthorize("hasAuthority('admin:home:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated HomeRequest request) {
        Home home = new Home();
        BeanUtils.copyProperties(request, home);
        home.setImgUrl(systemAttachmentService.clearPrefix(home.getImgUrl()));
        if (homeService.save(home)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:home:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestParam(value = "ids") String ids) {
        List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        for (Integer id : idList) {
            homeService.deleteById(id);
        }
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('admin:home:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody HomeRequest request) {
        Home upd = new Home();
        BeanUtils.copyProperties(request, upd);
        upd.setImgUrl(systemAttachmentService.clearPrefix(upd.getImgUrl()));
        if (homeService.updateById(upd)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:home:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<HomeVo> info(@PathVariable Integer id) {
        Home home = homeService.getById(id);
        HomeVo homeVo = new HomeVo();
        BeanUtils.copyProperties(home, homeVo);
        return CommonResult.success(homeVo);
   }
}



