package com.kingtom.shengtai.api.controller;

import java.util.Date;

import com.kingtom.kirin.api.security.SaSecurityUtils;
import com.kingtom.kirin.app.system.model.SystemUser;
import com.kingtom.kirin.app.system.service.ISystemUserService;
import com.kingtom.kirin.core.common.base.Result;
import com.kingtom.kirin.core.common.exception.AppException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 应用模块名称</p>
 * 代码描述</p>
 * Copyright: Copyright (C) 2024 , Inc. All rights reserved. <p>
 * Company: 成都诚唐科技有限责任公司</p>
 *
 * @author wany
 * @since 2024/1/18
 */
@RestController
@RequestMapping("/system_ext")
@Tag(name = "SystemExtController", description = "系统相关扩展接口。")
public class SystemExtController{

    @Autowired
    private ISystemUserService systemUserService;

    @Operation(summary = "更新用户的uniapp客户端Id")
    @PostMapping("/user/client_info")
    public Result<String> updateUserClientId(@RequestBody String clientId){
        String userId = SaSecurityUtils.findSessionData().getUserId();
        SystemUser update = new SystemUser();
        update.setId(userId);
        update.setExt1(clientId);
        update.setGmtModified(new Date());
        boolean b = systemUserService.updateById(update);
        if(b){
            return Result.ok("操作成功！");
        } else{
            return Result.fail("操作失败！");
        }
    }

    @Operation(summary = "更新用户的uniapp客户端Id")
    @PostMapping("/user/client")
    public Result<String> removeUserClientId(@RequestBody String clientId){
        String userId = SaSecurityUtils.findSessionData().getUserId();
        SystemUser update = new SystemUser();
        update.setId(userId);
        update.setExt1("");
        update.setGmtModified(new Date());
        boolean b = systemUserService.updateById(update);
        if(b){
            return Result.ok("操作成功！");
        } else{
            return Result.fail("操作失败！");
        }
    }

    @Operation(description = "获取导入的excel模板")
    @GetMapping("/{type}/importTemplate")
    public ModelAndView getImportTemplate(@PathVariable("type") String type){
        String url = switch(type){
            case "srm_supplier" -> "/srmSuppliers/importTemplate";
            case "org_person" -> "/org/org_persons/importTemplate";
            default -> throw new AppException("导入类型错误！");
        };
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward:" + url);
        return modelAndView;
    }

    @Operation(description = "批量导入")
    @PostMapping("/{type}/import")
    public ModelAndView executeImport(@PathVariable("type") String type){
        String url = switch(type){
            case "srm_supplier" -> "/srmSuppliers/import";
            case "org_person" -> "/org/org_persons/import";
            default -> throw new AppException("导入类型错误！");
        };
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward:" + url);
        return modelAndView;
    }
}
