package com.zhyc.module.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.domain.DaSheepfold;
import com.zhyc.module.base.mapper.BasSheepMapper;
import com.zhyc.module.base.mapper.DaSheepfoldMapper;
import com.zhyc.module.base.service.IDaSheepfoldService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 羊舍管理Service业务层处理
 *
 * @author wyt
 * @date 2025-07-11
 */
@Service
public class DaSheepfoldServiceImpl implements IDaSheepfoldService
{
    @Autowired
    private DaSheepfoldMapper daSheepfoldMapper;
    @Autowired
    private BasSheepMapper basSheepMapper;

    // 日志对象
    private static final Logger log = LoggerFactory.getLogger(DaSheepfoldServiceImpl.class);

    /**
     * 查询羊舍管理
     *
     * @param id 羊舍管理主键
     * @return 羊舍管理
     */
    @Override
    public DaSheepfold selectDaSheepfoldById(Long id)
    {
        return daSheepfoldMapper.selectDaSheepfoldById(id);
    }

    /**
     * 查询羊舍管理列表
     *
     * @param daSheepfold 羊舍管理
     * @return 羊舍管理
     */
    @Override
    public List<DaSheepfold> selectDaSheepfoldList(DaSheepfold daSheepfold)
    {
        List<DaSheepfold> sheepfoldList = daSheepfoldMapper.selectDaSheepfoldList(daSheepfold);
        return sheepfoldList; // 修复：原代码重复调用Mapper，改为返回已查询的列表
    }

    /**
     * 羊舍级别汇总查询（主表格）
     */
    public List<DaSheepfold> selectDaSheepfoldSummaryList(DaSheepfold daSheepfold) {
        return daSheepfoldMapper.selectDaSheepfoldSummaryList(daSheepfold);
    }

    /**
     * 新增羊舍管理
     *
     * @param daSheepfold 羊舍管理
     * @return 结果
     */
    @Override
    public int insertDaSheepfold(DaSheepfold daSheepfold)
    {
        return daSheepfoldMapper.insertDaSheepfold(daSheepfold);
    }

    /**
     * 修改羊舍管理
     *
     * @param daSheepfold 羊舍管理
     * @return 结果
     */
    @Override
    public int updateDaSheepfold(DaSheepfold daSheepfold)
    {
        return daSheepfoldMapper.updateDaSheepfold(daSheepfold);
    }

    /**
     * 批量删除羊舍管理
     *
     * @param ids 需要删除的羊舍管理主键
     * @return 结果
     */
    @Override
    public int deleteDaSheepfoldByIds(Long[] ids)
    {
        return daSheepfoldMapper.deleteDaSheepfoldByIds(ids);
    }

    /**
     * 删除羊舍管理信息
     *
     * @param id 羊舍管理主键
     * @return 结果
     */
    @Override
    public int deleteDaSheepfoldById(Long id)
    {
        return daSheepfoldMapper.deleteDaSheepfoldById(id);
    }

    @Override
    public boolean checkSheepfoldNoExist(Long ranchId, Long sheepfoldTypeId, String sheepfoldNo) {
        DaSheepfold query = new DaSheepfold();
        query.setRanchId(ranchId);
        query.setSheepfoldTypeId(sheepfoldTypeId);
        query.setSheepfoldNo(sheepfoldNo);
        return daSheepfoldMapper.selectCount(query) > 0;
    }

    @Override
    public List<BasSheep> sheepListById(String id) {
        List<BasSheep> basSheep = basSheepMapper.selectBasSheepBySheepfold(id);
        return basSheep;
    }

    /**
     * 核心实现：根据组合条件删除（完全自定义，无MP依赖）
     * 条件：ranch_id = ? AND sheepfold_no = ? AND sheepfold_type_id = ?
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSheepfoldByCondition(DaSheepfold sheepfold) {
        // 1. 前置校验：仅校验3个核心字段（移除sheepfoldName的校验）
        if (sheepfold.getRanchId() == null
                || !StringUtils.hasText(sheepfold.getSheepfoldNo())
                || sheepfold.getSheepfoldTypeId() == null) {
            log.error("删除失败：核心条件不能为空！牧场ID={}, 羊舍编号={}, 羊舍类型ID={}",
                    sheepfold.getRanchId(), sheepfold.getSheepfoldNo(), sheepfold.getSheepfoldTypeId());
            throw new IllegalArgumentException("删除失败：牧场ID、羊舍编号、羊舍类型ID不能为空");
        }

        // 2. 调用Mapper方法：仅传递3个核心字段（移除sheepfoldName）
        int deleteCount = daSheepfoldMapper.deleteSheepfoldByCondition(
                sheepfold.getRanchId(),
                sheepfold.getSheepfoldNo(),
                sheepfold.getSheepfoldTypeId()
        );
        boolean isSuccess = deleteCount > 0;

        // 3. 日志打印：移除sheepfoldName相关内容
        if (isSuccess) {
            log.info("成功删除羊舍记录：牧场ID={}, 羊舍编号={}, 羊舍类型ID={}",
                    sheepfold.getRanchId(), sheepfold.getSheepfoldNo(), sheepfold.getSheepfoldTypeId());
        } else {
            log.warn("未找到匹配条件的羊舍记录：牧场ID={}, 羊舍编号={}, 羊舍类型ID={}",
                    sheepfold.getRanchId(), sheepfold.getSheepfoldNo(), sheepfold.getSheepfoldTypeId());
        }

        return isSuccess;
    }
}