package com.gree.mall.manager.controller.goods;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.goods.GoodsLeaseBean;
import com.gree.mall.manager.bean.goods.GoodsLeaseCountBean;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsLeaseLogic;
import com.gree.mall.manager.plus.entity.GoodsLease;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "租赁商品列表", tags ={"租赁商品列表"} )
@RequestMapping(value = "/goods/lease", produces = "application/json; charset=utf-8")
public class GoodsLeaseController {

    @Resource
    GoodsLeaseLogic goodsLeaseLogic;

    @PostMapping("/list/page")
    @ApiOperation(value = "商品列表")
    public ResponseHelper<IPage<GoodsLeaseBean>> page(
            @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
            @ApiParam(value = "租赁价格开始区间") @RequestParam(required = false) BigDecimal startPrice,
            @ApiParam(value = "租赁价格结束区间") @RequestParam(required = false) BigDecimal endPrice,
            @ApiParam(value = "租赁定金开始区间") @RequestParam(required = false) BigDecimal startDeposit,
            @ApiParam(value = "租赁定金开始区间") @RequestParam(required = false) BigDecimal endDeposit,
            @ApiParam(value = "排序json格式[{sortColumn: 'PRICE', type: true },...] " +
                    "sortColumn:商品排序对应值 LEASE_PRICE=租赁价格 DEPOSIT=租赁定金" +
                    "type: true=升序 false=倒序")
            @RequestParam(required = false) String sortJson,
            @ApiParam(value = "商品分类") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "商品状态 true:上架  false:下架") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<GoodsLeaseBean> goodsSpecBeanIPage = goodsLeaseLogic.page(keyword, startPrice, endPrice,
                startDeposit, endDeposit, sortJson, categoryId, status, pageNum, pageSize , request);
        return ResponseHelper.success(goodsSpecBeanIPage);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<GoodsLease> detail(
            @ApiParam(value = "商品id") @RequestParam String id
    ) throws Exception {
        GoodsLease goods = goodsLeaseLogic.detail(id);
        return ResponseHelper.success(goods);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增/编辑")
    public ResponseHelper save(
            @RequestBody GoodsLease goods
    ) throws Exception {

        goodsLeaseLogic.save(goods);

        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ResponseHelper del(
            @ApiParam(value = "商品id") @RequestBody List<String> ids
    ) throws Exception {

        goodsLeaseLogic.del(ids);

        return ResponseHelper.success();
    }

    @PostMapping("/count")
    @ApiOperation(value = "数量")
    public ResponseHelper<GoodsLeaseCountBean> count() throws Exception {

        GoodsLeaseCountBean bean = goodsLeaseLogic.count();

        return ResponseHelper.success(bean);
    }
}
