package org.wcs.serve.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBaseMenuService;
import org.wcs.model.dto.serve.menu.AddMenuReq;
import org.wcs.model.dto.serve.menu.UpdateMenuReq;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.dto.serve.menu.QueryMenuReq;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.menu.QueryMenuResp;
import org.wcs.model.po.app.AppBaseMenu;
import org.wcs.serve.service.intf.IMenuService;
import org.wcs.utils.AppBeanUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuService implements IMenuService {

    private final AppBaseMenuService appBaseMenuService;

    /**
     * 查询菜单
     * @param queryMenuReq 查询参数
     * @return 结果
     */
    @Override
    public AppServeDataResponse<List<QueryMenuResp>> queryMenu(QueryMenuReq queryMenuReq) {
        if(queryMenuReq == null) {
            return AppServeResponseFactory.fail("无法识别的参数", null);
        }
        AppBaseMenu appBaseMenu = new AppBaseMenu();
        appBaseMenu.setMenuId(queryMenuReq.getMenuId());
        appBaseMenu.setMenuName(queryMenuReq.getMenuName());
        appBaseMenu.setMenuLevel(queryMenuReq.getMenuLevel());
        List<AppBaseMenu> menuList = appBaseMenuService.queryIndistinct(appBaseMenu);
        if(menuList == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常", null);
        }
        List<QueryMenuResp> returnMenuList = AppBeanUtils.copyListProperties(menuList, QueryMenuResp::new);
        return AppServeResponseFactory.success("查询成功", returnMenuList);
    }

    /**
     * 添加新菜单
     * @param addMenuReq 新菜单参数
     * @return 添加结果
     */
    @Override
    public AppServeResponse addMenu(AddMenuReq addMenuReq) {
        AppBaseMenu checkExist = new AppBaseMenu();
        checkExist.setMenuId(addMenuReq.getMenuId());
        List<AppBaseMenu> checkExistResult = appBaseMenuService.query(checkExist);
        if(checkExistResult == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(!checkExistResult.isEmpty()) {
            return AppServeResponseFactory.fail(String.format("菜单ID %s 已存在", addMenuReq.getMenuId()));
        }
        if(addMenuReq.getMenuLevel().equals(1) && addMenuReq.getFatherMenuId() != null) {
            addMenuReq.setFatherMenuId(null);
        }
        if(addMenuReq.getMenuLevel().equals(2) && addMenuReq.getFatherMenuId() == null) {
            return AppServeResponseFactory.fail("二级菜单必须指定父级菜单");
        }
        AppBaseMenu insertValue = new AppBaseMenu();
        AppBeanUtils.copyProperties(addMenuReq, insertValue);
        insertValue.setCreateTime(LocalDateTime.now());
        insertValue.setUpdateTime(LocalDateTime.now());
        int insertCount = appBaseMenuService.insert(insertValue);
        if(insertCount == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        return AppServeResponseFactory.success(String.format("添加成功，添加了 %d 条记录", insertCount));
    }

    /**
     * 删除一个菜单
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public AppServeResponse deleteMenu(String menuId) {
        /* 判断这个菜单在不在 */
        AppBaseMenu checkExist = new AppBaseMenu();
        checkExist.setMenuId(menuId);
        List<AppBaseMenu> checkExistResult = appBaseMenuService.query(checkExist);
        if(checkExistResult == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(checkExistResult.isEmpty()) {
            return AppServeResponseFactory.fail(String.format("菜单ID %s 不存在", menuId));
        }
        /* 删除菜单 */
        int deleteResult = appBaseMenuService.delete(menuId);
        if(deleteResult == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        return AppServeResponseFactory.success(String.format("删除成功，删除了 %d 条记录", deleteResult));
    }

    /**
     * 更新菜单
     * @param updateMenuReq 更新参数
     * @return 结果
     */
    @Override
    public AppServeResponse updateMenu(UpdateMenuReq updateMenuReq) {
        /* 判断这个菜单在不在 */
        AppBaseMenu checkExist = new AppBaseMenu();
        checkExist.setMenuId(updateMenuReq.getMenuId());
        List<AppBaseMenu> checkExistResult = appBaseMenuService.query(checkExist);
        if(checkExistResult == null) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        if(checkExistResult.isEmpty()) {
            return AppServeResponseFactory.fail(String.format("菜单ID %s 不存在", updateMenuReq.getMenuId()));
        }
        /* 更新菜单 */
        AppBaseMenu updateValue = new AppBaseMenu();
        AppBeanUtils.copyProperties(updateMenuReq, updateValue);
        updateValue.setUpdateTime(LocalDateTime.now());
        int updateResult = appBaseMenuService.update(updateValue);
        if(updateResult == 0) {
            return AppServeResponseFactory.internalServerError("数据服务异常");
        }
        return AppServeResponseFactory.success(String.format("更新成功，更新了 %d 条记录", updateResult));
    }
}
