package com.czg.controller.admin;

import com.czg.account.dto.WxMsgSubDTO;
import com.czg.account.dto.msg.ShopMsgEditDTO;
import com.czg.account.dto.msg.ShopPushOpenIdEditDTO;
import com.czg.account.entity.ShopPushOpenId;
import com.czg.account.param.ShopPushConfigParam;
import com.czg.account.service.ShopMsgStateService;
import com.czg.account.service.ShopPushOpenIdService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
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
 */
@Slf4j
@RestController
@RequestMapping("/admin/shopMsgPush")
public class ShopMsgPushController {
    @Resource
    private ShopMsgStateService shopMsgStateService;
    @Resource
    private ShopPushOpenIdService shopPushOpenIdService;


    /**
     * 店铺推送状态获取
     */
    @PostMapping("/subscribe")
    public CzgResult<Void> subscribe(@RequestBody WxMsgSubDTO wxMsgSubDTO) {
        log.info("接收到订阅消息接口调用，携带数据: {}", wxMsgSubDTO);
        if (wxMsgSubDTO.getOpenId() == null || wxMsgSubDTO.getShopId() == null) {
            return CzgResult.failure("shopId或openId缺失");
        }
        ShopPushOpenId entity = shopPushOpenIdService.getOne(QueryWrapper.create().eq(ShopPushOpenId::getOpenId, wxMsgSubDTO.getOpenId()).eq(ShopPushOpenId::getShopId, wxMsgSubDTO.getShopId()));
        if (entity == null) {
            entity = new ShopPushOpenId();
        }
        entity.setShopId(wxMsgSubDTO.getShopId());
        entity.setOpenId(wxMsgSubDTO.getOpenId());
        entity.setNickname(wxMsgSubDTO.getNickname());
        entity.setAvatar(wxMsgSubDTO.getAvatar());
        entity.setStatus(1);
        if (entity.getId() == null) {
            shopPushOpenIdService.save(entity);
        } else {
            shopPushOpenIdService.updateById(entity);
        }
        return CzgResult.success();
    }

    /**
     * 店铺推送状态获取
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:state:list", name = "店铺推送状态获取")
    @GetMapping("/shopState")
    public CzgResult<?> get() {
        return CzgResult.success(shopMsgStateService.getState(StpKit.USER.getShopId()));
    }

    /**
     * 店铺推送状态修改
     *
     * @param shopMsgEditDTO 修改嘻嘻你
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:state:edit", name = "店铺推送状态修改")
    @PutMapping("/shopState")
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopMsgEditDTO shopMsgEditDTO) {
        return CzgResult.success(shopMsgStateService.edit(StpKit.USER.getShopId(), shopMsgEditDTO));
    }

    /**
     * 订阅用户列表
     *
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:list", name = "订阅用户列表")
    @GetMapping
    public CzgResult<Page<ShopPushOpenId>> allInfo() {
        return CzgResult.success(shopPushOpenIdService.pageInfo(StpKit.USER.getShopId()));
    }

    /**
     * 订阅解绑
     *
     * @param openId 唯一推送标识
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:del", name = "订阅解绑")
    @DeleteMapping
    public CzgResult<Boolean> unbind(@RequestParam String openId) {
        return CzgResult.success(shopPushOpenIdService.remove(new QueryWrapper().eq(ShopPushOpenId::getShopId, StpKit.USER.getShopId()).eq(ShopPushOpenId::getOpenId, openId)));
    }

    /**
     * 推送状态修改
     *
     * @param shopPushOpenIdEditDTO 修改信息
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:edit", name = "推送状态修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopPushOpenIdEditDTO shopPushOpenIdEditDTO) {
        return CzgResult.success(shopPushOpenIdService.edit(StpKit.USER.getShopId(), shopPushOpenIdEditDTO));
    }

    /**
     * 获取订阅二维码
     *
     * @return 二维码信息
     * @throws Exception 异常
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:code", name = "获取订阅二维码")
    @GetMapping("/code")
    public CzgResult<String> getCoed() throws Exception {
        return CzgResult.success(shopMsgStateService.getCode(StpKit.USER.getShopId()));
    }


    /**
     * 配置订阅消息类型
     */
    @SaAdminCheckPermission(parentName = "店铺消息推送", value = "shopMsgPush:config", name = "配置订阅消息类型")
    @PostMapping("/config")
    public CzgResult<String> config(@RequestBody ShopPushConfigParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        shopMsgStateService.modifyConfig(param);
        return CzgResult.success();
    }
}
