package com.orangesale.cn.controller;

import com.orangesale.cn.entity.OrangeProductSale;
import com.orangesale.cn.entity.OrangeSale;
import com.orangesale.cn.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhijun
 * @Date 2019/12/13
 */
@RestController
@RequestMapping("/orange/sale")
@CrossOrigin
public class OrangeSaleController {
    @Autowired
    private SaleService saleService;

    /**
     * 根据销量从大到小排序
     *
     * @return
     */
    @RequestMapping(value = "/getBySaleNum", method = RequestMethod.GET)
    public Map<String, Object> getBySalesNum() {
        List<OrangeProductSale> productSaleList = saleService.getProductBySalesNum();
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(productSaleList)) {
            map.put("flag", false);
            map.put("msg", "商品信息为空！");
            return map;
        }
        map.put("flag", true);
        map.put("data", productSaleList);
        return map;
    }

    /**
     * 查询销量信息
     *
     * @return
     */
    @RequestMapping(value = "/searchSaleNum", method = RequestMethod.GET)
    public Map<String, Object> searchSaleNum() {
        List<OrangeProductSale> list = saleService.searchSaleNum();
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            map.put("flag", false);
            map.put("msg", "商品销量信息为空！");
            return map;
        }
        map.put("flag", true);
        map.put("data", list);
        return map;
    }

    /**
     * 更新销量信息
     *
     * @param orangeSale
     * @return
     */
    @RequestMapping(value = "/updateSaleNum", method = RequestMethod.POST)
    public Map<String, Object> updateSaleNum(@RequestBody OrangeSale orangeSale) {
        Integer updateFlag = saleService.updateSaleNum(orangeSale);
        Map<String, Object> map = new HashMap<>();
        if (updateFlag <= 0) {
            map.put("flag", false);
            map.put("msg", "update error！");
            return map;
        }
        map.put("flag", true);
        map.put("msg", "update success");
        return map;
    }
}
