package com.gic.haoban.manage.service.service.moment.impl;

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDataDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanDataListQDTO;
import com.gic.haoban.manage.service.dao.mapper.moment.QwMomentPlanDataMapper;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlanAttend;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlanData;
import com.gic.haoban.manage.service.service.ExternalClerkRelatedService;
import com.gic.haoban.manage.service.service.moment.QwMomentPlanDataService;
import com.gic.wechat.api.dto.qywx.moment.MomentComments;
import com.gic.wechat.api.dto.qywx.moment.MomentResultDTO;
import com.github.pagehelper.PageHelper;
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.*;

@Service("qwMomentPlanDataService")
public class QwMomentPlanDataServiceImpl implements QwMomentPlanDataService {

    @Autowired
    private QwMomentPlanDataMapper qwMomentPlanDataMapper ;
    @Autowired
    private ExternalClerkRelatedService externalClerkRelatedService ;

    @Override
    public ServiceResponse<Page<QwMomentPlanDataDTO>> dataList(PlanDataListQDTO qdto) {
        PageHelper.startPage(qdto);
        if(StringUtils.isNotEmpty(qdto.getClerkParams())) {
            qdto.setClerkParamsLike("%"+qdto.getClerkParams()+"%");
        }
        if(StringUtils.isNotBlank(qdto.getStaffName())) {
            qdto.setStaffNameLike("%"+qdto.getStaffName()+"%");
        }
        if(StringUtils.isBlank(qdto.getSortColumn())) {
            qdto.setSortColumn("create_time");
        }
        if(StringUtils.isBlank(qdto.getSortType())) {
            qdto.setSortType("desc");
        }
        List<QwMomentPlanDataDTO> list = this.qwMomentPlanDataMapper.list(qdto);
        Page<QwMomentPlanDataDTO> retPage = PageHelperUtils.changePageHelperToCurrentPage(list, QwMomentPlanDataDTO.class);
        return ServiceResponse.success(retPage);
    }

    @Override
    public ServiceResponse<Void> save(TabQwMomentPlanAttend task, MomentResultDTO dto) {
        Set<String> set = new HashSet<>() ;
        Map<String,TabQwMomentPlanData> map = new HashMap<>() ;
        List<MomentComments> likeList = dto.getLikeList() ;
        if(CollectionUtils.isNotEmpty(likeList)) {
            for(MomentComments item : likeList) {
                String externalUserid = item.getExternal_userid() ;
                String memberId = this.externalClerkRelatedService.getMemberIdByExternalId(task.getWxEnterpriseId(),externalUserid) ;
                if(StringUtils.isBlank(memberId)) {
                    set.add(externalUserid) ;
                    continue;
                }
                TabQwMomentPlanData data = new TabQwMomentPlanData() ;
                data.setMemberId(memberId);
                data.setLikeFlag(1);
                data.setLikeTime(new Date(item.getCreate_time()*1000));
                map.put(externalUserid,data) ;
            }
        }
        List<MomentComments> commentList = dto.getCommentList() ;
        if(CollectionUtils.isNotEmpty(commentList)) {
            for(MomentComments item : commentList) {
                String externalUserid = item.getExternal_userid() ;
                if(set.contains(externalUserid)) {
                    continue;
                }
                TabQwMomentPlanData data = map.get(externalUserid) ;
                if(null != data) {
                    data.setCommentFlag(1);
                    data.setCommentTime(new Date(item.getCreate_time()*1000));
                }else {
                    String memberId = this.externalClerkRelatedService.getMemberIdByExternalId(task.getWxEnterpriseId(),externalUserid) ;
                    if(StringUtils.isBlank(memberId)) {
                        continue;
                    }
                    data = new TabQwMomentPlanData() ;
                    data.setMemberId(memberId);
                    data.setCommentFlag(1);
                    data.setCommentTime(new Date(item.getCreate_time()*1000));
                    map.put(externalUserid,data) ;
                }
            }
        }
        List<TabQwMomentPlanData> addList = new ArrayList<>() ;
        Set<String> keySet = map.keySet() ;
        Iterator<String> it = keySet.iterator() ;
        while (it.hasNext()) {
            String key = it.next() ;
            TabQwMomentPlanData value = map.get(key) ;
            if(null != value) {
                value.setDataId(UniqueIdUtils.uniqueLong());
                value.setPlanId(task.getPlanId());
                value.setEnterpriseId(task.getEnterpriseId());
                value.setStaffId(task.getStaffId());
                value.setClerkId(task.getClerkId());
                value.setStoreId(task.getStoreId());
                value.setWxEnterpriseId(task.getWxEnterpriseId());
                value.setClerkCode(task.getClerkCode());
                value.setClerkName(task.getClerkName());
                addList.add(value);
            }
        }
        if(CollectionUtils.isNotEmpty(addList)) {
            this.qwMomentPlanDataMapper.batchInsert(addList);
        }
        return ServiceResponse.success();
    }
}
