package com.iformall.controller.basic;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
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;

/**
 * @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)})
    @SystemControllerLog(description = "品牌-列表")
    public ResultData list(@ModelAttribute WxBrand wxBrand, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::list");
        if (null == wxBrand) {
            wxBrand = new WxBrand();
        }
        wxBrand.setSortColumns(BaseEntity.SortField.Id_DESC);
        final PageInfo<WxBrand> page = wxBrandService.listAsPage(wxBrand, pageNum, pageSize);
        return new ResultData(page);
    }

    @PostMapping("add")
    @SystemControllerLog(description = "品牌-增加")
    public ResultData add(@RequestBody WxBrand wxBrand) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::add");
        wxBrand.setTenantId(getTenantId());
        return wxBrandService.save(wxBrand);
    }

    @PostMapping("update")
    @SystemControllerLog(description = "品牌-更新")
    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)
    @SystemControllerLog(description = "品牌-删除")
    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)
    @SystemControllerLog(description = "品牌-查找")
    public ResultData findById(Long id) {
        return new ResultData(Result.SUCCESS, "查询成功", wxBrandService.getById(id));
    }

    @GetMapping("/queryBrand")
    @SystemControllerLog(description = "品牌-租户-全部")
    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")})
    @SystemControllerLog(description = "品牌-名称是否存在")
    public ResultData hasBrand(String name, Long id) {
        logger.debug("[" + getIpAddr() + "] WxBrandController::hasBrand");
        return wxBrandService.hasBrand(getTenantId(), name, id);
    }


}
