package com.gic.haoban.manage.service.service.role.impl;

import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.service.dao.mapper.role.TabHaobanMenuMapper;
import com.gic.haoban.manage.service.entity.role.TabHaobanMenu;
import com.gic.haoban.manage.service.pojo.bo.role.HaobanMenuBO;
import com.gic.haoban.manage.service.service.role.HaobanMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author mozhu
 * @since 2022-09-06 16:35:11
 */
@Service
public class HaobanMenuServiceImpl implements HaobanMenuService {

    @Autowired
    private TabHaobanMenuMapper tabHaobanMenuMapper;

    @Override
    public TabHaobanMenu queryById(String menuCode) {
        if (StringUtils.isBlank(menuCode)) {
            return null;
        }
        return this.tabHaobanMenuMapper.queryById(menuCode);
    }

    /**
     * 新增数据
     *
     * @param tabHaobanMenu 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TabHaobanMenu tabHaobanMenu) {
        tabHaobanMenu.setCreateTime(new Date());
        tabHaobanMenu.setUpdateTime(new Date());
        return this.tabHaobanMenuMapper.insert(tabHaobanMenu);
    }

    @Override
    public int insertBatch(List<TabHaobanMenu> tabHaobanMenus) {
        for (TabHaobanMenu tabHaobanMenu : tabHaobanMenus) {
            tabHaobanMenu.setStatusFlag(1);
            tabHaobanMenu.setCreateTime(new Date());
            tabHaobanMenu.setUpdateTime(new Date());
        }
        return tabHaobanMenuMapper.insertBatch(tabHaobanMenus);
    }

    /**
     * 修改数据
     *
     * @param tabHaobanMenu 实例对象
     * @return 实例对象
     */
    @Override
    public int update(TabHaobanMenu tabHaobanMenu) {
        tabHaobanMenu.setUpdateTime(new Date());
        return this.tabHaobanMenuMapper.update(tabHaobanMenu);
    }

    @Override
    public List<HaobanMenuBO> getAllMenuList() {
        List<TabHaobanMenu> allMenuList = tabHaobanMenuMapper.getAllMenuList();
        return EntityUtil.changeEntityListNew(HaobanMenuBO.class, allMenuList);
    }

    @Override
    public List<HaobanMenuBO> getMenuTreeList() {
        List<TabHaobanMenu> pidMenuList = tabHaobanMenuMapper.getPidMenuList();
        List<TabHaobanMenu> notPidMenuList = tabHaobanMenuMapper.getNotPidMenuList();
        List<HaobanMenuBO> pidMenuBOList = EntityUtil.changeEntityListNew(HaobanMenuBO.class, pidMenuList);
        List<HaobanMenuBO> notPidMenuBOList = EntityUtil.changeEntityListNew(HaobanMenuBO.class, notPidMenuList);
        for (HaobanMenuBO haobanMenuBO : pidMenuBOList) {
            List<HaobanMenuBO> menus = iterateMenus(notPidMenuBOList, haobanMenuBO.getMenuCode());
            haobanMenuBO.setMenuBOList(menus);
        }
        return pidMenuBOList;
    }

    @Override
    public List<HaobanMenuBO> getHaobanMenuTreeList(String wxEnterpriseId, Integer clerkType) {
        List<TabHaobanMenu> pidMenuList = tabHaobanMenuMapper.getHaobanPidMenuList(wxEnterpriseId, clerkType, 0);
        List<TabHaobanMenu> notPidMenuList = tabHaobanMenuMapper.getHaobanNotPidMenuList(wxEnterpriseId, clerkType, 0);
        List<HaobanMenuBO> pidMenuBOList = EntityUtil.changeEntityListNew(HaobanMenuBO.class, pidMenuList);
        List<HaobanMenuBO> notPidMenuBOList = EntityUtil.changeEntityListNew(HaobanMenuBO.class, notPidMenuList);
        if (CollectionUtils.isEmpty(pidMenuBOList)) {
            return new ArrayList<>();
        }
        for (HaobanMenuBO haobanMenuBO : pidMenuBOList) {
            if (CollectionUtils.isNotEmpty(notPidMenuBOList)) {
                List<HaobanMenuBO> menus = iterateMenus(notPidMenuBOList, haobanMenuBO.getMenuCode());
                haobanMenuBO.setMenuBOList(menus);
            }
        }
        return pidMenuBOList;
    }

    @Override
    public List<String> getHaobanMenuList(String wxEnterpriseId, Integer clerkType) {
        return tabHaobanMenuMapper.getHaobanMenuListByClerkType(wxEnterpriseId, clerkType, 0);
    }

    @Override
    public List<String> getHaobanMenuByPid(List<String> menuCodes) {
        List<String> list = new ArrayList<>();
        List<String> pidCodes = tabHaobanMenuMapper.getHaobanMenuCodeListByCode(menuCodes);
        List<String> childCodes = tabHaobanMenuMapper.getHaobanChildMenuCodeByPCode(menuCodes);
        list.addAll(pidCodes);
        list.addAll(childCodes);
        return list;
    }

    @Override
    public List<HaobanMenuBO> getHaobanMenuList(List<String> menuCodes) {
        if (CollectionUtils.isEmpty(menuCodes)) {
            return Collections.emptyList();
        }
        List<TabHaobanMenu> haobanMenuListByCode = tabHaobanMenuMapper.getHaobanMenuListByCode(menuCodes);
        return EntityUtil.changeEntityListNew(HaobanMenuBO.class, haobanMenuListByCode);
    }

    @Override
    public HaobanMenuBO getMenuByCode(String menuCode) {
        if (StringUtils.isBlank(menuCode)) {
            return null;
        }
        return EntityUtil.changeEntityNew(HaobanMenuBO.class, tabHaobanMenuMapper.getMenuByCode(menuCode));
    }

    @Override
    public void deleteAll() {
        tabHaobanMenuMapper.deleteAll();
    }

    public List<HaobanMenuBO> iterateMenus(List<HaobanMenuBO> menuBOList, String pCode) {
        List<HaobanMenuBO> result = new ArrayList<>();
        for (HaobanMenuBO haobanMenuBO : menuBOList) {
            String menuCode = haobanMenuBO.getMenuCode();
            String parentCode = haobanMenuBO.getParentCode();
            if (StringUtils.isNotBlank(parentCode)) {
                if (parentCode.equals(pCode)) {
                    // 递归查询当前子菜单的子菜单
                    List<HaobanMenuBO> iterateMenu = iterateMenus(menuBOList, menuCode);
                    haobanMenuBO.setMenuBOList(iterateMenu);
                    result.add(haobanMenuBO);
                }
            }
        }
        return result;
    }

    @Override
    public void deleteByMenuCode(String menuCode) {
        this.tabHaobanMenuMapper.deleteByMenuCode(menuCode);
    }
}
