package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.SystemUserAddDto;
import shop.alien.entity.store.dto.SystemUserStatusDto;
import shop.alien.entity.store.dto.SystemUserUpdateDto;
import shop.alien.entity.store.vo.LifeSysVo;
import shop.alien.entity.store.vo.SystemLoginVo;
import shop.alien.store.service.SystemService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

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()));
    }

    @ApiOperation("web中台退出登录")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/logout")
    public R<SystemLoginVo> logout(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo) {
        return R.success("退出成功");
    }

    @ApiOperation("新增系统用户账号")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/addUser")
    public R<LifeSys> addUser(@RequestBody SystemUserAddDto addDto) {
        log.info("SystemController.addUser => userName={}, realName={}, phone={}, email={}, departmentId={},roleIds={}",
                addDto.getUserName(), addDto.getRealName(), addDto.getPhone(), 
                addDto.getEmail(), addDto.getDepartmentId(),addDto.getRoleIds());
        return systemService.addUser(addDto);
    }

    @ApiOperation("修改系统用户状态")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "/updateUserStatus")
    public R<LifeSys> updateUserStatus(@RequestBody SystemUserStatusDto statusDto) {
        log.info("SystemController.updateUserStatus => userId={}, status={}, remark={}", 
                statusDto.getUserId(), statusDto.getStatus(), statusDto.getRemark());
        return systemService.updateUserStatus(statusDto);
    }

    @ApiOperation("修改系统用户账号信息")
    @ApiOperationSupport(order = 5)
    @PostMapping(value = "/updateUser")
    public R<LifeSys> updateUser(@RequestBody SystemUserUpdateDto updateDto) {
        log.info("SystemController.updateUser => userId={}, userName={}, realName={}, phone={}, email={}, departmentId={},roleIds={}",
                updateDto.getUserId(), updateDto.getUserName(), updateDto.getRealName(), updateDto.getPhone(),
                updateDto.getEmail(), updateDto.getDepartmentId(),updateDto.getRoleIds());
        return systemService.updateUser(updateDto);
    }

    @ApiOperation("重置密码")
    @ApiOperationSupport(order = 6)
    @GetMapping(value = "/resetPassword")
    public R<LifeSys> resetPassword(@RequestParam Integer userId) {
        log.info("SystemController.resetPassword => userId={}", userId);
        return systemService.resetPassword(userId);
    }

    @ApiOperation("分页查询系统用户列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页大小（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "realName", value = "真实姓名（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系电话（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID（精确查询）", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "departmentId", value = "部门ID（精确查询）", dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/getUserPage")
    public R<IPage<LifeSysVo>> getUserPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String realName,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String departmentId,
            @RequestParam(required = false) Long roleId
            ) {
        log.info("SystemController.getUserPage => page={}, size={}, userName={}, realName={}, phone={}, roleId={}, departmentId={}, status={}", page, size, userName, realName, phone, roleId, departmentId);

        return R.data(systemService.getUserPage(page, size, userName, realName, phone, departmentId, roleId));
    }

    // 清除用户的部门
    @ApiOperation("清除用户的部门")
    @ApiOperationSupport(order = 8)
    @GetMapping(value = "/clearUserDepartment")
    public R clearUserDepartment(@RequestParam String userId) {
        log.info("SystemController.clearUserDepartment => userId={}", userId);
        return systemService.clearUserDepartment(userId);
    }

    // 查询部门所有用户
    @ApiOperation("查询所有用户")
    @ApiOperationSupport(order = 9)
    @GetMapping(value = "/getUsers")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页大小（默认10）", dataType = "int", paramType = "query"),
    })
    public R<List<LifeSys>> getUsers(@RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        log.info("SystemController.getUsers => page={}, size={}", page, size);
        List<LifeSys> records = systemService.page(new Page<>(page, size)).getRecords();
        return R.data(records);
    }

/**
 * 废弃
  */
//    @ApiOperation("系统用户注册")
//    @ApiOperationSupport(order = 3)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "body", required = true),
//            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "body", required = true),
//            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String", paramType = "body", required = false),
//    })
//    @PostMapping(value = "/register")
//    public R<LifeSys> register(@RequestBody SystemRegisterDto registerDto) {
//        log.info("SystemController.register?userName={},roleId={}", registerDto.getUserName(), registerDto.getRoleId());
//        return systemService.register(registerDto.getUserName(), registerDto.getPassword(), registerDto.getRoleId());
//    }

}
