package com.huashe.park.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huashe.park.common.consts.enums.ConsUnitType;
import com.huashe.park.core.mapper.ProjectInfoMapper;
import com.huashe.park.core.service.IConsUnitInfoService;
import com.huashe.park.core.service.IProjectInfoService;
import com.huashe.park.domain.entity.ConsUnitInfo;
import com.huashe.park.domain.entity.ProjectInfo;

/**
 * 工程信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {
    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    private IConsUnitInfoService consUnitInfoService;


    @Autowired
    public void setConsUnitInfoService(@Lazy IConsUnitInfoService consUnitInfoService) {
        this.consUnitInfoService = consUnitInfoService;
    }

    /**
     * 查询工程信息
     * 
     * @param id 工程信息主键
     * @return 工程信息
     */
    @Override
    public ProjectInfo selectProjectInfoById(Long id) {
        return projectInfoMapper.selectProjectInfoById(id);
    }

    /**
     * 查询工程信息列表
     * 
     * @param projectInfo 工程信息
     * @return 工程信息
     */
    @Override
    public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo) {
        return projectInfoMapper.selectProjectInfoList(projectInfo);
    }

    /**
     * 新增工程信息
     * 
     * @param projectInfo 工程信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProjectInfo(ProjectInfo projectInfo) {
        int i = projectInfoMapper.insertProjectInfo(projectInfo);
        ConsUnitInfo consUnitInfo = new ConsUnitInfo();
        consUnitInfo.setProjectId(projectInfo.getId());
        consUnitInfo.setName(projectInfo.getName());
        consUnitInfo.setParentId(0L);
        consUnitInfo.setType(ConsUnitType.DIRECTORY.getCode());
        consUnitInfo.setAncestors("0");
        consUnitInfoService.insertConsUnitInfo(consUnitInfo);
        return i;
    }

    /**
     * 修改工程信息
     * 
     * @param projectInfo 工程信息
     * @return 结果
     */
    @Override
    public int updateProjectInfo(ProjectInfo projectInfo) {
        return projectInfoMapper.updateProjectInfo(projectInfo);
    }

    /**
     * 批量删除工程信息
     * 
     * @param ids 需要删除的工程信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectInfoByIds(Long[] ids) {
        return projectInfoMapper.deleteProjectInfoByIds(ids);
    }

    /**
     * 删除工程信息信息
     * 
     * @param id 工程信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProjectInfoById(Long id) {
        int i = projectInfoMapper.deleteProjectInfoById(id);
        consUnitInfoService.deleteConsUnitInfoByProjectId(id);
        return i;
    }
}
