package com.slodon.b2b2c.controller.goods.seller;

import com.slodon.b2b2c.aop.VendorLogger;
import com.slodon.b2b2c.core.constant.GoodsConst;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.goods.dto.ApplyBrandAddDTO;
import com.slodon.b2b2c.goods.dto.ApplyBrandUpdateDTO;
import com.slodon.b2b2c.goods.example.GoodsBrandExample;
import com.slodon.b2b2c.goods.example.GoodsCategoryExample;
import com.slodon.b2b2c.goods.example.GoodsExample;
import com.slodon.b2b2c.goods.pojo.GoodsBrand;
import com.slodon.b2b2c.goods.pojo.GoodsCategory;
import com.slodon.b2b2c.model.goods.GoodsBrandModel;
import com.slodon.b2b2c.model.goods.GoodsCategoryModel;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.goods.BrandAndCateVO;
import com.slodon.b2b2c.vo.goods.SellerBrandDetailVO;
import com.slodon.b2b2c.vo.goods.SellerBrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author slodon
 */
@Api(tags = "seller品牌管理")
@RestController
@RequestMapping("v3/goods/seller/Brand")
public class ApplyBrandController {

    @Resource
    private GoodsBrandModel goodsBrandModel;
    @Resource
    private GoodsCategoryModel goodsCategoryModel;
    @Resource
    private GoodsModel goodsModel;

