package com.hys.app.controller.erp.use;

import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.goods.dos.BrandDO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.goods.BrandManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * 品牌控制器
 *
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018-03-16 16:32:46
 */
@RestController
@RequestMapping("/admin/goods/brands")
@Api(description = "品牌相关API")
@AllArgsConstructor
public class BrandManagerController {

    private BrandManager brandManager;

    @ApiOperation(value = "查询品牌列表", response = BrandDO.class)
    @GetMapping
    public WebPage list(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo,
                        @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize,
                        String name) {

        return this.brandManager.list(pageNo, pageSize, name);
    }

    @ApiOperation(value = "添加品牌", response = BrandDO.class)
    @PostMapping
    @Log(client = LogClient.admin, detail = "添加品牌")
    public BrandDO add(@Valid BrandDO brand) {
        AdminUserContext.hasCompany();
        this.brandManager.add(brand);
        return brand;
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改品牌", response = BrandDO.class)
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的品牌")
    public BrandDO edit(@Valid BrandDO brand, @PathVariable String id) {
        brandManager.edit(brand, id);
        return brand;
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除品牌")
    @Log(client = LogClient.admin, detail = "删除id为[${ids}]的品牌")
    public String delete(@PathVariable Long[] ids) {
        this.brandManager.delete(ids);
        return "";
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询一个品牌")
    public BrandDO get(@PathVariable String id) {
        return this.brandManager.getModel(id);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value = "查询所有品牌")
    public List<BrandDO> listAll() {
        List<BrandDO> brands = this.brandManager.getAllBrands();
        return brands;
    }
}
