package com.ics.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.ics.admin.domain.Building;
import com.ics.admin.domain.BuildingDetail;
import com.ics.admin.domain.Customer;
import com.ics.admin.service.IBuildingDetailService;
import com.ics.admin.service.IBuildingService;
import com.ics.common.core.controller.BaseController;
import com.ics.common.core.domain.R;
import com.ics.system.domain.User;
import com.ics.system.service.ICurrentUserService;
import com.ics.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;

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

/**
 * 楼层管理 提供者
 *
 * @author zzm
 * @date 2021-03-24
 */
@RestController
@RequestMapping("/admin/buildingDetail")
public class BuildingDetailController extends BaseController {

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private ICurrentUserService currentUserService;

    @Autowired
    private IUserService userService;

    /**
     * 查询楼层管理
     */
    @RequiresPermissions("admin:buildingDetail:view")
    @GetMapping("get/{id}")
    public BuildingDetail get(@PathVariable("id") Long id) {
        return buildingDetailService.selectBuildingDetailById(id);
    }

    /**
     * 查询楼层管理列表
     */
    @RequiresPermissions("admin:buildingDetail:list")
    @GetMapping("list")
    public R list(BuildingDetail buildingDetail) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            buildingDetail.setParkId(parkId);
        }
        return result(buildingDetailService.selectBuildingDetailList(buildingDetail));
    }


    /**
     * 新增保存楼层管理
     */
    @RequiresPermissions("admin:buildingDetail:add")
    @PostMapping("save")
    public R addSave(@RequestBody BuildingDetail buildingDetail) {
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"super");
        if (isAdmin){
            //获取楼宇id
            Long buildingId = buildingDetail.getBuildingId();
            Building building = buildingService.selectBuildingById(buildingId);
            if (null != building){
                buildingDetail.setParkId(building.getParkId());
                buildingDetail.setTenantId(building.getTenantId());
            }
        }

        return toAjax(buildingDetailService.insertBuildingDetail(buildingDetail));
    }

    /**
     * 修改保存楼层管理
     */
    @RequiresPermissions("admin:buildingDetail:edit")
    @PostMapping("update")
    public R editSave(@RequestBody BuildingDetail buildingDetail) {
        return toAjax(buildingDetailService.updateBuildingDetail(buildingDetail));
    }

    /**
     * 删除楼层管理
     */
    @RequiresPermissions("admin:buildingDetail:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(buildingDetailService.deleteBuildingDetailByIds(ids));
    }

}