package com.skr.mdm.service.impl.app;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.skr.mdm.annotation.CmdHandler;
import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dao.MdmAppPushUserDao;
import com.skr.mdm.dto.AppPushPauseCallBackDTO;
import com.skr.mdm.entity.MdmAppPushUserDO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nfq
 * @date 2022-12-29 15:55
 */
@Slf4j
@CmdHandler(cmdType = CmdTypeConstants.APP_PUSH_PAUSE_CB)
public class AppPushPauseServiceImpl implements CmdHandlerService {

    @Autowired
    private MdmAppPushUserDao appPushUserDao;

    @Override
    public ResponseResult cmdHandler(Integer equipmentId, String data) {
        try {
            List<AppPushPauseCallBackDTO> appPushPauseCallBackDTOList = JSONUtil.toList(JSONUtil.parseArray(data), AppPushPauseCallBackDTO.class);
            List<Integer> cidList = new ArrayList<>(appPushPauseCallBackDTOList.size());
            appPushPauseCallBackDTOList.forEach(appPushPauseCallBackDTO -> {
                new LambdaUpdateChainWrapper<>(appPushUserDao)
                        .set(MdmAppPushUserDO::getPauseStatus, appPushPauseCallBackDTO.getPauseStatus())
                        .eq(MdmAppPushUserDO::getAppPushId, appPushPauseCallBackDTO.getAppPushId())
                        .eq(MdmAppPushUserDO::getEquipmentId, equipmentId)
                        .update();
                cidList.add(appPushPauseCallBackDTO.getCid());
            });
            return ResponseResult.success(JSONUtil.toJsonStr(cidList));
        } catch (Exception e) {
            log.error("应用推送暂停状态回调异常: " + equipmentId + data, e);
            return ResponseResult.failure();
        }
    }

}
