package com.om.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.po.Manual;
import com.om.entity.vo.ManualVO;
import com.om.service.IManualService;
import com.om.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.print.Collation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author henry-ong
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/manual")
@Api(tags = "使用手册管理模块")
public class ManualController {
  private final IManualService manualService;
  @Autowired
  public ManualController(IManualService manualService) {
    this.manualService = manualService;
  }

  @ApiOperation("新增使用手册接口")
  @PostMapping("/save")
  public Result<Object> save(@RequestBody Manual manual) {
    return Result.succ(manualService.save(manual));
  }

  @ApiOperation("新增多版本使用手册接口")
  @PostMapping("/savemore")
  public Result<Object> saveMore(@RequestBody ManualVO manualVO) {
    Collection<Manual> collection = new ArrayList<>();
    String title = manualVO.getTitle();
    LocalDateTime createTime = manualVO.getCreateTime();
    String[] langs = manualVO.getLang();
    String[] urls = manualVO.getUrl();
    String[] fileNames = manualVO.getFileName();
    Arrays.stream(manualVO.getClientCode()).forEach(client -> {
      for (int i = 0; i < langs.length; i++) {
        Manual manual = new Manual();
        manual.setTitle(title);
        manual.setLang(langs[i]);
        manual.setFileName(fileNames[i]);
        manual.setUrl(urls[i]);
        manual.setClientCode(client);
        manual.setCreateTime(createTime);
        manual.setUpdateTime(LocalDateTime.now());
        collection.add(manual);
      }
    });

    return Result.succ(manualService.saveBatch(collection));
  }

  @ApiOperation("编辑使用手册接口")
  @PostMapping("/edit")
  public Result<Object> edit(@RequestBody Manual manual) {
    return Result.succ(manualService.saveOrUpdate(manual));
  }
  @ApiOperation("根据ID删除单个使用手册接口")
  @DeleteMapping("/delete/{id}")
  public Result<Object> delete(@PathVariable(name = "id") Integer id) {
    return Result.succ(manualService.removeById(id));
  }

  @ApiOperation("分页查询全部使用手册接口")
  @GetMapping("/page")
  public Result<Object> findPage(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize,
                         @RequestParam(defaultValue = "") String code,
                         @RequestParam(defaultValue = "") String title) {
    return Result.succ(manualService.findManual(new Page<>(pageNum, pageSize), code, title));
  }

    @GetMapping("/push")
    @ApiOperation("推送使用手册")
    public Result pushManual(@RequestParam Integer id) {
        return manualService.pushManual(id);
    }
}
