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

import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.ClerkMainStoreRelatedDTO;
import com.gic.haoban.manage.api.service.ClerkMainStoreRelatedApiService;
import com.gic.haoban.manage.service.entity.TabHaobanClerkMainStoreRelated;
import com.gic.haoban.manage.service.service.ClerkMainStoreRelatedService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

@Service
public class ClerkMainStoreRelatedApiServiceImpl implements ClerkMainStoreRelatedApiService {
    private static final Logger logger = getLogger(ClerkMainStoreRelatedApiServiceImpl.class);

    @Autowired
	ClerkMainStoreRelatedService clerkMainStoreRelatedService;
	
	@Override
	public void setMainStore(String staffId, String storeId, String wxEnterpriseId) {
		TabHaobanClerkMainStoreRelated related = clerkMainStoreRelatedService.selectStaffMainStore(staffId, wxEnterpriseId);
        if (related == null) {
            this.clerkMainStoreRelatedService.setMainStore(staffId, storeId, wxEnterpriseId);
            this.clerkMainStoreRelatedService.reflushExternal(wxEnterpriseId, staffId);
        } else if (null != related && (!related.getStoreId().equals(storeId))) {
            related.setStatusFlag(0);
			related.setUpdateTime(new Date());
			clerkMainStoreRelatedService.update(related);
            clerkMainStoreRelatedService.setMainStore(staffId, storeId, wxEnterpriseId);
        } else {
            logger.info("已经设置过主门店：{}", staffId);
        }
    }

	@Override
	public ClerkMainStoreRelatedDTO getWxEnterpriseIdAndStaffId(String wxEnterpriseId, String staffId) {
		return EntityUtil.changeEntityByJSON(ClerkMainStoreRelatedDTO.class, clerkMainStoreRelatedService.getMainStoreByStaffId(staffId, wxEnterpriseId));
	}
	
	@Override
	public ClerkMainStoreRelatedDTO getWxEnterpriseIdAndStaffId(String wxEnterpriseId, String staffId,
	        List<String> storeIdList) {
	    return EntityUtil.changeEntityByJSON(ClerkMainStoreRelatedDTO.class, clerkMainStoreRelatedService.getMainStoreByStaffId(staffId, wxEnterpriseId,storeIdList));
	}

	@Override
	public void setFreshFriend(String wxEnterpriseId, String staffId) {
		TabHaobanClerkMainStoreRelated related = clerkMainStoreRelatedService.getMainStoreByStaffId(staffId, wxEnterpriseId);
		if (related != null) {
			related.setUpdateTime(new Date());
			related.setFreshFriendFlag(1);
			clerkMainStoreRelatedService.update(related);
		}
	}
}
