package com.willalp.canteen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.canteen.domain.HsCanteenDiningPlace;
import com.willalp.canteen.mapper.HsCanteenDiningPlaceMapper;
import com.willalp.canteen.service.IHsCanteenDiningPlaceService;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 就餐地Service业务层处理
 *
 * @author songyu
 * @date 2022-01-07
 */
@Service
public class HsCanteenDiningPlaceServiceImpl extends ServiceImpl<HsCanteenDiningPlaceMapper, HsCanteenDiningPlace> implements IHsCanteenDiningPlaceService {
    @Autowired
    private HsCanteenDiningPlaceMapper hsCanteenDiningPlaceMapper;

    @Resource
    private ISysConfigService configService;


    /**
     * 查询就餐地列表
     *
     * @param hsCanteenDiningPlace 就餐地
     * @return 就餐地
     */
    @Override
    public List<HsCanteenDiningPlace> selectHsCanteenDiningPlaceList(HsCanteenDiningPlace hsCanteenDiningPlace) {
        return hsCanteenDiningPlaceMapper.selectHsCanteenDiningPlaceList(hsCanteenDiningPlace);
    }

    /**
     * 修改就餐地
     *
     * @param hsCanteenDiningPlace 就餐地
     * @return 结果
     */
    @Override
    public int updateHsCanteenDiningPlace(HsCanteenDiningPlace hsCanteenDiningPlace) {
        return hsCanteenDiningPlaceMapper.updateHsCanteenDiningPlace(hsCanteenDiningPlace);
    }

    /**
     * 删除就餐地信息
     *
     * @param placeId 就餐地主键
     * @return 结果
     */
    @Override
    public int deleteHsCanteenDiningPlaceByPlaceId(Long placeId) {
        return hsCanteenDiningPlaceMapper.deleteHsCanteenDiningPlaceByPlaceId(placeId);
    }

    @Override
    public HsCanteenDiningPlace selectById(Long placeId) {
        return hsCanteenDiningPlaceMapper.selectById(placeId);
    }

    @Override
    public List<HsCanteenDiningPlace> getPlaceByConfigKey(String configKey) {
        String placeNo = configService.selectConfigByKey(configKey);
        if (StringUtils.isBlank(placeNo)) {
            throw new BaseException("请添加对应地址配置Key");
        }
        List<HsCanteenDiningPlace> places = baseMapper.selectList(new QueryWrapper<HsCanteenDiningPlace>()
                .eq("parent_id", placeNo));
        if ( places == null){
            throw new BaseException("请维护兑换物品领取地点");
        }
        return places;
    }
}
