package com.ruoyi.cms.controller.cms;

import com.ruoyi.cms.service.IAppUserService;
import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.entity.UserWorkExperiences;
import com.ruoyi.cms.service.UserWorkExperiencesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户工作经历表
 *
 * @author 
 * @email 
 * @date 2025-10-10 16:26:26
 */
@RestController
@RequestMapping("/cms/userworkexperiences")
@Api(tags = "后台：用户工作经历")
public class UserWorkExperiencesController extends BaseController {

    @Autowired
    private UserWorkExperiencesService userWorkExperiencesService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 列表
     */
    @ApiOperation("工作经历列表信息")
    @PreAuthorize("@ss.hasPermi('cms:userworkexperiences:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserWorkExperiences userWorkExperiences){
        startPage();
        List<UserWorkExperiences> list=userWorkExperiencesService.getWorkExperiencesList(userWorkExperiences);
        return getDataTable(list);
    }


    /**
     * 信息
     */
    /**
     * 获取详细信息
     */
    @ApiOperation("获取工作经历详细信息")
    @PreAuthorize("@ss.hasPermi('cms:userworkexperiences:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult query(@PathVariable("id") Long id){
        return success(userWorkExperiencesService.getWorkExperiencesById(id));
    }

    /**
     * 保存
     */
    @ApiOperation("新增工作经历")
    @Log(title = "工作经历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserWorkExperiences userWorkExperiences){
        if(userWorkExperiences.getUserId()==null){
            AppUser appUser=appUserService.selectAppuserByIdcard(RoleUtils.getCurrentUseridCard());
            if(appUser==null){
                return AjaxResult.error("未传递userId!");
            }
            userWorkExperiences.setUserId(appUser.getUserId());
        }
        return toAjax(userWorkExperiencesService.insertWorkExperiences(userWorkExperiences));
    }

    /**
     * 修改
     */
    @ApiOperation("修改工作经历")
    @Log(title = "工作经历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody UserWorkExperiences userWorkExperiences){
        if (userWorkExperiences.getId()==null){
            return AjaxResult.error("参数id未传递!");
        }
        return toAjax(userWorkExperiencesService.updateWorkExperiencesById(userWorkExperiences));
    }

    /**
     * 删除
     */
    @ApiOperation("删除工作经历")
    @Log(title = "工作经历", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        if(ids.length==0){
            return AjaxResult.error("参数ids未传递!");
        }
        return toAjax(userWorkExperiencesService.deleteWorkExperiencesIds(ids));
    }

}
