package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.table.*;
import com.czg.account.entity.ShopTable;
import com.czg.account.service.ShopTableService;
import com.czg.account.vo.ShopTableVO;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.enums.ShopTableStatusEnum;
import com.czg.exception.CzgException;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 台桌管理
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopTable")
public class ShopTableController {
    @Resource
    private ShopTableService shopTableService;

    /**
     * 批量生成台桌码
     * 权限标识: shopTable:code
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:code", name = "生成台桌码")
    @PostMapping("/code")
    public void createCode(@RequestParam Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
        if (num > 100) {
            throw new CzgException("单次最多可获取100个");
        }
        shopTableService.createQrCode(StpKit.USER.getShopId(), num, response, request);
    }

    /**
     * 获取台桌列表
     * 权限标识: shopTable:list
     * @param areaId 区域id
     * @param tableCode 桌码
     * @param status 台桌状态
     * @return 台桌列表
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:list", name = "获取台桌列表")
    @GetMapping
    public CzgResult<Page<ShopTableVO>> list(Integer areaId, String tableCode, String status, String name, Boolean isBind) {
        return CzgResult.success(shopTableService.pageInfo(StpKit.USER.getShopId(), areaId, tableCode, status, name, isBind));
    }

    /**
     * 获取台桌详情
     * 权限标识: shopTable:list
     * @param id 台桌id
     * @return 台桌列表
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:detail", name = "获取台桌详情")
    @GetMapping("/detail")
    public CzgResult<ShopTable> detail(Long id, String tableCode) {
        if (id == null && StrUtil.isBlank(tableCode)) {
            return CzgResult.failure("id和桌码不能同时为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper().eq(ShopTable::getId, id).eq(ShopTable::getShopId, StpKit.USER.getShopId()).eq(ShopTable::getId, id);
        if (StrUtil.isNotBlank(tableCode)) {
            queryWrapper.eq(ShopTable::getTableCode, tableCode);
        }
        ShopTable one = shopTableService.getOne(queryWrapper);
        if (one != null) {
            one.setStatusMsg(ShopTableStatusEnum.getMsgByValue(one.getStatus()));
        }
        return CzgResult.success(one);
    }


    /**
     * 台桌信息修改
     * 权限标识: shopTable:edit
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:edit", name = "台桌信息修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopTableDTO shopTableDTO) {
        return CzgResult.success(shopTableService.updateInfo(StpKit.USER.getLoginIdAsLong(), shopTableDTO));
    }

    /**
     * 批量台桌配置
     * 权限标识: shopTable:edit
     * @return 是否成功
     */
    @PutMapping("/batch")
    public CzgResult<Boolean> editBatch(@RequestBody @Validated ShopTableEditBatchDTO shopTableDTO) {
        UpdateChain<ShopTable> query = shopTableService.updateChain().eq(ShopTable::getShopId, StpKit.USER.getShopId());
        if (shopTableDTO.getAutoClearTime() != null) {
            query.set(ShopTable::getAutoClearTime, shopTableDTO.getAutoClearTime());
        }

        if (shopTableDTO.getAutoClear() != null) {
            query.set(ShopTable::getAutoClear, shopTableDTO.getAutoClear());
        }
        return CzgResult.success(query.update());
    }


    /**
     * 获取清台配置信息
     * 权限标识: shopTable:edit
     * @return 是否成功
     */
    @GetMapping("/currentState")
    public CzgResult<Map<String, Serializable>> currentState() {
        ShopTable one = shopTableService.queryChain().eq(ShopTable::getShopId, StpKit.USER.getShopId()).limit(1).one();
        if (one == null) {
            one = new ShopTable();
            one.setAutoClear(0);
            one.setAutoClearTime(0);
        }
        ShopTable finalOne = one;
        return CzgResult.success(new HashMap<>(){{
            put("autoClear", finalOne.getAutoClear());
            put("autoClearTime", finalOne.getAutoClearTime());
        }});
    }

    /**
     * 台桌清台
     * 权限标识: shopTable:clear
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:clear", name = "台桌清台")
    @PutMapping("/clear")
    public CzgResult<Boolean> clear(@RequestBody @Validated ShopTableClearDTO shopTableClearDTO) {
        return CzgResult.success(shopTableService.clear(StpKit.USER.getLoginIdAsLong(), shopTableClearDTO));
    }

    /**
     * 台桌信息删除
     * 权限标识: shopTable:del
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:del", name = "台桌信息删除")
    @DeleteMapping
    public CzgResult<Boolean> remove(@RequestBody @Validated ShopTableDTO shopTableDTO) {
        return CzgResult.success(shopTableService.remove(new QueryWrapper().eq(ShopTable::getShopId, StpKit.USER.getShopId()).eq(ShopTable::getId, shopTableDTO.getId())));
    }

    /**
     * 台桌信息增加
     * 权限标识: shopTable:add
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:add", name = "台桌信息增加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopTableAddDTO shopTableAddDTO) {
        if (shopTableAddDTO.getStart() >= shopTableAddDTO.getEnd()){
            return CzgResult.failure("起始数不能大于结束数");
        }
        return CzgResult.success(shopTableService.add(StpKit.USER.getShopId(), shopTableAddDTO));
    }

    /**
     * 台桌绑定
     * 权限标识: shopTable:bind
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "台桌管理", value = "shopTable:bind", name = "台桌绑定")
    @PostMapping("/bind")
    public CzgResult<Boolean> bind(@RequestBody @Validated ShopTableBindDTO shopTableBindDTO) {
        return CzgResult.success(shopTableService.bind(StpKit.USER.getShopId(), shopTableBindDTO));
    }

}
