package com.younike.admin;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.system.domain.Goods;
import com.younike.system.domain.vo.GoodsVo;
import com.younike.system.param.GoodsQueryParam;
import com.younike.system.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author elev
 * @date 2022-01-06
 */
@Api("商品接口")
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController extends BaseController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/pageList")
    public TableDataInfo pageList(GoodsQueryParam param) {
        startPage();
        List<GoodsVo> list = goodsService.pageList(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @ApiOperation("下架商品")
    @PutMapping("/takeOff")
    public AjaxResult takeOff(@RequestBody Goods goods) {
        if (goods == null || goods.getGoodsId() == null) {
            return AjaxResult.error("参数错误");
        }
        //todo 获取当前修改用户信息
        goods.setIsSale(0);
        return toAjax(goodsService.updateGoods(goods));
    }

    @PreAuthorize("@ss.hasPermi('system:goods:delete')")
    @ApiOperation("删除商品")
    @DeleteMapping("/deleteGoods/{goodsId}")
    public AjaxResult deleteGoods(@PathVariable Long goodsId) {
        if (goodsId == null) {
            return AjaxResult.error("参数错误");
        }
        Goods goods = new Goods();
        //todo 获取当前修改用户信息
        goods.setGoodsId(goodsId);
        goods.setDelFlag("2");
        return toAjax(goodsService.updateGoods(goods));
    }

    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @ApiOperation("上架商品")
    @PutMapping("/takeOn")
    public AjaxResult takeOn(@RequestBody Goods goods) {
        if (goods == null || goods.getGoodsId() == null) {
            return AjaxResult.error("参数错误");
        }
        //todo 获取当前修改用户信息
        goods.setIsSale(1);
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 导出商品列表
     */
    @ApiOperation("导出商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        return AjaxResult.success(goodsService.selectGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods) {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods) {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }


}
