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.system.domain.Goods;
import com.younike.system.domain.ServerUser;
import com.younike.system.domain.vo.GoodsFormVO;
import com.younike.system.domain.vo.ServerUserSearchParam;
import com.younike.system.param.GroupGoodsAddParam;
import com.younike.system.service.IGoodsService;
import com.younike.system.service.IMerchantService;
import com.younike.system.service.IServerUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务项目信息Controller
 *
 * @author elev
 * @date 2022-01-04
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IServerUserService serverUserService;
    @Autowired
    private IMerchantService merchantService;

    @Log(title = "保存服务项目", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("save")
    public AjaxResult save(@RequestBody GoodsFormVO goodsFormVO)
    {
        return AjaxResult.success(goodsService.save(goodsFormVO));
    }

    @ApiOperation("查询服务列表")
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Goods goods) {
        return getDataTable(goodsService.list(goods));
    }

    @Log(title = "删除服务项目", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/remove")
    public AjaxResult remove(@RequestParam("goodsId") Long goodsId) {
//         return AjaxResult.success(goodsService.remove(goodsId));
        return goodsService.merchantRemove(goodsId);
    }

    @ApiOperation("上架")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/onShelf")
    public AjaxResult onShelf(@RequestParam("goodsId") Long goodsId) {
        return goodsService.onShelf(goodsId);

    }

    @ApiOperation("下架")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/offShelf")
    public AjaxResult offShelf(@RequestParam("goodsId") Long goodsId) {
        return AjaxResult.success(goodsService.offShelf(goodsId));
    }

    @ApiOperation("查询商品详细")
    @PreAuthorize("@ss.hasAnyRoles({'operation', 'merchant'})")
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("goodsId") Long goodsId) {
        return AjaxResult.success(goodsService.detail(goodsId));
    }

    @ApiOperation("查询服务人员列表")
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("/serverUserList")
    public TableDataInfo serverUserList(@RequestBody ServerUserSearchParam searchParam) {
        startPage();
        List<ServerUser> list = serverUserService.getServerUserList(searchParam);
        return getDataTable(list);
    }

    /**
     * 新增团购服务
     *
     * @param groupGoodsAddParam
     * @return
     */
    @Log(title = "新增团购服务", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("/saveGroupGoods")
    public AjaxResult saveGroupGoods(@RequestBody @Validated GroupGoodsAddParam groupGoodsAddParam) {
        return AjaxResult.success(goodsService.saveGroupGoods(groupGoodsAddParam));
    }

    /**
     * 查询团购服务列表
     *
     * @param goods
     * @return
     */
    @ApiOperation("查询团购服务列表")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/listGroupGoods")
    public TableDataInfo listGroupGoods(Goods goods) {
        return getDataTable(goodsService.listGroupGoods(goods));
    }

    /**
     * 查询团购服务详情
     *
     * @param goodsId
     * @return
     */
    @ApiOperation("查询团购服务详情")
    @PreAuthorize("@ss.hasAnyRoles({'operation', 'merchant'})")
    @GetMapping("/getGroupGoodsDetail")
    public AjaxResult getGroupGoodsDetail(@RequestParam("goodsId") Long goodsId) {
        return AjaxResult.success(goodsService.getGroupGoodsDetail(goodsId));
    }

    /**
     * 上架团购服务
     *
     * @param goods
     * @return
     */
    @ApiOperation("上架团购服务")
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("/onShelfGroup")
    public AjaxResult onShelfGroupGoods(@RequestBody Goods goods) {
        return goodsService.onShelfGroupGoods(goods);

    }


}
