package com.om.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.po.Apps;
import com.om.entity.po.AppsClients;
import com.om.entity.po.AppsDesc;
import com.om.entity.vo.AppsAddVO;
import com.om.service.IAppsClientsService;
import com.om.service.IAppsDescService;
import com.om.service.IAppsService;
import com.om.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;

/**
 * <p>
 * 软件应用表 前端控制器
 * </p>
 *
 * @author henry-ong
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/apps")
public class AppsController {
    private final IAppsService appsService;
    private final IAppsDescService appsDescService;
    private final IAppsClientsService appsClientsService;

    @ApiOperation("根据ID删除应用软件接口")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable(name = "id") Integer id) {
        return Result.succ(appsService.removeById(id));
    }

    public AppsController(IAppsService appsService, IAppsDescService appsDescService, IAppsClientsService appsClientsService) {
        this.appsService = appsService;
        this.appsDescService = appsDescService;
        this.appsClientsService = appsClientsService;
    }

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

    @ApiOperation("下载")
    @GetMapping("/download")
    public Result<Object> download(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String code,
                                   @RequestParam(defaultValue = "") String title) {
        return Result.succ(appsService.findApps(new Page<>(pageNum, pageSize), code, title));
    }

    @ApiOperation("新增多语言版本App接口")
    @PostMapping("/savemore")
    @Transactional
    public Result<Object> saveMore(@RequestBody AppsAddVO appsAddVO) {
        Collection<AppsClients> ac = new ArrayList<>();
        Collection<AppsDesc> ad = new ArrayList<>();
        Apps apps = new Apps();
        apps.setCreateTime(appsAddVO.getCreateTime());
        apps.setFileName(appsAddVO.getFileName());
        apps.setVersion(appsAddVO.getVersion());
        apps.setFileSize(appsAddVO.getFileSize());
        apps.setSavePath(appsAddVO.getSavePath());
        apps.setUpdateTime(appsAddVO.getUpdateTime());
        apps.setUploadBy(appsAddVO.getUploadBy());
        apps.setUType(appsAddVO.getUType());
        apps.setStatus(appsAddVO.getStatus());
        if (appsService.save(apps)) {
            String[] clients = appsAddVO.getNumbers();
            for (String client : clients) {
                AppsClients appsClients = new AppsClients();
                appsClients.setClientNum(client);
                appsClients.setAppId(apps.getId());
                ac.add(appsClients);
            }
            if(appsClientsService.saveBatch(ac)) {
                String[] languages = appsAddVO.getLanguages();
                String[] descriptions = appsAddVO.getDescriptions();
                for (int i = 0; i < languages.length; i++) {
                    AppsDesc appsDesc = new AppsDesc();
                    appsDesc.setLanguage(languages[i]);
                    appsDesc.setDescription(descriptions[i]);
                    appsDesc.setAppId(apps.getId());
                    ad.add(appsDesc);
                }
                return Result.succ(appsDescService.saveBatch(ad));
            } else {
                return Result.fail("客户端管理插入失败!");
            }
        } else {
            return Result.fail("软件版本插入失败!");
        }
    }

    @GetMapping("/push")
    @ApiOperation("推送应用软件")
    public Result pushApps(@RequestParam Integer id) {
        return appsService.push(id);
    }
}
