package com.xinelu.manage.service.sysarea.impl;


import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.RemoteSignsEnum;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.sysarea.SysArea;
import com.xinelu.manage.mapper.sysarea.SysAreaMapper;
import com.xinelu.manage.service.sysarea.ISysAreaService;
import com.xinelu.manage.vo.sysarea.ParentAreaVO;
import com.xinelu.manage.vo.sysarea.SysAreaVO;
import org.springframework.stereotype.Service;

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

/**
 * 区域Service业务层处理
 *
 * @author jihan
 * @date 2022-09-02
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService {
    @Resource
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询区域
     *
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public ParentAreaVO selectSysAreaById(Long id) {
        return sysAreaMapper.selectParentName(id);
    }

    /**
     * 查询区域列表
     *
     * @param sysArea 区域
     * @return 区域
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea) {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增区域
     *
     * @param sysArea 区域
     * @return 结果
     */
    @Override
    public AjaxResult insertSysArea(SysArea sysArea) {
        //检验区域编码是否重复添加
        SysArea sysAreaByCode = sysAreaMapper.selectSysAreaById(null, sysArea.getAreaCode());
        if (Objects.nonNull(sysAreaByCode)) {
            return AjaxResult.error("区域编码已存在，请勿重新添加！");
        }
        //是否是偏远地区
        if (sysArea.getRemoteSigns().equals(RemoteSignsEnum.REMOTE.getInfo())) {
            sysArea.setRemoteSigns(RemoteSignsEnum.REMOTE.getInfo());
        } else {
            sysArea.setRemoteSigns(RemoteSignsEnum.NOT_REMOTE.getInfo());
        }
        //若父级区域级别为空子级区域级别为零，父级区域不为空子级区域级别加1
        if (Objects.nonNull(sysArea.getAreaLevel())) {
            sysArea.setAreaLevel(sysArea.getAreaLevel() + 1);
        }
        sysArea.setCreateTime(LocalDateTime.now());
        sysArea.setCreateBy(SecurityUtils.getUsername());
        return AjaxResult.success(sysAreaMapper.insertSysArea(sysArea));
    }

    /**
     * 修改区域
     *
     * @param sysArea 区域
     * @return 结果
     */
    @Override
    public AjaxResult updateSysArea(SysArea sysArea) {
        //检验修改后区域编码是否与其他编码重复
        SysArea sysAreaByCode = sysAreaMapper.selectSysAreaById(null, sysArea.getAreaCode());
        if (Objects.nonNull(sysAreaByCode) && !sysArea.getId().equals(sysAreaByCode.getId())) {
            return AjaxResult.error("区域编码已存在，请勿重新添加！");
        }
        //是否是偏远地区
        if (sysArea.getRemoteSigns().equals(RemoteSignsEnum.REMOTE.getInfo())) {
            sysArea.setRemoteSigns(RemoteSignsEnum.REMOTE.getInfo());
        } else {
            sysArea.setRemoteSigns(RemoteSignsEnum.NOT_REMOTE.getInfo());
        }
        sysArea.setUpdateTime(LocalDateTime.now());
        sysArea.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(sysAreaMapper.updateSysArea(sysArea));
    }

    /**
     * 批量删除区域
     *
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(Long[] ids) {
        return sysAreaMapper.deleteSysAreaByIds(ids);
    }

    /**
     * 删除区域信息
     *
     * @param id 区域主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long id) {
        return sysAreaMapper.deleteSysAreaById(id);
    }

    /**
     * 下级区域寻找上级区域
     *
     * @param areaCode 区域编码
     * @return com.xinyilu.base.domain.vo.sysarea.SysAreaVO
     **/
    @Override
    public SysAreaVO getSubordinateRegionsFindSuperiorRegions(String areaCode) {
        return sysAreaMapper.getSubordinateRegionsFindSuperiorRegions(areaCode);
    }

    /**
     * 查询区域信息(不分页）
     *
     * @param sysArea 区域信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectAreaList(SysArea sysArea) {
        return AjaxResult.success(sysAreaMapper.selectSysAreaList(sysArea));
    }

    /**
     * 偏远地区状态修改
     *
     * @param sysArea 区域信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult updateRemoteSigns(SysArea sysArea) {
        if (Objects.isNull(sysArea) || Objects.isNull(sysArea.getId())) {
            return AjaxResult.success();
        }
        sysArea.setUpdateTime(LocalDateTime.now());
        sysArea.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(sysAreaMapper.updateSysArea(sysArea));
    }
}