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

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.hm.HmClerkRelationDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmQrcodeQDTO;
import com.gic.haoban.manage.api.service.hm.HmClerkRelationApiService;
import com.gic.haoban.manage.api.service.hm.HmQrcodeApiService;
import com.gic.haoban.manage.service.pojo.bo.hm.HmClerkRelationBO;
import com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeBO;
import com.gic.haoban.manage.service.service.hm.HmClerkRelationService;
import com.gic.haoban.manage.service.service.hm.HmQrcodeService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("hmClerkRelationApiService")
public class HmClerkRelationApiServiceImpl implements HmClerkRelationApiService {

    private static Logger logger = LoggerFactory.getLogger(HmClerkRelationApiServiceImpl.class);

    @Autowired
    private HmQrcodeApiService hmQrcodeApiService;
    @Autowired
    private HmQrcodeService hmQrcodeService;
    @Autowired
    private HmClerkRelationService hmClerkRelationService;

    @Override
    public ServiceResponse delByClerkId(String clerkId, String enterpriseId, String wxEnterpriseId, Integer invokingType) {
       return this.delByClerkId(clerkId, enterpriseId, wxEnterpriseId, invokingType, null) ;
    }
    
    @Override
    public ServiceResponse delByClerkId(String clerkId, String enterpriseId, String wxEnterpriseId,
            Integer invokingType, String newClerkId) {
        List<HmClerkRelationBO> hmClerkRelationList = hmClerkRelationService.queryByClerkId(clerkId, enterpriseId);
        logger.info("无多人活码,newClerkId={}",newClerkId);
        HmQrcodeBO hmQrcodeBO = hmQrcodeService.queryByClerkId(clerkId, wxEnterpriseId);
        if(null != newClerkId) {
            if(null != hmQrcodeBO) {
                this.hmQrcodeService.updateClerkInfo(hmQrcodeBO.getHmId(),newClerkId) ;
            }
            if(CollectionUtils.isNotEmpty(hmClerkRelationList)) {
                this.hmClerkRelationService.updateByClerkChangeStore(enterpriseId, clerkId ,newClerkId); 
            }
        }else {
            if (hmQrcodeBO != null) {
                hmQrcodeApiService.delById(hmQrcodeBO.getHmId(), invokingType, null);
            }
            if(CollectionUtils.isNotEmpty(hmClerkRelationList)) {
                for (HmClerkRelationBO item : hmClerkRelationList) {
                    Long hmId = item.getHmId();
                    List<String> clerkIds = hmClerkRelationService.getClerkIdByHmId(hmId);
                    hmClerkRelationService.updateStatusById(0, item.getRelationId());
                    clerkIds.remove(clerkId);
                    if (CollectionUtils.isNotEmpty(clerkIds)) {
                        // 更新活码
                        HmQrcodeQDTO hmQrcodeQDTO = new HmQrcodeQDTO();
                        hmQrcodeQDTO.setHmId(hmId);
                        hmQrcodeQDTO.setClerkIdList(clerkIds);
                        hmQrcodeQDTO.setWxEnterpriseId(wxEnterpriseId);
                        hmQrcodeApiService.update(hmQrcodeQDTO);
                    } else {
                        hmQrcodeApiService.delById(hmId, invokingType, null);
                    }
                }
            }
        }
        return ServiceResponse.success();
    }


    @Override
    public Page<HmClerkRelationDTO> getHmClerkByHmId(BasePageInfo basePageInfo, Long hmId) {
        Page<HmClerkRelationBO> hmClerkByHmId = hmClerkRelationService.getHmClerkByHmId(basePageInfo, hmId);
        return PageUtil.changeToCurrentPage(hmClerkByHmId, HmClerkRelationDTO.class);
    }

    @Override
    public HmClerkRelationDTO getHmClerkByHmIdAndClerkId(Long hmId, String clerkId) {
        return EntityUtil.changeEntityNew(HmClerkRelationDTO.class, hmClerkRelationService.queryByHmIdAndClerkId(hmId, clerkId));
    }

    @Override
    public HmClerkRelationDTO getOneHmClerkByHmId(Long hmId) {
        HmClerkRelationBO hmClerkRelationBO = hmClerkRelationService.getHmClerkByHmId(hmId).get(0);
        return EntityUtil.changeEntityNew(HmClerkRelationDTO.class, hmClerkRelationBO);
    }

    @Override
    public HmClerkRelationDTO getOneHmClerkByHmIdAndStaffId(Long hmId, String staffId) {
        return EntityUtil.changeEntityNew(HmClerkRelationDTO.class, hmClerkRelationService.getHmClerkByHmIdAndStaffId(hmId, staffId));
    }
}
