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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.pldoor.PlPropertyPassDTO;
import com.yuchen.saas.park.api.dto.property.PropertyHouseOwnerQueryDto;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorPoint;
import com.yuchen.saas.park.api.entity.pldoor.PlPropertyPass;
import com.yuchen.saas.park.api.entity.pldoor.PlVisitorInfo;
import com.yuchen.saas.park.api.vo.pldoor.PlPropertyPassVO;
import com.yuchen.saas.park.api.vo.request.PlPropertyPassReq;
import com.yuchen.saas.park.service.service.pldoor.PlPropertyPassService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 物品放行 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-07-03
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plPropertyPass")
public class PlPropertyPassController {

    private final PlPropertyPassService plPropertyPassService;

    /**
     * 物品放行 新增/修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "物品放行 新增/修改", notes = "物品放行 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody PlPropertyPassDTO plPropertyPassDTO) {
        if(Func.isEmpty(plPropertyPassDTO)) {
            return R.error("物品放行不能为空");
        }
        return R.data(plPropertyPassService.saveOrUpdateByParam(plPropertyPassDTO));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "物品放行详情", notes = "物品放行详情")
    public R detail(Long id) {
        return R.data(plPropertyPassService.detailById(id));
    }

    /**
     * 根据主表id删除物品放行
     * @param plPropertyPassDTO
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id物品放行删除", notes = "根据主表id物品放行删除")
    public R removeId(@RequestBody PlPropertyPassDTO plPropertyPassDTO) throws Exception {
        return R.data(plPropertyPassService.deleteById(plPropertyPassDTO.getId()));
    }

    /**
     * 分页 物品放行
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 访客登记", notes = "分页 访客登记")
    public R<NestPage<PlPropertyPassVO>> page(Page<PlPropertyPassReq> page, PlPropertyPassReq req) {
        IPage<PlPropertyPassVO> pages = plPropertyPassService.pageByParam(page, req);
        return  R.data(NestPage.of(pages));
    }

    /**
     * 导出
     */
    @PostMapping("/exportPropertyPass")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "导出物品放行登记", notes = "导出物品放行登记")
    public void exportPropertyPass(@RequestBody PlPropertyPassReq req, HttpServletResponse response) {
        plPropertyPassService.exportPropertyPass(req, response);
    }

    @PostMapping("/updateState")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "物品放行 修改审批状态", notes = "物品放行 修改审批状态")
    public R updateState(@Valid @RequestBody PlPropertyPass plPropertyPass) {
        if(Func.isEmpty(plPropertyPass)) {
            return R.error("物品放行不能为空");
        }
        return R.data(plPropertyPassService.updateState(plPropertyPass));
    }

    @GetMapping("/getPropertyPassQrcode")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取物品放行详情二维码", notes = "获取物品放行详情二维码")
    public R getPropertyPassQrcode(Long propertyPassId) throws UnsupportedEncodingException {
        return R.data(plPropertyPassService.getPropertyPassQrcode(propertyPassId));
    }

    @PostMapping("/getPropertyPassByQrcode")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "通过二维码获取物品放行详情", notes = "通过二维码获取物品放行详情")
    public R getPropertyPassByQrcode(@Valid @RequestBody PlPropertyPassVO plPropertyPassVO) throws Exception {
        return R.data(plPropertyPassService.getPropertyPassByQrcode(plPropertyPassVO));
    }


    /**
     * 分页 开门记录
     */
    @GetMapping("/pageOpenDoorLog")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "分页 开门记录", notes = "分页 开门记录")
    public R<NestPage<PlPropertyPassVO>> pageOpenDoorLog(Page<PlPropertyPassReq> page, PlPropertyPassReq req) {
        IPage<PlPropertyPassVO> pages = plPropertyPassService.pageOpenDoorLog(page, req);
        return  R.data(NestPage.of(pages));
    }

}
