package com.muyu.enterprise.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.security.annotation.InnerAuth;
import com.muyu.common.system.domain.SysUser;
import com.muyu.domain.FirmUser;
import com.muyu.enterprise.service.FirmUserService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author：杨鹏
 * @Package：com.muyu.enterprise.controller
 * @Project：cloud-vehicle
 * @name：SysUserController
 * @Date：2024/10/8 18:59
 */
@RestController
@RequestMapping("/firm_user")
@Tag(name = "企业用户控制层")
public class FirmUserController extends BaseController {

    /**
     * 企业用户业务层
     */
    @Resource
    private FirmUserService firmUserService;

    /**
     * 企业用户列表
     * @return 返回企业用户列表
     */
    @Schema(description = "企业用户列表")
    @GetMapping("/list")
    public Result<FirmUser> list(){
        return success(firmUserService.list());
    }

    /**
     * 查询企业用户
     * @param userId 主键
     * @return 返回用户信息
     */
    @Schema(description = "查询企业用户")
    @GetMapping("/query/{userId}")
    public Result<FirmUser> query(@PathVariable("userId") Long userId){
        return success(firmUserService.getById(userId));
    }

    @Schema(description = "根据用户名查询企业用户")
    @GetMapping("/query_by_username/{username}")
    @InnerAuth
    public Result<SysUser> queryByUsername(@PathVariable("username") String username){
        LambdaQueryWrapper<FirmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FirmUser::getUserName, username);
        FirmUser firmUser = firmUserService.getOne(queryWrapper);
        return success(FirmUser.builderSysUser(firmUser));
    }

    /**
     * 新增企业用户
     * @param sysUser 新增参数
     * @return 返回新增主键
     */
    @Schema(description = "新增企业用户")
    @PostMapping("/add")
    public Result<Long> add(@RequestBody SysUser sysUser){
        FirmUser firmUser = FirmUser.builderFirmUser(sysUser);
        firmUserService.save(firmUser);
        return success(firmUser.getUserId());
    }

    /**
     * 修改企业用户
     * @param sysUser 修改参数
     * @return
     */
    @Schema(description = "修改企业用户")
    @PutMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser){
        FirmUser firmUser = FirmUser.builderFirmUser(sysUser);
        firmUserService.updateById(firmUser);
        return success();
    }

    /**
     * 删除企业用户
     * @param userId 主键
     * @return
     */
    @Schema(description = "删除企业用户")
    @DeleteMapping("/remove/{userId}")
    public Result remove(@PathVariable("userId") Long userId){
        firmUserService.removeById(userId);
        return success();
    }


}
