package com.om.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.po.Wxsc;
import com.om.entity.vo.WxscVO;
import com.om.service.IWxscService;
import com.om.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author henry-ong
 * @since 2024-03-09
 */
@RestController
@RequestMapping("/wxsc")
@Api(tags = "维修手册管理模块")
public class WxscController {
  private final IWxscService wxscService;

  @Autowired
  public WxscController(IWxscService wxscService) {
    this.wxscService = wxscService;
  }

  @ApiOperation("新增多版本维修手册接口")
  @PostMapping("/savemore")
  public Result<Object> saveMore(@RequestBody WxscVO wxscVO) {
    Collection<Wxsc> collection = new ArrayList<>();
    String title = wxscVO.getTitle();
    LocalDateTime createTime = wxscVO.getCreateTime();
    String[] langs = wxscVO.getLang();
    String[] urls = wxscVO.getFileUrl();
    String[] scNames = wxscVO.getScName();
    String brandCode = wxscVO.getBrandCode();
    Arrays.stream(wxscVO.getClientCode()).forEach(client -> {
      for (int i = 0; i < langs.length; i++) {
        Wxsc wxsc = new Wxsc();
        wxsc.setTitle(title);
        wxsc.setBrandCode(brandCode);
        wxsc.setLang(langs[i]);
        wxsc.setScName(scNames[i]);
        wxsc.setFileUrl(urls[i]);
        wxsc.setClientCode(client);
        wxsc.setCreateTime(createTime);
        wxsc.setUpdateTime(LocalDateTime.now());
        collection.add(wxsc);
      }
    });

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

  @ApiOperation("修改维修手册接口")
  @PostMapping("/save")
  public Result<Object> saveMore(@RequestBody Wxsc wxsc) {
    return Result.succ(wxscService.saveOrUpdate(wxsc));
  }


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

    @ApiOperation("删除维修手册")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        return wxscService.deleteById(id);
    }

    @GetMapping("/push")
    @ApiOperation("推送维修资料")
    public Result pushWxzl(@RequestParam Integer id) {
        return wxscService.pushWxzl(id);
    }
}
