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

package com.yami.shop.multishop.controller;

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.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
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
 */
@RestController
@RequestMapping("/admin/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

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

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

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid Brand brand) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Brand dbBrand = brandService.getByBrandName(brand.getBrandName(),shopId);
        if (dbBrand != null) {
            throw new GlobalException("该品牌名称已存在");
        }
        Date date = new Date();
        brand.setUpdateTime(date);
        brand.setRecTime(date);
        brand.setShopId(shopId);
        brandService.save(brand);
        return ResponseEntity.ok().build();
    }

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

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

}
