package com.ruoyi.ss.feeRule.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.feeRule.domain.FeeRule;
import com.ruoyi.ss.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.feeRule.mapper.FeeRuleMapper;
import com.ruoyi.ss.room.domain.BandRuleDTO;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.service.IRoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.feeRule.domain.FeeRuleQuery;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import org.springframework.transaction.support.TransactionTemplate;


/**
 * 收费模板Service业务层处理
 *
 * @author qzz
 * @date 2024-11-20
 */
@Service
public class FeeRuleServiceImpl implements IFeeRuleService
{
    private static final Logger log = LoggerFactory.getLogger(FeeRuleServiceImpl.class);

    @Autowired
    private FeeRuleMapper eFeeRuleMapper;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private TransactionTemplate transactionTemplate;


    /**
     * 查询收费模板
     *
     * @param ruleId 收费模板主键
     * @return 收费模板
     */
    @Override
    public FeeRuleVO selectEFeeRuleByRuleId(Long ruleId)
    {
        return eFeeRuleMapper.selectEFeeRuleByRuleId(ruleId);
    }

    /**
     * 查询收费模板列表
     *
     * @param query 收费模板
     * @return 收费模板
     */
    @Override
    public List<FeeRuleVO> selectEFeeRuleList(FeeRuleQuery query)
    {
        if(query.getRoomId() != null){
            RoomVO roomVO = roomService.selectERoomByRoomId(query.getRoomId());
            ServiceUtil.assertion(roomVO == null, "房间不存在");
            List<Long> ruleIds = roomVO.getRuleIds();
            if(ruleIds.isEmpty()){
                query.setRuleIds(Collections.singletonList(0L));
            }else{
                query.setRuleIds(ruleIds);
            }
        }
        return eFeeRuleMapper.selectEFeeRuleList(query);
    }

    /**
     * 新增收费模板
     *
     * @param eFeeRule 收费模板
     * @return 结果
     */
    @Override
    public int insertEFeeRule(FeeRule eFeeRule)
    {
        eFeeRule.setCreateTime(DateUtils.getNowDate());
        Boolean execute = transactionTemplate.execute(e -> {
            int i = eFeeRuleMapper.insertEFeeRule(eFeeRule);
            ServiceUtil.assertion(i == 0, "新增收费模板失败");
            bandRoom(eFeeRule);
            return Boolean.TRUE;
        });
        if(Boolean.FALSE.equals(execute))throw new ServiceException("新增收费模板失败");
        return 1;
    }

//    /**
//     * 校验小时模式唯一
//     *
//     * @param eFeeRule 收费模板
//     * @return 结果
//     */
//    private void verifyHourlyUnique(FeeRule eFeeRule) {
//        if (eFeeRule.getMode().equals(CHARGE_MODE_HOURLY)){
//            int i = eFeeRuleMapper.verifyHourlyUnique(eFeeRule.getRoomId());
//            ServiceUtil.assertion(i > 0, "该房间已存在小时计费模板");
//        }
//    }

    /**
     * 修改收费模板
     *
     * @param eFeeRule 收费模板
     * @return 结果
     */
    @Override
    public int updateEFeeRule(FeeRule eFeeRule)
    {
        Boolean execute = transactionTemplate.execute(e -> {
            int i = eFeeRuleMapper.updateEFeeRule(eFeeRule);
            ServiceUtil.assertion(i == 0, "修改收费模板失败");
            int i2 = eFeeRuleMapper.unBandRuleById(eFeeRule.getRuleId());
            bandRoom(eFeeRule);
            return Boolean.TRUE;
        });
        if(Boolean.FALSE.equals(execute))throw new ServiceException("修改收费模板失败");
        return 1;
    }


    private void bandRoom(FeeRule eFeeRule) {
        if(eFeeRule.getRoomIds() !=null && !eFeeRule.getRoomIds().isEmpty()){
            List<Long> roomIds = eFeeRule.getRoomIds();
            for (Long roomId : roomIds){
                BandRuleDTO dto = new BandRuleDTO(eFeeRule.getRuleId(),roomId);
                int i1 = roomService.bandRule(dto);
                ServiceUtil.assertion(i1 == 0, "修改收费模板，绑定套餐失败");
            }
        }
    }

    /**
     * 批量删除收费模板
     *
     * @param ruleIds 需要删除的收费模板主键
     * @return 结果
     */
    @Override
    public int deleteEFeeRuleByRuleIds(Long[] ruleIds)
    {
        return eFeeRuleMapper.deleteEFeeRuleByRuleIds(ruleIds);
    }

    /**
     * 删除收费模板信息
     *
     * @param ruleId 收费模板主键
     * @return 结果
     */
    @Override
    public int deleteEFeeRuleByRuleId(Long ruleId)
    {
        return eFeeRuleMapper.deleteEFeeRuleByRuleId(ruleId);
    }

    /**
     * 根据房间id获取套餐
     */
    @Override
    public List<FeeRuleVO> selectEFeeRuleListByRuleIds(List<Long> ruleIds) {
        FeeRuleQuery dto = new FeeRuleQuery();
        dto.setRuleIds(ruleIds);
        return selectEFeeRuleList(dto);
    }

    @Override
    public FeeRule selectEveryHoursFeeRuleByRoomId(Long roomId) {
        return null;
    }

    @Override
    public List<FeeRuleVO> selectCustomFeeRuleByRoomId(Long roomId) {
        return Collections.emptyList();
    }

    /**
     * 校验是否存在基础收费模板
     */
    @Override
    public FeeRuleVO isExistBasicsRule(List<Long> ids) {
        FeeRuleQuery dto = new FeeRuleQuery();
        dto.setRuleIds(ids);
        List<FeeRuleVO> feeRuleVOS = selectEFeeRuleList(dto);
        Optional<FeeRuleVO> result = feeRuleVOS.stream()
                .filter(feeRule -> feeRule.getMode().equals("1"))
                .findFirst();
        return result.orElse(null);
    }
}
