package com.flossom.system.controller;

import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.entity.WxInstrumentInstructions;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.flossom.system.service.IWxInstrumentInstructionsService;

/**
 * 仪器说明书Controller
 *
 * @author flossom
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/instructions")
public class WxInstrumentInstructionsController extends BaseController {

    @Autowired
    private IWxInstrumentInstructionsService wxInstrumentInstructionsService;

    /**
     * 查询仪器说明书列表
     */
    @GetMapping("/list")
    public R list(WxInstrumentInstructions wxInstrumentInstructions) {
        return R.ok(wxInstrumentInstructionsService.selectWxInstrumentInstructionsList(wxInstrumentInstructions));
    }

    /**
     * 获取仪器说明书详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxInstrumentInstructionsService.selectWxInstrumentInstructionsById(id));
    }

    /**
     * 新增仪器说明书
     */
    @PostMapping
    public AjaxResult add(@RequestBody WxInstrumentInstructions wxInstrumentInstructions) {
        return toAjax(wxInstrumentInstructionsService.insertWxInstrumentInstructions(wxInstrumentInstructions));
    }

    /**
     * 修改仪器说明书
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WxInstrumentInstructions wxInstrumentInstructions) {
        return toAjax(wxInstrumentInstructionsService.updateWxInstrumentInstructions(wxInstrumentInstructions));
    }

    /**
     * 删除仪器说明书
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxInstrumentInstructionsService.deleteWxInstrumentInstructionsByIds(ids));
    }

    /**
     * TODO： 要求跟原来的一样
     * 预览仪器说明书
     *
     * @param id   仪器id
     * @param type 仪器别名（要求唯一）
     *             <p>
     *             另一个方法
     *             {@link utilsTest.pdfPreview.PdfPreviewTest#method1(HttpServletResponse, String)}
     */
    @GetMapping("/book")
    public void book(HttpServletResponse response, @RequestParam("type") String type, @RequestParam("id") Integer id) throws IOException, ServletException {
        WxInstrumentInstructions query = new WxInstrumentInstructions();
        query.setInstrumentId(id.longValue());
        query.setName(type);
        List<WxInstrumentInstructions> instructionList = wxInstrumentInstructionsService.selectWxInstrumentInstructionsList(query);
        if (instructionList != null && instructionList.size() > 0 && StringUtils.isNotBlank(instructionList.get(0).getLink())) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(instructionList.get(0).getLink());
            try (CloseableHttpResponse resp = httpClient.execute(httpGet)) {
                InputStream inputStream = resp.getEntity().getContent();
                ServletOutputStream outputStream = response.getOutputStream();
                IOUtils.copy(inputStream, outputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
