package com.czg.service.account.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.dto.msg.ShopPushOpenIdEditDTO;
import com.czg.exception.CzgException;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopPushOpenId;
import com.czg.account.service.ShopPushOpenIdService;
import com.czg.service.account.mapper.ShopPushOpenIdMapper;
import org.springframework.stereotype.Service;

/**
 * 用户推送信息表 服务层实现。
 *
 * @author zs
 * @since 2025-03-03
 */
@Service
public class ShopPushOpenIdServiceImpl extends ServiceImpl<ShopPushOpenIdMapper, ShopPushOpenId> implements ShopPushOpenIdService{

    @Override
    public Page<ShopPushOpenId> pageInfo(Long shopId) {
//        PageHelper.startPage(PageUtil.buildPageHelp());
//        return PageUtil.convert(new PageInfo<>(mapper.pageInfo(shopId)));
        Page<ShopPushOpenId> page = page(PageUtil.buildPage(), new QueryWrapper().eq(ShopPushOpenId::getShopId, shopId));
        page.getRecords().forEach(item -> item.setTypeInfo(JSONArray.parseArray((String) item.getTypeInfo())));
        return page;
    }

    @Override
    public Boolean edit(Long shopId, ShopPushOpenIdEditDTO shopPushOpenIdEditDTO) {
        ShopPushOpenId pushOpenId = queryChain().eq(ShopPushOpenId::getShopId, shopId).eq(ShopPushOpenId::getOpenId, shopPushOpenIdEditDTO.getOpenId()).one();
        if (pushOpenId == null) {
            throw new CzgException("订阅用户不存在");
        }

        pushOpenId.setTypeInfo(JSONObject.toJSONString(shopPushOpenIdEditDTO.getTypeInfo()));
        pushOpenId.setStatus(shopPushOpenIdEditDTO.getStatus());
        return updateById(pushOpenId);
    }
}
