package com.gic.haoban.manage.service.service.out.impl.chat;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.BusinessException;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityBaseDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivitySearchQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityApiService;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivity;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangzubo on 2022/11/1.
 */
@Service("groupChatActivityApiService")
public class GroupChatActivityApiServiceImpl implements GroupChatActivityApiService {

    @Autowired
    private GroupChatActivityService activityService;

    @Override
    public ServiceResponse<Long> saveActivity(GroupChatActivityQDTO qdto) {
        if (StringUtils.isAnyBlank(
                qdto.getEnterpriseId(),
                qdto.getWxEnterpriseId(),
                qdto.getCreatorId(),
                qdto.getCreatorName(),
                qdto.getActivityName())) {
            return ServiceResponse.failure("500", "参数异常");
        }
        try {
            Long chatActivityId = activityService.saveActivity(qdto);
            activityService.delActivityDetailCache(qdto.getEnterpriseId(),chatActivityId);
            return ServiceResponse.success(chatActivityId);
        } catch (BusinessException e) {
            return ServiceResponse.failure(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ServiceResponse<GroupChatActivityDTO> getActivityDetail(String enterpriseId, Long chatActivityId) {
        if (StringUtils.isBlank(enterpriseId) || chatActivityId == null) {
            return ServiceResponse.failure("500", "参数异常");
        }
        GroupChatActivityDTO activityDetail = activityService.getActivityDetail(enterpriseId, chatActivityId);
        return ServiceResponse.success(activityDetail);
    }

    @Override
    public ServiceResponse<GroupChatActivityDTO> getActivityDetailCache(String enterpriseId, Long chatActivityId) {
        if (StringUtils.isBlank(enterpriseId) || chatActivityId == null) {
            return ServiceResponse.failure("500", "参数异常");
        }
        GroupChatActivityDTO activityDetail = activityService.getActivityDetailCache(enterpriseId, chatActivityId);
        return ServiceResponse.success(activityDetail);
    }

    @Override
    public ServiceResponse<List<GroupChatActivityBaseDTO>> listByActivityIds(String enterpriseId, List<Long> chatActivityIdList) {
        if (StringUtils.isBlank(enterpriseId) || CollectionUtils.isEmpty(chatActivityIdList)) {
            return ServiceResponse.failure("500", "参数异常");
        }
        List<TabGroupChatActivity> list = activityService.listByActivityIds(enterpriseId, chatActivityIdList);
        return ServiceResponse.success(EntityUtil.changeEntityListNew(GroupChatActivityBaseDTO.class,list));
    }

    @Override
    public ServiceResponse<List<GroupChatActivityBaseDTO>> listByActivityIdsDel(String enterpriseId, List<Long> chatActivityIdList) {
        if (StringUtils.isBlank(enterpriseId) || CollectionUtils.isEmpty(chatActivityIdList)) {
            return ServiceResponse.failure("500", "参数异常");
        }
        List<TabGroupChatActivity> list = activityService.listByActivityIdsDel(enterpriseId, chatActivityIdList);
        return ServiceResponse.success(EntityUtil.changeEntityListNew(GroupChatActivityBaseDTO.class,list));
    }

    @Override
    public ServiceResponse<Page<GroupChatActivityDTO>> pageActivity(String enterpriseId, GroupChatActivitySearchQDTO searchQDTO, BasePageInfo pageInfo) {
        if (StringUtils.isBlank(enterpriseId)) {
            return ServiceResponse.failure("500", "参数异常");
        }
        Page<GroupChatActivityDTO> page = activityService.pageActivity(enterpriseId, searchQDTO, pageInfo);
        return ServiceResponse.success(page);
    }

    @Override
    public ServiceResponse<GroupChatActivityDTO> stopActivity(String enterpriseId, Long chatActivityId) {
        if (StringUtils.isBlank(enterpriseId) || chatActivityId == null) {
            return ServiceResponse.failure("500", "参数异常");
        }
        try {
            GroupChatActivityDTO activityDTO = activityService.stopActivity(enterpriseId, chatActivityId);
            activityService.delActivityDetailCache(enterpriseId,chatActivityId);
            return ServiceResponse.success(activityDTO);
        } catch (BusinessException e) {
            return ServiceResponse.failure(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ServiceResponse<Void> delActivity(String enterpriseId, Long chatActivityId) {
        if (StringUtils.isBlank(enterpriseId) || chatActivityId == null) {
            return ServiceResponse.failure("500", "参数异常");
        }
        try {
            activityService.delActivity(enterpriseId, chatActivityId);
            activityService.delActivityDetailCache(enterpriseId,chatActivityId);
            return ServiceResponse.success();
        } catch (BusinessException e) {
            return ServiceResponse.failure(e.getCode(), e.getMessage());
        }
    }
}
