
package com.lideeyunji.core.framework.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lideeyunji.core.framework.entity.TabEntity;
import com.lideeyunji.core.framework.entity.TabJsEntity;
import com.lideeyunji.core.framework.params.PageTabParam;
import com.lideeyunji.core.framework.params.TabAddOrUpdateParam;
import com.lideeyunji.core.framework.params.vo.PageVo;
import com.lideeyunji.core.framework.params.vo.TabDetailVo;
import com.lideeyunji.core.framework.params.vo.TabWebConfigVo;
import com.lideeyunji.core.framework.service.ITabService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.core.framework.utils.FuncWeb;
import com.lideeyunji.tool.framework.constants.FrameErrorCodeConstants;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiAdapter;
import com.lideeyunji.tool.framework.yunji.annotation.ApiDecryptAes;
import com.lideeyunji.tool.framework.yunji.annotation.ApiEncryptAes;
import com.lideeyunji.tool.framework.yunji.component.redis.lideeYunjiRedisUtils;
import com.lideeyunji.tool.framework.yunji.constant.lideeRedisConstants;
import com.lideeyunji.tool.framework.yunji.model.global.BaseWebResult;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "低代码框架-模块组合接口")
@RestController
@AllArgsConstructor
@RequestMapping(lideeYunJiBaseConstant.REQUEST_URL_START + "/tab")
public class TabController extends BaseController {

    private final ITabService tabService;

    private final lideeYunjiRedisUtils redisUtils;

    private final IlideeYunjiAdapter adapter;


    @GetMapping("/get/web-config")
    @ApiOperationSupport(order = 9)
    @Operation(tags = "表单开发",summary = "获取页面信息配置")
    public BaseWebResult getWebConfig(@RequestParam("tabId")Long tabId) {
        TabWebConfigVo vo = tabService.getWebConfig(tabId);
        return BaseWebResult.success(vo);
    }

    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:create')")
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "模块组合",summary = "模块组合 - 保存")
    public BaseWebResult saveTab(@RequestBody TabAddOrUpdateParam param) {
        tabService.saveOrUpdateTab(param.getTab().getId(), param);
        return BaseWebResult.success("成功");
    }


    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:update')")
    @PutMapping("/update")
    @ApiOperationSupport(order = 1)
    @Operation(tags = "模块组合",summary = "模块组合 - 保存")
    public BaseWebResult updateTab(@RequestBody TabAddOrUpdateParam param) {
        tabService.saveOrUpdateTab(param.getTab().getId(), param);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:delete')")
    @DeleteMapping("/delete")
    @ApiOperationSupport(order = 3)
    @Operation(tags = "模块组合",summary = "模块组合 - 删除")
    public BaseWebResult delTab(@RequestBody List<Long> tabIdList) {
        tabService.delTabList(tabIdList);
        return BaseWebResult.success("成功");
    }

    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:query')")
    @PostMapping("/detail")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "模块组合",summary = "模块组合 - 获取详情")
    public BaseWebResult getTab(Long id) {
        TabDetailVo vo = tabService.getDetail(id);
        return BaseWebResult.success(vo);
    }

    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:query')")
    @PostMapping("/page")
    @ApiOperationSupport(order = 5)
    @Operation(tags = "模块组合",summary = "模块组合 - 获取分页数据")
    public BaseWebResult getPageReportConfig(@RequestBody PageTabParam param, PageVo pageVo) {
        Page page = FuncWeb.getPage(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<TabEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FuncBase.isNotEmpty(param.getId()), TabEntity::getId, param.getId());
        wrapper.eq(FuncBase.isNotEmpty(param.getModuleName()), TabEntity::getModuleName, param.getModuleName());
        wrapper.eq(FuncBase.isNotEmpty(param.getGroupModuleId()), TabEntity::getGroupModuleId, param.getGroupModuleId());
        wrapper.orderByDesc(TabEntity::getId);
        Page<TabEntity> pages = tabService.page(page, wrapper);
        return BaseWebResult.success(pages);
    }

    //=========js增强相关===========
    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:js')")
    @PostMapping("/js/unlock/{tabId}")
    @ApiOperationSupport(order = 6)
    @Operation(tags = "Js增强",summary = "Js增强-解锁")
    public BaseWebResult jsUnlock(@PathVariable("tabId") Long tabId, @RequestParam String type) {

        String redisKey = String.format(lideeRedisConstants.ENHANCE_TAB_JS_LOCK,tabId+":"+type);
        if (redisUtils.hasKey(redisKey)) {
            redisUtils.del(redisKey);
        }
        return BaseWebResult.success("成功");
    }


    //返回数据加密
    @ApiEncryptAes
    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:js')")
    @GetMapping("/js/detail")
    @ApiOperationSupport(order = 7)
    @Operation(tags = "Js增强",summary = "Js增强-详情")
    public BaseWebResult jsDetail(@RequestParam Long tabId,
                                  @RequestParam String type,
                                  @RequestParam String lock){
        if (FuncBase.isNotEmpty(lock) && FuncBase.equals(lock, "true")) {
            String redisKey = String.format(lideeRedisConstants.ENHANCE_TAB_JS_LOCK,tabId+":"+type);
            if (redisUtils.hasKey(redisKey)) {
                String lockMapStr = (String)redisUtils.get(redisKey);
                Map lockMap = Func.json2Bean(lockMapStr, Map.class);
                //返回锁定信息
                return BaseWebResult.error(FrameErrorCodeConstants.FRAME_ENHANCE_ERROR,lockMap);
            }

            String userName = adapter.getOnlineUserName();

            String timeStr = DateUtil.now();
            //存储锁定信息
            Map<String,Object> lockMap=new HashMap<>();
            lockMap.put("userName",userName);
            lockMap.put("timeStr",timeStr);
            redisUtils.set(redisKey, Func.json2Str(lockMap));//上锁
        }
        return BaseWebResult.success(tabService.getJsDetail(tabId,type));
    }

    //解密body数据
    @ApiDecryptAes
    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:js')")
    @PostMapping("/js/save")
    @ApiOperationSupport(order = 8)
    @Operation(tags = "Js增强",summary = "js增强-新增")
    public BaseWebResult jsSave(@RequestBody TabJsEntity model) {
        tabService.saveJs(model);
        return BaseWebResult.success(model.getId());
    }

    //解密body数据
    @ApiDecryptAes
    @PreAuthorize("@ss.hasPermission('lideeyunji:tab:js')")
    @PutMapping("/js/update")
    @ApiOperationSupport(order = 9)
    @Operation(tags = "Js增强",summary = "js增强-修改")
    public BaseWebResult jsUpdate(@RequestBody TabJsEntity model) {
        tabService.updateJs(model);
        return BaseWebResult.success(model.getId());
    }

}
