package com.ruoyi.bst.agreement.service.impl;

import java.util.List;

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.agreement.mapper.AgreementMapper;
import com.ruoyi.bst.agreement.domain.Agreement;
import com.ruoyi.bst.agreement.domain.AgreementVO;
import com.ruoyi.bst.agreement.domain.AgreementQuery;
import com.ruoyi.bst.agreement.service.AgreementService;

/**
 * 协议Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class AgreementServiceImpl implements AgreementService
{
    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private AreaService areaService;
    @Autowired
    private UserService userService;

    /**
     * 查询协议
     *
     * @param id 协议主键
     * @return 协议
     */
    @Override
    public AgreementVO selectAgreementById(Long id)
    {
        return agreementMapper.selectAgreementById(id);
    }

    /**
     * 查询协议列表
     *
     * @param agreement 协议
     * @return 协议
     */
    @Override
    public List<AgreementVO> selectAgreementList(AgreementQuery agreement)
    {
        return agreementMapper.selectAgreementList(agreement);
    }

    /**
     * 新增协议
     *
     * @param agreement 协议
     * @return 结果
     */
    @Override
    public int insertAgreement(AgreementQuery agreement)
    {
        agreement.setCreateTime(DateUtils.getNowDate());
        UserVO user = userService.selectUserById(agreement.getUserId());
        if (user != null){
            if (user.getAreaId() != null){
                agreement.setAreaId(user.getAreaId());
            }
            agreement.setStoreId(agreement.getUserId());
        }
        distinct(agreement);
        return agreementMapper.insertAgreement(agreement);
    }

    /**
     * 修改协议
     *
     * @param agreement 协议
     * @return 结果
     */
    @Override
    public int updateAgreement(Agreement agreement)
    {
        agreement.setUpdateTime(DateUtils.getNowDate());
        distinct(agreement);
        return agreementMapper.updateAgreement(agreement);
    }

    private void distinct(Agreement agreement) {
        if (agreement.getAreaId() != null){
            AreaVO area = areaService.selectAreaById(agreement.getAreaId());
            ServiceUtil.assertion(area == null,"当前区域不存在");
            agreement.setStoreId(area.getUserId());
            ServiceUtil.assertion(agreementMapper.selectDistinct(agreement)!= null,"当前运营区已存在同类协议");
        }
    }

    /**
     * 批量删除协议
     *
     * @param ids 需要删除的协议主键
     * @return 结果
     */
    @Override
    public int deleteAgreementByIds(List<Long> ids)
    {
        return agreementMapper.deleteAgreementByIds(ids);
    }

    /**
     * 删除协议信息
     *
     * @param id 协议主键
     * @return 结果
     */
    @Override
    public int deleteAgreementById(Long id)
    {
        return agreementMapper.deleteAgreementById(id);
    }

    /**
     * 查询最新的协议信息
     * @param query
     * @return
     */
    @Override
    public AgreementVO selectLatest(AgreementQuery query) {
        return agreementMapper.selectLatest(query);
    }
}
