package com.cool.store.controller.webb;

import com.cool.store.common.InsertGroup;
import com.cool.store.common.UpdateGroup;
import com.cool.store.request.store.StoreTypeOptionSaveRequest;
import com.cool.store.request.store.StoreTypeQueryRequest;
import com.cool.store.request.store.StoreTypeUpdateRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.store.StoreTypeService;
import com.cool.store.vo.store.StoreTypeOptionVO;
import com.cool.store.vo.store.StoreTypeVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "店型管理")
@RestController
@RequestMapping("/pc/storeType")
@RequiredArgsConstructor
public class StoreTypeController {

    private final StoreTypeService storeTypeService;

    @ApiOperation("新增店型")
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated(InsertGroup.class) StoreTypeUpdateRequest request) {
        return ResponseResult.success(storeTypeService.add(request));
    }

    @ApiOperation("编辑店型")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Validated(UpdateGroup.class) StoreTypeUpdateRequest request) {
        return ResponseResult.success(storeTypeService.update(request));
    }

    @ApiOperation("分页查询店型")
    @PostMapping("/queryPage")
    public ResponseResult<PageInfo<StoreTypeVO>> queryPage(@RequestBody StoreTypeQueryRequest request) {
        return ResponseResult.success(storeTypeService.queryPage(request));
    }

    @ApiOperation("批量删除店型(仅停用状态可删除)")
    @PostMapping("/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        return ResponseResult.success(storeTypeService.deleteByIds(ids));
    }

    @ApiOperation("启用店型")
    @PostMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Long id) {
        return ResponseResult.success(storeTypeService.changeActive(id, true));
    }

    @ApiOperation("停用店型")
    @PostMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Long id) {
        return ResponseResult.success(storeTypeService.changeActive(id, false));
    }

    @ApiOperation("保存店型可用配置选项")
    @PostMapping("/config/save")
    public ResponseResult<Boolean> saveStoreTypeOptions(@RequestBody @Valid StoreTypeOptionSaveRequest request) {
        return ResponseResult.success(storeTypeService.saveStoreTypeOptions(request));
    }

    @ApiOperation("查询店型已配置的选项绑定")
    @GetMapping("/config/detail")
    public ResponseResult<List<StoreTypeOptionVO>> listStoreTypeOptions(@RequestParam("storeTypeId") Long storeTypeId) {
        return ResponseResult.success(storeTypeService.listStoreTypeOptions(storeTypeId));
    }
}
