package com.orangesale.cn.controller;

import com.orangesale.cn.entity.OrangeProduct;
import com.orangesale.cn.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

/**
 * @Author zhijun
 * @Date 2019/12/13
 */
@RestController
@RequestMapping("/orange/product")
@CrossOrigin
public class OrangeProductController {
    @Autowired
    private ProductService productService;
    @Value("${uploadFilePath}")
    private String uploadFilePath;

    /**
     * 根据id查询商品信息
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "/searchById", method = RequestMethod.POST)
    public Map<String, Object> searchProductById(String productId) {
        OrangeProduct orangeProduct = productService.selectById(Integer.valueOf(productId));
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(orangeProduct)) {
            map.put("flag", false);
            map.put("msg", "search by id error");
            return map;
        }
        map.put("flag", true);
        map.put("data", orangeProduct);
        return map;
    }

    /**
     * 查询商品详细信息
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Map<String, Object> searchProduct() {
        List<OrangeProduct> productList = productService.selectAll();
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(productList)) {
            map.put("flag", false);
            map.put("msg", "search error");
            return map;
        }
        map.put("flag", true);
        map.put("data", productList);
        return map;
    }

    /**
     * 更新商品信息
     *
     * @param orangeProduct
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updateProduct(@RequestBody OrangeProduct orangeProduct) {
        Integer updateFlag = productService.updateProduct(orangeProduct);
        Map<String, Object> map = new HashMap<>();
        if (updateFlag <= 0) {
            map.put("flag", false);
            map.put("msg", "search error");
            return map;
        }
        map.put("flag", true);
        map.put("msg", "update success");
        return map;
    }

    /**
     * 删除商品信息
     *
     * @param orangeProduct
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Map<String, Object> deleteProduct(@RequestBody OrangeProduct orangeProduct) {
        Integer deleteFlag = productService.deleteProduct(orangeProduct);
        Map<String, Object> map = new HashMap<>();
        if (deleteFlag <= 0) {
            map.put("flag", false);
            map.put("msg", "delete error");
            return map;
        }
        map.put("flag", true);
        map.put("msg", "delete success");
        return map;
    }

    /**
     * 添加商品信息
     *
     * @param orangeProduct
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> addProduct(@RequestBody OrangeProduct orangeProduct) {
        Integer addFlag = productService.addProduct(orangeProduct);
        Map<String, Object> map = new HashMap<>();
        if (addFlag <= 0) {
            map.put("flag", false);
            map.put("msg", "add error");
            return map;
        }
        map.put("flag", true);
        map.put("msg", "add success");
        return map;
    }

    /**
     * 上传图片
     *
     * @param file 上传文件
     * @return
     */
    @RequestMapping("/uploadCover")
    public Map<String, Object> uploadCover(@RequestParam("file") MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        try {
            String fileName = file.getOriginalFilename();
            String changeFileName = uploadFilePath + fileName;
            File destFile = new File(changeFileName);
            if (!destFile.exists()) {
                destFile.getParentFile().mkdirs();
                file.transferTo(destFile);
            }
            map.put("flag", true);
            map.put("data", "http://localhost:8081/uploaded/" + fileName);
            return map;
        } catch (Exception e) {
            map.put("flag", false);
            map.put("msg", "upload error");
            return map;
        }
    }
}
