package com.chaozhanggui.admin.system.controller;


import com.chaozhanggui.admin.system.service.PromotionImageService;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.dao.system.entity.TbPlussAppGuide;
import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import com.dianguang.cloud.ossservice.service.OSSFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 *
 * @author lyf
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/promotion")
@Slf4j
public class PromotionImageController {
    @Resource
    private PromotionImageService promotionImageService;
    @Autowired
    CloudStorageConfig config;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/promotionImageList")
    public RespBody promotionImageList(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return promotionImageService.getPromotionImage(pageNum,pageSize);
    }

    /**
     * 改
     * @param appGuide
     * @return
     */
    @PostMapping ("/updateById")
    public RespBody updateByPrimaryKey(@RequestBody TbPlussAppGuide appGuide){
        return promotionImageService.updateByPrimaryKey(appGuide);
    }

    /**
     * 增加
     * @param appGuide
     * @return
     */
    @PostMapping("/insert")
    public RespBody insert(@RequestBody TbPlussAppGuide appGuide){
        return promotionImageService.insert(appGuide);
    }

    @PostMapping("/OSSUpdate")
    public RespBody OSSUpdate(@RequestBody MultipartFile file){

        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        try {
            Object url = OSSFactory.build(config).uploadSuffix(file.getBytes(), extension);
            return new RespBody("000000",url);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return new RespBody("999994");
    }

    @PostMapping("/testRequest")
    public RespBody testRequest(){
        int randomNumber = new Random().nextInt(6);
        List<Object> objects = new ArrayList<>();
        return new RespBody(String.valueOf(randomNumber),objects);
    }
}
