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.park.SycnPersonDTO;
import com.yuchen.saas.park.api.dto.pldoor.PlDoorUserPointSetDTO;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorPoint;
import com.yuchen.saas.park.api.vo.pldoor.PlDoorUserPointSetVO;
import com.yuchen.saas.park.api.vo.request.PlDoorUserPointSetReq;
import com.yuchen.saas.park.service.service.PmgBatParkPeopleService;
import com.yuchen.saas.park.service.service.pldoor.PlDoorUserPointSetService;
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.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 人员登记配置 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-04-08
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plDoorUserPointSet")
public class PlDoorUserPointSetController {

    private final PlDoorUserPointSetService plDoorUserPointSetService;

    @Resource
    private PmgBatParkPeopleService pmgBatParkPeopleService;

    /**
     * 人员登记 新增或修改
     * @param plDoorUserPointSetDTO
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "人员登记 新增或修改", notes = "人员登记 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlDoorUserPointSetDTO plDoorUserPointSetDTO) {
        if(Func.isEmpty(plDoorUserPointSetDTO)) {
            return R.error("人员登记不能为空");
        }
        return R.data(plDoorUserPointSetService.saveOrUpdateByParam(plDoorUserPointSetDTO));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "人员登记 详情", notes = "人员登记详情")
    public R detail(Long id) {
        return R.data(plDoorUserPointSetService.getDetailById(id));
    }

    /**
     * 根据主表id删除人员登记
     * @param plDoorUserPointSetDTO
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 人员登记 删除", notes = "根据主表id 人员登记 删除")
    public R removeId(@RequestBody PlDoorUserPointSetDTO plDoorUserPointSetDTO) {
        return R.data(plDoorUserPointSetService.removePointSetById(plDoorUserPointSetDTO.getId()));
    }

    /**
     * 分页 人员登记
     * @param req
     * @return
     */
    @PostMapping("/pageByParam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 人员登记", notes = "分页 人员登记")
    public R<NestPage<PlDoorUserPointSetVO>> pageByparam(@RequestBody PlDoorUserPointSetReq req) {
        Page<PlDoorUserPointSetReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlDoorUserPointSetVO> pages = plDoorUserPointSetService.pageByparam(page, req);
        return  R.data(NestPage.of(pages));
    }


    /**
     * 分页 综合权限查询
     * @param req
     * @return
     */
    @GetMapping("/pageByIntegrated")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页 综合权限查询", notes = "分页 综合权限查询")
    public R<NestPage<PlDoorUserPointSetVO>> pageByIntegrated(PlDoorUserPointSetReq req) {
        Page<PlDoorUserPointSetReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlDoorUserPointSetVO> pages = plDoorUserPointSetService.pageByPower(page, req);
        return  R.data(NestPage.of(pages));
    }

    /**
     * 人员门禁权限下发 人员登记
     * @param id
     * @return
     */
    @GetMapping("/sendPower")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "人员门禁权限下发 人员登记", notes = "人员门禁权限下发 人员登记")
    public R sendPower(Long id) {
        return R.data(plDoorUserPointSetService.sendUserPower(id));
    }

    /**
     * 检测人脸图片是否合格
     * @param pointId
     * @param faceUrl
     * @return
     */
    @GetMapping("/chenkFace")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "检测人脸图片是否合格", notes = "检测人脸图片是否合格")
    public R chenkFace(Long pointId,String faceUrl) {
        return R.data(plDoorUserPointSetService.chenkFace(pointId,faceUrl));
    }

    /**
     * 远程开门
     * @param userId
     * @param pointId
     * @return
     */
    @GetMapping("/doorOperation")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "远程开门", notes = "远程开门")
    public R doorOperation(Long userId, Long pointId) {
        return R.data(plDoorUserPointSetService.doorOperation(userId, pointId));
    }

    /**
     * 获取人员门禁点列表
     * @param userId
     * @return
     */
    @GetMapping("/getUserPoint")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "获取人员门禁点列表", notes = "获取人员门禁点列表")
    public R getUserPoint(Long userId) {
        return R.data(plDoorUserPointSetService.getUserPointList(userId));
    }

    /**
     * 获取人员门禁权限二维码
     * @param userId
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/getUserQrcode")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取人员门禁权限二维码", notes = "获取人员门禁权限二维码")
    public R getUserQrcode(Long userId) throws UnsupportedEncodingException {
        return R.data(plDoorUserPointSetService.getUserQrcode(userId));
    }

    /**
     * 测试人员门禁权限二维码
     * @return
     */
    @GetMapping("/testUserQrcode")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "测试人员门禁权限二维码", notes = "测试人员门禁权限二维码")
    public R testUserQrcode() {
        return R.data(plDoorUserPointSetService.testUserQrcode());
    }

    /**
     * 获取住户门禁权限
     * @param parkId 项目id
     * @param userId 住户id
     * @return
     */
    @GetMapping("/getUserPowerList")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "获取住户门禁权限", notes = "获取住户门禁权限")
    public R<List<PlDoorPoint>> getUserPowerList(Long parkId, Long userId) {
        List<PlDoorPoint> plVisitorPowerList = plDoorUserPointSetService.getUserPowerList(parkId, userId);
        return  R.data(plVisitorPowerList);
    }


    /**
     * 手动同步人员信息到门禁的人员中心
     * @param sycnPersonDTO
     * @return
     */
    @PostMapping("/handleSyncDoorPerson")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "手动同步人员信息到门禁的人员中心", notes = "手动同步人员信息到门禁的人员中心")
    public R handleSyncDoorPerson(@Valid @RequestBody SycnPersonDTO sycnPersonDTO) {
        Long parkId = sycnPersonDTO.getParkId();
        pmgBatParkPeopleService.syncDoorPerson(parkId,sycnPersonDTO.getPersonId());
        return R.success();
    }


}
