package com.xueyi.system.staff.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.constant.basic.SecurityConstants;
import com.xueyi.system.api.device.domain.vo.DeviceTenantSourceMergeVo;
import com.xueyi.system.api.staff.domain.dto.DmVisitCommonDto;
import com.xueyi.system.api.staff.feign.RemoteVisitorService;
import com.xueyi.system.resource.controller.api.BaseApiController;
import com.xueyi.system.staff.mapper.DmStaffMapper;
import com.xueyi.system.staff.mapper.DmVisitRecordsMapper;
import com.xueyi.system.staff.mapper.DmVisitorsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 访客管理 API业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/visit/api")
public class DmVisitorApiController extends BaseApiController {


    @Autowired
    private DmVisitRecordsMapper dmVisitRecordsMapper;

    @Autowired
    private DmStaffMapper dmStaffMapper;

    @Autowired
    private DmVisitorsMapper dmVisitorsMapper;


    @Autowired
    private RemoteVisitorService visitorService;

    @PostMapping(value = "confirm-visitor")
    @ResponseBody
    public JSONObject queryVisit(String visitorName, Long empId, String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return visitorService.queryVisit(visitorName, empId, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @GetMapping(value = "list")
    @ResponseBody
    public JSONObject queryVisitList(Long empId, String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return visitorService.visitList(empId, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);

    }

    @GetMapping(value = "list-all")
    @ResponseBody
    public JSONObject visitList(String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return visitorService.visitListAll(vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);

    }

    @PostMapping(value = "new-visitor")
    @ResponseBody
    public JSONObject newVisit(@RequestBody DmVisitCommonDto commonDto){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(commonDto.getDevId());
        return visitorService.newVisit(commonDto, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @DeleteMapping(value = "del-visitor")
    @ResponseBody
    public JSONObject del(@RequestBody DmVisitCommonDto commonDto){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(commonDto.getDevId());
        return visitorService.delVisit(commonDto, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @GetMapping(value = "sms-config/{devId}")
    @ResponseBody
    public JSONObject smsConfig(@PathVariable("devId") String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);
        return visitorService.smsConfig(devId, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @GetMapping(value = "send-sms/{type}/{devId}")
    @ResponseBody
    public JSONObject sendSms(@PathVariable("type") Integer type, @PathVariable("devId") String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return visitorService.sendSms(type, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @GetMapping(value = "get-sms-users/{devId}")
    @ResponseBody
    public JSONObject smsUserConfig(@PathVariable("devId") String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return visitorService.smsUserConfig(vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

}
