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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.protobuf.ServiceException;
import com.yuchen.saas.park.api.entity.park.PmgBatParkVisitor;
import com.yuchen.saas.park.service.service.PmgBatParkVisitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Condition;
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.validation.Valid;

/**
 * @ClassName ParkCompanyController
 * @Description 园区访客信息控制器
 * @Author 刘通
 * @Date 2020/12/8 11:13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/visitor")
@Api(value = "到访人员信息表", tags = "到访人员信息接口")
public class ParkVisitorController {

    /**
     * 注入访客信息接口
     */
    @Resource
    private PmgBatParkVisitorService pmgBatParkVisitorService;

    /**
     * 查询到访人员信息
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询到访人员信息", notes = "查询到访人员信息")
    public R<PmgBatParkVisitor> detail(PmgBatParkVisitor pmgBatParkVisitor) {
        PmgBatParkVisitor detail = pmgBatParkVisitorService.getOne(Condition.getQueryWrapper(pmgBatParkVisitor));
        return R.data(detail);
    }

    /**
     * 分页 到访人员信息
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 到访人员信息", notes = "分页 到访人员信息")
    public R<IPage<PmgBatParkVisitor>> list(PmgBatParkVisitor pmgBatParkVisitor, Query query) {
        IPage<PmgBatParkVisitor> pages = pmgBatParkVisitorService.page(Condition.getPage(query),
                Condition.getQueryWrapper(pmgBatParkVisitor));
        return  R.data(pages);
    }

    /**
     * 新增 到访人员信息
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增 到访人员信息", notes = "新增 到访人员信息")
    public R save(@Valid @RequestBody PmgBatParkVisitor pmgBatParkVisitor) throws ServiceException {
        return R.status(pmgBatParkVisitorService.submit(pmgBatParkVisitor));
    }

    /**
     * 新增或修改 到访人员信息
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增或修改 到访人员信息", notes = "新增或修改 到访人员信息")
    public R submit(@Valid @RequestBody PmgBatParkVisitor pmgBatParkVisitor) throws ServiceException {
        return R.status(pmgBatParkVisitorService.submit(pmgBatParkVisitor));
    }

    /**
     * 修改 到访人员信息
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改 到访人员信息", notes = "修改 到访人员信息")
    public R update(@Valid @RequestBody PmgBatParkVisitor pmgBatParkVisitor) {
        return R.status(pmgBatParkVisitorService.updateById(pmgBatParkVisitor));
    }

    /**
     * 删除 到访人员信息
     */
    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除 到访人员信息", notes = "删除 到访人员信息")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) throws ServiceException {
        return R.status(pmgBatParkVisitorService.deletePeople(Func.toLongList(ids)));
    }

    /**
     * 访客下发
     * @param visitorId
     * @return
     */
    @PostMapping("/propertySet")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "访客下发", notes = "访客下发")
    public R propertySet(Long visitorId) throws ServiceException {
        return R.status(pmgBatParkVisitorService.propertySet(visitorId));
    }

    // ---------------------------------- 邀请访客 -----------------------------

    /**
     * 邀请访客
     * @param pmgBatParkVisitor 访客信息
     * @return
     */
    @PostMapping("/inviteGuests")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "邀请访客", notes = "邀请访客")
    public R inviteGuests(@RequestBody PmgBatParkVisitor pmgBatParkVisitor){
        return R.status(pmgBatParkVisitorService.inviteGuests(pmgBatParkVisitor));
    }

    /**
     * 分页 到访人员信息
     * @param parkId 园区ID
     * @param name 访客姓名
     * @param query 分页数据
     * @return
     */
    @GetMapping("/pageVisitor")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 到访人员信息", notes = "分页 到访人员信息")
    public R<IPage<PmgBatParkVisitor>> pageVisitor(Long parkId, String name, Query query) {
        IPage<PmgBatParkVisitor> pages = pmgBatParkVisitorService.pageVisitor(parkId, name, query);
        return  R.data(pages);
    }

    /**
     * 根据ID查询访客提交的信息
     */
    @GetMapping("/getById")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据ID查询访客提交的信息", notes = "根据ID查询访客提交的信息")
    public R<PmgBatParkVisitor> getById(Long id) {
        return R.data(pmgBatParkVisitorService.getById(id));
    }


    // ------------------------------- 移动端专用 --------------------------

    /**
     * 移动端--访客申请分页
     * @param payUserId 移动端用户ID
     * @param current 当前页
     * @param size 分页数量
     * @return
     */
    @GetMapping("/getPageVisitor")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 到访人员信息", notes = "分页 到访人员信息")
    public R<IPage<PmgBatParkVisitor>> getPageVisitor(@RequestParam String payUserId,
                                                      @RequestParam Integer current,
                                                      @RequestParam Integer size) {
        IPage<PmgBatParkVisitor> pages = pmgBatParkVisitorService.pageVisitor(payUserId, current, size);
        return  R.data(pages);
    }

    /**
     * 移动端--访客申请
     * @param pmgBatParkVisitor 访客申请信息
     * @return
     */
    @PostMapping("/visitorRequest")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "移动端--访客申请", notes = "移动端--访客申请")
    public R visitorRequest(@RequestBody PmgBatParkVisitor pmgBatParkVisitor){
        return pmgBatParkVisitorService.visitorRequest(pmgBatParkVisitor);
    }

    /**
     * 移动端--访客取消
     * @param pmgBatParkVisitor 访客申请信息
     * payUserId 用户ID （管理者则为 平台的UserId）
     * id 取消的记录ID
     * @return
     */
    @PostMapping("/visitorCancel")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "移动端--访客信息取消", notes = "移动端--访客信息取消")
    public R visitorCancel(@RequestBody PmgBatParkVisitor pmgBatParkVisitor){
        // 获取参数
        String payUserId = pmgBatParkVisitor.getPayUserId();
        Long id = pmgBatParkVisitor.getId();
        return pmgBatParkVisitorService.visitorCancel(payUserId,id);
    }

    /**
     * 移动端--访客审核分页
     * @param userId 平台用户ID
     * @param current 当前页
     * @param size 分页数量
     * @return
     */
    @GetMapping("/getPageVisitorReview")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "访客审核分页", notes = "访客审核分页")
    public R<IPage<PmgBatParkVisitor>> getPageVisitorReview(@RequestParam String userId,
                                                      @RequestParam Integer current,
                                                      @RequestParam Integer size) {
        IPage<PmgBatParkVisitor> pages = pmgBatParkVisitorService.pageVisitorReview(userId, current, size);
        return  R.data(pages);
    }

    /**
     * 移动端--审核访客信息
     * @param pmgBatParkVisitor 访客申请信息
     * userId 操作用户ID
     * id 预约信息ID
     * auditStatus 审核状态
     * auditReason 审核理由
     * @return
     */
    @PostMapping("/visitorReview")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "审核访客信息", notes = "审核访客信息")
    public R visitorReview(@RequestBody PmgBatParkVisitor pmgBatParkVisitor) {
        // 获取参数
        Long userId = pmgBatParkVisitor.getUserId();
        Long id = pmgBatParkVisitor.getId();
        Integer auditStatus = pmgBatParkVisitor.getAuditStatus();
        String auditReason = pmgBatParkVisitor.getAuditReason();
        return pmgBatParkVisitorService.visitorReview(userId, id, auditStatus, auditReason);
    }

    /**
     * 移动端--主动邀请访客
     * @param pmgBatParkVisitor 访客信息
     * @return
     */
    @PostMapping("/visitorInvitation")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "访客邀约", notes = "访客邀约")
    public R visitorInvitation(@RequestBody PmgBatParkVisitor pmgBatParkVisitor) {
        return pmgBatParkVisitorService.visitorInvitation(pmgBatParkVisitor);
    }
}
