package com.flossom.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.entity.WxUserInstrument;
import com.flossom.common.core.domain.entity.WxUserInstrumentLog;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.system.service.IWxUserMemberService;
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.flossom.system.service.IWxUserInstrumentLogService;

/**
 * 用户仪器绑定日志Controller
 *
 * @author flossom
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/log")
public class WxUserInstrumentLogController extends BaseController
{
    @Autowired
    private IWxUserInstrumentLogService wxUserInstrumentLogService;
    @Autowired
    private IWxUserMemberService wxUserMemberService;

    /**
     * 查询用户仪器绑定日志列表
     */
    @RequiresPermissions("system:log:list")
    @GetMapping("/list")
    public TableDataInfo list(WxUserInstrumentLog wxUserInstrumentLog)
    {
        startPage();
        List<WxUserInstrumentLog> list = wxUserInstrumentLogService.selectWxUserInstrumentLogList(wxUserInstrumentLog);
        for (WxUserInstrumentLog instrumentLog: list) {
            if (Objects.nonNull(instrumentLog.getUserId())) {
                WxUserMember wxUserMember = wxUserMemberService.selectWxUserMemberByIdInit(instrumentLog.getUserId());
                instrumentLog.setWxUserMember(wxUserMember);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户仪器绑定日志列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "用户仪器绑定日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUserInstrumentLog wxUserInstrumentLog)
    {
        List<WxUserInstrumentLog> list = wxUserInstrumentLogService.selectWxUserInstrumentLogList(wxUserInstrumentLog);
        ExcelUtil<WxUserInstrumentLog> util = new ExcelUtil<WxUserInstrumentLog>(WxUserInstrumentLog.class);
        util.exportExcel(response, list, "用户仪器绑定日志数据");
    }

    /**
     * 获取用户仪器绑定日志详细信息
     */
    @RequiresPermissions("system:log:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxUserInstrumentLogService.selectWxUserInstrumentLogById(id));
    }

    /**
     * 新增用户仪器绑定日志
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "用户仪器绑定日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserInstrumentLog wxUserInstrumentLog)
    {
        return toAjax(wxUserInstrumentLogService.insertWxUserInstrumentLog(wxUserInstrumentLog));
    }

    /**
     * 修改用户仪器绑定日志
     */
    @RequiresPermissions("system:log:edit")
    @Log(title = "用户仪器绑定日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserInstrumentLog wxUserInstrumentLog)
    {
        return toAjax(wxUserInstrumentLogService.updateWxUserInstrumentLog(wxUserInstrumentLog));
    }

    /**
     * 删除用户仪器绑定日志
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "用户仪器绑定日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxUserInstrumentLogService.deleteWxUserInstrumentLogByIds(ids));
    }
}
