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

import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.qdto.role.HaobanMenuQDTO;
import com.gic.haoban.manage.api.dto.role.HaobanMenuDTO;
import com.gic.haoban.manage.api.service.role.HaobanMenuApiService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HaobanMenuApiServiceImpl implements HaobanMenuApiService {

    @Autowired
    private HaobanMenuService haobanMenuService;


    @Override
    public ServiceResponse add(List<HaobanMenuQDTO> haobanMenuDTOList) {
        for (HaobanMenuQDTO haobanMenuQDTO : haobanMenuDTOList) {
            TabHaobanMenu tabHaobanMenu = haobanMenuService.queryById(haobanMenuQDTO.getMenuCode());
            if (tabHaobanMenu != null) {
                haobanMenuService.update(EntityUtil.changeEntityNew(TabHaobanMenu.class, haobanMenuQDTO));
                continue;
            }
            haobanMenuService.insert(EntityUtil.changeEntityNew(TabHaobanMenu.class, haobanMenuQDTO));
        }
        return ServiceResponse.success();
    }


    @Override
    public ServiceResponse addList(List<HaobanMenuQDTO> haobanMenuDTOList) {
        haobanMenuService.deleteAll();
        haobanMenuService.insertBatch(EntityUtil.changeEntityListNew(TabHaobanMenu.class, haobanMenuDTOList));
        return ServiceResponse.success();
    }


    @Override
    public ServiceResponse<List<HaobanMenuDTO>> getAllMenuList() {
        List<HaobanMenuBO> allMenuList = haobanMenuService.getAllMenuList();
        return ServiceResponse.success(EntityUtil.changeEntityListNew(HaobanMenuDTO.class,allMenuList));
    }

    @Override
    public ServiceResponse<List<HaobanMenuDTO>> getMenuTreeList() {
        List<HaobanMenuBO> menuTreeList = haobanMenuService.getMenuTreeList();
        return ServiceResponse.success(EntityUtil.changeEntityListNew(HaobanMenuDTO.class, menuTreeList));
    }

    @Override
    public ServiceResponse<List<HaobanMenuDTO>> getHaobanMenuTreeList(String wxEnterpriseId, Integer clerkType) {
        List<HaobanMenuBO> haobanMenuBOList = haobanMenuService.getHaobanMenuTreeList(wxEnterpriseId, clerkType);
        return ServiceResponse.success(EntityUtil.changeEntityListNew(HaobanMenuDTO.class, haobanMenuBOList));
    }

    @Override
    public ServiceResponse<String> getMenuNameByCode(String menuCode) {
        HaobanMenuBO haobanMenuBO = haobanMenuService.getMenuByCode(menuCode);
        if (haobanMenuBO == null) {
            return ServiceResponse.success();
        }
        return ServiceResponse.success(haobanMenuBO.getMenuName());
    }
}
