package com.xinyilu.system.service.organize.impl;

import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.exception.ServiceException;
import com.xinyilu.common.utils.DateUtils;
import com.xinyilu.system.domain.dto.OrganizeInfoDTO;
import com.xinyilu.system.domain.organize.OrganizeInfo;
import com.xinyilu.system.domain.systemarea.SysArea;
import com.xinyilu.system.domain.vo.OrganizeInfoVO;
import com.xinyilu.system.domain.vo.sysarea.AreaInfoVO;
import com.xinyilu.system.mapper.organize.OrganizeInfoMapper;
import com.xinyilu.system.mapper.systemarea.SysAreaMapper;
import com.xinyilu.system.service.organize.IOrganizeInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 组织Service业务层处理
 *
 * @author jihan
 * @date 2022-07-28
 */
@Service
public class OrganizeInfoServiceImpl implements IOrganizeInfoService {

    @Resource
    private OrganizeInfoMapper organizeInfoMapper;
    @Resource
    private SysAreaMapper sysAreaMapper;

    /**
     * 二级区域
     */
    private static final Integer SECOND_LEVEL = 2;

    /**
     * 三级区域
     */
    private static final Integer THIRD_LEVEL = 3;

    /**
     * 查询组织
     *
     * @param id 组织主键
     * @return 组织
     */
    @Override
    public OrganizeInfoDTO selectOrganizeInfoById(Long id) {
        //查询组织信息
        OrganizeInfoDTO dto = organizeInfoMapper.selectOrganizeInfoById(id);
        //查询组织对应的区域信息
        if (Objects.nonNull(dto.getAreaId())) {
            SysArea sysArea = sysAreaMapper.selectSysAreaById(dto.getAreaId());
            if (Objects.isNull(sysArea.getLevel())) {
                throw new ServiceException("区域信息不存在！");
            }
            //二级区域到市
            if (SECOND_LEVEL.equals(sysArea.getLevel())) {
                AreaInfoVO secondAreaInfo = sysAreaMapper.getSecondAreaInfo(dto.getAreaId());
                if (Objects.nonNull(secondAreaInfo)) {
                    setAreaInfo(dto, secondAreaInfo);
                }
            }
            //三级区域到区
            if (THIRD_LEVEL.equals(sysArea.getLevel())) {
                AreaInfoVO threeAreaInfo = sysAreaMapper.getThreeAreaInfo(dto.getAreaId());
                if (Objects.nonNull(threeAreaInfo)) {
                    setAreaInfo(dto, threeAreaInfo);
                    dto.setRegionId(Objects.isNull(threeAreaInfo.getRegionId()) ? null : threeAreaInfo.getRegionId());
                    dto.setRegionCode(StringUtils.isBlank(threeAreaInfo.getRegionCode()) ? "" : threeAreaInfo.getRegionCode());
                    dto.setRegionName(StringUtils.isBlank(threeAreaInfo.getRegionName()) ? "" : threeAreaInfo.getRegionName());
                }
            }
        }
        return dto;
    }

    /**
     * 查询组织列表
     *
     * @param organizeInfo 组织
     * @return 组织
     */
    @Override
    public List<OrganizeInfoDTO> selectOrganizeInfoList(OrganizeInfoVO organizeInfo) {
        return organizeInfoMapper.selectOrganizeInfoList(organizeInfo);
    }

    /**
     * 新增组织
     *
     * @param organizeInfo 组织
     * @return 结果
     */
    @Override
    public AjaxResult insertOrganizeInfo(OrganizeInfo organizeInfo) {
        //判断当前组织是否存在
        int organizeCount = organizeInfoMapper.getOrganizeInfoByName(StringUtils.isBlank(organizeInfo.getOrganizeName()) ? "" : organizeInfo.getOrganizeName());
        if (organizeCount > 0) {
            return AjaxResult.error("新增组织'" + organizeInfo.getOrganizeName() + "'失败，该组织名称已存在");
        }
        //如果父级id位空，说明为一级节点，节点的值为0
        if (Objects.isNull(organizeInfo.getParentId())) {
            organizeInfo.setParentId(0L);
        } else {
            organizeInfo.setParentId(organizeInfo.getParentId());
        }
        //创建时间
        organizeInfo.setCreateTime(DateUtils.getNowDate());
        //是否删除
        organizeInfo.setDelFlag(0);
        int count = organizeInfoMapper.insertOrganizeInfo(organizeInfo);
        if (count <= 0) {
            return AjaxResult.error("新增组织信息失败");
        }
        return AjaxResult.success();
    }

    /**
     * 修改组织
     *
     * @param organizeInfo 组织
     * @return 结果
     */
    @Override
    public AjaxResult updateOrganizeInfo(OrganizeInfo organizeInfo) {
        //根据id查询当前组织信息
        OrganizeInfoDTO organizeInfoDTO = organizeInfoMapper.selectOrganizeInfoById(organizeInfo.getId());
        if (Objects.isNull(organizeInfoDTO)) {
            return AjaxResult.error("当前组织信息不存在");
        }
        if (!organizeInfo.getOrganizeName().equals(organizeInfoDTO.getOrganizeName())) {
            //判断当前组织名称是否存在
            int organizeCount = organizeInfoMapper.getOrganizeInfoByName(StringUtils.isBlank(organizeInfo.getOrganizeName()) ? "" : organizeInfo.getOrganizeName());
            if (organizeCount > 0) {
                return AjaxResult.error("新增组织'" + organizeInfo.getOrganizeName() + "'失败，该组织名称已存在");
            }
        }
        if (Objects.isNull(organizeInfo.getParentId())) {
            organizeInfo.setParentId(0L);
        } else {
            organizeInfo.setParentId(organizeInfo.getParentId());
        }
        //修改时间
        organizeInfo.setUpdateTime(DateUtils.getNowDate());
        //是否删除
        organizeInfo.setDelFlag(0);
        organizeInfoMapper.updateOrganizeInfo(organizeInfo);
        return AjaxResult.success();
    }

    /**
     * 批量删除组织
     *
     * @param ids 需要删除的组织主键
     * @return 结果
     */
    @Override
    public int deleteOrganizeInfoByIds(Long[] ids) {
        return organizeInfoMapper.deleteOrganizeInfoByIds(ids);
    }

    /**
     * 删除组织信息
     *
     * @param id 组织主键
     * @return 结果
     */
    @Override
    public int deleteOrganizeInfoById(Long id) {
        return organizeInfoMapper.deleteOrganizeInfoById(id);
    }

    /**
     * 设置省市区域信息
     *
     * @param dto      输入参数
     * @param areaInfo 返回值实体类
     */
    private void setAreaInfo(OrganizeInfoDTO dto, AreaInfoVO areaInfo) {
        dto.setProvinceId(Objects.isNull(areaInfo.getProvinceId()) ? null : areaInfo.getProvinceId());
        dto.setProvinceCode(StringUtils.isBlank(areaInfo.getProvinceCode()) ? "" : areaInfo.getProvinceCode());
        dto.setProvinceName(StringUtils.isBlank(areaInfo.getProvinceName()) ? "" : areaInfo.getProvinceName());
        dto.setCityId(Objects.isNull(areaInfo.getCityId()) ? null : areaInfo.getCityId());
        dto.setCityCode(StringUtils.isBlank(areaInfo.getCityCode()) ? "" : areaInfo.getCityCode());
        dto.setCityName(StringUtils.isBlank(areaInfo.getCityName()) ? "" : areaInfo.getCityName());
    }
}
