package com.flossom.miniProgram.controller;

import com.flossom.common.core.constant.Constants;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.SysFile;
import com.flossom.common.core.enums.InstrumentFileClassifyEnum;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.miniProgram.service.IWxInstrumentService;
import com.flossom.system.api.RemoteFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 仪器列Controller
 *
 * @author flossom
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/instrument")
public class WxInstrumentController extends BaseController {

    @Autowired
    private IWxInstrumentService wxInstrumentService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 判断用户是否绑定序列号
     */
    @GetMapping(value = "/isBindingSerial")
    public R isBindingSerial(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) {
        wxInstrumentService.binding(serial, null, false);
        return R.ok();
    }

    /**
     * 用户、序列号与仪器绑定
     */
    @GetMapping(value = "/binding")
    public R binding(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) {
        wxInstrumentService.binding(serial, null, true);
        return R.ok();
    }

    /**
     * 换绑
     */
    @GetMapping(value = "/exchangeBinding")
    public R exchangeBinding(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) {
        wxInstrumentService.exchangeBinding(serial);
        return R.ok();
    }

    /**
     * 手动输入序列号绑定仪器
     *
     * @return
     */
    @PostMapping("/manualCodeBinding")
    public R manualCodeBinding(@NotNull(message = "请上传序列号图片") @RequestParam(value = "serialImage") MultipartFile file,
                               @NotBlank(message = "仪器ID不能为空") @RequestParam("instrumentId") Long instrumentId,
                               @NotBlank(message = "请输入序列号") String serial) {
        String serialImage = "";
        if (file != null) {
            R<SysFile> result = remoteFileService.upload(file);
            if (result.getCode() != Constants.SUCCESS) {
                logger.error("上传序列号图片失败");
                throw new ServiceException("上传序列号图片失败");
            }
            serialImage = result.getData().getUrl();
        }
        wxInstrumentService.determineSerialIsSameInstrument(serial, instrumentId);
        wxInstrumentService.binding(serial, serialImage, true);
        return R.ok();
    }

    /**
     * 用户选择仪器后，通过扫码序列号与仪器绑定
     */
    @GetMapping(value = "/scanCodeBinding")
    public R scanCodeBinding(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial,
                             @NotBlank(message = "仪器ID不能为空") @RequestParam("instrumentId") Long instrumentId) {
        return R.ok(wxInstrumentService.determineSerialIsSameInstrument(serial, instrumentId));
    }

    /**
     * 获取已绑定的仪器列表
     */
    @GetMapping(value = "/bindingInstrumentList")
    public R bindingInstrumentList() {
        return R.ok(wxInstrumentService.bindingInstrumentList());
    }

    /**
     * 获取未绑定的仪器列表
     */
    @GetMapping(value = "/unbindingInstrumentInfoList")
    public R unbindingInstrumentInfoList() {
        return R.ok(wxInstrumentService.unbindingInstrumentInfoList());
    }

    /**
     * 根据序列号获取仪器信息
     */
    @GetMapping(value = "/getInstrumentInfoBySerial")
    public R getInstrumentInfoBySerial(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) {
        return R.ok(wxInstrumentService.getInstrumentInfoBySerial(serial));
    }

    /**
     * 根据仪器ID获取仪器介绍信息
     */
    @GetMapping(value = "/instructionInfo")
    public R instructionInfo(@NotNull(message = "仪器ID不能为空") @RequestParam("instrumentId") Long instrumentId) {
        return R.ok(wxInstrumentService.getInstrumentFileRelateByInstrumentId(instrumentId, InstrumentFileClassifyEnum.INSTRUMENT_INSTRUCTION.getCode()));
    }

    /**
     * 根据仪器ID获取仪器的初次护理信息
     */
    @GetMapping(value = "/firstNurseInfo")
    public R getInfoByInstrumentId(@NotNull(message = "仪器ID不能为空") @RequestParam("instrumentId") Long instrumentId) {
        return R.ok(wxInstrumentService.getInstrumentFileRelateByInstrumentId(instrumentId, InstrumentFileClassifyEnum.FIRST_NURSE.getCode()));
    }

    /**
     * 根据仪器ID获取仪器模式
     */
    @GetMapping(value = "/modeInfoList")
    public R modeInfo(@NotNull(message = "仪器ID不能为空") @RequestParam("instrumentId") Long instrumentId) {
        return R.ok(wxInstrumentService.getInstrumentModeByInstrumentId(instrumentId));
    }

    /**
     * 获取设备升级数据
     * 由小程序读取设备版本号，与后管的升级版本号做对比
     * 1）第一位不相同的情况下，判断后面两位，后台的比设备的大，则强行升级
     */
    @GetMapping(value = "/upgrade")
    public R upgrade(@RequestParam("instrumentId") Long instrumentId) {
        return R.ok(wxInstrumentService.upgrade(instrumentId));
    }

}
