package com.zcxk.rmcp.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.zcxk.core.common.enums.StatusEnum;
import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.rmcp.api.dto.community.CommunityAddDto;
import com.zcxk.rmcp.api.dto.community.CommunityDto;
import com.zcxk.rmcp.api.dto.community.CommunityPageDto;
import com.zcxk.rmcp.api.enums.RmcpErrorEnum;
import com.zcxk.rmcp.api.vo.CommunityVo;
import com.zcxk.rmcp.core.dao.CommunityMapper;
import com.zcxk.rmcp.core.entity.Community;
import com.zcxk.rmcp.web.service.CommunityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 
 * @author linqingwei
 * @date 2021-07-19 9:45
 */
@Service
@Slf4j
public class CommunityServiceImpl implements CommunityService {

    private static final String DEFAULT_CODE = "001";

    @Resource
    private CommunityMapper communityMapper;

    @Override
    public int updateByPrimaryKeySelective(CommunityAddDto communityDto) {
        Community community = new Community();
        BeanCopyUtils.copyProperties(communityDto, community, Community.class);
        // 验证同一个地址里面的小区名字是否存在
        if (communityMapper.findByNameUnique(community) > 0) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_COMMUNITY_NAME_UNIQUE_ERROR);
        }
        community.setUpdateDate(LocalDateTime.now());
        community.setUpdateBy(UserUtil.getCurrentUser().getUsername());
        return communityMapper.updateByPrimaryKeySelective(community);
    }

    @Override
    public int insertCommunity(CommunityAddDto communityAddDto) {
        log.debug("addCommunity CommunityAddDto " + communityAddDto.toString());
        Community community = new Community();
        BeanCopyUtils.copyProperties(communityAddDto, community, Community.class);
        // 验证同一个地址里面的小区名字是否存在
        if (communityMapper.findByNameUnique(community) > 0) {
            throw BusinessException.builder(RmcpErrorEnum.RMCP_COMMUNITY_NAME_UNIQUE_ERROR);
        }
        LoginUser loginUser = UserUtil.getCurrentUser();
        // 获取小区最大值，然后加1
        String communityCode = communityMapper.getMaxCodeBySiteId(loginUser.getTenantId());
        String code = StringUtils.isNotEmpty(communityCode) ? DEFAULT_CODE :String.format("%03d",Integer.valueOf(communityCode));
        community.setCode(code);
        community.setStatus(StatusEnum.OK.getCode());
        community.setTenantId(loginUser.getTenantId());
        community.setCreateBy(loginUser.getName());
        community.setCreateDate(LocalDateTime.now());
        return communityMapper.insertCommunity(community);
    }

    @Override
    public Community findById(Integer id) {
        return communityMapper.findCommunity(id);
    }

    @Override
    public void deleteCommunity(Integer id) {
        Community community = new Community();
        community.setUpdateBy(UserUtil.getCurrentUser().getUsername());
        community.setUpdateDate(LocalDateTime.now());
        community.setId(id);
        community.setStatus(StatusEnum.DELETE.getCode());
        communityMapper.updateByPrimaryKeySelective(community);
    }

    @Override
    public Pagination<CommunityVo> pageCommunity(CommunityPageDto communityPageDto) {
        Community community = new Community();
        BeanCopyUtils.copyProperties(communityPageDto, community, Community.class);
        PageHelper.startPage(communityPageDto.getPageNum(), communityPageDto.getPageSize());
        List<CommunityVo> list = communityMapper.listCommunity(community, UserUtil.getCurrentUser().getUserCondition());
        if (CollectionUtils.isEmpty(list)){
            return new Pagination<>();
        }
        return new Pagination<>(list);
    }

    @Override
    public List<CommunityVo> listCommunity(CommunityDto communityDto) {
        Community community = new Community();
        BeanCopyUtils.copyProperties(communityDto, community, Community.class);
        return communityMapper.listCommunity(community, UserUtil.getCurrentUser().getUserCondition());
    }

    @Override
    public Community findByName(String tenantId, String communityName) {
        return communityMapper.findByName(tenantId,communityName);
    }
}
