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

import com.microservices.common.core.constant.SecurityConstants;
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.system.api.RemoteFileService;
import com.microservices.system.api.domain.SysFileInfo;
import com.microservices.system.api.utils.FeignUtils;
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.vo.ZoneSpecialProjectInputVo;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectUpdateVo;
import com.microservices.zone.specialProject.mapper.ZoneSpecialProjectMapper;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectRelevancyService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 特色专区专项项目Service业务层处理
 *
 * @author otto
 * @date 2024-10-26
 */
@Service
public class ZoneSpecialProjectServiceImpl implements IZoneSpecialProjectService {
    @Autowired
    private ZoneSpecialProjectMapper zoneSpecialProjectMapper;
    @Autowired
    @Lazy
    private IZoneSpecialProjectTypeService zoneSpecialProjectTypeService;
    @Autowired
    private IZoneDetailService zoneDetailService;
    @Autowired
    private IZoneSortService zoneSortService;
    @Autowired
    private IZoneSpecialProjectRelevancyService zoneSpecialProjectRelevancyService;
    @Autowired
    private RemoteFileService remoteFileService;

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

    /**
     * 查询特色专区专项项目列表
     *
     * @param zoneSpecialProject 特色专区专项项目
     * @return 特色专区专项项目
     */
    @Override
    public List<ZoneSpecialProject> selectZoneSpecialProjectList(ZoneSpecialProject zoneSpecialProject) {
        return setZoneSpecialProjectDetail(zoneSpecialProjectMapper.selectZoneSpecialProjectList(zoneSpecialProject));
    }

    private List<ZoneSpecialProject> setZoneSpecialProjectDetail(List<ZoneSpecialProject> zoneSpecialProjectList) {
        zoneSpecialProjectList.forEach(x -> {
            x.setZoneSpecialProjectType(zoneSpecialProjectTypeService.selectZoneSpecialProjectTypeById(x.getTypeId()));
        });
        return zoneSpecialProjectList;
    }

    /**
     * 新增特色专区专项项目
     *
     * @param zoneSpecialProjectInputVo 特色专区专项项目
     * @return 结果
     */
    @Override
    public int insertZoneSpecialProject(ZoneSpecialProjectInputVo zoneSpecialProjectInputVo) {
        ZoneDetail zoneDetail = zoneDetailService.selectZoneDetailById(zoneSpecialProjectInputVo.getZoneId());
        checkNameInZone(zoneSpecialProjectInputVo.getZoneId(), zoneSpecialProjectInputVo.getName());
        checkParameters(zoneSpecialProjectInputVo.getTypeId(), zoneSpecialProjectInputVo.getIntroductionImageIdentifier());
        ZoneSpecialProject zoneSpecialProject = zoneSpecialProjectInputVo.toZoneSpecialProjectSearchVo();
        zoneSpecialProject.setCreateTime(DateUtils.getNowDate());
        zoneSpecialProject.setCreateBy(SecurityUtils.getUsername());
        zoneSpecialProject.setDeptId(zoneDetail.getDeptId());
        Integer maxSort = zoneSortService.getMaxSort(zoneSpecialProjectInputVo.getZoneId(), ZoneSpecialProject.table_name);
        zoneSpecialProject.setSort(maxSort);
        return zoneSpecialProjectMapper.insertZoneSpecialProject(zoneSpecialProject);
    }

    private void checkNameInZone(Long zoneId, String name) {
        ZoneSpecialProject zoneSpecialProject = zoneSpecialProjectMapper.selectZoneSpecialProjectByNameAndZoneId(name, zoneId);
        if (zoneSpecialProject != null) {
            throw new ServiceException("该特色专区下专项项目名称已存在(专项项目名称[" + name + "])");
        }
    }

    private void checkParameters(Long typeId, String introductionImageIdentifier) {
        if (typeId != null) {
            zoneSpecialProjectTypeService.selectZoneSpecialProjectTypeById(typeId);
        }
        if (StringUtils.isNotEmpty(introductionImageIdentifier)) {
            SysFileInfo sysFileInfo = FeignUtils.getReturnData(
                    remoteFileService.getFileByFileIdentifier(introductionImageIdentifier, SecurityConstants.INNER)
            );
            if (sysFileInfo == null) {
                throw new ServiceException("专项项目介绍图片文件不存在(文件标识符[" + introductionImageIdentifier + "])");
            }
        }
    }

    /**
     * 修改特色专区专项项目
     *
     * @param zoneSpecialProjectUpdateVo 特色专区专项项目
     * @return 结果
     */
    @Override
    public int updateZoneSpecialProject(ZoneSpecialProjectUpdateVo zoneSpecialProjectUpdateVo) {
        ZoneSpecialProject oldZoneSpecialProject = selectZoneSpecialProjectById(zoneSpecialProjectUpdateVo.getId());
        boolean isChangeName = StringUtils.isNotEmpty(zoneSpecialProjectUpdateVo.getName())
                && !oldZoneSpecialProject.getName().equals(zoneSpecialProjectUpdateVo.getName());
        if (isChangeName) {
            checkNameInZone(oldZoneSpecialProject.getZoneId(), zoneSpecialProjectUpdateVo.getName());
        }
        checkParameters(zoneSpecialProjectUpdateVo.getTypeId(), zoneSpecialProjectUpdateVo.getIntroductionImageIdentifier());
        ZoneSpecialProject zoneSpecialProject = zoneSpecialProjectUpdateVo.toZoneSpecialProjectSearchVo();
        zoneSpecialProject.setUpdateTime(DateUtils.getNowDate());
        zoneSpecialProject.setUpdateBy(SecurityUtils.getUsername());
        if (zoneSpecialProjectUpdateVo.getOrder() != null) {
            Integer setSort =
                    zoneSortService.setSortByOrder(oldZoneSpecialProject, zoneSpecialProjectUpdateVo.getOrder(), ZoneSpecialProject.table_name);
            zoneSpecialProject.setSort(setSort);
        }
        return zoneSpecialProjectMapper.updateZoneSpecialProject(zoneSpecialProject);
    }

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

    /**
     * 删除特色专区专项项目信息
     *
     * @param id 特色专区专项项目主键
     * @return 结果
     */
    @Override
    public int deleteZoneSpecialProjectById(Long id) {
        selectZoneSpecialProjectById(id);
        zoneSpecialProjectRelevancyService.deleteZoneSpecialProjectRelevancyBySpecialProjectId(id);
        return zoneSpecialProjectMapper.deleteZoneSpecialProjectById(id);
    }
}