package com.younike.member;

import com.younike.common.utils.StringUtils;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.framework.config.AnonymousAccess;
import com.younike.system.domain.Goods;
import com.younike.system.domain.vo.GoodsCommentVo;
import com.younike.system.domain.vo.GoodsVo;
import com.younike.system.param.GoodsParam;
import com.younike.system.service.IGoodsCommentService;
import com.younike.system.service.IGoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.younike.common.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.common.core.page.TableDataInfo;

import java.util.List;

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

    //留言
    @Autowired
    private IGoodsCommentService goodsCommentService;

    /**
     * 查询商品列表
     */
    @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: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));
    }


    /**
     * 搜索：服务人员
     */
    @GetMapping("/seekCrewList")
    @ApiOperation("搜索：服务人员:查询商品列表")
    @AnonymousAccess
    public Object seekCrewList(GoodsParam goods) {

        //参数验证
        if (ObjectUtils.isEmpty(goods) || goods.getBizType() == null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        //搜索服务人员
        List<GoodsVo> goodsVos = goodsService.seekCrewList(goods);
        return getDataTable(goodsVos);
    }

    /**
     * 搜索：商家
     */
    @GetMapping("/seekMerchantList")
    @ApiOperation("搜索：商家:查询商品列表")
    @AnonymousAccess
    public Object seekMerchantList(GoodsParam goods) {
        //参数验证
        if (ObjectUtils.isEmpty(goods) || goods.getBizType() == null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        //搜索商家
        List<GoodsVo> goodsVos = goodsService.seekMerchantList(goods);
        return getDataTable(goodsVos);
    }

    /**
     * 频道页搜索：服务人员
     */
    @GetMapping("/channelCrewList")
    @ApiOperation("频道页搜索：服务人员:查询商品列表")
    @AnonymousAccess
    public Object channelCrewList(GoodsParam goods) {

        //参数验证
        if (ObjectUtils.isEmpty(goods) || goods.getBizType() == null || goods.getParentId() == null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        //搜索服务人员
        List<GoodsVo> goodsVos = goodsService.seekCrewList(goods);
        return getDataTable(goodsVos);
    }

    /**
     * 频道页搜索：商家
     */
    @GetMapping("/channelMerchantList")
    @ApiOperation("频道页搜索：商家:查询商品列表")
    @AnonymousAccess
    public Object channelMerchantList(GoodsParam goods) {
        //参数验证
        if (ObjectUtils.isEmpty(goods) || goods.getBizType() == null || goods.getParentId() == null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        //搜索商家
        List<GoodsVo> goodsVos = goodsService.seekMerchantList(goods);
        return getDataTable(goodsVos);
    }

    /**
     * 首页：商家主页
     */
    @GetMapping("/merchantGoods")
    @ApiOperation("首页:商家主页")
    @AnonymousAccess
    public AjaxResult merchantGoods(GoodsParam goods) {
        //参数验证
        if (ObjectUtils.isEmpty(goods)) {
            return AjaxResult.error("参数错误");
        }
        //校验 商品id不能为空
        if (StringUtils.isNull(goods.getGoodsId())) {
            return AjaxResult.error("参数错误");
        }
        //验证业务类型
        if (StringUtils.isNull(goods.getBizType())) {
            return AjaxResult.error("参数错误");
        }

        return goodsService.merchantGoods(goods);
    }

    /**
     * 首商家主页:商家服务项
     */
    @GetMapping("/merchantServe")
    @ApiOperation("商家主页:商家服务列表")
    @AnonymousAccess
    public Object merchantServe(GoodsParam goods) {
        //参数验证
        if (ObjectUtils.isEmpty(goods) || goods.getBizType()==null || goods.getMerchantId()==null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        //搜索服务人员
        List<GoodsVo> goodsVos = goodsService.seekCrewList(goods);
        return getDataTable(goodsVos);

    }

    /**
     * 首页：服务员主页
     */
    @GetMapping("/serverGoods")
    @ApiOperation("首页:服务员主页")
    @AnonymousAccess
    public AjaxResult serverGoods(GoodsParam goods) {
        //参数验证
        if (ObjectUtils.isEmpty(goods)) {
            return AjaxResult.error("参数错误");
        }
        //校验 商品id不能为空
        if (!StringUtils.isNotNull(goods.getGoodsId())) {
            return AjaxResult.error("参数错误");
        }
        //验证业务类型
        if (!StringUtils.isNotNull(goods.getBizType())) {
            return AjaxResult.error("参数错误");
        }
        return goodsService.serverGoods(goods);
    }

    /**
     * 根据商品id :查询服务商品评价列表
     * 根据商品id查询服务商品评价
     */
    @AnonymousAccess
    @ApiOperation("首页：服务商品评价")
    @GetMapping("/goodsCommentList")
    public Object getgoodsCommentList(GoodsParam goods) {
        startPage();
        //业务类型不能为空
        if (goods.getGoodsId() == null) {
            return AjaxResult.error("参数错误(商品id不能为空)");
        }
        List<GoodsCommentVo> commentList = goodsCommentService.getCommentVolist(goods.getGoodsId());
        return getDataTable(commentList);
    }
}
