package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBrand;
import com.iformall.service.WxBrandService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.iformall.domain.po.WxBrand.Field.Id_DESC;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxBrand")
public class WxBrandController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxBrandService wxBrandService;


    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData list(@ModelAttribute WxBrand wxBrand, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::list");
        if (null == wxBrand) {
            wxBrand = new WxBrand();
        }
        wxBrand.setSortColumns(Id_DESC);
        final PageInfo<WxBrand> page = wxBrandService.listAsPage(wxBrand, pageNum, pageSize);
        return new ResultData(page);
    }

    @PostMapping("add")
    public ResultData add(@RequestBody WxBrand wxBrand) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::add");
        wxBrand.setTenantId(getTenantId());
        return wxBrandService.save(wxBrand);
    }

    @PostMapping("update")
    public ResultData update(@RequestBody WxBrand wxBrand) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::update");
        wxBrand.setTenantId(getTenantId());
        return wxBrandService.update(wxBrand);
    }

    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::del");
        wxBrandService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findById(Long id) {
        return new ResultData(Result.SUCCESS, "查询成功", wxBrandService.getById(id));
    }

    @GetMapping("/queryBrand")
    public ResultData queryBrand() {
        List<Map<String,Object>>  brandList = wxBrandService.queryBrand(getTenantId());
        return new ResultData(brandList);
    }


    @ApiOperation("查询品牌名称是否存在")
    @GetMapping("hasBrand")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query")})
    public ResultData hasBrand(String name, Long id) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::hasBrand");
        return wxBrandService.hasBrand(getTenantId(), name, id);
    }


}
