package com.bonus.material.project.service.impl;

import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.project.domain.Project;
import com.bonus.material.project.domain.ProjectType;
import com.bonus.material.project.mapper.ProjectMapper;
import com.bonus.material.project.mapper.RegionMapper;
import com.bonus.material.project.service.ProjectService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 30791
 * @version 1.0
 * Create by 2025/11/13 11:25
 */

@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private RegionMapper regionMapper;

    /**
     * 查询工程列表
     *
     * @param project 工程信息
     * @return 工程集合
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {

        return projectMapper.selectProjectList(project);
    }

    /**
     * 通过ID查询工程
     *
     * @param projectId 工程ID
     * @return 工程对象
     */
    @Override
    public Project selectProjectById(Long projectId)
    {
        return projectMapper.selectProjectById(projectId);
    }

    /**
     * 新增工程
     *
     * @param project 工程信息
     */
    @Override
    public void insertProject(Project project)
    {
        int count = projectMapper.countByProjectName(project.getPro_name());
        if (count > 0) {
            throw new RuntimeException(project.getPro_name()+"已存在，请勿重复新增");
        }

        int count1 =projectMapper.countByProjectCode(project.getPro_code());
        if(count1>0){
            throw new RuntimeException("项目编号已存在，请勿重复新增");
        }

        project.setProvince(regionMapper.getShortNameByCode(project.getProvince()));
        project.setCity(regionMapper.getShortNameByCode(project.getCity()));
        project.setCounty(regionMapper.getNameByCode(project.getCounty()));
        projectMapper.insertProject(project);
    }

    /**
     * 修改工程
     *
     * @param project 工程信息
     */
    @Override
    public void updateProject(Project project)
    {
        int count =projectMapper.countByProjectNameById(project.getPro_name(),project.getId());
        if(count>0){
            throw new RuntimeException(project.getPro_name()+"已存在，请重新修改");
        }

        project.setProvince(regionMapper.getShortNameByCode(project.getProvince()));
        project.setCity(regionMapper.getShortNameByCode(project.getCity()));
        project.setCounty(regionMapper.getNameByCode(project.getCounty()));
        projectMapper.updateProject(project);
    }

    /**
     * 删除工程信息
     *
     * @param projectId 工程ID
     */
    @Override
    public void deleteProjectById(Long projectId)
    {
        projectMapper.deleteProjectById(projectId);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public boolean delProjectBatch(List<Long> ids) {
        return projectMapper.delProjectBatch(ids) > 0;
    }

    /**
     * 获取工程类型列表
     *
     * @return 工程类型集合
     */
    @Override
    public List<ProjectType> selectProjectTypeList() {
        // 这里假设你的工程类型是存在一个单独的表中
        return projectMapper.selectProjectTypeList();
    }

}
