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

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
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.specialProject.domain.ZoneSpecialProject;
import com.microservices.zone.specialProject.domain.ZoneSpecialProjectType;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectTypeInputVo;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectTypeUpdateVo;
import com.microservices.zone.specialProject.mapper.ZoneSpecialProjectTypeMapper;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 特色专区专项项目分类Service业务层处理
 * 
 * @author otto
 * @date 2024-10-26
 */
@Service
public class ZoneSpecialProjectTypeServiceImpl implements IZoneSpecialProjectTypeService {
    @Autowired
    private ZoneSpecialProjectTypeMapper zoneSpecialProjectTypeMapper;
    @Autowired
    private IZoneDetailService zoneDetailService;
    @Autowired
    private IZoneSortService zoneSortService;
    @Autowired
    private IZoneSpecialProjectService zoneSpecialProjectService;

    /**
     * 查询特色专区专项项目分类
     * 
     * @param id 特色专区专项项目分类主键
     * @return 特色专区专项项目分类
     */
    @Override
    public ZoneSpecialProjectType selectZoneSpecialProjectTypeById(Long id) {
        ZoneSpecialProjectType zoneSpecialProjectType = zoneSpecialProjectTypeMapper.selectZoneSpecialProjectTypeById(id);
        if (zoneSpecialProjectType == null) {
            throw new ServiceException("该专项项目分类不存在或无权限访问(资专项项目分类id[" + id + "])");
        }
        return zoneSpecialProjectType;
    }

    /**
     * 查询特色专区专项项目分类列表
     *
     * @param zoneSpecialProjectType 特色专区专项项目分类
     * @param isOpen 是否公开接口
     * @return 特色专区专项项目分类
     */
    @Override
    public List<ZoneSpecialProjectType> selectZoneSpecialProjectTypeList(ZoneSpecialProjectType zoneSpecialProjectType, boolean isOpen) {
        return zoneSpecialProjectTypeMapper.selectZoneSpecialProjectTypeList(zoneSpecialProjectType);
    }

    /**
     * 新增特色专区专项项目分类
     *
     * @param zoneSpecialProjectTypeInputVo 特色专区专项项目分类
     * @return 结果
     */
    @Override
    public int insertZoneSpecialProjectType(ZoneSpecialProjectTypeInputVo zoneSpecialProjectTypeInputVo) {
        ZoneDetail zoneDetail = zoneDetailService.selectZoneDetailById(zoneSpecialProjectTypeInputVo.getZoneId());
        checkNameInZone(zoneSpecialProjectTypeInputVo.getZoneId(), zoneSpecialProjectTypeInputVo.getName());
        ZoneSpecialProjectType zoneSpecialProjectType = zoneSpecialProjectTypeInputVo.toZoneSpecialProjectType();
        zoneSpecialProjectType.setCreateBy(SecurityUtils.getUsername());
        zoneSpecialProjectType.setCreateTime(DateUtils.getNowDate());
        zoneSpecialProjectType.setDeptId(zoneDetail.getDeptId());
        Integer maxSort = zoneSortService.getMaxSort(zoneSpecialProjectTypeInputVo.getZoneId(), ZoneSpecialProjectType.table_name);
        zoneSpecialProjectType.setSort(maxSort);
        return zoneSpecialProjectTypeMapper.insertZoneSpecialProjectType(zoneSpecialProjectType);
    }

    private void checkNameInZone(Long zoneId, String name) {
        ZoneSpecialProjectType zoneSpecialProjectType = zoneSpecialProjectTypeMapper.selectZoneSpecialProjectTypeByNameAndZoneId(name, zoneId);
        if (zoneSpecialProjectType != null) {
            throw new ServiceException("该特色专区下专项项目分类名称已存在(专项项目分类名称[" + name + "])");
        }
    }

    /**
     * 修改特色专区专项项目分类
     *
     * @param zoneSpecialProjectTypeUpdateVo 特色专区专项项目分类
     * @return 结果
     */
    @Override
    public int updateZoneSpecialProjectType(ZoneSpecialProjectTypeUpdateVo zoneSpecialProjectTypeUpdateVo) {
        ZoneSpecialProjectType oldZoneSpecialProjectType = selectZoneSpecialProjectTypeById(zoneSpecialProjectTypeUpdateVo.getId());
        boolean isChangeName = StringUtils.isNotEmpty(zoneSpecialProjectTypeUpdateVo.getName())
                && !oldZoneSpecialProjectType.getName().equals(zoneSpecialProjectTypeUpdateVo.getName());
        if (isChangeName) {
            checkNameInZone(oldZoneSpecialProjectType.getZoneId(), zoneSpecialProjectTypeUpdateVo.getName());
        }
        ZoneSpecialProjectType zoneSpecialProjectType = zoneSpecialProjectTypeUpdateVo.toZoneSpecialProjectType();
        zoneSpecialProjectType.setUpdateTime(DateUtils.getNowDate());
        zoneSpecialProjectType.setUpdateBy(SecurityUtils.getUsername());
        if (zoneSpecialProjectTypeUpdateVo.getOrder() != null) {
            Integer setSort =
                    zoneSortService.setSortByOrder(oldZoneSpecialProjectType, zoneSpecialProjectTypeUpdateVo.getOrder(), ZoneSpecialProjectType.table_name);
            zoneSpecialProjectType.setSort(setSort);
        }
        return zoneSpecialProjectTypeMapper.updateZoneSpecialProjectType(zoneSpecialProjectType);
    }

    /**
     * 批量删除特色专区专项项目分类
     * 
     * @param ids 需要删除的特色专区专项项目分类主键
     * @return 结果
     */
    @Override
    public int deleteZoneSpecialProjectTypeByIds(Long[] ids) {
        int success = 0;
        for (Long id : ids) {
            success += deleteZoneSpecialProjectTypeById(id);
        }
        return success;
    }

    /**
     * 删除特色专区专项项目分类信息
     * 
     * @param id 特色专区专项项目分类主键
     * @return 结果
     */
    @Override
    public int deleteZoneSpecialProjectTypeById(Long id) {
        selectZoneSpecialProjectTypeById(id);
        ZoneSpecialProject zoneSpecialProject = new ZoneSpecialProject();
        zoneSpecialProject.setTypeId(id);
        List<ZoneSpecialProject> zoneSpecialProjectList = zoneSpecialProjectService.selectZoneSpecialProjectList(zoneSpecialProject);
        if (!zoneSpecialProjectList.isEmpty()) {
            throw new ServiceException("该专项项目分类被专项项目关联，无法删除(专项项目分类id[" + id + "])");
        }
        return zoneSpecialProjectTypeMapper.deleteZoneSpecialProjectTypeById(id);
    }
}
