package com.ylx.massage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.constant.MassageConstants;
import com.ylx.common.exception.ServiceException;
import com.ylx.massage.mapper.TFareSettingMapper;
import com.ylx.massage.domain.TFareSetting;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;
import com.ylx.massage.service.TFareSettingService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 车费设置表(TFareSetting)表服务实现类
 *
 * @author makejava
 * @since 2024-08-22 17:41:00
 */
@Service("tFareSettingService")
public class TFareSettingServiceImpl extends ServiceImpl<TFareSettingMapper, TFareSetting> implements TFareSettingService {

    @Override
    public TFareSetting add(TFareSetting tFareSetting) {
        //判断部门重复设置
        if (tFareSetting.getEnable().equals(MassageConstants.INTEGER_ONE)) {
            LambdaQueryWrapper<TFareSetting> tFareSettingLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tFareSettingLambdaQueryWrapper.eq(TFareSetting::getDeptId, tFareSetting.getDeptId())
                    .eq(TFareSetting::getEnable, MassageConstants.INTEGER_ONE);
            List<TFareSetting> fareSettings = this.list(tFareSettingLambdaQueryWrapper);
            if (!Collections.isEmpty(fareSettings)) {
                throw new ServiceException("该部门已有启用的设置");
            }
        }
        this.save(tFareSetting);
        return tFareSetting;
    }

    @Override
    public TFareSetting updateFareSetting(TFareSetting tFareSetting) {
        if (tFareSetting.getEnable().equals(MassageConstants.INTEGER_ONE)) {
            LambdaQueryWrapper<TFareSetting> tFareSettingLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tFareSettingLambdaQueryWrapper.eq(TFareSetting::getDeptId, tFareSetting.getDeptId())
                    .eq(TFareSetting::getEnable, MassageConstants.INTEGER_ONE);
            //该部门已启用的
            List<TFareSetting> fareSettings = this.list(tFareSettingLambdaQueryWrapper);
            if (!Collections.isEmpty(fareSettings)) {
                if(!fareSettings.get(MassageConstants.INTEGER_ZERO).getId().equals(tFareSetting.getId())){
                    throw new ServiceException("该部门已有启用的设置");
                }
            }
        }
        this.updateById(tFareSetting);
        return tFareSetting;
    }
}

