package com.yuchen.saas.park.service.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFloorInfo;
import com.yuchen.saas.park.api.vo.park.PmgBatParkFloorInfoVo;
import com.yuchen.saas.park.api.vo.park.RoomVo;
import com.yuchen.saas.park.service.service.PmgBatParkFloorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 刘碧清
 * @date 2021/04/26 10:51
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pmgBatParkFloorInfo")
@Api(value = "楼层信息对象", tags = "楼层信息接口")
public class PmgBatParkFloorInfoController {
    @Autowired
    private PmgBatParkFloorInfoService pmgBatParkFloorInfoService;

    /**
     * 获取所有楼层数据
     */
    @PostMapping("/getAll")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取所有楼层数据", notes = "楼宇ID")
    public R<List<PmgBatParkFloorInfo>> getAll(@RequestBody RoomVo roomVo) {

        return R.data(pmgBatParkFloorInfoService.getAll(roomVo));
    }

    /**
     * 新增楼宇数据
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增楼宇数据", notes = "传入楼宇管理信息")
    public R submit(@Valid @RequestBody List<PmgBatParkFloorInfoVo> pmgBatParkFloorInfoEntities) {
        return pmgBatParkFloorInfoService.submit(pmgBatParkFloorInfoEntities);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改", notes = "传入楼层信息")
    public R update(@Valid @RequestBody PmgBatParkFloorInfo pmgBatParkFloorInfo) {
        // 判断楼层名字和唯一标识是否重复
        LambdaQueryWrapper<PmgBatParkFloorInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmgBatParkFloorInfo::getBuildingId,pmgBatParkFloorInfo.getBuildingId());
        wrapper.nested(qw -> qw.eq(PmgBatParkFloorInfo::getLMark,pmgBatParkFloorInfo.getLMark()).or().eq(PmgBatParkFloorInfo::getFloorName,
                pmgBatParkFloorInfo.getFloorName()));
        if (pmgBatParkFloorInfoService.count(wrapper) > 0){
            return R.error("楼层名称或楼层标识重复！");
        }
        return R.status(pmgBatParkFloorInfoService.updateById(pmgBatParkFloorInfo));
    }

    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(pmgBatParkFloorInfoService.del(Func.toLongList(ids)));
    }

    @GetMapping("/getByBuildingId")
    @ApiOperation(value = "通过楼栋id获取楼层信息", notes = "通过楼栋id获取楼层信息")
    public R<List<PmgBatParkFloorInfo>> getByParkId(@RequestParam Long buildingId) {
        List<PmgBatParkFloorInfo> list = pmgBatParkFloorInfoService.list(Wrappers.<PmgBatParkFloorInfo>lambdaQuery().
                eq(PmgBatParkFloorInfo::getBuildingId, buildingId).orderByDesc(PmgBatParkFloorInfo::getCreateTime));
        return R.data(list);
    }
}
