package com.flossom.system.controller;

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

import com.flossom.common.core.domain.entity.WxInstrument;
import com.flossom.common.core.domain.entity.WxUserInstrument;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.domain.req.WxUserInstrumentExportVm;
import com.flossom.common.core.domain.req.WxUserMemberExportVm;
import com.flossom.common.core.domain.ret.WxUserMemberRet;
import com.flossom.common.core.domain.vo.WxUserMemberVo;
import com.flossom.common.core.exception.ServiceException;
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.IWxInstrumentService;
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.IWxUserInstrumentService;

/**
 * 用户仪器绑定Controller
 *
 * @author flossom
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/userInstrument")
public class WxUserInstrumentController extends BaseController
{
    @Autowired
    private IWxUserInstrumentService wxUserInstrumentService;
    @Autowired
    private IWxUserMemberService wxUserMemberService;
    @Autowired
    private IWxInstrumentService wxInstrumentService;

    /**
     * 查询用户仪器绑定列表
     */
    @RequiresPermissions("system:userInstrument:list")
    @GetMapping("/list")
    public TableDataInfo list(WxUserInstrument wxUserInstrument)
    {
        startPage();
        List<WxUserInstrument> list = wxUserInstrumentService.selectWxUserInstrumentList(wxUserInstrument);
        //循环查询对应的信息
        for (WxUserInstrument userInstrument: list) {
            if (Objects.nonNull(userInstrument.getInstrumentId())) {
                WxInstrument wxInstrument = wxInstrumentService.selectWxInstrumentById(userInstrument.getInstrumentId());
                userInstrument.setWxInstrument(wxInstrument);
            }
            if (Objects.nonNull(userInstrument.getUserId())) {
                WxUserMember wxUserMember = wxUserMemberService.selectWxUserMemberByIdInit(userInstrument.getUserId());
                userInstrument.setWxUserMember(wxUserMember);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户仪器绑定列表
     */
    @RequiresPermissions("system:userInstrument:export")
    @Log(title = "用户仪器绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUserInstrument wxUserInstrument)
    {
        List<WxUserInstrument> list = wxUserInstrumentService.selectWxUserInstrumentList(wxUserInstrument);
        ExcelUtil<WxUserInstrument> util = new ExcelUtil<WxUserInstrument>(WxUserInstrument.class);
        util.exportExcel(response, list, "用户仪器绑定数据");
    }

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

    /**
     * 新增用户仪器绑定
     */
    @RequiresPermissions("system:userInstrument:add")
    @Log(title = "用户仪器绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserInstrument wxUserInstrument)
    {
        return toAjax(wxUserInstrumentService.insertWxUserInstrument(wxUserInstrument));
    }

    /**
     * 修改用户仪器绑定
     */
    @RequiresPermissions("system:userInstrument:edit")
    @Log(title = "用户仪器绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserInstrument wxUserInstrument)
    {
        return toAjax(wxUserInstrumentService.updateWxUserInstrument(wxUserInstrument));
    }

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

    /**
     * 更换序列号
     */
    @Log(title = "用户仪器更换序列号", businessType = BusinessType.UPDATE)
    @PutMapping("/changeSerial")
    public AjaxResult changeSerial(@RequestBody WxUserInstrument wxUserInstrument)
    {
        Map<String, String> stringStringMap = wxUserInstrumentService.changeSerial(wxUserInstrument);
        return AjaxResult.success(stringStringMap);
    }

    /**
     * 更换序列号
     */
    @Log(title = "更换序列号", businessType = BusinessType.UPDATE)
    @PutMapping("/changeGuarantee")
    public AjaxResult changeGuarantee(@RequestBody WxUserInstrument wxUserInstrument)
    {
        return toAjax(wxUserInstrumentService.changeGuarantee(wxUserInstrument));
    }

    /**
     * 批量导出用户列表
     */
    @Log(title = "批量导出", businessType = BusinessType.EXPORT)
    @PostMapping("/batchExport")
    public void batchExport(HttpServletResponse response, WxUserInstrument wxUserInstrument) {
        List<WxUserInstrumentExportVm> userInstrumentExportData = wxUserInstrumentService.findUserInstrumentExportData(wxUserInstrument);
        ExcelUtil<WxUserInstrumentExportVm> util = new ExcelUtil<WxUserInstrumentExportVm>(WxUserInstrumentExportVm.class);
        util.exportExcel(response, userInstrumentExportData, "用户仪器绑定数据", wxUserInstrument.getExportFields());
    }

}
