package com.hwrj.cloud.admin.controller;


import com.hwrj.cloud.admin.common.CmsSubjectProductRelationInput;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import com.hwrj.cloud.admin.model.CmsSubject;
import com.hwrj.cloud.admin.service.CmsSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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 = "CmsSubjectController", description = "商品专题管理")
@RequestMapping("/subject")
public class CmsSubjectController {
    @Autowired
    private CmsSubjectService subjectService;

    @ApiOperation("获取全部商品专题")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CmsSubject>> listAll() {
        List<CmsSubject> subjectList = subjectService.listAll();
        return CommonResult.success(subjectList);
    }

    @ApiOperation(value = "根据专题名称分页获取专题")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CmsSubject>> getList(@RequestParam(value = "keyword", required = false) String keyword,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<CmsSubject> subjectList = subjectService.list(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(subjectList));
    }

    @ApiOperation("批量插入关联优选")
    @RequestMapping(value = "/relateAndInsertList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult relateAndInsertList(@RequestBody List<CmsSubjectProductRelationInput> productRelationInputs ,
                                            @RequestParam("productId") Long productId ) {
        subjectService.relateAndInsertList(productRelationInputs,productId);
        return CommonResult.success(null);
    }

    @ApiOperation("批量更新关联优选")
    @RequestMapping(value = "/relateAndUpdateList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult relateAndUpdateList(@RequestBody List<CmsSubjectProductRelationInput> productRelationInputs ,
                                            @RequestParam("productId") Long productId ) {
        subjectService.relateAndUpdateList(productRelationInputs,productId);
        return CommonResult.success(null);
    }

    @ApiOperation("通过id查询关联优选")
    @RequestMapping(value = "/relationByProductId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CmsSubjectProductRelationInput>> relationByProductId(@RequestParam("productId") Long productId ) {
        List<CmsSubjectProductRelationInput> productRelationList = subjectService.relationByProductId(productId);
        return CommonResult.success(productRelationList);
    }


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

    @ApiOperation("修改")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody CmsSubject subject){
        int i = subjectService.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 = subjectService.delete(id);
        if (i==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

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


    @ApiOperation("是否展示专题")
    @PostMapping("/is-show")
    @ResponseBody
    public CommonResult isShow(@RequestParam(value = "id")Long id,@ApiParam(name = "0-不显示，1-展示") @RequestParam(value = "showStatus",required = true)Integer showStatus){
        int i = subjectService.show(id,showStatus);
        if (i<0){
            return CommonResult.failed();
        }
        return CommonResult.success(i);
    }

    @ApiOperation("获取所有专题")
    @PostMapping("/all")
    @ResponseBody
    public CommonResult<List<CmsSubject>> get(@RequestParam(value = "keyword",required = false)String keyword){
        List<CmsSubject> i = subjectService.all(keyword);
        if (i==null){
            return CommonResult.failed();
        }
        return CommonResult.success(i);
    }
}
