package com.orangesale.cn.service;

import com.orangesale.cn.entity.OrangeProduct;
import com.orangesale.cn.entity.OrangeProductSale;
import com.orangesale.cn.entity.OrangeSale;
import com.orangesale.cn.mapper.ProductMapper;
import com.orangesale.cn.mapper.SaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhijun
 * @Date 2019/12/13
 */
@Service
public class SaleService {

    @Autowired
    private SaleMapper saleMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据销量查询
     *
     * @return
     */
    private List<OrangeSale> getBySalesNum() {
        return saleMapper.getBySalesNum();
    }

    /**
     * 按照销量排序好的商品信息
     *
     * @return
     */
    public List<OrangeProductSale> getProductBySalesNum() {
        List<OrangeSale> saleList = getBySalesNum();
        return selectAllProductSale(saleList);
    }

    /**
     * 查询销量信息
     */
    public List<OrangeProductSale> searchSaleNum() {
        List<OrangeProduct> productList = productMapper.selectAll();
        List<OrangeSale> saleList = saleMapper.selectAll();
        List<Integer> productIdList = new ArrayList<>();
        List<Integer> productIdExistList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productList)) {
            for (OrangeProduct orangeProduct : productList) {
                productIdList.add(orangeProduct.getId());
            }
        }
        if (!CollectionUtils.isEmpty(saleList)) {
            for (OrangeSale orangeSale : saleList) {
                productIdExistList.add(orangeSale.getProductId());
            }
        }
        if (!CollectionUtils.isEmpty(productIdList)) {
            for (Integer productId : productIdExistList) {
                if (!productIdList.contains(productId)) {
                    OrangeSale orangeSale = new OrangeSale();
                    orangeSale.setProductId(productId);
                    orangeSale.setSalesNum(0);
                    saleMapper.addSalesNum(orangeSale);
                }
            }
        }
        saleList = saleMapper.selectAll();
        return selectAllProductSale(saleList);
    }

    /**
     * 组装商品和销量信息
     *
     * @param orangeSaleList
     * @return
     */
    private List<OrangeProductSale> selectAllProductSale(List<OrangeSale> orangeSaleList) {
        List<OrangeProductSale> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orangeSaleList)) {
            for (OrangeSale orangeSale : orangeSaleList) {
                OrangeProduct orangeProduct = productMapper.selectById(orangeSale.getProductId());
                OrangeProductSale orangeProductSale = new OrangeProductSale();
                orangeProductSale.setAddTime(orangeProduct.getAddTime());
                orangeProductSale.setImgUrl(orangeProduct.getImgUrl());
                orangeProductSale.setName(orangeProduct.getName());
                orangeProductSale.setPrice(orangeProduct.getPrice());
                orangeProductSale.setSalesNum(orangeSale.getSalesNum());
                list.add(orangeProductSale);
            }
        }
        return list;
    }

    /**
     * 更新销量信息
     *
     * @param orangeSale
     * @return
     */
    public Integer updateSaleNum(OrangeSale orangeSale) {
        return saleMapper.updateSalesNum(orangeSale);
    }
}
