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

import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.clerk.api.service.PowerService;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.service.SyncHaobanToGicApiService;
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;

@Service
public class SyncHaobanToGicApiServiceImpl implements SyncHaobanToGicApiService {
	
	private static Logger logger  = LoggerFactory.getLogger(SyncHaobanToGicApiServiceImpl.class);
	
	@Autowired
	private StoreService storeService;
	
	@Autowired
	private ClerkService clerkService;

	@Autowired
	private PowerService powerService;


	@Override
	public void delGicClerk(String gicClerkId) {
		logger.info("【删除gicClerk】clerkID={}",gicClerkId);
		clerkService.delClerk(gicClerkId);
	}

//	@Override
//	public void updateGicClerk(String gicClerkId, String name) {
//		logger.info("【删除gicClerk】clerkID={},name={}",gicClerkId,name);
//		ClerkDTO clerkDto = new ClerkDTO();
//		ClerkDTO clerk = clerkService.getClerkByClerkId(gicClerkId);
//		if(clerk == null || clerk.getClerkName().equals(name)){
//			return;
//		}else{
//			logger.info("【更新gicClerk】clerkID={},name={}",gicClerkId,name);
//			clerkDto.setClerkId(gicClerkId);
//			clerkDto.setClerkName(name);
//			clerkService.updateClerk(clerkDto);
//		}
//
//	}

//	@Override
//	public boolean validateGicClerkAdd(String gicEnterpriseId,String phoneNumber,String code,String gicStoreId) {
//		logger.info("【店员新增校验】gicEnterpriseId={},phoneNumber={},code={}",gicEnterpriseId,phoneNumber,code);
//		ClerkDTO clerk = clerkService.getClerkByCodeNoDel(gicEnterpriseId, code);
//		//检验code是否唯一,并且手机号不为空
//		if (null!=clerk && StringUtils.isNotBlank(clerk.getPhoneNumber())) {
//			return false;
//		//检验code唯一，并且手机为空，并且门店id不同
//		}else if(null!=clerk && StringUtils.isBlank(clerk.getPhoneNumber()) && !gicStoreId.equals(clerk.getStoreId())){
//			return false;
//		}else if(null != clerk && StringUtils.isBlank(clerk.getPhoneNumber()) && gicStoreId.equals(clerk.getStoreId())){
//			String message = clerkService.validatePhoneNumber(gicEnterpriseId, gicStoreId, phoneNumber, clerk.getClerkId());
//			logger.info("【新增用户校验】message={},gicEnterpriseId={},gicStoreId={},phoneNumber={}",message,gicEnterpriseId,gicStoreId,phoneNumber);
//			if(StringUtils.isNotBlank(message)){
//				return false;
//			}
//		}else{
//			//code不存在的情况下 判断是否能新增
//			String message = clerkService.validatePhoneNumber(gicEnterpriseId, gicStoreId, phoneNumber, "");
//			logger.info("【新增用户校验】message={},gicEnterpriseId={},gicStoreId={},phoneNumber={}",message,gicEnterpriseId,gicStoreId,phoneNumber);
//			if(StringUtils.isNotBlank(message)){
//				return false;
//			}
//		}
//		return true;
//	}

//	@Override
//	public boolean gicClerkMove(String gicClerkId, String gicOldStoreId, String gicNewStoreId, int clerkChangeReason,
//			int memberChangeReason, String operId) {
//		logger.info("【门店转移】gicClerkId={},gicOldStoreId={},gicNewStoreId={},clerkChangeReason={},memberChangeReason={},operId={}",
//				gicClerkId,gicOldStoreId,gicNewStoreId,clerkChangeReason,memberChangeReason,operId);
//		if(StringUtils.isAnyBlank(gicClerkId,gicOldStoreId,gicNewStoreId,operId)){
//			return false;
//		}
//		clerkService.clerkMove(gicClerkId, gicOldStoreId, gicNewStoreId, clerkChangeReason, memberChangeReason, operId);
//		return true;
//	}

//	@Override
//	public boolean setStoreManager(String gicEnterpriseId, String gicStoreId, String gicClerkId) {
//		powerService.setStoreManager(gicEnterpriseId, gicStoreId, gicClerkId);
//		return true;
//	}

