package com.microservices.zone.resource.service.impl;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.datascope.annotation.DataScope;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.zone.common.service.IZoneSortService;
import com.microservices.zone.detail.domain.ZoneDetail;
import com.microservices.zone.detail.service.IZoneDetailService;
import com.microservices.zone.resource.domain.ZoneResource;
import com.microservices.zone.resource.domain.ZoneResourceDomain;
import com.microservices.zone.resource.domain.vo.ZoneResourceDomainInputVo;
import com.microservices.zone.resource.domain.vo.ZoneResourceDomainUpdateVo;
import com.microservices.zone.resource.mapper.ZoneResourceDomainMapper;
import com.microservices.zone.resource.mapper.ZoneResourceMapper;
import com.microservices.zone.resource.service.IZoneResourceDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 特色专区资源领域Service业务层处理
 *
 * @author otto
 * @date 2023-06-27
 */
@Service
public class ZoneResourceDomainServiceImpl implements IZoneResourceDomainService {
    @Autowired
    private ZoneResourceDomainMapper zoneResourceDomainMapper;
    @Autowired
    private IZoneSortService zoneSortService;
    @Autowired
    private IZoneDetailService zoneDetailService;
    @Autowired
    private ZoneResourceMapper zoneResourceMapper;

    /**
     * 查询特色专区资源领域
     *
     * @param id 特色专区资源领域主键
     * @return 特色专区资源领域
     */
    @Override
    public ZoneResourceDomain selectZoneResourceDomainById(Long id) {
        ZoneResourceDomain zoneResourceDomain = zoneResourceDomainMapper.selectZoneResourceDomainById(id);
        if (zoneResourceDomain == null) {
            throw new ServiceException("该资源领域不存在或无权限访问(资源领域id[" + id + "])");
        }
        zoneDetailService.checkZoneDataScope(zoneResourceDomain.getZoneId());
        return zoneResourceDomain;
    }

    /**
     * 查询特色专区资源领域列表
     *
     * @param zoneResourceDomain 特色专区资源领域
     * @return 特色专区资源领域
     */
    @Override
    @DataScope(deptAlias = "zrd")
    public List<ZoneResourceDomain> selectZoneResourceDomainList(ZoneResourceDomain zoneResourceDomain) {
        return zoneResourceDomainMapper.selectZoneResourceDomainList(zoneResourceDomain);
    }

    /**
     * 新增特色专区资源领域
     *
     * @param zoneResourceDomainInputVo 特色专区资源领域
     * @return 结果
     */
    @Override
    public int insertZoneResourceDomain(ZoneResourceDomainInputVo zoneResourceDomainInputVo) {
        ZoneDetail zoneDetail = zoneDetailService.selectZoneDetailById(zoneResourceDomainInputVo.getZoneId());
        checkNameInZone(zoneResourceDomainInputVo.getZoneId(), zoneResourceDomainInputVo.getName());
        ZoneResourceDomain zoneResourceDomain = zoneResourceDomainInputVo.toZoneResourceDomain();
        zoneResourceDomain.setCreateTime(DateUtils.getNowDate());
        zoneResourceDomain.setCreateBy(SecurityUtils.getUsername());
        zoneResourceDomain.setDeptId(zoneDetail.getDeptId());
        Integer maxSort = zoneSortService.getMaxSort(zoneResourceDomainInputVo.getZoneId(), ZoneResourceDomain.table_name);
        zoneResourceDomain.setSort(maxSort);
        return zoneResourceDomainMapper.insertZoneResourceDomain(zoneResourceDomain);
    }

    /**
     * 检查该特色专区下是否已存在该名称的资源领域
     *
     * @param zoneId
     * @param name
     */
    private void checkNameInZone(Long zoneId, String name) {
        ZoneResourceDomain oldZoneResourceDomain = zoneResourceDomainMapper.selectZoneResourceDomainByNameAndZoneId(name, zoneId);
        if (oldZoneResourceDomain != null) {
            throw new ServiceException("该特色专区下资源领域名称已存在(资源领域名称[" + name + "])");
        }
    }

    /**
     * 修改特色专区资源领域
     *
     * @param zoneResourceDomainUpdateVo 特色专区资源领域
     * @return 结果
     */
    @Override
    public int updateZoneResourceDomain(ZoneResourceDomainUpdateVo zoneResourceDomainUpdateVo) {
        ZoneResourceDomain oldZoneResourceDomain = selectZoneResourceDomainById(zoneResourceDomainUpdateVo.getId());
        if (!oldZoneResourceDomain.getName().equals(zoneResourceDomainUpdateVo.getName())) {
            checkNameInZone(oldZoneResourceDomain.getZoneId(), zoneResourceDomainUpdateVo.getName());
        }
        ZoneResourceDomain zoneResourceDomain = zoneResourceDomainUpdateVo.toZoneResourceDomain();
        zoneResourceDomain.setUpdateTime(DateUtils.getNowDate());
        zoneResourceDomain.setUpdateBy(SecurityUtils.getUsername());
        if (zoneResourceDomainUpdateVo.getOrder() != null) {
            Integer setSort =
                    zoneSortService.setSortByOrder(oldZoneResourceDomain, zoneResourceDomainUpdateVo.getOrder(), ZoneResourceDomain.table_name);
            zoneResourceDomain.setSort(setSort);
        }
        return zoneResourceDomainMapper.updateZoneResourceDomain(zoneResourceDomain);
    }

    /**
     * 批量删除特色专区资源领域
     *
     * @param ids 需要删除的特色专区资源领域主键
     * @return 结果
     */
    @Override
    public int deleteZoneResourceDomainByIds(Long[] ids) {
        int success = 0;
        for (Long id : ids) {
            success += deleteZoneResourceDomainById(id);
        }
        return success;
    }

    private int deleteZoneResourceDomainById(Long id) {
        selectZoneResourceDomainById(id);
        ZoneResource zoneResourceSearch = new ZoneResource();
        zoneResourceSearch.setDomainId(id);
        List<ZoneResource> zoneResourceList = zoneResourceMapper.selectZoneResourceList(zoneResourceSearch);
        if (!zoneResourceList.isEmpty()) {
            throw new ServiceException("该资源领域被资源关联，无法删除(资源领域id[" + id + "])");
        }
        return zoneResourceDomainMapper.deleteZoneResourceDomainById(id);
    }
}
