package com.ruoyi.system.controller;

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

import com.ruoyi.system.domain.Users;
import com.ruoyi.system.service.IUsersService;
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 com.ruoyi.system.domain.IntegralLog;
import com.ruoyi.system.service.IIntegralLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.domain.entity.SysUser;

/**
 * 积分日志Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/system/IntegralLog")
public class IntegralLogController extends BaseController
{
    @Autowired
    private IIntegralLogService integralLogService;
    @Autowired
    private IUsersService usersService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询积分日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(IntegralLog integralLog)
    {
        startPage();
        List<IntegralLog> list = integralLogService.selectIntegralLogList(integralLog);
        for(IntegralLog data:list){
            Users user=usersService.selectUsersById(data.getUid());
            if (user!=null){
                data.setUname(user.getName());
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出积分日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:export')")
    @Log(title = "积分日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntegralLog integralLog)
    {
        List<IntegralLog> list = integralLogService.selectIntegralLogList(integralLog);
        ExcelUtil<IntegralLog> util = new ExcelUtil<IntegralLog>(IntegralLog.class);
        util.exportExcel(response, list, "积分日志数据");
    }

    /**
     * 获取积分日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(integralLogService.selectIntegralLogById(id));
    }

    /**
     * 新增积分日志
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:add')")
    @Log(title = "积分日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntegralLog integralLog)
    {
        return toAjax(integralLogService.insertIntegralLog(integralLog));
    }

    /**
     * 修改积分日志
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:edit')")
    @Log(title = "积分日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntegralLog integralLog)
    {
        return toAjax(integralLogService.updateIntegralLog(integralLog));
    }

    /**
     * 删除积分日志
     */
    @PreAuthorize("@ss.hasPermi('system:IntegralLog:remove')")
    @Log(title = "积分日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralLogService.deleteIntegralLogByIds(ids));
    }

    /**
     * 获取用户下拉列表
     */
    @GetMapping("/userOptions")
    public AjaxResult getUserOptions() {
        List<SysUser> users = sysUserService.selectUserList(new SysUser());
        return AjaxResult.success(users);
    }
}
