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

import java.util.List;
import java.util.Map;

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.ScBreastRatingMapper;
import com.zhyc.module.produce.bodyManage.domain.ScBreastRating;
import com.zhyc.module.produce.bodyManage.service.IScBreastRatingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 乳房评分Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class ScBreastRatingServiceImpl implements IScBreastRatingService {
    @Autowired
    private ScBreastRatingMapper scBreastRatingMapper;

    @Autowired
    private IBasSheepService basSheepService;

    @Autowired
    private BasSheepMapper basSheepMapper;

    /**
     * 查询乳房评分
     *
     * @param id 乳房评分主键
     * @return 乳房评分
     */
    @Override
    public ScBreastRating selectScBreastRatingById(Long id) {
        return scBreastRatingMapper.selectScBreastRatingById(id);
    }

    /**
     * 查询乳房评分列表
     *
     * @param scBreastRating 乳房评分
     * @return 乳房评分
     */
    @Override
    @DataScope(deptAlias = "sbr", userAlias = "sbr")
    public List<ScBreastRating> selectScBreastRatingList(ScBreastRating scBreastRating) {
        return scBreastRatingMapper.selectScBreastRatingList(scBreastRating,
                scBreastRating.getManageTagsList());
    }

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

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

        if (rows > 0 && scBreastRating.getSheepId() != null) {
            Long sheepId = Long.parseLong(scBreastRating.getSheepId());
            Long score = scBreastRating.getScore().longValue();

            BasSheep updateSheep = new BasSheep();
            updateSheep.setId(sheepId);
            updateSheep.setBreast(score);

            basSheepMapper.updateBasSheep(updateSheep);
        }
        return rows;
    }

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

    /**
     * 批量删除乳房评分
     *
     * @param ids 需要删除的乳房评分主键
     * @return 结果
     */
    @Override
    public int deleteScBreastRatingByIds(Long[] ids) {
        return scBreastRatingMapper.deleteScBreastRatingByIds(ids);
    }

    /**
     * 删除乳房评分信息
     *
     * @param id 乳房评分主键
     * @return 结果
     */
    @Override
    public int deleteScBreastRatingById(Long id) {
        return scBreastRatingMapper.deleteScBreastRatingById(id);
    }
}
