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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.PageHelperUtils;
import com.gic.haoban.manage.api.dto.chat.ChatOwnerTotalDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatPlanDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatPlanLogDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatPlanOwnerLogDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatPlanApiService;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatPlanOwnerLog;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatPlanBO;
import com.gic.haoban.manage.service.service.chat.GroupChatPlanOwnerLogService;
import com.gic.haoban.manage.service.service.chat.GroupChatPlanService;

@Service("groupChatPlanApiService")
public class GroupChatPlanApiServiceImpl implements GroupChatPlanApiService {

    @Autowired
    private GroupChatPlanService groupChatPlanService;
    @Autowired
    private GroupChatPlanOwnerLogService groupChatPlanOwnerLogService;
    @Autowired
    private ClerkService clerkService ;

    @Override
    public ServiceResponse<Page<GroupChatPlanDTO>> listPage(GroupChatPlanSearchQDTO qdto, BasePageInfo basePageInfo) {
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        Page<GroupChatPlanBO> page = this.groupChatPlanService.listPage(qdto, basePageInfo);
        Page<GroupChatPlanDTO> resultPage = PageHelperUtils.changePageToCurrentPage(page, GroupChatPlanDTO.class);
        return ServiceResponse.success(resultPage);
    }

    @Override
    public ServiceResponse<GroupChatPlanDTO> detail(Long chatPlanId) {
        GroupChatPlanBO bo = this.groupChatPlanService.getById(chatPlanId);
        GroupChatPlanDTO dto = EntityUtil.changeEntityByJSON(GroupChatPlanDTO.class, bo);
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<GroupChatPlanDTO> save(GroupChatPlanDTO dto) {
        this.groupChatPlanService.save(dto);
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<GroupChatPlanDTO> updateRemark(GroupChatPlanDTO dto) {
        this.groupChatPlanService.updateRemark(dto);
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<GroupChatPlanOwnerLogDTO> getOwnerInfo(Long planId, String staffId, String clerkId,
            String wxEnterpriseId) {
        GroupChatPlanOwnerLogDTO dto = this.groupChatPlanOwnerLogService.getOwnerInfo(planId, staffId, clerkId,
                wxEnterpriseId);
        return ServiceResponse.success(dto);
    }

    /**
     * 
    * @Title: doPlan
    * @Description: 定时执行计划
    * @author xugh
    * @param planId
    * @return
    * @throws
     */
    @Override
    public ServiceResponse<Void> doPlanTimer(String params) {
        Long planId = Long.parseLong(params);
        this.groupChatPlanService.doPlan(planId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> doPlanSendInfoTimer(String params) {
        this.groupChatPlanService.doPlanSendInfoTimer(params);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> reflushSendInfo(Long ownerLogId) {
        this.groupChatPlanService.doPlanSendInfo(ownerLogId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<GroupChatPlanOwnerLogDTO> getOwnerSendInfo(Long ownerLogId) {
        TabGroupChatPlanOwnerLog log = this.groupChatPlanOwnerLogService.getById(ownerLogId);
        GroupChatPlanOwnerLogDTO dto = EntityUtil.changeEntityByJSON(GroupChatPlanOwnerLogDTO.class, log);
        if(null != dto && StringUtils.isNotBlank(dto.getClerkId())) {
            ClerkDTO clerk =  this.clerkService.getclerkById(dto.getClerkId());
            dto.setClerkName(clerk.getClerkName());
         }
        Long planId = log.getPlanId() ;
        GroupChatPlanBO plan = this.groupChatPlanService.getById(planId) ;
        dto.setPlanName(plan.getName());
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<Page<GroupChatPlanLogDTO>> listLogPage(Long planId, GroupChatPlanSearchQDTO qdto,
            BasePageInfo basePageInfo) {
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        Page<GroupChatPlanLogDTO> page = this.groupChatPlanService.listLogPage(qdto, basePageInfo);
        return ServiceResponse.success(page);
    }

    @Override
    public ServiceResponse<Page<GroupChatPlanOwnerLogDTO>> listOwnerLogPage(Long planId, GroupChatPlanSearchQDTO qdto,
            BasePageInfo basePageInfo) {
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        if (StringUtils.isNotBlank(qdto.getSearchParams())) {
            qdto.setSearchParams("%" + qdto.getSearchParams() + "%");
        }
        Page<GroupChatPlanOwnerLogDTO> page = this.groupChatPlanOwnerLogService.listPage(planId, qdto, basePageInfo);
        return ServiceResponse.success(page);
    }

    @Override
    public ServiceResponse<Void> del(String clerkId, String clerkName, Long planId) {
        this.groupChatPlanService.del(clerkId, clerkName, planId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> stop(String clerkId, String clerkName, Long planId) {
        this.groupChatPlanService.stop(clerkId, clerkName, planId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Page<GroupChatPlanOwnerLogDTO>> listOwnerLogPageForWxa(GroupChatPlanSearchQDTO qdto,
            BasePageInfo basePageInfo) {
        Page<GroupChatPlanOwnerLogDTO> page = this.groupChatPlanOwnerLogService.listOwnerLogPageForWxa(qdto,
                basePageInfo);
        return ServiceResponse.success(page);
    }

    @Override
    public ServiceResponse<Page<ChatOwnerTotalDTO>> listOwnerLogPageForWxaTotal(GroupChatPlanSearchQDTO qdto,
            BasePageInfo basePageInfo) {
        Page<ChatOwnerTotalDTO> page = this.groupChatPlanOwnerLogService.listOwnerLogPageForWxaTotal(qdto,
                basePageInfo);
        return ServiceResponse.success(page);
    }

}
