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

import com.gic.haoban.manage.api.qdto.StaffSendResultQDTO;
import com.gic.haoban.manage.service.service.content.impl.GroupMessageServiceImpl;
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;

import java.util.Date;

@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
    * @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);
        if(null == log) {
            return ServiceResponse.failure("9999","记录不存在") ;
        }
        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());
        if(dto.getSendStatus()==1) {
            if(plan.getSendFlag()==0 || plan.getEndTime().getTime() < System.currentTimeMillis()) {
                dto.setSendStatus(0);
            }
        }
        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()));
        }
        qdto.setPlanId(planId);
        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<Integer> getTotalCount(String enterpriseId, String clerkId, String storeId) {
        return ServiceResponse.success(groupChatPlanOwnerLogService.getTotalCount(enterpriseId,clerkId,storeId));
    }

    @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);
    }

    @Override
    public void staffSendResult(StaffSendResultQDTO qdto) {
        this.groupChatPlanService.staffSendResult(qdto);
    }

    @Override
    public String getQwMsgId(String staffId, Date sendTime) {
        return this.groupChatPlanService.getQwMsgId(staffId,sendTime, GroupMessageServiceImpl.GroupMsgChatType.single);
    }

    @Override
    public void noticeTimer(String params) {
        this.groupChatPlanService.noticeTimer(params) ;
    }

    @Override
    public Integer getTaskNum(String wxEnterpriseId, String enterpriseId, String storeId, String clerkId, String staffId) {
        return this.groupChatPlanService.getTaskNum(wxEnterpriseId,enterpriseId,storeId,clerkId,staffId);
    }
}
