package com.cxyxiaomo.epp.shop.service;

import com.cxyxiaomo.epp.common.pojo.Good;
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
import com.cxyxiaomo.epp.common.vo.GoodVO;
import com.cxyxiaomo.epp.shop.dao.GoodCategoryDao;
import com.cxyxiaomo.epp.shop.dao.GoodDao;
import org.springframework.stereotype.Service;

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

@Service
public class GoodService {

    @Resource
    GoodDao goodDao;

    @Resource
    GoodCategoryDao goodCategoryDao;

    public List<GoodVO> list(Integer cateId, String searchText) {
        List<Good> list = goodDao.list(cateId, searchText);
        List<GoodVO> goodVOS = GoodVO.convertFrom(list);
        return goodVOS;
    }

    // 只列出上架状态的商品
    public List<GoodVO> listOnSale(Integer cateId, String searchText) {
        List<Good> list = goodDao.listOnSale(cateId, searchText);
        List<GoodVO> goodVOS = GoodVO.convertFrom(list);
        return goodVOS;
    }

    public GoodVO getById(Long id) {
        Good good = goodDao.getById(id);
        GoodVO goodVO = GoodVO.convertFrom(good);
        return goodVO;
    }

    public List<GoodCategoryVO> cateListVO() {
        List<GoodCategory> list = goodCategoryDao.list();
        List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(list);
        return goodCategoryVOList;
    }

    public List<GoodVO> listByIds(List<Long> goodIdList) {
        if (goodIdList == null || goodIdList.size() == 0) {
            return new ArrayList<>();
        }
        List<Good> list = goodDao.listByIds(goodIdList);
        List<GoodVO> goodVOList = GoodVO.convertFrom(list);
        return goodVOList;
    }


    /**
     * 获取分类列表（不带查询参数）
     *
     * @return
     */
    public List<GoodCategory> getCateList() {
        List<GoodCategory> list = goodCategoryDao.list();
        return list;
    }

    /**
     * 获取分类列表（带查询参数）
     *
     * @param goodCategoryVO
     * @return
     */
    public List<GoodCategory> getCateListWithQuery(GoodCategoryVO goodCategoryVO) {
        List<GoodCategory> list = goodCategoryDao.getCategoryList(goodCategoryVO);
        return list;
    }


    public Good getGoodById(Long id) {
        if (id == null) {
            return null;
        }
        return goodDao.selectById(id);
    }

    public List<Good> getGoodList(GoodVO goodVO) {
        List<Good> goodList = goodDao.getGoodList(goodVO);
        return goodList;
    }

    public boolean addGood(Good good) {
        good.setId(null);
        return goodDao.addGood(good);
    }

    public boolean updateGood(Good good) {
        return goodDao.updateGood(good);
    }

    public boolean deleteGood(Long goodId) {
        return goodDao.deleteGoodById(goodId);
    }





    public boolean addGoodCategory(GoodCategory goodCategory) {
        goodCategory.setId(null);
        return goodCategoryDao.addCategory(goodCategory);
    }

    public boolean updateGoodCategory(GoodCategory goodCategory) {
        return goodCategoryDao.updateCategory(goodCategory);
    }

    public boolean deleteGoodCategory(Long goodCategoryId) {
        return goodCategoryDao.deleteCategoryById(goodCategoryId);
    }





    public GoodCategory getGoodCategoryById(Long id) {
        if (id == null) {
            return null;
        }
        return goodCategoryDao.getById(id);
    }
}
