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.HsCanteenMenu;
import com.willalp.canteen.domain.HsCanteenMenuRegister;
import com.willalp.canteen.domain.resp.OrderPaginatedListResq;
import com.willalp.common.enums.PartternIdEnum;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.canteen.domain.resp.CuisineListResp;
import com.willalp.canteen.mapper.HsCanteenMenuMapper;
import com.willalp.canteen.mapper.HsCanteenMenuRegisterMapper;
import com.willalp.canteen.service.IHsCanteenMenuRegisterService;
import com.willalp.device.domain.Device;
import com.willalp.device.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 菜单登记Service业务层处理
 *
 * @author songyu
 * @date 2021-11-29
 */
@Service
public class HsCanteenMenuRegisterServiceImpl extends ServiceImpl<HsCanteenMenuRegisterMapper, HsCanteenMenuRegister> implements IHsCanteenMenuRegisterService {
    @Autowired
    private HsCanteenMenuRegisterMapper hsCanteenMenuRegisterMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private HsCanteenMenuMapper canteenMenuMapper;

    /**
     * 查询菜单登记列表
     *
     * @param hsCanteenMenuRegister 菜单登记
     * @return 菜单登记
     */
    @Override
    public List<HsCanteenMenuRegister> selectHsCanteenMenuRegisterList(HsCanteenMenuRegister hsCanteenMenuRegister) {
        return hsCanteenMenuRegisterMapper.selectHsCanteenMenuRegisterList(hsCanteenMenuRegister);
    }

    /**
     * 查询菜单登记列表(包含下单数量)
     *
     * @param hsCanteenMenuRegister 菜单登记
     * @return 菜单登记
     */
    @Override
    public List<HsCanteenMenuRegister> selectRegisterStockList(HsCanteenMenuRegister hsCanteenMenuRegister) {
//        return hsCanteenMenuRegisterMapper.selectRegisterStockList(hsCanteenMenuRegister);
        return hsCanteenMenuRegisterMapper.selectHsCanteenMenuRegisterList(hsCanteenMenuRegister);
    }

    /**
     * 查询菜单登记列表(图片)
     *
     * @param hsCanteenMenuRegister 菜单登记
     * @return 菜单登记
     */
    @Override
    public List<CuisineListResp> selectCuisineList(HsCanteenMenuRegister hsCanteenMenuRegister, Integer pageNum, Integer pageSize) {
        return hsCanteenMenuRegisterMapper.selectCuisineList(hsCanteenMenuRegister, pageNum, pageSize);
    }

    @Override
    public OrderPaginatedListResq appGetDeviceOrderMenu(String deviceNo, Integer pageNum, Integer pageSize) {
        if (StringUtils.isBlank(deviceNo)) {
            throw new BaseException("请补充设备编号");
        }
        Device device = deviceMapper.selectOne(new QueryWrapper<Device>().eq("device_no", deviceNo));
        String placeNumber = device.getPlaceNumber();
        if (StringUtils.isBlank(placeNumber)) {
            throw new BaseException("非法设备，未初始化就餐地");
        }
        List<HsCanteenMenu> hsCanteenMenus = canteenMenuMapper.selectList(new QueryWrapper<HsCanteenMenu>()
                .eq("dining_place", placeNumber)
                .in("pattern_id", PartternIdEnum.FREEDOM.getValue()));
        if (0 == hsCanteenMenus.size()) {
            return new OrderPaginatedListResq();
        }
        List<CuisineListResp> list = new ArrayList<>();
        String menuNumber = hsCanteenMenus.get(0).getMenuNumber();
        OrderPaginatedListResq orderPaginatedListResq = new OrderPaginatedListResq();
        if (StringUtils.isNotBlank(menuNumber)) {
            HsCanteenMenuRegister cmr = new HsCanteenMenuRegister();
            cmr.setMenuNumber(menuNumber);
            orderPaginatedListResq.setList(this.selectCuisineList(cmr, (pageNum - 1) * pageSize, pageSize));
            Integer integer = hsCanteenMenuRegisterMapper.selectCuisineCount(cmr);
            orderPaginatedListResq.setTotal(integer);
            Integer pageListSize = 0;
            if ((integer % pageSize) == 0) {
                pageListSize = integer / pageSize;
            } else {
                pageListSize = (integer / pageSize) + 1;
            }
            for (Integer i = 1; i <= pageListSize; i++) {
                orderPaginatedListResq.getPageList().add(i);
            }
        }
        return orderPaginatedListResq;
    }
}
