package com.hwrj.cloud.admin.controller;


import com.hwrj.cloud.admin.common.CmsSubjectProductRelationInput;
import com.hwrj.cloud.admin.model.CmsSubject;
import com.hwrj.cloud.admin.model.CmsSubjectCategory;
import com.hwrj.cloud.admin.service.CmsSubjectCategoryService;
import com.hwrj.cloud.admin.service.CmsSubjectService;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品专题Controller
 */
@Controller
@Api(tags = "CmsSubjectCategoryController", description = "专题类型管理")
@RequestMapping("/subject/category")
public class CmsSubjectCategoryController {
    @Autowired
    private CmsSubjectCategoryService subjectCategoryService;

    @ApiOperation("获取全部")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CmsSubjectCategory>> listAll(@RequestParam(value = "keyword",required = false)String keyword) {
        List<CmsSubjectCategory> listAll = subjectCategoryService.listAll(keyword);
        return CommonResult.success(listAll);
    }



    @ApiOperation("添加")
    @PostMapping("/add")
    @ResponseBody
    public CommonResult add(@RequestBody CmsSubjectCategory subject){
        int i = subjectCategoryService.add(subject);
        if (i==0){
           return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody CmsSubjectCategory subject){
        int i = subjectCategoryService.update(subject);
        if (i==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }


    @ApiOperation("删除")
    @PostMapping("/del")
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "id")Long id){
        int i = subjectCategoryService.delete(id);
        if (i==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation("详细信息")
    @PostMapping("/get")
    @ResponseBody
    public CommonResult<CmsSubjectCategory> get(@RequestParam(value = "id")Long id){
        CmsSubjectCategory i = subjectCategoryService.info(id);
        if (i==null){
            return CommonResult.failed();
        }
        return CommonResult.success(i);
    }

    @ApiOperation("分页获取")
    @PostMapping("/page")
    @ResponseBody
    public CommonResult<CommonPage<CmsSubjectCategory>> get(@RequestParam(value = "keyword",required = false)String keyword
            ,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum
            ,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        List<CmsSubjectCategory> categories = subjectCategoryService.pageInfo(keyword, pageNum, pageSize);

        return CommonResult.success(CommonPage.restPage(categories));
    }
}
