package com.miaxis.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.exception.CustomException;
import com.miaxis.customer.service.ICustomerInfoService;
import com.miaxis.product.domain.ProductTypeInfo;
import com.miaxis.product.mapper.ProductTypeInfoMapper;
import com.miaxis.product.service.IProductTypeInfoService;
import com.miaxis.product.vo.AppletProductTypeInfoVo;
import com.miaxis.product.vo.ProductTypeInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 品类Service业务层处理
 *
 * @author miaxis
 * @date 2021-03-11
 */
@Service
@RequiredArgsConstructor
public class ProductTypeInfoServiceImpl extends ServiceImpl<ProductTypeInfoMapper, ProductTypeInfo> implements IProductTypeInfoService {

    private final ProductTypeInfoMapper productTypeInfoMapper;

    private final ICustomerInfoService customerInfoService;

    /**
     * 查询品类列表
     *
     * @param productTypeInfo 品类
     * @return 品类
     */
    @Override
    public List<ProductTypeInfoVo> selectProductTypeInfoList(ProductTypeInfo productTypeInfo){
        return productTypeInfoMapper.selectProductTypeInfoList(productTypeInfo);
    }

    /**
     * 删除品类
     * @param ids
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response<Integer> removeProductByIds(Long[] ids) {
        for (Long id : ids) {
            this.update(new UpdateWrapper<ProductTypeInfo>().set("status","1").eq("id",id));
        }
        return Response.success();
    }

    /**
     * 获取品类详情信息
     * @param id
     * @return
     */
    @Override
    public Response<ProductTypeInfoVo> getProductDetailsById(Long id) {

        return Response.success(productTypeInfoMapper.getProductDetailsById(id));
    }

    /**
     * 新增品类
     * @param productTypeInfo
     * @return
     */
    @Override
    public Response saveProductTypeInfo(ProductTypeInfo productTypeInfo) {

        List<ProductTypeInfo> productTypeInfos = productTypeInfoMapper.selectByMap(new HashMap<String, Object>() {{
            put("product_name", productTypeInfo.getProductName());
        }});
        if (!productTypeInfos.isEmpty()) throw new CustomException("分类名称重复");

        productTypeInfoMapper.insert(productTypeInfo);
        return Response.success();

    }

    /**
     * applet
     * 分类列表
     * @return
     */
    @Override
    public List<AppletProductTypeInfoVo> selectAppletProductTypeInfoList() {
        List<AppletProductTypeInfoVo> productTypeInfoVos = productTypeInfoMapper.selectAppletProductTypeInfoList();
        return builTree(productTypeInfoVos);
    }


    //建立树形结构
    public List<AppletProductTypeInfoVo> builTree(List<AppletProductTypeInfoVo> productList){
        List<AppletProductTypeInfoVo> infoVos =new  ArrayList<>();
        //获取根节点
        List<AppletProductTypeInfoVo> rootMenuLists =new  ArrayList<>();
        for(AppletProductTypeInfoVo productType : productList) {
            if(productType.getPid().toString().equals("0")) {
                rootMenuLists.add(productType);
            }
        }
        for(AppletProductTypeInfoVo productTypeInfoVo : rootMenuLists) {
            productTypeInfoVo=getTree(productTypeInfoVo,productList);
            infoVos.add(productTypeInfoVo);
        }
        return infoVos;
    }
    //递归，建立子树形结构
    private AppletProductTypeInfoVo getTree(AppletProductTypeInfoVo productTypeInfo,List<AppletProductTypeInfoVo> menuList){
        List<AppletProductTypeInfoVo> infoVos = new ArrayList<>();
        for (AppletProductTypeInfoVo productTypeInfoVo : menuList) {
            if (productTypeInfoVo.getPid().toString().equals(productTypeInfo.getId().toString())){
                infoVos.add(getTree(productTypeInfoVo,menuList));
            }
        }
        productTypeInfo.setChildren(infoVos);
        return productTypeInfo;
    }

}
