package com.younike.server.controller;

import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.framework.config.AnonymousAccess;
import com.younike.framework.config.ServerAccess;
import com.younike.system.domain.vo.OrderGoodsVo;
import com.younike.system.param.AccessPasscodeVerifyRecordParam;
import com.younike.system.param.AccessPasscodeVerifyRecordQueryParam;
import com.younike.system.service.IAccessPasscodeVerifyRecordService;
import com.younike.system.service.IOrderGoodsService;
import com.younike.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 门岗通行证核查记录Controller
 *
 * @author dail
 * @date 2022-04-16
 */
@RestController
@RequestMapping("/server/accessPasscodeVerifyRecord")
public class ServerAccessPasscodeVerifyRecordController extends BaseController {
    @Autowired
    private IAccessPasscodeVerifyRecordService accessPasscodeVerifyRecordService;

    @Autowired
    private IOrderGoodsService orderGoodsService;

    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 保安扫码-保存门岗通行证核查记录
     */
    @PostMapping(value = "/save")
    @AnonymousAccess
    public AjaxResult saveAccessPasscodeVerifyRecord(@RequestBody @Valid AccessPasscodeVerifyRecordParam accessPasscodeVerifyRecordParam) {
        return accessPasscodeVerifyRecordService.saveAccessPasscodeVerifyRecord(accessPasscodeVerifyRecordParam);
    }

    /**
     * 根据ID查询门岗二维码信息
     */
    @GetMapping(value = "/getById/{accessPasscodeVerifyRecordId}")
    @ServerAccess
    public AjaxResult getById(@PathVariable("accessPasscodeVerifyRecordId") Long accessPasscodeVerifyRecordId) {
        return AjaxResult.success(accessPasscodeVerifyRecordService.getByAccessPasscodeVerifyRecordId(accessPasscodeVerifyRecordId));
    }

    /**
     * 根据门岗通行证id查询门岗通行证核查记录
     */
    @GetMapping(value = "/getByAccessPasscodeId")
    @PreAuthorize("@ss.hasAnyRoles({'merchant'})")
    @ServerAccess
    public AjaxResult getByAccessPasscodeId(AccessPasscodeVerifyRecordQueryParam queryParam) {
        if (queryParam.getAccessPasscodeId() == null) {
            return AjaxResult.error("查询参数accessPasscodeId不能为空!");
        }
        return AjaxResult.success(accessPasscodeVerifyRecordService.getByAccessPasscodeId(queryParam));
    }

    /**
     * 保安扫码-获取订单详情
     */
    @ApiOperation("保安扫码获取订单详情")
    @AnonymousAccess
    @PostMapping("/getOrderInfo")
    public AjaxResult getOrderInfo(@RequestBody OrderGoodsVo orderGoodsVo) {
        OrderGoodsVo info = orderGoodsService.getOrderInfo(orderGoodsVo);
        return AjaxResult.success(info);
    }

    /**
     * 保安扫码-根据参数键名查询参数值
     */
    @AnonymousAccess
    @GetMapping(value = "/getByConfigKey/{configKey}")
    public AjaxResult getByConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(sysConfigService.selectConfigByKey(configKey));
    }
}
