package com.yunzhupaas.base.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.Page;
import com.yunzhupaas.base.Pagination;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.entity.ModuleDataEntity;
import com.yunzhupaas.base.entity.ModuleEntity;
import com.yunzhupaas.base.service.ModuleDataService;
import com.yunzhupaas.base.service.ModuleService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.consts.DeviceType;
import com.yunzhupaas.model.login.AllMenuSelectVO;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ：云筑产品开发平台组
 * @version: V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date ：2024/5/6 上午11:19
 */
@Tag(name = "常用菜单", description = "data")
@RestController
@RequestMapping("/api/system/MenuData")
public class ModuleDataController extends SuperController<ModuleDataService, ModuleDataEntity> {

    @Autowired
    private ModuleService moduleService;
    @Autowired
    private ModuleDataService moduleDataService;

    /**
     * 常用菜单
     *
     * @return ignore
     */
    @Operation(summary = "常用菜单")
    @GetMapping
    public ActionResult<ListVO<AllMenuSelectVO>> getDataList(Page page) {
        UserInfo user = UserProvider.getUser();
        String category = DeviceType.PC.getDevice().equals(user.getLoginDevice())?"Web":"App";
        List<ModuleDataEntity> list = moduleDataService.getList(category,page);
        List<AllMenuSelectVO> listVO = new ArrayList<>();
        for (ModuleDataEntity entity : list) {
            ModuleEntity info = moduleService.getInfo(entity.getModuleId());
            if(info != null && Objects.equals(info.getEnabledMark(),1)) {
                AllMenuSelectVO vo = JsonUtil.getJsonToBean(info, AllMenuSelectVO.class);
                vo.setHasChildren(false);
                listVO.add(vo);
            }
        }
        ListVO vo = new ListVO();
        vo.setList(listVO);
        return ActionResult.success(vo);
    }

    /**
     * 新建
     *
     * @return
     */
    @PostMapping("/{id}")
    @Operation(summary = "新建")
    public ActionResult create(@PathVariable("id") String id) {
        if (moduleDataService.isExistByObjectId(id)) {
            return ActionResult.fail(MsgCode.FA036.get());
        }
        moduleDataService.create(id);
        return ActionResult.success(MsgCode.SU016.get());
    }

    /**
     * 删除
     *
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult delete(@PathVariable("id") String id) {
        ModuleDataEntity entity = moduleDataService.getInfo(id);
        if (entity != null) {
            moduleDataService.delete(entity);
            return ActionResult.success(MsgCode.SU021.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }

    /**
     * app常用菜单
     *
     * @return
     */
    @Operation(summary = "app常用菜单")
    @GetMapping("/getDataList")
    public ActionResult<ListVO<AllMenuSelectVO>> getAllList(Page page) {
        List<AllMenuSelectVO> list = moduleDataService.getDataList(page);
        ListVO listVO = new ListVO();
        listVO.setList(list);
        return ActionResult.success(listVO);
    }

    /**
     * app常用详情
     *
     * @return
     */
    @Operation(summary = "app常用详情")
    @GetMapping("/getAppDataList")
    public ActionResult<ListVO<AllMenuSelectVO>> getAppDataList(Pagination pagination) {
        List<AllMenuSelectVO> list = moduleDataService.getAppDataList(pagination);
        ListVO listVO = new ListVO();
        listVO.setList(list);
        return ActionResult.success(listVO);
    }

}

