/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.platform.controller;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.R;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Brand;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;


/**
 * 品牌信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/platform/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public R<IPage<Brand>> page(Brand brand,PageParam<Brand> page) {
        IPage<Brand> brands = brandService.page(page, new LambdaQueryWrapper<Brand>()
                .eq(Brand::getShopId,Constant.PLATFORM_SHOP_ID)
                .eq(Objects.nonNull(brand.getStatus()),Brand::getStatus,brand.getStatus())
                .like(StrUtil.isNotBlank(brand.getBrandName()), Brand::getBrandName, brand.getBrandName())
                .orderByAsc(Brand::getFirstChar));
        return R.SUCCESS(brands);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public R<Brand> info(@PathVariable("id") Long id) {
        Brand brand = brandService.getById(id);
        return R.SUCCESS(brand);
    }

    /**
     * 保存
     */
    @PostMapping
    public R<Void> save(@RequestBody @Valid Brand brand) {
        Brand dbBrand = brandService.getByBrandName(brand.getBrandName(),Constant.PLATFORM_SHOP_ID);
        if (dbBrand != null) {
            throw new GlobalException("该品牌名称已存在");
        }
        Date date = new Date();
        brand.setRecTime(date);
        brand.setUpdateTime(date);
        brand.setShopId(Constant.PLATFORM_SHOP_ID);
        brandService.save(brand);
        return R.SUCCESS();
    }

    /**
     * 修改
     */
    @PutMapping
    public R<Void> update(@RequestBody @Valid Brand brand) {
        Brand dbBrand = brandService.getByBrandName(brand.getBrandName(),Constant.PLATFORM_SHOP_ID);
        if (dbBrand != null && !Objects.equals(dbBrand.getBrandId(), brand.getBrandId())) {
            throw new GlobalException("该品牌名称已存在");
        }
        brand.setUpdateTime(new Date());
        brandService.updateById(brand);
        return R.SUCCESS();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        brandService.deleteByBrand(id);
        return R.SUCCESS();
    }

}
