package com.yunzhupaas.base.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.entity.PortalManageEntity;
import com.yunzhupaas.base.model.portalManage.*;
import com.yunzhupaas.base.service.PortalManageService;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.constant.MsgCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门户管理
 *
 * @author 云筑产品开发平台组
 * @version v3.4.6
 * @copyrignt 深圳市乐程软件有限公司
 * @date 2023-02-16
 */
@Slf4j
@RestController
@Tag(name = "门户管理", description = "PortalManage")
@RequestMapping("/api/system/PortalManage")
public class PortalManageController extends SuperController<PortalManageService, PortalManageEntity> {

    @Autowired
    PortalManageService portalManageService;
//    @Autowired
//    PortalService portalApi;
//    @Autowired
//    private AuthorizeService authorizeService;

    @Operation(summary = "新增")
    @PostMapping
    public ActionResult<String> create(@RequestBody @Valid PortalManageCreForm portalManageForm) {
        PortalManageEntity entity = portalManageForm.convertEntity();
        try {
            portalManageService.checkCreUp(entity);
        } catch (Exception e) {
            return ActionResult.fail(e.getMessage());
        }
        portalManageService.save(entity);
        return ActionResult.success(MsgCode.SU018.get());
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public ActionResult<String> delete(@PathVariable String id) {
        boolean flag = portalManageService.removeById(id);
        if(flag){
            // 删除绑定的所有权限
//            authorizeService.remove(new AuthorizePortalManagePrimary(null, id).getQuery());
            return ActionResult.success(MsgCode.SU003.get());
        } else {
            return ActionResult.fail(MsgCode.FA003.get());
        }
    }

    @Operation(summary = "编辑")
    @PutMapping("/{id}")
    public ActionResult<String> update(@PathVariable("id") String id, @RequestBody @Valid PortalManageUpForm portalManageUpForm){
        PortalManageEntity update = portalManageUpForm.convertEntity();
        try {
            portalManageService.checkCreUp(update);
        } catch (Exception e) {
            return ActionResult.fail(e.getMessage());
        }
        portalManageService.updateById(update);
        return ActionResult.success(MsgCode.SU004.get());
    }

    @Operation(summary = "查看")
    @GetMapping("/{id}")
    public ActionResult<PortalManageVO> getOne(@PathVariable("id") String id) {
        PortalManageEntity entity = portalManageService.getById(id);
        return ActionResult.success(portalManageService.convertVO(entity));
    }

    @Operation(summary = "列表")
    @GetMapping("/list/{systemId}")
    public ActionResult<PageListVO<PortalManageVO>> getPage(@PathVariable("systemId") String systemId, PortalManagePage pmPage) {
        pmPage.setSystemId(systemId);
        return ActionResult.page(
                portalManageService.getPage(pmPage).getRecords()
                        .stream().map(PortalManagePageDO::convert).collect(Collectors.toList()),
                pmPage.getPaginationVO());
    }

}

