package cn.pluss.platform.controller;


import cn.pluss.platform.DeviceGoodsService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.device.DeviceGoodTagService;
import cn.pluss.platform.entity.DeviceGoodTag;
import cn.pluss.platform.entity.DeviceGoods;
import cn.pluss.platform.vo.DeviceGoodsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备商品展示表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/deviceGoods")
public class DeviceGoodsController extends BaseModelController<DeviceGoodsService, DeviceGoods> {

    @Autowired
    private DeviceGoodTagService goodTagService;

    @GetMapping("/goodList")
    public Result<PageInfo<DeviceGoodsVO>> goodList(@RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "size", defaultValue = "10") Integer size, DeviceGoods condition) {
        PageInfo<DeviceGoodsVO> pageData = baseService.goodList(page, size, condition);
        return ResultGenerator.genSuccessResult(pageData);
    }

    @GetMapping("/goodListSimple")
    public Result<PageInfo<DeviceGoodsVO>> goodListSimple(@RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "size", defaultValue = "10") Integer size, DeviceGoods condition) {
        PageInfo<DeviceGoodsVO> pageData = baseService.goodListSimple(page, size, condition);
        return ResultGenerator.genSuccessResult(pageData);
    }

    @GetMapping("/deviceDetail/{code}")
    public Result<DeviceGoods> deviceDetail(@PathVariable("code") String code) {
        LambdaQueryWrapper<DeviceGoods> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(DeviceGoods::getCode, code);
        DeviceGoods entity = baseService.getOne(qWrapper);
        return ResultGenerator.genSuccessResult("获取成功", entity);
    }

    @GetMapping("/tagList")
    public Result<List<DeviceGoodTag>> tagList() {
        LambdaQueryWrapper<DeviceGoodTag> qWrapper = Wrappers.lambdaQuery();
        qWrapper.orderByAsc(DeviceGoodTag::getSort);
        List<DeviceGoodTag> result = goodTagService.list(qWrapper.last("limit 20"));
        return ResultGenerator.genSuccessResult("数据获取成功", result);
    }
}
