package shop.alien.gateway.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeSys;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.SystemLoginDto;
import shop.alien.entity.store.dto.SystemRegisterDto;
import shop.alien.entity.store.vo.SystemLoginVo;
import shop.alien.gateway.service.SystemService;

import java.util.List;

/**
 * 商家会员记录 前端控制器
 *
 * @author ssk
 * @since 2025-02-20
 */
@Slf4j
@Api(tags = {"数据中台基础服务接口类"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/sys")
@RequiredArgsConstructor
public class SystemController {

    private final SystemService systemService;

    @ApiOperation("web中台登录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/login")
    public R<SystemLoginVo> getStoreVipLogPage(@RequestBody SystemLoginDto systemLogin) {
        return R.data(systemService.login(systemLogin.getUsername(), systemLogin.getPassword()));
    }

    /**
     * web中台退出登录
     * <p>
     * 退出登录时会执行以下操作：
     * 1. 参数校验：验证用户登录信息是否有效
     * 2. 删除Redis中的token缓存
     * 3. 记录退出登录日志
     * </p>
     *
     * @param userLoginInfo 用户登录信息（从token中解析，包含userId、userName等信息）
     * @return R<SystemLoginVo> 退出登录结果，包含退出状态信息
     * @throws RuntimeException 当用户信息为空或退出登录失败时抛出异常
     */
    @ApiOperation(value = "web中台退出登录", notes = "用户退出登录，清除Redis中的token缓存")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/logout")
    public R<SystemLoginVo> logout(@RequestBody UserLoginInfo userLoginInfo) {
        log.info("SystemController.logout?userId={}, userName={}", 
                userLoginInfo != null ? userLoginInfo.getUserId() : null,
                userLoginInfo != null ? userLoginInfo.getUserName() : null);
        
        try {
            SystemLoginVo result = systemService.logout(userLoginInfo);
            log.info("SystemController.logout成功?userId={}, userName={}", 
                    userLoginInfo != null ? userLoginInfo.getUserId() : null,
                    userLoginInfo != null ? userLoginInfo.getUserName() : null);
            return R.data(result, "退出成功");
        } catch (Exception e) {
            log.error("SystemController.logout失败?userId={}, userName={}, error={}", 
                    userLoginInfo != null ? userLoginInfo.getUserId() : null,
                    userLoginInfo != null ? userLoginInfo.getUserName() : null,
                    e.getMessage(), e);
            return R.fail("退出登录失败：" + e.getMessage());
        }
    }

    @ApiOperation("web中台分配律师账号")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "userPassword", value = "密码", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "Integer", paramType = "body", required = true),
            @ApiImplicitParam(name = "status", value = "启用 禁用", dataType = "Integer", paramType = "body", required = true),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "body", required = true),
    })
    @PostMapping(value = "/register")
    public R<LifeSys> register(@RequestBody SystemRegisterDto registerDto) {
        log.info("SystemController.register?userName={},roleId={},phone={}", registerDto.getUserName(), registerDto.getRoleId());
        return systemService.register(registerDto.getUserName(), registerDto.getUserPassword(), registerDto.getRoleId(), registerDto.getStatus(), registerDto.getRemark());
    }

    @ApiOperation("web中台编辑账号")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "userPassword", value = "密码", dataType = "String", paramType = "body", required = true)
    })
    @PostMapping(value = "/updateAccounInfo")
    public int updateAccounInfo(@RequestBody LifeSys lifeSys) {
        log.info("SystemController.register?lifeSys={}", lifeSys);
        return systemService.updateAccounInfo(lifeSys);
    }

    @ApiOperation("根据ID查询系统用户")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping(value = "/getById")
    public R<LifeSys> getById(@RequestParam("id") Integer id) {
        log.info("SystemController.getById?id={}", id);
        return systemService.getById(id);
    }

    @ApiOperation("查询系统用户列表")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "账号状态", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping(value = "/list")
    public R<List<LifeSys>> list(@RequestParam(value = "userName", required = false) String userName,
                                  @RequestParam(value = "status", required = false) Integer status) {
        log.info("SystemController.list?userName={},status={}", userName, status);
        return systemService.list(userName, status);
    }

    @ApiOperation("删除系统用户")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @DeleteMapping(value = "/delete")
    public R<String> deleteById(@RequestParam("id") Integer id) {
        log.info("SystemController.deleteById?id={}", id);
        return systemService.deleteById(id);
    }

    @ApiOperation("中台路由配置")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "路由类型", dataType = "Integer", paramType = "query")
    })
    @GetMapping(value = "/routingInfo")
    public String routingInfo(@RequestParam(value = "type") Integer type) {
        log.info("SystemController.list?type={}", type);
        //type 是 1 为admin登录
        if(type == 1){
            String strInfo = "{\n" +
                    "  \"code\": 200,\n" +
                    "  \"data\": [\n" +
                    "    {\n" +
                    "      \"path\": \"/home/index\",\n" +
                    "      \"name\": \"home\",\n" +
                    "      \"component\": \"/home/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"List\",\n" +
                    "        \"title\": \"首页\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": false,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/lawyerManagement\",\n" +
                    "      \"name\": \"lawyerManagement\",\n" +
                    "      \"component\": \"/lawyerManagement/lawFirm/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"Opportunity\",\n" +
                    "        \"title\": \"律师管理\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": false,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      },\n" +
                    "      \"children\": [\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/lawFirm\",\n" +
                    "          \"name\": \"lawFirm\",\n" +
                    "          \"component\": \"/lawyerManagement/lawFirm/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"律所信息\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/lawyer\",\n" +
                    "          \"name\": \"lawyer\",\n" +
                    "          \"component\": \"/lawyerManagement/lawyer/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"律师信息\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/legalScene\",\n" +
                    "          \"name\": \"legalScene\",\n" +
                    "          \"component\": \"/lawyerManagement/legalScene/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"法律场景\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/professionalField\",\n" +
                    "          \"name\": \"professionalField\",\n" +
                    "          \"component\": \"/lawyerManagement/professionalField/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"专业领域\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/reconciliation\",\n" +
                    "          \"name\": \"reconciliation\",\n" +
                    "          \"component\": \"/lawyerManagement/reconciliation/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"对账总览\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/reconciliation/lawyerDetail\",\n" +
                    "          \"name\": \"lawyerDetail\",\n" +
                    "          \"component\": \"/lawyerManagement/reconciliation/lawyerDetail\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"对账总览\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/userManagement\",\n" +
                    "      \"name\": \"userManagement\",\n" +
                    "      \"component\": \"/userManagement/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"用户管理\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/refund\",\n" +
                    "      \"name\": \"refund\",\n" +
                    "      \"component\": \"/refund/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"举报审核\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/appeal\",\n" +
                    "      \"name\": \"appeal\",\n" +
                    "      \"component\": \"/appeal/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"申诉审核\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    }\n" +
                    "  ],\n" +
                    "  \"msg\": \"成功\"\n" +
                    "}";
            return strInfo;
        }else{
            String strInfo = "{\n" +
                    "  \"code\": 200,\n" +
                    "  \"data\": [\n" +
                    "    {\n" +
                    "      \"path\": \"/home/index\",\n" +
                    "      \"name\": \"home\",\n" +
                    "      \"component\": \"/home/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"List\",\n" +
                    "        \"title\": \"首页\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": false,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/lawyerManagement\",\n" +
                    "      \"name\": \"lawyerManagement\",\n" +
                    "      \"component\": \"/lawyerManagement/lawFirm/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"Opportunity\",\n" +
                    "        \"title\": \"律师管理\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": false,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      },\n" +
                    "      \"children\": [\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/lawFirm\",\n" +
                    "          \"name\": \"lawFirm\",\n" +
                    "          \"component\": \"/lawyerManagement/lawFirm/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"律所信息\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/lawyer\",\n" +
                    "          \"name\": \"lawyer\",\n" +
                    "          \"component\": \"/lawyerManagement/lawyer/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"律师信息\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/legalScene\",\n" +
                    "          \"name\": \"legalScene\",\n" +
                    "          \"component\": \"/lawyerManagement/legalScene/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"法律场景\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/professionalField\",\n" +
                    "          \"name\": \"professionalField\",\n" +
                    "          \"component\": \"/lawyerManagement/professionalField/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"专业领域\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/reconciliation\",\n" +
                    "          \"name\": \"reconciliation\",\n" +
                    "          \"component\": \"/lawyerManagement/reconciliation/index\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"对账总览\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": true,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"path\": \"/lawyerManagement/reconciliation/lawyerDetail\",\n" +
                    "          \"name\": \"lawyerDetail\",\n" +
                    "          \"component\": \"/lawyerManagement/reconciliation/lawyerDetail\",\n" +
                    "          \"meta\": {\n" +
                    "            \"icon\": \"Briefcase\",\n" +
                    "            \"title\": \"对账总览\",\n" +
                    "            \"isLink\": \"\",\n" +
                    "            \"isHide\": false,\n" +
                    "            \"isFull\": false,\n" +
                    "            \"isAffix\": false,\n" +
                    "            \"isKeepAlive\": false\n" +
                    "          }\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/userManagement\",\n" +
                    "      \"name\": \"userManagement\",\n" +
                    "      \"component\": \"/userManagement/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"用户管理\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/refund\",\n" +
                    "      \"name\": \"refund\",\n" +
                    "      \"component\": \"/refund/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"举报审核\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"path\": \"/appeal\",\n" +
                    "      \"name\": \"appeal\",\n" +
                    "      \"component\": \"/appeal/index\",\n" +
                    "      \"meta\": {\n" +
                    "        \"icon\": \"UserFilled\",\n" +
                    "        \"title\": \"申诉审核\",\n" +
                    "        \"isLink\": \"\",\n" +
                    "        \"isHide\": true,\n" +
                    "        \"isFull\": false,\n" +
                    "        \"isAffix\": false,\n" +
                    "        \"isKeepAlive\": false\n" +
                    "      }\n" +
                    "    }\n" +
                    "  ],\n" +
                    "  \"msg\": \"成功\"\n" +
                    "}";
            return strInfo;
        }

    }

}
