package com.gree.mall.manager.logic.goods;

import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.goods.GoodsCategoryBean;
import com.gree.mall.manager.commonmapper.CustomGoodsCategoryMapper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.GoodsCategory;
import com.gree.mall.manager.plus.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GoodsCategoryLogic {

    @Resource
    CustomGoodsCategoryMapper customGoodsCategoryMapper;
    @Resource
    GoodsCategoryService goodsCategoryService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 分类管理列表
     *
     * @param level
     * @param status
     * @return
     */
    public List<GoodsCategoryBean> list(Integer level, String name, Boolean status, HttpServletRequest request) {
        //获取当前登录企业微信id
        AdminUserCom adminUser = commonLogic.getAdminUser(request);


        return customGoodsCategoryMapper.list(level, status, name);
    }

    /**
     * 新增分类
     *
     * @param goodsCategoryBean
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(GoodsCategoryBean goodsCategoryBean, HttpServletRequest request) {
        Date createDate = new Date();
        if (goodsCategoryBean.getChildren() != null) {
            List<GoodsCategory> goodsCategoryList = goodsCategoryBean.getChildren()
                    .stream()
                    .map(c -> c.setCreateTime(createDate))
                    .collect(Collectors.toList());
            for (GoodsCategory goodsCategory : goodsCategoryList) {
                goodsCategory.setLevel(2);
                goodsCategory.setCreateTime(new Date());

            }
            goodsCategoryService.saveBatch(goodsCategoryList);
        } else {
            goodsCategoryBean.setCreateTime(createDate);
            goodsCategoryService.save(goodsCategoryBean);
        }
    }

    /**
     * 修改分类
     *
     * @param goodsCategory
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(GoodsCategory goodsCategory) {
        goodsCategoryService.updateById(goodsCategory);
        //如果有子集，覆盖子集的一些参数
//        goodsCategoryService.lambdaUpdate()
//                .set(GoodsCategory::getIsPromotionDiscount,goodsCategory.getIsPromotionDiscount())
//                .eq(GoodsCategory::getParentId,goodsCategory.getCategoryId()).update();
    }

    /**
     * 删除分类
     *
     * @param categoryId
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String categoryId) {
//        goodsCategoryService.removeById(categoryId);
        // 删除子类别
        goodsCategoryService.lambdaUpdate()
                .eq(GoodsCategory::getCategoryId, categoryId)
                .or()
                .eq(GoodsCategory::getParentId, categoryId)
                .remove();
    }

    /**
     * 分类详情
     */
    public GoodsCategory detail(String goodsCategoryId) {
        return goodsCategoryService.getById(goodsCategoryId);
    }

}
