package com.huaxu.zoniot.service.impl; import com.alibaba.fastjson.JSON; import com.huaxu.zoniot.common.ModelValidate; import com.huaxu.zoniot.common.ResultStatus; import com.huaxu.zoniot.common.ServiceException; import com.huaxu.zoniot.dao.CommunityMapper; import com.huaxu.zoniot.entity.Community; import com.huaxu.zoniot.service.CommunityService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; /** *

* * @Author wilian.peng * @Date 2021/1/9 19:16 * @Version 1.0 */ @Slf4j @Service public class CommunityServiceImpl implements CommunityService, ModelValidate { @Autowired CommunityMapper communityMapper ; @Override public Community findCommunity(String name, Integer siteId , Integer customerId , Integer provinceCode, Integer cityCode, Integer regionCode) { Community object = new Community(); object.setSiteId(siteId); object.setCustomerId(customerId); object.setName(name); object.setProvince(provinceCode); object.setCity(cityCode); object.setRegion(regionCode); validate(object); Community community = communityMapper.findCommunityWithName(name, siteId , customerId, provinceCode, cityCode, regionCode); return community; } @Override public Community saveCommunity(Community community) { validate(community); Community exist = findCommunity(community.getName(), community.getSiteId(), community.getCustomerId() , community.getProvince(), community.getCity(), community.getRegion()); if(exist != null){ return exist ; } else { community.setDateCreate(new Date()); community.setStatus(1); communityMapper.insert(community); return community ; } } @Override public Community findCommunityById(Integer communityId) { communityMapper.selectByPrimaryKey(communityId); return null; } @Override public boolean validate(Community object) { log.info("Validate Community , Object = {}", JSON.toJSONString(object)); if(object == null){ throw new ServiceException(ResultStatus.COMMUNITY_ERROR); } if(StringUtils.isBlank(object.getName())){ throw new ServiceException(ResultStatus.COMMUNITY_NAME_ERROR); } if(object.getSiteId() == null){ throw new ServiceException(ResultStatus.COMMUNITY_SITE_ERROR); } if(object.getCustomerId() == null){ throw new ServiceException(ResultStatus.COMMUNITY_CUSTOMER_ERROR); } if(object.getProvince() == null){ throw new ServiceException(ResultStatus.COMMUNITY_PROVINCE_ERROR); } if(object.getCity() == null){ throw new ServiceException(ResultStatus.COMMUNITY_CITY_ERROR); } if(object.getRegion() == null){ throw new ServiceException(ResultStatus.COMMUNITY_REGION_ERROR); } return true; } }