package com.org.hktshop.controller.system;

import com.org.hktshop.Interface.system.HktFavoritesService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.base.UserContext;
import com.org.hktshop.controller.HktBaseController;
import com.org.hktshop.dto.CategoryInfo;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.system.HktFavorites;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop/favorite")
@Api(tags = "收藏接口")
public class HktFavoritesController extends HktBaseController {

    @Autowired
    private HktFavoritesService hktFavoritesService;

    /**
     * 新增收藏
     *
     * @param hktFavorites
     * @return
     */
    @PostMapping("/saveHktFavorites")
    @ApiOperation(value = "新增收藏", notes = "新增收藏")
    public BaseResult save(@RequestBody HktFavorites hktFavorites) {
        try {
            hktFavorites.setUsersId(userInfo.getId());
            hktFavoritesService.saveHktFavorites(hktFavorites);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    /**
     * 更新收藏
     *
     * @param hktFavorites
     * @return
     */
    @PostMapping("/updateHktFavorites")
    @ApiOperation(value = "更新收藏", notes = "更新收藏")
    public BaseResult update(@RequestBody HktFavorites hktFavorites) {
        try {
            hktFavoritesService.updateHktFavorites(hktFavorites);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    /**
     * 删除收藏
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteFavorites")
    @ApiOperation(value = "删除收藏", notes = "删除收藏")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public BaseResult delete(@RequestBody HktFavorites hktFavorites) {
        try {
            hktFavoritesService.deleteHktFavorites(hktFavorites);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    /**
     * 收藏商品id查
     *
     * @param goodId
     * @return
     */
    @GetMapping("/getFavoritesByGoodsId")
    @ApiOperation(value = "收藏商品id查", notes = "收藏商品id查")
    @ApiImplicitParam(name = "goodId", value = "商品id", required = true, paramType = "query")
    public BaseResult getFavoritesById(@RequestParam("goodId") String goodId) {
        try {
            HktFavorites hktFavorites = hktFavoritesService.getHktFavoritesById(goodId);
            return BaseResult.successWithData(hktFavorites);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping(value = "/favoritePage")
    @ApiOperation(value = "收藏分页")
    public BaseResult favoritePage(@RequestBody HktFavorites hktFavorites) {
        try {
            Page<HktFavorites> favoritesPage = hktFavoritesService.favoritePage(hktFavorites);
            return BaseResult.successWithData(favoritesPage);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    /**
     * 种类信息统计
     *
     * @return
     */
    @GetMapping("/getCategoryInfo")
    @ApiOperation(value = "种类信息统计", notes = "种类信息统计")
    public BaseResult getCategoryInfo() {
        try {
            List<CategoryInfo> categoryInfo = hktFavoritesService.getCategoryInfo(userInfo.getId());
            return BaseResult.successWithData(categoryInfo);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
