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.security.utils.SecurityUtils;
import com.microservices.zone.common.service.IZoneSortService;
import com.microservices.zone.specialProject.domain.ZoneSpecialProject;
import com.microservices.zone.specialProject.domain.ZoneSpecialProjectReport;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectReportInputVo;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectReportUpdateVo;
import com.microservices.zone.specialProject.mapper.ZoneSpecialProjectReportMapper;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectReportService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectService;
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-11-14
 */
@Service
public class ZoneSpecialProjectReportServiceImpl implements IZoneSpecialProjectReportService {
    @Autowired
    private ZoneSpecialProjectReportMapper zoneSpecialProjectReportMapper;
    @Autowired
    @Lazy
    private IZoneSpecialProjectService zoneSpecialProjectService;
    @Autowired
    private IZoneSortService zoneSortService;

    /**
     * 查询专项项目会议报告
     *
     * @param id 专项项目会议报告主键
     * @return 专项项目会议报告
     */
    @Override
    public ZoneSpecialProjectReport selectZoneSpecialProjectReportById(Long id) {
        ZoneSpecialProjectReport zoneSpecialProjectReport = zoneSpecialProjectReportMapper.selectZoneSpecialProjectReportById(id);
        if (zoneSpecialProjectReport == null) {
            throw new ServiceException("该专项项目会议报告不存在或无权限访问(专项项目会议报告id[" + id + "])");
        }
        return zoneSpecialProjectReport;
    }

    /**
     * 查询专项项目会议报告列表
     *
     * @param zoneSpecialProjectReport 专项项目会议报告
     * @return 专项项目会议报告
     */
    @Override
    public List<ZoneSpecialProjectReport> selectZoneSpecialProjectReportList(ZoneSpecialProjectReport zoneSpecialProjectReport) {
        return zoneSpecialProjectReportMapper.selectZoneSpecialProjectReportList(zoneSpecialProjectReport);
    }

    /**
     * 新增专项项目会议报告
     *
     * @param zoneSpecialProjectReportInputVo 专项项目会议报告
     * @return 结果
     */
    @Override
    public int insertZoneSpecialProjectReport(ZoneSpecialProjectReportInputVo zoneSpecialProjectReportInputVo) {
        ZoneSpecialProject zoneSpecialProject = zoneSpecialProjectService.selectZoneSpecialProjectById(zoneSpecialProjectReportInputVo.getSpecialProjectId());
        ZoneSpecialProjectReport zoneSpecialProjectReport = zoneSpecialProjectReportInputVo.toZoneSpecialProjectReport();
        zoneSpecialProjectReport.setCreateTime(DateUtils.getNowDate());
        zoneSpecialProjectReport.setCreateBy(SecurityUtils.getUsername());
        zoneSpecialProjectReport.setDeptId(zoneSpecialProject.getDeptId());
        zoneSpecialProjectReport.setZoneId(zoneSpecialProject.getZoneId());
        Integer maxSort = zoneSortService.getMaxSort(zoneSpecialProject.getZoneId(), ZoneSpecialProjectReport.table_name);
        zoneSpecialProjectReport.setSort(maxSort);
        return zoneSpecialProjectReportMapper.insertZoneSpecialProjectReport(zoneSpecialProjectReport);
    }

    /**
     * 修改专项项目会议报告
     *
     * @param zoneSpecialProjectReportUpdateVo 专项项目会议报告
     * @return 结果
     */
    @Override
    public int updateZoneSpecialProjectReport(ZoneSpecialProjectReportUpdateVo zoneSpecialProjectReportUpdateVo) {
        ZoneSpecialProjectReport oldZoneSpecialProjectReport = selectZoneSpecialProjectReportById(zoneSpecialProjectReportUpdateVo.getId());
        ZoneSpecialProjectReport zoneSpecialProjectReport = zoneSpecialProjectReportUpdateVo.toZoneSpecialProjectReport();
        zoneSpecialProjectReport.setUpdateTime(DateUtils.getNowDate());
        zoneSpecialProjectReport.setUpdateBy(SecurityUtils.getUsername());
        if (zoneSpecialProjectReportUpdateVo.getOrder() != null) {
            Integer setSort =
                    zoneSortService.setSortByOrder(oldZoneSpecialProjectReport, zoneSpecialProjectReportUpdateVo.getOrder(), ZoneSpecialProjectReport.table_name);
            zoneSpecialProjectReport.setSort(setSort);
        }
        return zoneSpecialProjectReportMapper.updateZoneSpecialProjectReport(zoneSpecialProjectReport);
    }

    /**
     * 批量删除专项项目会议报告
     *
     * @param ids 需要删除的专项项目会议报告主键
     * @return 结果
     */
    @Override
    public int deleteZoneSpecialProjectReportByIds(Long[] ids) {
        return zoneSpecialProjectReportMapper.deleteZoneSpecialProjectReportByIds(ids);
    }

    /**
     * 删除专项项目会议报告信息
     *
     * @param id 专项项目会议报告主键
     * @return 结果
     */
    @Override
    public int deleteZoneSpecialProjectReportById(Long id) {
        return zoneSpecialProjectReportMapper.deleteZoneSpecialProjectReportById(id);
    }
}
