package com.railway.web.controller.business.safetool;

import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.enums.ToolStateEnum;
import com.railway.business.safetool.enums.ToolTestStateEnum;
import com.railway.business.safetool.service.IBaseSafetyToolService;
import com.railway.common.annotation.Log;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.enums.BusinessType;
import com.railway.common.utils.QrCodeUtil;
import com.railway.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author ZhaoMn
 */
@Slf4j
@Api(value = "rest/safetool/base/safety/tool", tags = "安全工具 - 二维码")
@RestController
@Validated
@RequestMapping(value = "st")
public class SecQcodeController extends BaseController {

  private final IBaseSafetyToolService safetyToolService;

  public SecQcodeController(IBaseSafetyToolService safetyToolService) {
    this.safetyToolService = safetyToolService;
  }

  @ApiOperation(value = "单个")
  @GetMapping(value = {"/", "/{qcode}"})
  public AjaxResult getInfo(String qcode) {
    BaseSafetyTool tool = safetyToolService.getInfoByQcode(qcode);
    if (null == tool) {
      return AjaxResult.error("二维码已失效，请检查！");
    }
    Long deptId = SecurityUtils.getDeptId();
    if (tool.getDeptId().compareTo(deptId) != 0) {
      return AjaxResult.error("非本车间安全工具，请检查！");
    }
    // 是否已报废
    if (tool.getState().equals(ToolStateEnum.NORMAL.getCode())) {
      safetyToolService.setBaseSafetyTool(tool);
    } else {
      tool.setTestState(ToolTestStateEnum.SCRAP.getCode());
    }
    AjaxResult ajax = AjaxResult.success();
    ajax.put("info", tool);
    return ajax;
  }

  @ApiOperation(value = "更新二维码")
  @GetMapping(value = {"/refresh"})
  public AjaxResult refresh() {
    safetyToolService.refresh();
    return AjaxResult.success();
  }

  /**
   * 生成二维码
   */
  @ApiOperation(value = "二维码生成", notes = "二维码生成", response = ModelAndView.class)
  @GetMapping("/qcodeImage")
  public AjaxResult getCode(Long toolId) {
    AjaxResult ajax = AjaxResult.success();
    BaseSafetyTool tool = safetyToolService.getInfo(toolId);
    if (null == tool) {
      return ajax;
    }
    List<String> bottomDes = Arrays.asList(tool.getDeptName(), tool.getStorePlace(),
        tool.getToolName() + " - " + tool.getToolCode());
    String base64Code;
    try {
      base64Code = QrCodeUtil.encodeStr(tool.getToolQcode(), bottomDes);
    } catch (Exception e) {
      return AjaxResult.error(e.getMessage());
    }
    ajax.put("img", base64Code);
    return ajax;
  }

  @ApiOperation(value = "批量导出二维码")
  @Log(title = "批量导出二维码", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public AjaxResult export(Long[] id) {
    return safetyToolService.export(id);
  }

  @ApiOperation(value = "批量导出二维码")
  @Log(title = "批量导出二维码", businessType = BusinessType.EXPORT)
  @GetMapping("/exportAll")
  public AjaxResult exportAll(BaseSafetyTool safetyTool) {
    return safetyToolService.export(safetyTool);
  }

}
