package com.ruoyi.cms.controller.cms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.common.annotation.BussinessLog;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.cms.service.IAppUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * APP用户Controller
 * 
 * @author lishundong
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/cms/appUser")
@Api(tags = "后台：APP用户管理")
public class CmsAppUserController extends BaseController
{
    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询APP用户列表
     */
    @ApiOperation("查询APP用户列表")
//    @PreAuthorize("@ss.hasPermi('cms:appUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser appUser)
    {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 导出APP用户列表
     */
    @ApiOperation("导出APP用户列表")
    @PreAuthorize("@ss.hasPermi('cms:appUser:export')")
    @Log(title = "APP用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppUser appUser)
    {
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        util.exportExcel(response, list, "APP用户数据");
    }

    /**
     * 获取APP用户详细信息
     */
    @ApiOperation("获取APP用户详细信息")
    @PreAuthorize("@ss.hasPermi('bussiness:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(appUserService.selectAppUserByUserId(userId));
    }

    /**
     * 新增APP用户
     */
    @ApiOperation("新增APP用户")
    @PreAuthorize("@ss.hasPermi('bussiness:user:add')")
    @Log(title = "APP用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppUser appUser)
    {
        return toAjax(appUserService.insertAppUser(appUser));
    }

    /**
     * 修改APP用户
     */
    @ApiOperation("修改APP用户")
    @Log(title = "APP用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppUser appUser)
    {
        if(appUser.getUserId()==null){
            return AjaxResult.error("参数userId为空");
        }
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除APP用户
     */
    @ApiOperation("删除APP用户")
    @PreAuthorize("@ss.hasPermi('bussiness:user:remove')")
    @Log(title = "APP用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(appUserService.deleteAppUserByUserIds(userIds));
    }

    /**
     * 获取APP用户详细信息（pc端获取移动端简历信息）
     */
    @ApiOperation("pc端个人简历信息-获取APP用户详细信息")
    @GetMapping(value = "/getUserInfo")
    public AjaxResult getUserInfo()
    {
        if(!SecurityUtils.isLogin()){
            return AjaxResult.error("未登录!");
        }
        return success(appUserService.getUserInfo());
    }

    /**
     * 修改用户简历信息-(技能信息、经历信息)
     */
    @ApiOperation("修改用户简历信息")
    @PostMapping("/editRegisterUser")
    @BussinessLog(title = "保存简历")
    public AjaxResult editRegisterUser(@RequestBody RegisterBody registerBody)
    {
        if (registerBody == null) {
            return AjaxResult.error("入参registerBody不能为空!");
        }
        if(registerBody.getAppUser()==null){
            return AjaxResult.error("用户信息为空!");
        }
        return AjaxResult.success(appUserService.editRegisterUser(registerBody));
    }

    @ApiOperation("查询APP用户列表")
    @GetMapping("/noTmlist")
    public TableDataInfo noTmlist(AppUser appUser)
    {
        startPage();
        List<AppUser> list = appUserService.selectNoTmAppUserList(appUser);
        return getDataTable(list);
    }

    @ApiOperation("查询我的中（已经投递的，收藏的，足迹，预约）")
    @GetMapping("/getMyTj")
    public AjaxResult getMyTj()
    {
        if(!SecurityUtils.isLogin()){
            return AjaxResult.error("未登录!");
        }
        if(StringUtils.isEmpty(RoleUtils.getCurrentUseridCard())){
            return AjaxResult.error("用户信息为空!");
        }
        AppUser appUser=appUserService.selectAppuserByIdcard(RoleUtils.getCurrentUseridCard());
        return AjaxResult.success(appUserService.getMyTj(appUser.getUserId()));
    }
}
