package com.ruoyi.web.modules.farming.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.modules.farming.domain.BizTechServicePersonnel;
import com.ruoyi.web.modules.farming.domain.dto.TechServicePersonnelSaveRequest;
import com.ruoyi.web.modules.farming.domain.vo.TechServicePersonnelVo;
import com.ruoyi.web.modules.farming.mapper.BizTechServicePersonnelMapper;
import com.ruoyi.web.modules.farming.service.IBizTechServicePersonnelService;
import com.ruoyi.web.modules.farming.support.TechServicePersonnelRules;
import com.ruoyi.web.modules.farming.support.TechServicePersonnelValidation;

/**
 * 科技服务人员管理服务实现（单例 upsert）。
 */
@Service
public class BizTechServicePersonnelServiceImpl implements IBizTechServicePersonnelService
{
    @Autowired
    private BizTechServicePersonnelMapper techServicePersonnelMapper;

    @Override
    public TechServicePersonnelVo getCurrentConfig()
    {
        BizTechServicePersonnel row =
                techServicePersonnelMapper.selectBizTechServicePersonnelById(TechServicePersonnelRules.SINGLETON_ID);
        return TechServicePersonnelValidation.toVo(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveConfig(TechServicePersonnelSaveRequest request, String operator)
    {
        TechServicePersonnelValidation.validateForSave(request);
        BizTechServicePersonnel row = TechServicePersonnelValidation.toEntity(request);
        BizTechServicePersonnel existing =
                techServicePersonnelMapper.selectBizTechServicePersonnelById(TechServicePersonnelRules.SINGLETON_ID);
        if (existing == null)
        {
            row.setCreateBy(operator);
            return techServicePersonnelMapper.insertBizTechServicePersonnel(row);
        }
        row.setUpdateBy(operator);
        return techServicePersonnelMapper.updateBizTechServicePersonnel(row);
    }
}