    @ApiOperation("品牌列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点 1:国外 2:国内"),
            @ApiImplicitParam(name = "brandName", value = "品牌名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<SellerBrandVO>> getList(HttpServletRequest request, @RequestParam(value = "webSite", required = false, defaultValue = "1") String webSite,String brandName) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        GoodsBrandExample example = new GoodsBrandExample();
        example.setBrandNameLike(brandName);
        example.setWebSite(webSite);
        example.setApplyStoreId(vendor.getStoreId());
        List<GoodsBrand> list = goodsBrandModel.getGoodsBrandList(example, pager);
        List<SellerBrandVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(brand -> {
                vos.add(new SellerBrandVO(brand));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("申请品牌")
    @VendorLogger(option = "申请品牌")
    @PostMapping("apply")
    public JsonResult<Integer> applyBrand(HttpServletRequest request, ApplyBrandAddDTO applyBrandAddDTO) {
        String logMsg = "品牌名称" + applyBrandAddDTO.getBrandName();
        // 验证参数是否为空
        AssertUtil.notNull(applyBrandAddDTO, "品牌信息不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandAddDTO.getWebSite(), "品牌站点不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandAddDTO.getBrandName(), "品牌名称不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandAddDTO.getImage(), "品牌图片不能为空，请重试！");
        AssertUtil.notNullOrZero(applyBrandAddDTO.getCategoryId(), "分类不能为空，请重试！");

        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        goodsBrandModel.saveSellersBrand(applyBrandAddDTO, vendor);
        return SldResponse.success("添加成功", logMsg);
    }

    @ApiOperation("编辑品牌")
    @VendorLogger(option = "编辑品牌")
    @PostMapping("edit")
    public JsonResult<Integer> editBrand(HttpServletRequest request, ApplyBrandUpdateDTO applyBrandUpdateDTO) {
        AssertUtil.notNull(applyBrandUpdateDTO, "品牌信息不能为空，请重试！");
        AssertUtil.notNullOrZero(applyBrandUpdateDTO.getBrandId(), "品牌ID不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandUpdateDTO.getWebSite(), "品牌站点不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandUpdateDTO.getBrandName(), "品牌名称不能为空，请重试！");
        AssertUtil.notEmpty(applyBrandUpdateDTO.getImage(), "品牌图片不能为空，请重试！");
        AssertUtil.notNullOrZero(applyBrandUpdateDTO.getCategoryId(), "分类不能为空，请重试！");

        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //查询品牌信息
        GoodsBrand goodsBrand = goodsBrandModel.getGoodsBrandByBrandId(applyBrandUpdateDTO.getBrandId());
        AssertUtil.notNull(goodsBrand, "未查询到该品牌，请重试！");
        AssertUtil.isTrue(!goodsBrand.getApplyStoreId().equals(vendor.getStoreId()), "无操作权限，请重试！");

        goodsBrandModel.updateSellersBrand(applyBrandUpdateDTO, vendor);
        return SldResponse.success("编辑成功", "品牌id" + applyBrandUpdateDTO.getBrandId());
    }

    @ApiOperation("删除品牌")
    @VendorLogger(option = "删除品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true)
    })
    @PostMapping("delete")
    public JsonResult<Integer> deleteBrand(HttpServletRequest request, @RequestParam("brandId") Integer brandId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //查询品牌信息
        GoodsBrand goodsBrand = goodsBrandModel.getGoodsBrandByBrandId(brandId);
        AssertUtil.notNull(goodsBrand, "未查询到该品牌");
        AssertUtil.isTrue(!goodsBrand.getApplyStoreId().equals(vendor.getStoreId()), "无操作权限，请重试！");

        GoodsExample example = new GoodsExample();
        example.setBrandId(brandId);
        example.setStateNotEquals(GoodsConst.GOODS_STATE_DELETE);
        Integer count = goodsModel.getGoodsCount(example);
        AssertUtil.isTrue(count > 0, "该品牌正在使用，不能删除");

        goodsBrandModel.deleteGoodsBrand(brandId);
        return SldResponse.success("删除品牌成功");
    }

    @ApiOperation("查看品牌详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true)
    })
    @GetMapping("detail")
    public JsonResult<SellerBrandDetailVO> detail(HttpServletRequest request, @RequestParam("brandId") Integer brandId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        GoodsBrand goodsBrand = goodsBrandModel.getGoodsBrandByBrandId(brandId);
        AssertUtil.notNull(goodsBrand, "未获取到品牌信息");
        AssertUtil.isTrue(!vendor.getStoreId().equals(goodsBrand.getApplyStoreId()), "无操作权限，请重试！");

        SellerBrandDetailVO vo = new SellerBrandDetailVO(goodsBrand);
        //获取品牌绑定的分类列表
        List<BrandAndCateVO> brandAndCateVOList = cateList(goodsBrand.getBrandId());
        vo.setBrandAndCateVOList(brandAndCateVOList);
        return SldResponse.success(vo);
    }

    /**
     * 获取分类列表
     *
     * @param brandId 品牌id
     * @return 分类列表
     */
    private List<BrandAndCateVO> cateList(Integer brandId) {
        List<BrandAndCateVO> list = new LinkedList<>();
        //根据品牌id查询品牌分类
        GoodsBrand goodsBrand = goodsBrandModel.getGoodsBrandByBrandId(brandId);
        //根据分类id查询分类
        GoodsCategory goodsCategory3 = goodsCategoryModel.getGoodsCategoryByCategoryId(goodsBrand.getGoodsCategoryId3());

        //获取二级分类id
        Integer categoryId2 = goodsCategory3.getPid();
        GoodsCategory goodsCategoryByCategoryId = goodsCategoryModel.getGoodsCategoryByCategoryId(categoryId2);
        //获取一级分类id
        Integer categoryId1 = goodsCategoryByCategoryId.getPid();

        //获取所有一级分类
        GoodsCategoryExample goodsCategoryExample1 = new GoodsCategoryExample();
        goodsCategoryExample1.setGrade(1);
        List<GoodsCategory> goodsCategoryList = goodsCategoryModel.getGoodsCategoryList(goodsCategoryExample1, null);
        for (GoodsCategory goodsCategory1 : goodsCategoryList) {
            BrandAndCateVO vo = new BrandAndCateVO();
            vo.setCategoryId(goodsCategory1.getCategoryId());
            vo.setCategoryName(goodsCategory1.getCategoryName());
            if (goodsCategory1.getCategoryId().equals(categoryId1)) {

                GoodsCategoryExample goodsCategoryExample2 = new GoodsCategoryExample();
                goodsCategoryExample2.setPid(goodsCategory1.getCategoryId());
                List<GoodsCategory> goodsCategoryList2 = goodsCategoryModel.getGoodsCategoryList(goodsCategoryExample2, null);
                List<BrandAndCateVO> vos2 = new LinkedList<>();
                for (GoodsCategory category2 : goodsCategoryList2) {
                    BrandAndCateVO vo2 = new BrandAndCateVO();
                    vo2.setCategoryId(category2.getCategoryId());
                    vo2.setCategoryName(category2.getCategoryName());
                    if (category2.getCategoryId().equals(categoryId2)) {
                        GoodsCategoryExample goodsCategoryExample3 = new GoodsCategoryExample();
                        goodsCategoryExample3.setPid(category2.getCategoryId());
                        List<GoodsCategory> goodsCategoryList3 = goodsCategoryModel.getGoodsCategoryList(goodsCategoryExample3, null);
                        List<BrandAndCateVO> vos3 = new LinkedList<>();
                        for (GoodsCategory category3 : goodsCategoryList3) {
                            BrandAndCateVO vo3 = new BrandAndCateVO();
                            vo3.setCategoryId(category3.getCategoryId());
                            vo3.setCategoryName(category3.getCategoryName());
                            vos3.add(vo3);
                        }
                        vo2.setChildren(vos3);
                    }
                    vos2.add(vo2);
                }
                vo.setChildren(vos2);
            }
            list.add(vo);
        }
        return list;
    }
}
