package com.willalp.canteen.service.impl;

import cn.hutool.core.util.ArrayUtil;
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.vo.GetLimitMenuVo;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.PartternIdEnum;
import com.willalp.canteen.mapper.HsCanteenMenuMapper;
import com.willalp.canteen.service.IHsCanteenMenuService;
import com.willalp.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 菜单Service业务层处理
 *
 * @author songyu
 * @date 2021-12-02
 */
@Service
public class HsCanteenMenuServiceImpl extends ServiceImpl<HsCanteenMenuMapper, HsCanteenMenu> implements IHsCanteenMenuService {
    @Autowired
    private HsCanteenMenuMapper hsCanteenMenuMapper;

    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 查询菜单
     *
     * @param menuId 菜单主键
     * @return 菜单
     */
    @Override
    public HsCanteenMenu selectHsCanteenMenuByMenuId(String menuId) {
        return hsCanteenMenuMapper.selectHsCanteenMenuByMenuId(menuId);
    }

    /**
     * 查询菜单列表
     *
     * @param hsCanteenMenu 菜单
     * @return 菜单
     */
    @Override
    public List<HsCanteenMenu> selectHsCanteenMenuList(HsCanteenMenu hsCanteenMenu) {
        return hsCanteenMenuMapper.selectHsCanteenMenuList(hsCanteenMenu);
    }

    /**
     * 新增菜单
     *
     * @param hsCanteenMenu 菜单
     * @return 结果
     */
    @Override
    public AjaxResult insertHsCanteenMenu(HsCanteenMenu hsCanteenMenu) {
        //订单记账消费禁止重叠
        if (hsCanteenMenu.getPatternId() == PartternIdEnum.RESERVE.getValue() ||
                hsCanteenMenu.getPatternId() == PartternIdEnum.CREDIT.getValue()) {
            HsCanteenMenu existMenu = new HsCanteenMenu();
            existMenu.setUseDate(hsCanteenMenu.getUseDate());
            existMenu.setPeriodId(hsCanteenMenu.getPeriodId());
            existMenu.setJgid(hsCanteenMenu.getJgid());
            List<HsCanteenMenu> existMenus = hsCanteenMenuMapper.selectHsCanteenMenuList(existMenu);
            Integer[] patternIds = {1, 2};
            existMenus = existMenus.stream().filter(o -> ArrayUtil.contains(patternIds, o.getPatternId())).collect(Collectors.toList());
            if (existMenus.size() > 0) {
                for (HsCanteenMenu t : existMenus) {
                    if (t.getPatternId() == hsCanteenMenu.getPatternId()) {
                        return AjaxResult.error("已有相同时段菜单");
                    } else {
                        List<String> list = Arrays.asList(hsCanteenMenu.getConsumerType().split(","));
                        List<String> existList = Arrays.asList(t.getConsumerType().split(","));
                        for (String s : list) {
                            if (existList.contains(s)) {
                                return AjaxResult.error("人员类型发生重叠");
                            }
                        }
                    }
                }
            }
        } else if (hsCanteenMenu.getPatternId() == PartternIdEnum.FREEDOM.getValue()) {
            HsCanteenMenu existMenu = new HsCanteenMenu();
            existMenu.setJgid(hsCanteenMenu.getJgid());
            existMenu.setPatternId(PartternIdEnum.FREEDOM.getValue());
            List<HsCanteenMenu> existMenus = hsCanteenMenuMapper.selectHsCanteenMenuList(existMenu);
            if (existMenus.size() != 0) {
                return AjaxResult.error("已存在点餐消费菜单");
            }
        }
        hsCanteenMenuMapper.insert(hsCanteenMenu);
        return AjaxResult.success("保存成功");
    }

    /**
     * 修改菜单
     *
     * @param hsCanteenMenu 菜单
     * @return 结果
     */
    @Override
    public AjaxResult updateHsCanteenMenu(HsCanteenMenu hsCanteenMenu) {
        //订单记账消费禁止重叠
        if (hsCanteenMenu.getPatternId() == PartternIdEnum.RESERVE.getValue() ||
                hsCanteenMenu.getPatternId() == PartternIdEnum.CREDIT.getValue()) {
            HsCanteenMenu existMenu = new HsCanteenMenu();
            existMenu.setUseDate(hsCanteenMenu.getUseDate());
            existMenu.setPeriodId(hsCanteenMenu.getPeriodId());
            existMenu.setJgid(hsCanteenMenu.getJgid());
            List<HsCanteenMenu> existMenus = hsCanteenMenuMapper.selectHsCanteenMenuList(existMenu);
            Integer[] patternIds = {1, 2};
            existMenus = existMenus.stream().filter(o -> ArrayUtil.contains(patternIds, o.getPatternId()) && !o.getMenuId().equals(hsCanteenMenu.getMenuId())).collect(Collectors.toList());
            if (existMenus.size() > 0) {
                for (HsCanteenMenu t : existMenus) {
                    if (t.getPatternId() == hsCanteenMenu.getPatternId()) {
                        return AjaxResult.error("已有相同时段菜单");
                    } else {
                        List<String> list = Arrays.asList(hsCanteenMenu.getConsumerType().split(","));
                        List<String> existList = Arrays.asList(t.getConsumerType().split(","));
                        for (String s : list) {
                            if (existList.contains(s)) {
                                return AjaxResult.error("人员类型发生重叠");
                            }
                        }
                    }
                }
            }
        }
        hsCanteenMenuMapper.updateById(hsCanteenMenu);
        return AjaxResult.success("修改成功");
    }

    /**
     * 批量删除菜单
     *
     * @param menuIds 需要删除的菜单主键
     * @return 结果
     */
    @Override
    public int deleteHsCanteenMenuByMenuIds(String[] menuIds) {
        return hsCanteenMenuMapper.deleteHsCanteenMenuByMenuIds(menuIds);
    }

    /**
     * 删除菜单信息
     *
     * @param menuId 菜单主键
     * @return 结果
     */
    @Override
    public int deleteHsCanteenMenuByMenuId(String menuId) {
        return hsCanteenMenuMapper.deleteHsCanteenMenuByMenuId(menuId);
    }

    @Override
    public List<HsCanteenMenu> getLimitMenu(Date date, String jgid) {
        Integer days = Integer.valueOf(sysConfigService.selectConfigByKey("ydxfxzts"));
        if (null != date) {
            GetLimitMenuVo vo = new GetLimitMenuVo();
            vo.setJgid(jgid);
            vo.setDate(date);
            if (days == null) {
                days = 0;
            }
            vo.setDays(days);
            return hsCanteenMenuMapper.getLimitMenu(vo);
        } else {
            return hsCanteenMenuMapper.selectList(
                    new QueryWrapper<HsCanteenMenu>()
                            .eq("jgid", jgid)
                            .isNull("use_date"));
        }
    }

    @Override
    public List<HsCanteenMenu> selectAppMenuList(List<String> nums, String jgid) {
        return hsCanteenMenuMapper.selectAppMenuList(nums, jgid);
    }
}
