package com.ruoyi.brewery.service.impl;

import java.util.List;

import com.ruoyi.brewery.domain.WyBrewery;
import com.ruoyi.brewery.service.IWyBreweryService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyBreweryAuthMapper;
import com.ruoyi.brewery.domain.WyBreweryAuth;
import com.ruoyi.brewery.service.IWyBreweryAuthService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主体认证信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Service
public class WyBreweryAuthServiceImpl implements IWyBreweryAuthService 
{
    @Autowired
    private WyBreweryAuthMapper wyBreweryAuthMapper;

    @Autowired
    private IWyBreweryService wyBreweryService;
    /**
     * 查询主体认证信息
     * 
     * @param id 主体认证信息主键
     * @return 主体认证信息
     */
    @Override
    public WyBreweryAuth selectWyBreweryAuthByBreweryId(Long id)
    {
        return wyBreweryAuthMapper.selectWyBreweryAuthByBreweryId(id);
    }

    /**
     * 查询主体认证信息
     *
     * @param id 主体认证信息主键
     * @return 主体认证信息
     */
    @Override
    public WyBreweryAuth selectWyBreweryAuthById(Long id)
    {
        return wyBreweryAuthMapper.selectWyBreweryAuthById(id);
    }

    /**
     * 查询主体认证信息列表
     * 
     * @param wyBreweryAuth 主体认证信息
     * @return 主体认证信息
     */
    @Override
    public List<WyBreweryAuth> selectWyBreweryAuthList(WyBreweryAuth wyBreweryAuth)
    {
        return wyBreweryAuthMapper.selectWyBreweryAuthList(wyBreweryAuth);
    }

    /**
     * 新增主体认证信息
     * 
     * @param wyBreweryAuth 主体认证信息
     * @return 结果
     */
    @Override
    public int insertWyBreweryAuth(WyBreweryAuth wyBreweryAuth)
    {
        wyBreweryAuth.setCreateTime(DateUtils.getNowDate());
        final WyBreweryAuth wyBreweryAuth1 = wyBreweryAuthMapper.selectWyBreweryAuthByBreweryId(wyBreweryAuth.getBreweryId());
        if (wyBreweryAuth1 != null){
            throw new RuntimeException("已存在认证信息，请勿重复添加");
        }
        return wyBreweryAuthMapper.insertWyBreweryAuth(wyBreweryAuth);
    }

    /**
     * 修改主体认证信息
     * 
     * @param wyBreweryAuth 主体认证信息
     * @return 结果
     */
    @Override
    public int updateWyBreweryAuth(WyBreweryAuth wyBreweryAuth)
    {
        wyBreweryAuth.setUpdateTime(DateUtils.getNowDate());
        return wyBreweryAuthMapper.updateWyBreweryAuth(wyBreweryAuth);
    }

    @Transactional
    @Override
    public int approvalWyBreweryAuth(WyBreweryAuth wyBreweryAuth)
    {
        final WyBreweryAuth auth = this.selectWyBreweryAuthById(wyBreweryAuth.getId());
        if (auth == null){
            throw new RuntimeException("认证信息不存在");
        }
        final WyBrewery wyBrewery = wyBreweryService.selectWyBreweryById(auth.getBreweryId());
        if (wyBrewery.getAuthorized() == 1) {
            throw new RuntimeException("该酒厂主体已认证");
        }
        wyBreweryAuth.setUpdateTime(DateUtils.getNowDate());
        wyBreweryAuthMapper.updateWyBreweryAuth(wyBreweryAuth);
        if (wyBreweryAuth.getState() == 1){
            // 认证通过,酒厂状态也同步更新
            wyBrewery.setAuthorized(1);
            wyBreweryService.updateWyBrewery(wyBrewery);
        }
        return 1;
    }

    /**
     * 批量删除主体认证信息
     * 
     * @param ids 需要删除的主体认证信息主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryAuthByIds(Long[] ids)
    {
        return wyBreweryAuthMapper.deleteWyBreweryAuthByIds(ids);
    }

    /**
     * 删除主体认证信息信息
     *
     * @param breweryId 主体认证信息主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryAuthByBreweryId(Long breweryId)
    {
        return wyBreweryAuthMapper.deleteWyBreweryAuthByBreweryId(breweryId);
    }

    /**
     * 删除主体认证信息信息
     * 
     * @param id 主体认证信息主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryAuthById(Long id)
    {
        return wyBreweryAuthMapper.deleteWyBreweryAuthById(id);
    }
}