	@Override
	public boolean syncClerkToGicClerkAdd(String storeId,String clerkCode,Integer sex,String name,
										  String phoneNumber,String nationCode,String postionName) {
		logger.info("【新增gicClerk】storeId={},clerkCode={},name={},phoneNumber={},nationCode={}",storeId,clerkCode,name,phoneNumber,nationCode);

		com.gic.enterprise.api.dto.StoreDTO store = storeService.getStoreByIdNoStatus(storeId);
		//校验手机是否唯一
		if (store == null) {
			return false;
		}
		String enterpriseId = store.getEnterpriseId();
		ClerkDTO clerk = clerkService.getClerkByCodeNoDel(enterpriseId, clerkCode);
		if(clerk != null){
			logger.info("【同步店员新增校验】flag={}",StringUtils.isBlank(clerk.getPhoneNumber()) || "--".equals(clerk.getPhoneNumber()));
		}
		//检验code是否唯一
		if (null!=clerk && StringUtils.isNotBlank(clerk.getPhoneNumber()) && !"--".equals(clerk.getPhoneNumber())) {
			return false;
			//如果code存在,并且在相同门店并且手机号为空，则更新手机号
		}else if(null != clerk && (StringUtils.isBlank(clerk.getPhoneNumber()) || "--".equals(clerk.getPhoneNumber())) && storeId.equals(clerk.getStoreId())){
			String message = clerkService.validatePhoneNumber(enterpriseId, storeId, phoneNumber, clerk.getClerkId());
			logger.info("【同步店员新增校验】message={}",message);
			if(StringUtils.isBlank(message)){
				clerk.setClerkName(name);
				clerk.setPhoneNumber(phoneNumber);
				clerkService.updateClerk(clerk);
				return true;
			}
		}

		ClerkDTO clerkDTO = new ClerkDTO();
		clerkDTO.setClerkName(name);
		clerkDTO.setClerkCode(clerkCode);
		clerkDTO.setStoreId(storeId);
		clerkDTO.setStoreName(store.getStoreName());
		clerkDTO.setDepartId(store.getStoreGroupId());
		clerkDTO.setDeptName(store.getStoreGroupName());
		clerkDTO.setClerkType(0);
		clerkDTO.setStatus(1);
		clerkDTO.setPhoneNumber(phoneNumber);
		clerkDTO.setPositionName("店员");
		clerkDTO.setEnterpriseId(enterpriseId);

		String s = clerkService.saveHaobanClerk(enterpriseId, clerkCode, name, storeId, nationCode, phoneNumber, 1);
		logger.info("【同步店员新增返回参数】s={}",s);
		if(StringUtils.isBlank(s)){
			return false;
		}
		return true;
	}

	@Override
	public String syncClerkToGicClerkAddNew(String storeId,String clerkCode,Integer sex,String name,
			String phoneNumber,String nationCode,String postionName) {
		logger.info("【新增gicClerk】storeId={},clerkCode={},name={},phoneNumber={},nationCode={}",storeId,clerkCode,name,phoneNumber,nationCode);
		
		com.gic.enterprise.api.dto.StoreDTO store = storeService.getStoreByIdNoStatus(storeId);
		//校验手机是否唯一
		if (store == null) {
			logger.info("门店不存在，导购新增失败");
			return "门店不存在，导购新增失败";
		}
		String enterpriseId = store.getEnterpriseId();
		ClerkDTO clerk = clerkService.getClerkByCodeNoDel(enterpriseId, clerkCode);
		if(clerk != null){
			logger.info("【同步店员新增校验】flag={}",StringUtils.isBlank(clerk.getPhoneNumber()) || "--".equals(clerk.getPhoneNumber()));
		}
		//检验code是否唯一
		if (null!=clerk && StringUtils.isNotBlank(clerk.getPhoneNumber()) && !"--".equals(clerk.getPhoneNumber())) {
			logger.info("导购已存在且手机号不为空");
			return "导购已存在且手机号不为空";
		//如果code存在,并且在相同门店并且手机号为空，则更新手机号
		}else if(null != clerk && (StringUtils.isBlank(clerk.getPhoneNumber()) || "--".equals(clerk.getPhoneNumber())) && storeId.equals(clerk.getStoreId())){
			String message = clerkService.validatePhoneNumber(enterpriseId, storeId, phoneNumber, clerk.getClerkId());
			logger.info("【同步店员新增校验】message={}",message);
			if(StringUtils.isBlank(message)){
				clerk.setClerkName(name);
				clerk.setPhoneNumber(phoneNumber);
				clerkService.updateClerk(clerk);
				logger.info("校验成功");
				return "1";
			}
		}
		
		ClerkDTO clerkDTO = new ClerkDTO();
		clerkDTO.setClerkName(name);
		clerkDTO.setClerkCode(clerkCode);
		clerkDTO.setStoreId(storeId);
		clerkDTO.setStoreName(store.getStoreName());
		clerkDTO.setDepartId(store.getStoreGroupId());
		clerkDTO.setDeptName(store.getStoreGroupName());
		clerkDTO.setClerkType(0);
		clerkDTO.setStatus(1);
		clerkDTO.setPhoneNumber(phoneNumber);
		clerkDTO.setPositionName("店员");
		clerkDTO.setEnterpriseId(enterpriseId);

		String s = clerkService.saveHaobanClerk(enterpriseId, clerkCode, name, storeId, nationCode, phoneNumber, 1);
		logger.info("【同步店员新增返回参数】s={}",s);
		if(StringUtils.isBlank(s)){
			return "新增失败，可能原因：GIC已存在相同手机号或code";
		}
		return "1";
		
	}
}
