
package com.tmerclub.cloud.platform.controller;

import cn.hutool.core.util.PhoneUtil;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.platform.vo.SysUserVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.platform.dto.SysUserDTO;
import com.tmerclub.cloud.platform.model.SysUser;
import com.tmerclub.cloud.platform.service.SysUserService;
import com.tmerclub.cloud.platform.vo.SysUserSimpleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author lhd
 * @date 2020/12/21
 */
@RequestMapping("/p/sys_user")
@RestController
@Tag(name = "平台用户信息")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @DubboReference
    private AccountFeignClient accountFeignClient;

    @GetMapping("/info")
    @Operation(summary = "登陆平台用户信息", description = "获取当前登陆平台用户的用户信息")
    public ServerResponseEntity<SysUserSimpleVO> info() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        SysUserSimpleVO sysUserSimple = sysUserService.getSimpleByUserId(uidInfoBO.getUserId());
        sysUserSimple.setIsAdmin(uidInfoBO.getIsAdmin());
        sysUserSimple.setUserId(uidInfoBO.getUserId());
        sysUserSimple.setUserName(uidInfoBO.getUsername());
        return ServerResponseEntity.success(sysUserSimple);
    }

    @GetMapping("/page")
    @Operation(summary = "平台用户列表", description = "获取平台用户列表")
    public ServerResponseEntity<PageVO<SysUserVO>> page(@Valid PageDTO pageDTO, SysUserDTO sysUserDTO) {
        sysUserDTO.setShopId(AuthUserContext.getShopId());
        PageVO<SysUserVO> sysUserPage = sysUserService.pageByShopId(pageDTO, sysUserDTO);
        for (SysUserVO sysUserVO : sysUserPage.getList()) {
            sysUserVO.setPhoneNum(Objects.isNull(sysUserVO.getPhoneNum()) ? null : PhoneUtil.hideBetween(sysUserVO.getPhoneNum()).toString());
        }
        return ServerResponseEntity.success(sysUserPage);
    }

    @GetMapping
    @Operation(summary = "获取平台用户信息", description = "根据用户id获取平台用户信息")
    public ServerResponseEntity<SysUserVO> detail(@RequestParam Long sysUserId) {
        SysUserVO sysUserVO = sysUserService.getByUserId(sysUserId);
        sysUserVO.setPhoneNum(Objects.isNull(sysUserVO.getPhoneNum()) ? null : PhoneUtil.hideBetween(sysUserVO.getPhoneNum()).toString());
        ServerResponseEntity<AuthAccountVO> authResponseEntity = accountFeignClient.getByUserIdAndType(sysUserVO.getSysUserId(), SysTypeEnum.PLATFORM.value());
        if (!authResponseEntity.isSuccess()) {
            throw new LuckException(authResponseEntity.getMsg());
        }
        sysUserVO.setIsAdmin(authResponseEntity.getData().getIsAdmin());
        return ServerResponseEntity.success(sysUserVO);
    }

    @PostMapping
    @Operation(summary = "保存平台用户信息", description = "保存平台用户信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SysUserDTO sysUserDTO) {
        SysUser sysUser = BeanUtil.map(sysUserDTO, SysUser.class);
        sysUser.setSysUserId(null);
        sysUser.setHasAccount(0);
        List<Long> roleIds = sysUserDTO.getRoleIds();
        sysUserService.save(sysUser, roleIds);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新平台用户信息", description = "更新平台用户信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SysUserDTO sysUserDTO) {
        SysUser sysUser = BeanUtil.map(sysUserDTO, SysUser.class);
        sysUserService.update(sysUser, sysUserDTO.getRoleIds());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除平台用户信息", description = "根据平台用户id删除平台用户信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long sysUserId) {
        if (Objects.equals(sysUserId, 1L)) {
            throw new LuckException("系统超级管理员不能删除");
        }
        if (Objects.equals(AuthUserContext.get().getUserId(), sysUserId)) {
            throw new LuckException("您正在使用当前账号，无法进行删除操作");
        }
        sysUserService.deleteById(sysUserId);
        return ServerResponseEntity.success();
    }
}
