/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.base.entity.App;
import com.tofly.base.service.AppService;

import com.tofly.common.oauth.util.SecurityUtils;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;


import java.util.*;

/**
 * APP管理
 *
 * @author fcl
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app")
@Api(tags="APP管理接口")
public class AppController {

    private final  AppService appService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param app
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getAppPage(Page page, @ModelAttribute App app) {
      return ResultRespone.success(appService.page(page, Wrappers.query(app)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(appService.getById(id));
    }

    /**
     * 新增
     * @param app
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增" )
    @ApiOperation(value = "新增")
    @PostMapping
    public ResultRespone save(@RequestBody App app) {
      return ResultRespone.success(appService.save(app));
    }

    /**
     * 修改
     * @param app
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改" )
    @ApiOperation(value = "修改(发布即是修改发布状态)")
    @PutMapping
    public ResultRespone updateById(@RequestBody App app) {
        app.setReleasestatus("2");
        app.setReleaser(SecurityUtils.getUserId());
        app.setReleasetime(new Date());
        return ResultRespone.success(appService.updateById(app));
    }
    /**
     * 通过id删除$
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除" )
    @ApiOperation(value = "通过ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(appService.removeById(id));
    }

    /**
     * 通过id批量删除
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除" )
    @ApiOperation(value = "通过ID批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(appService.removeByIds(Arrays.asList(ids.split(","))));
    }
    /**
     *
     * APP包上传
     */
    @ToFlyAppLog(title="APP包上传" )
    @ApiOperation(value = "APP包上传")
    @PutMapping("upLoadApp")
    public ResultRespone upLoadApp(@RequestParam("appFile")MultipartFile appFile,@RequestParam("txtFile")MultipartFile txtFile ,@RequestParam("type")String type) throws Exception {
        return ResultRespone.success(appService.upLoadApp(appFile,txtFile,type),type);
    }


    /**
     *
     * APP版本号获取
     */
    @GetMapping("/getVersion" )
    @ApiOperation(value = "当前APP版本号获取")
    public ResultRespone getVersion(@RequestParam("type")String type){
        return ResultRespone.success(appService.selectOldApp(type));
    }

    /**
     * app下载路径
     *
     * @return ResultRespone
     */
    @GetMapping(value = "/appDownlod")
    @ApiOperation(value = "app下载路径", notes = "app下载路径")
    public ResultRespone appDownlod(String type) throws Exception {
        return ResultRespone.success(appService.appDownlod(type));
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @return
     */

    @GetMapping("/pageNew" )
    @ApiOperation(value = "分页查询-根据类型和日期查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = false, dataType = "String"),
            @ApiImplicitParam(name = "releasestatus", value = "状态", required = false, dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = false, dataType = "String")
    })
    public ResultRespone getAppPageNew(Page page,String releasestatus,String startDate,String endDate,String type ) {

        Map map=new HashMap();
        map.put("releaseStatus",releasestatus);
        map.put("type",type);
        map.put("startDate", StringUtil.isEmpty(startDate)?null:startDate+" 00:00:00");
        map.put("endDate",StringUtil.isEmpty(endDate)?null:endDate+" 23:59:59");
        return ResultRespone.success(appService.listAppPage(page,map));

    }

    /**
     *
     * 获取最新的一条app上传信息
     */
    @GetMapping("/getNewVersionInformation" )
    @ApiOperation(value = "获取最新的一条app上传信息")
    public ResultRespone getNewVersionInformation(@RequestParam(value = "type") String type, @RequestParam(value = "userId",required = false) String userId, @RequestParam(value = "nowVersion",required = false) String nowVersion){
        return appService.getNewVersionInformation(type ,userId, nowVersion);
    }

}
