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.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanAttendDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanClerkListQDTO;
import com.gic.haoban.manage.service.dao.mapper.moment.QwMomentPlanAttendMapper;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlanAttend;
import com.gic.haoban.manage.service.service.moment.QwMomentPlanAttendService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("qwMomentPlanAttendService")
public class QwMomentPlanAttendServiceImpl implements QwMomentPlanAttendService {

    private final Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    private QwMomentPlanAttendMapper qwMomentPlanAttendMapper ;
    @Autowired
    private ClerkService clerkService ;

    @Override
    public ServiceResponse<Page<QwMomentPlanAttendDTO>> attendList(PlanClerkListQDTO qdto) {
        return null;
    }

    @Override
    public void saveClerk(String enterpriseId, Long planId , List<String> clerkIdList) {
        List<String> existClerkIdList = this.qwMomentPlanAttendMapper.listExistClerkIds(planId,clerkIdList) ;
        if(CollectionUtils.isNotEmpty(existClerkIdList)) {
            clerkIdList = clerkIdList.stream().filter(o->!existClerkIdList.contains(o)).collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(clerkIdList)) {
            logger.info("所有导购都已添加过");
            return ;
        }
        List<TabQwMomentPlanAttend> list = new ArrayList<>() ;
        for(String clerkId :  clerkIdList) {
            ClerkDTO clerkDTO = this.clerkService.getclerkById(clerkId) ;
            if(null != clerkDTO) {
                TabQwMomentPlanAttend attend = new TabQwMomentPlanAttend() ;
                attend.setAttendId(UniqueIdUtils.uniqueLong());
                attend.setPlanId(planId);
                attend.setEnterpriseId(enterpriseId);
                attend.setClerkId(clerkId);
                attend.setClerkCode(clerkDTO.getClerkCode());
                attend.setClerkName(clerkDTO.getClerkName());
                list.add(attend) ;
            }
        }
        this.qwMomentPlanAttendMapper.batchInsert(list);
    }


}
