package com.younike.merchant;

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.GoodsSpecification;
import com.younike.system.service.IGoodsSpecificationService;
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.ArrayList;
import java.util.List;

/**
 * 团购商品规格Controller
 * 
 * @author dail
 * @date 2022-03-11
 */
@Api("团购商品规格接口")
@RestController
@RequestMapping("/merchant/goodsSpecification")
public class GoodsSpecificationController extends BaseController {
    @Autowired
    private IGoodsSpecificationService goodsSpecificationService;

    /**
     * 查询团购商品规格列表
     */
    @ApiOperation("查询团购商品规格列表")
    @PreAuthorize("@ss.hasPermi('system:specification:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpecification goodsSpecification) {
        startPage();
        List<GoodsSpecification> list = goodsSpecificationService.selectGoodsSpecificationList(goodsSpecification);
        return getDataTable(list);
    }

    /**
     * 导出团购商品规格列表
     */
    @ApiOperation("导出团购商品规格列表")
    @PreAuthorize("@ss.hasPermi('system:specification:export')")
    @Log(title = "团购商品规格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsSpecification goodsSpecification) {
        List<GoodsSpecification> list = goodsSpecificationService.selectGoodsSpecificationList(goodsSpecification);
        ExcelUtil<GoodsSpecification> util = new ExcelUtil<GoodsSpecification>(GoodsSpecification.class);
        return util.exportExcel(list, "团购商品规格数据");
    }

    /**
     * 获取团购商品规格详细信息
     */
    @ApiOperation("获取团购商品规格详细信息")
    @PreAuthorize("@ss.hasPermi('system:specification:query')")
    @GetMapping(value = "/{goodsSpecificationId}")
    public AjaxResult getInfo(@PathVariable("goodsSpecificationId") Long goodsSpecificationId) {
        return AjaxResult.success(goodsSpecificationService.selectGoodsSpecificationByGoodsSpecificationId(goodsSpecificationId));
    }

    /**
     * 新增团购商品规格
     */
    @ApiOperation("新增团购商品规格")
    @PreAuthorize("@ss.hasPermi('system:specification:add')")
    @Log(title = "团购商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSpecification goodsSpecification) {
        return toAjax(goodsSpecificationService.insertGoodsSpecification(goodsSpecification));
    }

    /**
     * 修改团购商品规格
     */
    @ApiOperation("修改团购商品规格")
    @PreAuthorize("@ss.hasPermi('system:specification:edit')")
    @Log(title = "团购商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSpecification goodsSpecification) {
        return toAjax(goodsSpecificationService.updateGoodsSpecification(goodsSpecification));
    }

    /**
     * 删除团购商品规格
     */
    @ApiOperation("删除团购商品规格")
    @PreAuthorize("@ss.hasPermi('system:specification:remove')")
    @Log(title = "团购商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsSpecificationIds}")
    public AjaxResult remove(@PathVariable Long[] goodsSpecificationIds) {
        return toAjax(goodsSpecificationService.deleteGoodsSpecificationByGoodsSpecificationIds(goodsSpecificationIds));
    }

    /**
     * 根据商品id查询团购商品规格
     */
    @ApiOperation("根据商品id查询团购商品规格")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/listByGoodsId")
    public AjaxResult listByGoodsId(@RequestParam("goodsId") Long goodsId) {
        if (goodsId == null) {
            return AjaxResult.error("商品id不能为空!");
        }
        List<Long> goodsIds = new ArrayList<>();
        goodsIds.add(goodsId);
        return AjaxResult.success(goodsSpecificationService.listByGoodsIds(goodsIds));
    }
}
