package cn.iocoder.yudao.module.server.service.techorg;

import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.techorg.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.techorg.TechOrgDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.server.dal.mysql.techorg.TechOrgMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.server.enums.ErrorCodeConstants.*;

/**
 * 技术运维单位 Service 实现类
 *
 * @author TRS
 */
@Service
@Validated
public class TechOrgServiceImpl implements TechOrgService {

    @Resource
    private TechOrgMapper techOrgMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public Long createTechOrg(TechOrgSaveReqVO createReqVO) {
        // 插入
        TechOrgDO techOrg = BeanUtils.toBean(createReqVO, TechOrgDO.class);

        DeptDO deptDO = deptMapper.selectById(createReqVO.getWebsiteDeptName());
        techOrg.setWebsiteDeptName(deptDO.getName());
        techOrg.setWebsiteDept(deptDO.getId());

        DeptDO deptDO2 = deptMapper.selectById(createReqVO.getTechOrgName());
        techOrg.setTechOrgName(deptDO2.getName());
        techOrg.setTechOrgId(deptDO2.getId());

        techOrgMapper.insert(techOrg);
        // 返回
        return techOrg.getId();
    }

    @Override
    public void updateTechOrg(TechOrgSaveReqVO updateReqVO) {
        // 校验存在
        validateTechOrgExists(updateReqVO.getId());
        // 更新
        TechOrgDO updateObj = BeanUtils.toBean(updateReqVO, TechOrgDO.class);

        // 判断websiteDeptName不为空，并且可以转换为数字
        if (updateReqVO.getWebsiteDeptName() != null && isNumeric(updateReqVO.getWebsiteDeptName())) {
            Long deptId = Long.parseLong(updateReqVO.getWebsiteDeptName());
            DeptDO deptDO = deptMapper.selectById(deptId);
            if (deptDO != null) {
                updateObj.setWebsiteDeptName(deptDO.getName());
                updateObj.setWebsiteDept(deptDO.getId());
            }
        }

        if (updateReqVO.getTechOrgName() != null && isNumeric(updateReqVO.getTechOrgName())) {
            DeptDO deptDO2 = deptMapper.selectById(updateReqVO.getTechOrgName());
            if (deptDO2 != null) {
                updateObj.setTechOrgName(deptDO2.getName());
                updateObj.setTechOrgId(deptDO2.getId());
            }
        }


        techOrgMapper.updateById(updateObj);
    }
    // 辅助方法：判断字符串是否可以转换为数字
    private boolean isNumeric(String str) {
        if (str == null || str.trim().isEmpty()) {
            return false;
        }
        try {
            Long.parseLong(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @Override
    public void deleteTechOrg(Long id) {
        // 校验存在
        validateTechOrgExists(id);
        // 删除
        techOrgMapper.deleteById(id);
    }

    private void validateTechOrgExists(Long id) {
        if (techOrgMapper.selectById(id) == null) {
            throw exception(TECH_ORG_NOT_EXISTS);
        }
    }

    @Override
    public TechOrgDO getTechOrg(Long id) {
        TechOrgDO techOrgDO = techOrgMapper.selectById(id);
//        DeptDO deptDO = deptMapper.selectById(techOrgDO.getWebsiteDept());
//        techOrgDO.setWebsiteDeptName(deptDO.getName());
        return techOrgDO;
    }

    @Override
    public PageResult<TechOrgDO> getTechOrgPage(TechOrgPageReqVO pageReqVO) {
        return techOrgMapper.selectPage(pageReqVO);
    }

}