package com.zhyc.module.produce.bodyManage.service.impl;

import java.util.List;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.SecurityUtils;
import com.zhyc.common.utils.StringUtils;
import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.mapper.BasSheepMapper;
import com.zhyc.module.base.service.IBasSheepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.produce.bodyManage.mapper.ScBodyScoreMapper;
import com.zhyc.module.produce.bodyManage.domain.ScBodyScore;
import com.zhyc.module.produce.bodyManage.service.IScBodyScoreService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 体况评分Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class ScBodyScoreServiceImpl implements IScBodyScoreService {
    @Autowired
    private ScBodyScoreMapper scBodyScoreMapper;
    @Autowired
    private IBasSheepService basSheepService;
    @Autowired
    private BasSheepMapper basSheepMapper;

    /**
     * 查询体况评分
     *
     * @param id 体况评分主键
     * @return 体况评分
     */
    @Override
    public ScBodyScore selectScBodyScoreById(Long id) {
        return scBodyScoreMapper.selectScBodyScoreById(id);
    }

    /**
     * 查询体况评分列表
     *
     * @param scBodyScore 体况评分
     * @return 体况评分
     */
    @Override
    @DataScope(deptAlias = "sbs", userAlias = "sbs")
    public List<ScBodyScore> selectScBodyScoreList(ScBodyScore scBodyScore) {
        return scBodyScoreMapper.selectScBodyScoreList(scBodyScore,
                scBodyScore.getManageTagsList());
    }

    @Override
    public List<String> searchEarNumbers(String query) {
        return scBodyScoreMapper.searchEarNumbers(query.trim());
    }

    /**
     * 新增体况评分
     *
     * @param scBodyScore 体况评分
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertScBodyScore(ScBodyScore scBodyScore) {
        String manageTags = scBodyScore.getManageTags();
        if (StringUtils.isNotBlank(manageTags)) {
            BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
            if (sheep == null) {
                throw new RuntimeException("耳号不存在，请检查输入");
            }
            scBodyScore.setSheepId(sheep.getId().toString());
        }
        scBodyScore.setCreateTime(DateUtils.getNowDate());
        scBodyScore.setCreateBy(SecurityUtils.getUsername());

        int insertResult = scBodyScoreMapper.insertScBodyScore(scBodyScore);

        if (insertResult > 0 && scBodyScore.getSheepId() != null) {
            BasSheep updateSheep = new BasSheep();
            updateSheep.setId(Long.parseLong(scBodyScore.getSheepId()));
            updateSheep.setBody(scBodyScore.getScore());
            basSheepMapper.updateBasSheep(updateSheep);
        }

        return insertResult;
    }

    /**
     * 修改体况评分
     *
     * @param scBodyScore 体况评分
     * @return 结果
     */
    @Override
    public int updateScBodyScore(ScBodyScore scBodyScore) {
        String manageTags = scBodyScore.getManageTags();
        if (StringUtils.isNotBlank(manageTags)) {
            BasSheep sheep = basSheepService.selectBasSheepByManageTags(manageTags);
            if (sheep == null) {
                throw new RuntimeException("耳号不存在，请检查输入");
            }
            scBodyScore.setSheepId(sheep.getId().toString());
        }
        return scBodyScoreMapper.updateScBodyScore(scBodyScore);
    }

    /**
     * 批量删除体况评分
     *
     * @param ids 需要删除的体况评分主键
     * @return 结果
     */
    @Override
    public int deleteScBodyScoreByIds(Long[] ids) {
        return scBodyScoreMapper.deleteScBodyScoreByIds(ids);
    }

    /**
     * 删除体况评分信息
     *
     * @param id 体况评分主键
     * @return 结果
     */
    @Override
    public int deleteScBodyScoreById(Long id) {
        return scBodyScoreMapper.deleteScBodyScoreById(id);
    }
}
