package com.liangjian.dataplatform.member.controller;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.liangjian.dataplatform.member.entity.po.secondary.*;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.jobUtil.EsUtils;
import com.liangjian.dataplatform.member.service.IUserOrdersService;
import com.liangjian.dataplatform.member.util.FastDfsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/memorder")
@Api(value = "会员系统订单", tags = "会员系统订单", description = "会员系统订单")
public class MemOrdersController {

    @Autowired
    private IUserOrdersService iUserOrdersService;

    //图片存放路径
    public static String fileDirPath = "http://120.78.254.166/";

    @Autowired
    private FastDfsUtil fastDfsUtil;

    private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
            // 最大缓存
            .maximumSize(100)
            // 设置过期
            .expireAfterWrite(10, TimeUnit.SECONDS)
            .build();

    @PostMapping("/orderList")
    @ApiOperation(value = "获取订单列表")
    public Map<String,Object> getLastLog(@ApiParam(value = "参数", required = true) @RequestBody FiltersData data) {

        return iUserOrdersService.getFilters(data.getPageIndex()-1,data.getPageSize(),data.getFilters());
    }


    @PostMapping("/productList")
    @ApiOperation(value = "获取产品列表")
    public Map<String,Object> productList(@ApiParam(value = "参数", required = true) @RequestBody FiltersData data) {
        return iUserOrdersService.getProductFilters(data.getPageIndex()-1,data.getPageSize(),data.getFilters());
    }

    @PostMapping("/productLevel")
    @ApiOperation(value = "会员权益列表")
    public Map<String,Object> productLevel(@ApiParam(value = "参数", required = true) @RequestBody FiltersData data) {
        return iUserOrdersService.getProductLevel(data.getPageIndex()-1,data.getPageSize(),data.getFilters());
    }

    @GetMapping("/getlevel")
    @ApiOperation(value = "有效等级列表")
    public List<Map<String,Object>> getlevel() {
        return iUserOrdersService.getLevel();
    }

    @GetMapping("/getProductLog")
    @ApiOperation(value = "产品日志")
    public Map<String,Object> getProductLog(@ApiParam(value = "产品编号", required = true) @RequestParam String productCode) {
        return iUserOrdersService.getProductLog(productCode);
    }

    @GetMapping("/getProductLevel")
    @ApiOperation(value = "商品价格列表")
    public Map<String,Object> getProductLevel(@ApiParam(value = "产品编号", required = true) @RequestParam String productCode) {
        return iUserOrdersService.getProductLevel(productCode);
    }

    @PostMapping("/addMemProductLevel")
    @ApiOperation(value = "产品定价")
    public ResultObject<Object> addMemProductLevel(@ApiParam(value = "定价列表", required = true) @RequestBody LevelUtils data,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        iUserOrdersService.addMemProductLevel(data.getProductCode(),data.getLevelProduct(),MemberId,MemberName);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/updaState")
    @ApiOperation(value = "上下架")
    public ResultObject<Object> updaState(@ApiParam(value = "产品信息", required = true) @RequestBody MemIngProduct memIngProduct,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        iUserOrdersService.editMemProductLevel(memIngProduct,MemberId,MemberName);
        return ResultObject.success("保存成功",null);
    }


    @PostMapping("/AllproductType")
    @ApiOperation(value = "商品类目列表")
    public Map<String,Object> AllproductType(@ApiParam(value = "参数", required = true) @RequestBody FiltersData data) {
        return iUserOrdersService.AllproductType(data.getPageIndex()-1,data.getPageSize(),data.getFilters());
    }

    @PostMapping("/addMemProductType")
    @ApiOperation(value = "修改添加商品类目")
    public ResultObject<Object> addMemProductType(@ApiParam(value = "参数", required = true) @RequestBody FiltersUtils data,@ApiParam(value = "操作人", required = false) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        iUserOrdersService.addMemProductType(data);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/editProductType")
    @ApiOperation(value = "商品移出类目")
    public ResultObject<Object> editProductType(@ApiParam(value = "参数", required = true) @RequestBody FiltersUtils data) {
        iUserOrdersService.editProductType(data);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/editProductAmount")
    @ApiOperation(value = "修改库存")
    public ResultObject<Object> editProductAmount(@ApiParam(value = "参数", required = true) @RequestBody FiltersUtils data,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        iUserOrdersService.editProductAmount(data,MemberId,MemberName);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/addMemProduct")
    @ApiOperation(value = "添加修改商品")
    public ResultObject<Object> addMemProduct(@ApiParam(value = "参数", required = true) @RequestBody MemIngProduct data,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        iUserOrdersService.addMemProduct(data,MemberId,MemberName);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/editOrder")
    @ApiOperation(value = "审核订单")
    public ResultObject<Object> editOrder(@ApiParam(value = "参数", required = true) @RequestBody FiltersUtils data,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        if (CACHES.getIfPresent("order") != null && CACHES.getIfPresent("order").equals(MemberId)) {
            return ResultObject.success("请勿重复提交！",null);
        }else{
            CACHES.put("order",MemberId);
        }
        String str = iUserOrdersService.editOrder(data,MemberId,MemberName);
        return ResultObject.success(str,null);
    }

    @PostMapping("/callOrder")
    @ApiOperation(value = "取消订单")
    public ResultObject<Object> callOrder(@ApiParam(value = "参数", required = true) @RequestBody FiltersUtils data,@ApiParam(value = "操作人", required = true) @RequestParam String MemberId,@ApiParam(value = "操作人", required = true) @RequestParam String MemberName) {
        if (CACHES.getIfPresent("order") != null && CACHES.getIfPresent("order").equals(MemberId)) {
            return ResultObject.success("请勿重复提交！",null);
        }else{
            CACHES.put("order",MemberId);
        }
        iUserOrdersService.callOrder(data,MemberId,MemberName);
        return ResultObject.success("保存成功",null);
    }

    @PostMapping("/uploadprofile")
    @ApiOperation(value = "上传图片")
    public ResultObject<Object> uploadprofile(@RequestParam("file") MultipartFile file) {
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取源文件名称
        String originalFileName = file.getOriginalFilename();
        //获取文件后缀
        String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        String fileName = file.getName();
        //获取文件大小
        long fileSize = file.getSize();
        String path = fastDfsUtil.uploadFile(bytes, fileSize, extension);
        return ResultObject.success("上传成功", fileDirPath+path);

        /*
        String filename = file.getOriginalFilename();
        String uid=UUID.randomUUID().toString();
        File fileDir = getImgDirFile();
        try {
            // 构建真实的文件路径
            uid=fileDir.getAbsolutePath() + File.separator + uid+filename;
            File newFile = new File(uid);
            // 上传
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            String path = storePath.getFullPath();
        }

         */
    }

    public static File getImgDirFile(){
        // 构建上传文件的存放
        File fileDir = new File(fileDirPath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        return fileDir;
    }
}
