/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.action;

import com.ifish.bean.CommodityInfoBean;
import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.helper.CommodityInfoHelperI;
import com.ifish.helper.RedisHelperI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author Administrator
 */
@RestController
public class CommodityAction {

    @Autowired
    private CommodityInfoHelperI commodityInfoHelperI;

    /**
     * 创建商品
     *
     * @param commodityInfoBean
     * @return
     */
    @RequestMapping(value = "/createCommodity", method = RequestMethod.POST)
    public JsonResult<?> createCommodityInfo(CommodityInfoBean commodityInfoBean, MultipartFile img, MultipartFile video) {
        return commodityInfoHelperI.createCommodityInfo(commodityInfoBean, img, video);
    }

    /**
     * 修改商品
     *
     * @param commodityInfoBean
     * @return
     */
    @RequestMapping(value = "/updateCommodity", method = RequestMethod.POST)
    public JsonResult<?> updateCommodityInfo(CommodityInfoBean commodityInfoBean) {
        return commodityInfoHelperI.updateCommodityInfo(commodityInfoBean);
    }

    /**
     * 根据商品ID查询商品详情
     *
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/getCommodityById", method = RequestMethod.GET)
    public JsonResult<?> getCommodityInfo(Integer commodityId) {
        return commodityInfoHelperI.getCommodityInfo(commodityId);
    }

    /**
     * 根据店铺ID分页获取商品列表
     *
     * @param pageSize
     * @param firstResult
     * @param shopId
     * @param commodityState
     * @return
     */
    @RequestMapping(value = "/getCommodityInfoByPage", method = RequestMethod.GET)
    public PageingDto<?> getCommodityInfoByPage(Integer pageSize, Integer firstResult, Integer shopId, Integer commodityState, String orderBy) {
        //参数校验
        if (firstResult < 0 || (pageSize < 1 || pageSize > 20)) {
            throw new IfishException(ResultEnum.error401);
        }
        if (commodityState == null) {
            commodityState = 1;
        }
        return commodityInfoHelperI.getCommodityInfoByPage(pageSize, firstResult, shopId, commodityState, orderBy);
    }

    /**
     * 删除商品
     *
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/deleteCommodityById", method = RequestMethod.POST)
    public JsonResult<?> deleteCommodityInfo(Integer commodityId) {
        return commodityInfoHelperI.deleteCommodityInfo(commodityId);
    }
}
