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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorTalkBackRecode;
import com.yuchen.saas.park.service.service.pldoor.PlDoorTalkBackRecodeService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 呼叫对讲记录 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-04-12
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plDoorTalkBackRecode")
public class PlDoorTalkBackRecodeController {

    private final PlDoorTalkBackRecodeService plDoorTalkBackRecodeService;

    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "呼叫对讲记录 新增或修改", notes = "呼叫对讲记录 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlDoorTalkBackRecode plDoorTalkBackRecode) {
        if(Func.isEmpty(plDoorTalkBackRecode)) {
            return R.error("呼叫对讲记录不能为空");
        }
        return R.data(plDoorTalkBackRecodeService.saveOrUpdate(plDoorTalkBackRecode));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "呼叫对讲记录 详情", notes = "呼叫对讲记录详情")
    public R detail(Long id) {
        return R.data(plDoorTalkBackRecodeService.getById(id));
    }

    /**
     * 根据主表id删除呼叫对讲记录
     * @param plDoorTalkBackRecode
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 呼叫对讲记录 删除", notes = "根据主表id 呼叫对讲记录 删除")
    public R removeId(@RequestBody PlDoorTalkBackRecode plDoorTalkBackRecode) {
        plDoorTalkBackRecodeService.removeById(plDoorTalkBackRecode.getId());
        return R.success();
    }

    /**
     * 分页 呼叫对讲记录
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 呼叫对讲记录", notes = "分页 呼叫对讲记录")
    public R<NestPage<PlDoorTalkBackRecode>> pageByparam(PlDoorTalkBackRecode plDoorTalkBackRecode, Query query) {
        IPage<PlDoorTalkBackRecode> pages = plDoorTalkBackRecodeService.page(Condition.getPage(query),
                new LambdaQueryWrapper<PlDoorTalkBackRecode>()
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getParkId()), PlDoorTalkBackRecode::getParkId,
                                plDoorTalkBackRecode.getParkId())
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getBuildingId()), PlDoorTalkBackRecode::getBuildingId,
                                plDoorTalkBackRecode.getBuildingId())
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getElementId()), PlDoorTalkBackRecode::getElementId,
                                plDoorTalkBackRecode.getElementId())
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getFloorId()), PlDoorTalkBackRecode::getFloorId,
                                plDoorTalkBackRecode.getFloorId())
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getRoomId()), PlDoorTalkBackRecode::getRoomId,
                                plDoorTalkBackRecode.getRoomId())
                        .gt(Func.isNotEmpty(plDoorTalkBackRecode.getStartTime()), PlDoorTalkBackRecode::getCallBackTime,
                                plDoorTalkBackRecode.getStartTime())
                        .lt(Func.isNotEmpty(plDoorTalkBackRecode.getEndTime()), PlDoorTalkBackRecode::getCallBackTime,
                                plDoorTalkBackRecode.getEndTime())
                        .like(Func.isNotEmpty(plDoorTalkBackRecode.getUserName()), PlDoorTalkBackRecode::getUserName,
                                plDoorTalkBackRecode.getUserName())
                        .eq(Func.isNotEmpty(plDoorTalkBackRecode.getCallBackType()), PlDoorTalkBackRecode::getCallBackType,
                                plDoorTalkBackRecode.getCallBackType())
                        .orderByDesc(PlDoorTalkBackRecode::getCreateTime));
        return  R.data(NestPage.of(pages));
    }

    
}
