package com.czg.controller.admin;

import com.czg.account.dto.calltable.*;
import com.czg.account.entity.CallConfig;
import com.czg.account.entity.CallQueue;
import com.czg.account.entity.CallTable;
import com.czg.account.service.CallTableService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 叫号管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/callTable")
@Slf4j
public class CallTableController {
    @Resource
    private CallTableService callTableService;

    /**
     * 叫号桌型获取
     *
     * @param callTableId 叫号桌型id
     * @param state       0禁用 1使用
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:list", name = "叫号桌型获取")
    @GetMapping
    public CzgResult<CallTablePage> get(Long callTableId, Integer state) {
        return CzgResult.success(callTableService.get(StpKit.USER.getShopId(), callTableId, state));
    }

    /**
     * 叫号桌型新增
     *
     * @param addCallTableDTO 新增数据
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:add", name = "叫号桌型添加")
    @PostMapping
    public CzgResult<Boolean> add(@Validated @RequestBody CallTableDTO addCallTableDTO) {
        return CzgResult.success(callTableService.add(StpKit.USER.getShopId(), addCallTableDTO));
    }

    /**
     * 叫号桌型修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:edit", name = "叫号桌型修改")
    @PutMapping
    public CzgResult<Boolean> update(@Validated @RequestBody UpdateCallTableDTO callTableDTO) {
        return CzgResult.success(callTableService.update(StpKit.USER.getShopId(), callTableDTO));
    }

    /**
     * 叫号桌型删除
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:del", name = "叫号桌型删除")
    @DeleteMapping
    public CzgResult<Boolean> delete(@Validated @RequestBody BaseCallTableDTO baseCallTableDTO) {
        return CzgResult.success(callTableService.remove(new QueryWrapper().eq(CallTable::getShopId, StpKit.USER.getShopId()).eq(CallTable::getId, baseCallTableDTO.getCallTableId())));
    }

    /**
     * 获取叫号号码
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:takeNumber", name = "获取叫号号码")
    @PostMapping("takeNumber")
    public CzgResult<CallTableNumDTO> takeNumber(@Validated @RequestBody TakeNumberDTO takeNumberDTO) {
        return CzgResult.success(callTableService.takeNumber(StpKit.USER.getShopId(), takeNumberDTO));
    }


    /**
     * 获取叫号页面二维码
     *
     * @param callTableId 叫号桌型id
     * @return base64编码
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:takeNumberCode", name = "获取叫号页面二维码")
    @GetMapping("takeNumberCode")
    public CzgResult<String> takeNumberCode(@RequestParam Integer callTableId) {
        return CzgResult.success(callTableService.takeNumberCode(StpKit.USER.getShopId(), callTableId));
    }


    /**
     * 执行叫号
     *
     * @return 0失败 1成功 -1用户未订阅
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:call", name = "执行叫号")
    @PostMapping("call")
    public CzgResult<Integer> call(@Validated @RequestBody CallQueueDTO callQueueDTO) {
        try {
            return CzgResult.success(callTableService.call(StpKit.USER.getShopId(), callQueueDTO));
        } catch (Exception e) {
            log.error("异常", e);
            return CzgResult.success(1);
        }
    }


    /**
     * 修改叫号队列状态
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:updateState", name = "修改叫号队列状态")
    @PutMapping("updateState")
    public CzgResult<Boolean> confirm(@Validated @RequestBody UpdateCallQueueDTO updateCallQueueDTO) {
        return CzgResult.success(callTableService.updateInfo(StpKit.USER.getShopId(), updateCallQueueDTO));
    }


    /**
     * 获取叫号队列
     *
     * @param callTableId 桌型id
     * @param state       状态 -1已取消 0排队中 1叫号中 2已入座 3 已过号
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:queue:list", name = "获取叫号队列")
    @GetMapping("queue")
    public CzgResult<Page<CallQueue>> getQueue(Long callTableId, Integer state, String openId) {
        return CzgResult.success(callTableService.getQueue(StpKit.USER.getShopId(), openId, callTableId, state));
    }


    /**
     * 获取叫号记录列表
     *
     * @param callTableId 桌型id
     * @return 数据
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:callRecord:list", name = "获取叫号记录列表")
    @GetMapping("callRecord")
    public CzgResult<Page<CallRecordVO>> getCallRecord(Integer callTableId) {
        return CzgResult.success(callTableService.getCallRecord(StpKit.USER.getShopId(), callTableId));
    }

    /**
     * 获取叫号配置
     *
     * @return 配置信息
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:config:list", name = "获取叫号配置")
    @GetMapping("config")
    public CzgResult<CallConfig> getConfig() {
        return CzgResult.success(callTableService.getConfig(StpKit.USER.getShopId()));
    }

    /**
     * 修改叫号配置
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "叫号管理", value = "callTable:config:edit", name = "修改叫号配置")
    @PutMapping("config")
    public CzgResult<Boolean> updateConfig(@RequestBody UpdateConfigDTO configDTO) {
        return CzgResult.success(callTableService.updateConfig(StpKit.USER.getShopId(), configDTO));
    }

    /**
     * 消息订阅
     *
     * @return 是否成功
     */
    @PostMapping("subMsg")
    public CzgResult<?> subMsg(
            @Validated @RequestBody CallSubMsgDTO subMsgDTO
    ) {
        return CzgResult.success(callTableService.subMsg(subMsgDTO));
    }
}
