
package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.CategoryProd;
import com.yami.shop.bean.param.CategoryProductDTO;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.CategoryProdMapper;
import com.yami.shop.service.CategoryProdService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lgh on 2018/07/13.
 */
@Service
@Slf4j
public class CategoryProdServiceImpl extends ServiceImpl<CategoryProdMapper, CategoryProd> implements CategoryProdService {

    @Autowired
    private CategoryProdMapper categoryPropMapper;

    @Override
    public IPage<CategoryProd> pageByCode(CategoryProd categoryProd, PageParam<CategoryProd> page) {
        return categoryPropMapper.pageByCode(page, categoryProd);
    }


    @Override
    public void categoryProduct(CategoryProductDTO categoryProductDTO) {
        List<Long> prodIdList = categoryProductDTO.getProdIdList();
        if (ObjectUtils.isEmpty(prodIdList)) {
            new GlobalException("新增商品为空！请选择商品");
        }
        for (Long aLong : prodIdList) {

            Integer integer = categoryPropMapper.selectCount(new LambdaQueryWrapper<CategoryProd>().eq(CategoryProd::getProdId, aLong)
                    .eq(CategoryProd::getIsDelete, 0)
                    .eq(CategoryProd::getShopId, categoryProductDTO.getShopId())
                    .eq(CategoryProd::getCode, categoryProductDTO.getCode()));

            if (integer > 0) {
                new GlobalException("该门店分类对应商品ID=" + aLong + " 已存在！请勿重复添加");
            }
            CategoryProd categoryProd = new CategoryProd();
            categoryProd.setCategoryId(categoryProductDTO.getCategoryId());
            categoryProd.setProdId(aLong);
            categoryProd.setShopId(categoryProductDTO.getShopId());
            categoryProd.setCode(categoryProductDTO.getCode());
            categoryProd.setIsDelete(0);
            categoryProd.setCreateTime(new Date());
            categoryPropMapper.insert(categoryProd);

            //查看父类绑定商品是否存在不存在添加
            Integer integer1 = categoryPropMapper.selectCount(new LambdaQueryWrapper<CategoryProd>()
                    .eq(CategoryProd::getCode, categoryProductDTO.getParentCode())
                    .eq(CategoryProd::getShopId, categoryProductDTO.getShopId())
                    .eq(CategoryProd::getIsDelete, 0));
            log.info("查看父类绑定商品是否存在不存在添加:{}", integer1);
            if (integer1 == 0) {
                CategoryProd categoryProdParent = new CategoryProd();
                categoryProdParent.setCategoryId(categoryProductDTO.getCategoryId());
                categoryProdParent.setProdId(aLong);
                categoryProdParent.setShopId(categoryProductDTO.getShopId());
                categoryProdParent.setCode(categoryProductDTO.getParentCode());
                categoryProdParent.setIsDelete(0);
                categoryProdParent.setCreateTime(new Date());
                categoryPropMapper.insert(categoryProdParent);
            }

        }
    }

    @Override
    public void cancelCategoryProduct(CategoryProductDTO categoryProductDTO) {
        List<Long> prodIdList = categoryProductDTO.getProdIdList();
        if (ObjectUtils.isEmpty(prodIdList)) {
            new GlobalException("取消商品为空！请选择商品");
        }

        for (Long aLong : prodIdList) {

            List<CategoryProd> categoryProdList = categoryPropMapper.selectList(new LambdaQueryWrapper<CategoryProd>().eq(CategoryProd::getProdId, aLong)
                    .eq(CategoryProd::getIsDelete, 0)
                    .eq(CategoryProd::getShopId, categoryProductDTO.getShopId())
                    .eq(CategoryProd::getCode, categoryProductDTO.getCode()));

            if (ObjectUtils.isEmpty(categoryProdList)) {
                new GlobalException("该门店分类对应商品ID=" + aLong + " 不存在！请勿重复取消");
            }
            for (CategoryProd categoryProd : categoryProdList) {
                categoryProd.setIsDelete(1);
                categoryPropMapper.deleteById(categoryProd);
            }
        }

    }
}
