package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.dto.shop.*;
import cn.ysk.cashier.mybatis.entity.TbShopOpenId;
import cn.ysk.cashier.pojo.shop.TbFullShopId;
import cn.ysk.cashier.service.shop.MsgService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/msg")
public class MsgController {


    private final MsgService msgService;

    public MsgController(MsgService msgService) {
        this.msgService = msgService;
    }

    /**
     * 获取所有订阅消息用户
     * @return
     */
    @AnonymousAccess

    @GetMapping("/all")
    public ResponseEntity<Page<TbFullShopId>> all(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Integer shopId,
            @RequestParam(required = false) String nickName,
            @RequestParam(required = false) String openId,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) Integer type
    ) {
        Page<TbFullShopId> all = msgService.all(page, size, shopId, nickName, openId, state, type);
        return ResponseEntity.ok(all);
    }
    @AnonymousAccess

    @PutMapping("/shopState")
    public ResponseEntity<Boolean> shopMsgState(
            @Valid @RequestBody ShopMsgStateDTO shopMsgStateDTO
    ) {
        return ResponseEntity.of(msgService.updateShopState(shopMsgStateDTO));
    }
    @AnonymousAccess

    @DeleteMapping
    public ResponseEntity<Boolean> removeSubUser(
            @Valid @RequestBody ShopMsgRemoveDTO shopMsgRemoveDTO
    ) {
        return ResponseEntity.of(msgService.removeSubUser(shopMsgRemoveDTO));
    }
    @AnonymousAccess

    @PutMapping("/info")
    public ResponseEntity<Boolean> updateInfo(
            @Valid @RequestBody ShopInfoUpdateDTO shopInfoUpdateDTO
    ) {
        return ResponseEntity.of(msgService.updateInfo(shopInfoUpdateDTO));
    }
    @AnonymousAccess

    @GetMapping("/state")
    public ResponseEntity<HashMap<String, Object>> updateInfo(
            @RequestParam Integer shopId
    ) {
        return ResponseEntity.ok(msgService.getInfo(shopId, null));
    }
}
