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.PlDoorSendCardRecode;
import com.yuchen.saas.park.service.service.pldoor.PlDoorSendCardRecodeService;
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("/plDoorSendCardRecode")
public class PlDoorSendCardRecodeController {

    private final PlDoorSendCardRecodeService plDoorSendCardRecodeService;

    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "发卡挂失记录表 新增或修改", notes = "发卡挂失记录表 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlDoorSendCardRecode plDoorSendCardRecode) {
        if(Func.isEmpty(plDoorSendCardRecode)) {
            return R.error("发卡挂失记录表不能为空");
        }
        return R.data(plDoorSendCardRecodeService.saveOrUpdate(plDoorSendCardRecode));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "发卡挂失记录表 详情", notes = "发卡挂失记录表详情")
    public R detail(Long id) {
        return R.data(plDoorSendCardRecodeService.getById(id));
    }

    /**
     * 根据主表id删除发卡挂失记录表
     * @param plDoorSendCardRecode
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 发卡挂失记录表 删除", notes = "根据主表id 发卡挂失记录表 删除")
    public R removeId(@RequestBody PlDoorSendCardRecode plDoorSendCardRecode) {
        plDoorSendCardRecodeService.removeById(plDoorSendCardRecode.getId());
        return R.success();
    }

    /**
     * 分页 发卡挂失记录表
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 发卡挂失记录表", notes = "分页 发卡挂失记录表")
    public R<NestPage<PlDoorSendCardRecode>> pageByparam(PlDoorSendCardRecode plDoorSendCardRecode, Query query) {
        IPage<PlDoorSendCardRecode> pages = plDoorSendCardRecodeService.page(Condition.getPage(query),
                new LambdaQueryWrapper<PlDoorSendCardRecode>()
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getType()), PlDoorSendCardRecode::getType,
                                plDoorSendCardRecode.getType())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getParkId()), PlDoorSendCardRecode::getParkId,
                                plDoorSendCardRecode.getParkId())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getBuildingId()), PlDoorSendCardRecode::getBuildingId,
                                plDoorSendCardRecode.getBuildingId())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getElementId()), PlDoorSendCardRecode::getElementId,
                                plDoorSendCardRecode.getElementId())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getFloorId()), PlDoorSendCardRecode::getFloorId,
                                plDoorSendCardRecode.getFloorId())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getRoomId()), PlDoorSendCardRecode::getRoomId,
                                plDoorSendCardRecode.getRoomId())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getStatus()), PlDoorSendCardRecode::getStatus,
                                plDoorSendCardRecode.getStatus())
                        .like(Func.isNotEmpty(plDoorSendCardRecode.getUserName()), PlDoorSendCardRecode::getUserName,
                                plDoorSendCardRecode.getUserName())
                        .eq(Func.isNotEmpty(plDoorSendCardRecode.getCardNum()), PlDoorSendCardRecode::getCardNum,
                                plDoorSendCardRecode.getCardNum())
                        .orderByDesc(PlDoorSendCardRecode::getCreateTime));
        return  R.data(NestPage.of(pages));
    }
}
