package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.ProductCraft;
import com.ruoyi.project.system.domain.ProductCraftCate;
import com.ruoyi.project.system.domain.ProductCraftParams;
import com.ruoyi.project.system.domain.ProductMaterial;
import com.ruoyi.project.system.domain.vo.*;
import com.ruoyi.project.system.enums.DelFlagEnum;
import com.ruoyi.project.system.mapper.ProductCraftCateMapper;
import com.ruoyi.project.system.mapper.ProductCraftMapper;
import com.ruoyi.project.system.mapper.ProductCraftParamsMapper;
import com.ruoyi.project.system.mapper.ProductMaterialMapper;
import com.ruoyi.project.system.service.IProductCraftCateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 工艺分类Service业务层处理
 *
 * @author zhu
 * @date 2023-10-26
 */
@Service
public class ProductCraftCateServiceImpl implements IProductCraftCateService {
    @Autowired
    private ProductCraftCateMapper productCraftCateMapper;

    @Autowired
    private ProductCraftMapper productCraftMapper;

    @Autowired
    private ProductCraftParamsMapper productCraftParamsMapper;

    @Autowired
    private ProductMaterialMapper productMaterialMapper;

    /**
     * 查询工艺分类
     *
     * @param id 工艺分类主键
     * @return 工艺分类
     */
    @Override
    public ProductCraftCate selectProductCraftCateById(Long id) {
        return productCraftCateMapper.selectProductCraftCateById(id);
    }

    /**
     * 查询工艺分类列表
     *
     * @param productCraftCate 工艺分类
     * @return 工艺分类
     */
    @Override
    public List<ProductCraftCate> selectProductCraftCateList(ProductCraftCate productCraftCate) {
        return productCraftCateMapper.selectProductCraftCateList(productCraftCate);
    }

    /**
     * 新增工艺分类
     *
     * @param productCraftCate 工艺分类
     * @return 结果
     */
    @Override
    public int insertProductCraftCate(ProductCraftCate productCraftCate) {
        productCraftCate.setCreateTime(DateUtils.getNowDate());
        return productCraftCateMapper.insertProductCraftCate(productCraftCate);
    }

    /**
     * 修改工艺分类
     *
     * @param productCraftCate 工艺分类
     * @return 结果
     */
    @Override
    public int updateProductCraftCate(ProductCraftCate productCraftCate) {
        productCraftCate.setUpdateTime(DateUtils.getNowDate());
        return productCraftCateMapper.updateProductCraftCate(productCraftCate);
    }

    /**
     * 批量删除工艺分类
     *
     * @param ids 需要删除的工艺分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCraftCateByIds(Long[] ids) {
        return productCraftCateMapper.deleteProductCraftCateByIds(ids);
    }

    /**
     * 删除工艺分类信息
     *
     * @param id 工艺分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCraftCateById(Long id) {
        return productCraftCateMapper.deleteProductCraftCateById(id);
    }

    @Override
    public int updatePrice(List<ProductCraftCateVo> productCates, Long productId, Long materialId) {
        List<ProductCraftCateVo> cates = productCates;
        List<ProductCraftVo> craftVos;
        List<ProductCraftParams> params;
        productCraftCateMapper.deleteByProductId(productId, materialId);

        if (!StringUtils.isEmpty(cates)) {
            ProductCraftCate productCraftCate = null;
            ProductCraft productCraft = null;
            Long cateId = null;
            Long craftId = null;
            Boolean carftDel = false;
            Boolean paramsDel = false;

            for (ProductCraftCateVo productCraftCateVo : cates) {
                productCraftCate = new ProductCraftCate();
                BeanUtils.copyProperties(productCraftCateVo, productCraftCate);
                productCraftCate.setProductId(productId);
                productCraftCate.setMaterialId(materialId);
                productCraftCate.setUpdateBy(SecurityUtils.getUsername());

                //先把数据删除再选择更新或者新增数据
                if (productCraftCate.getId() != null) {
                    productCraftCate.setDelFlag(DelFlagEnum.NORMAL.getCode());
                    productCraftCateMapper.updateProductCraftCate(productCraftCate);
                }

                if (productCraftCate.getId() == null) {

                    productCraftCate.setCreateBy(SecurityUtils.getUsername());
                    productCraftCateMapper.insertProductCraftCate(productCraftCate);
                }
                cateId = productCraftCate.getId();
                craftVos = productCraftCateVo.getChildren();
                //先把数据删除
                if (!carftDel) {
                    productCraftMapper.deleteByProductId(productId, materialId);
                    carftDel = true;//防止第二个重复删除
                }
                if (!StringUtils.isEmpty(craftVos)) {

                    for (ProductCraftVo productCraftVo : craftVos) {
                        productCraft = new ProductCraft();
                        params = productCraftVo.getParams();

                        BeanUtils.copyProperties(productCraftVo, productCraft);

                        if (!StringUtils.isEmpty(params)) {
                            productCraft.setOpenParams(true);
                        }
                        //指定父级
                        productCraft.setProductId(productId);
                        productCraft.setMaterialId(materialId);
                        productCraft.setCateId(cateId);
                        productCraft.setUpdateBy(SecurityUtils.getUsername());

                        if (productCraft.getId() != null) {
                            productCraft.setDelFlag(DelFlagEnum.NORMAL.getCode());
                            productCraftMapper.updateProductCraft(productCraft);
                        }

                        if (productCraft.getId() == null) {
                            productCraft.setCreateBy(SecurityUtils.getUsername());

                            productCraftMapper.insertProductCraft(productCraft);
                        }
                        craftId = productCraft.getId();

                        if (!paramsDel) {
                            productCraftParamsMapper.deleteByProductId(productId, materialId);
                            paramsDel = true;
                        }
                        if (!StringUtils.isEmpty(params)) {

                            for (ProductCraftParams productCraftParams : params) {
                                productCraftParams.setCraftId(craftId);
                                productCraftParams.setProductId(productId);
                                productCraftParams.setMaterialId(materialId);
                                productCraftParams.setUpdateBy(SecurityUtils.getUsername());
                                if (productCraftParams.getId() != null) {
                                    productCraftParams.setDelFlag(DelFlagEnum.NORMAL.getCode());
                                    productCraftParamsMapper.updateProductCraftParams(productCraftParams);
                                }
                                if (productCraftParams.getId() == null) {
                                    productCraftParams.setCreateBy(SecurityUtils.getUsername());
                                    productCraftParamsMapper.insertProductCraftParams(productCraftParams);
                                }
                            }
                        }
                    }
                }
            }
        }
        return 1;
    }

    @Override
    public List<ProductCraftCateVo> selectProductDetail(Long productId, Long materialId) {
        List<ProductCraftCate> cates = productCraftCateMapper.selectProductCraftCateList(new ProductCraftCate().setProductId(productId).setMaterialId(materialId));
        List<ProductCraft> crafts = productCraftMapper.selectProductCraftList(new ProductCraft().setProductId(productId).setMaterialId(materialId));
        List<ProductCraftParams> params = productCraftParamsMapper.selectProductCraftParamsList(new ProductCraftParams().setProductId(productId).setMaterialId(materialId));

        if (StringUtils.isEmpty(cates)) {
            return null;
        }

        Map<Long, List<ProductCraftParams>> paramsMap = new HashMap<>();
        Map<Long, List<ProductCraftVo>> craftsMap = new HashMap<>();

        for (ProductCraftParams productCraftParams : params) {
            List<ProductCraftParams> paramsList = paramsMap.computeIfAbsent(productCraftParams.getCraftId(), k -> new ArrayList<>());
            paramsList.add(productCraftParams);
        }

        for (ProductCraft productCraft : crafts) {
            ProductCraftVo craftVo = new ProductCraftVo();
            BeanUtils.copyProperties(productCraft, craftVo);
            craftVo.setParams(paramsMap.getOrDefault(productCraft.getId(), new ArrayList<>()));

            List<ProductCraftVo> craftsVo = craftsMap.computeIfAbsent(productCraft.getCateId(), k -> new ArrayList<>());
            craftsVo.add(craftVo);

        }

        List<ProductCraftCateVo> catesVo = new ArrayList<>();
        for (ProductCraftCate productCraftCate : cates) {
            ProductCraftCateVo productCraftCateVo = new ProductCraftCateVo();
            BeanUtils.copyProperties(productCraftCate, productCraftCateVo);
            productCraftCateVo.setChildren(craftsMap.getOrDefault(productCraftCate.getId(), new ArrayList<>()));

            catesVo.add(productCraftCateVo);
        }

        return catesVo;
    }

    @Override
    public FrontProductDetailVo selectProductDetailById(ProductVo product) {

        //查除所以更商品关联的数据
        List<ProductCraftCate> cates = productCraftCateMapper.selectProductCraftCateList(new ProductCraftCate().setProductId(product.getId()));
        List<ProductCraft> crafts = productCraftMapper.selectProductCraftList(new ProductCraft().setProductId(product.getId()));
        List<ProductCraftParams> params = productCraftParamsMapper.selectProductCraftParamsList(new ProductCraftParams().setProductId(product.getId()));
        List<ProductMaterial> materials = productMaterialMapper.selectProductMaterialList(new ProductMaterial().setProductId(product.getId()));

        FrontProductDetailVo frontProductDetailVo = new FrontProductDetailVo();
        BeanUtils.copyProperties(product, frontProductDetailVo);
        //处理获取的数据进行分类整理
        List<FrontProductCraftCateVo> catesList = getCates(cates, crafts, params);
        List<FrontProductCraftCateVo> productCate = new ArrayList<>();
        Map<Long, List<FrontProductCraftCateVo>> materialsCatesMap = new HashMap<>();
        //把分类筛选出来放入指定位置
        if (StringUtils.isNotEmpty(catesList)) {

            for (FrontProductCraftCateVo frontProductCraftCateVo : catesList) {
                if (frontProductCraftCateVo.getMaterialId() == 0) {
                    productCate.add(frontProductCraftCateVo);
                    continue;
                }
                List<FrontProductCraftCateVo> materialsCates = materialsCatesMap.computeIfAbsent(frontProductCraftCateVo.getMaterialId(), k -> new ArrayList<>());
                materialsCates.add(frontProductCraftCateVo);
            }
        }

        List<FrontProductMaterialVo> frontProductMaterialVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(materials)) {
            for (ProductMaterial material : materials) {
                FrontProductMaterialVo frontProductMaterialVo = new FrontProductMaterialVo();
                BeanUtils.copyProperties(material, frontProductMaterialVo);
                frontProductMaterialVo.setProductCates(materialsCatesMap.getOrDefault(material.getId(), new ArrayList<>()));
                frontProductMaterialVos.add(frontProductMaterialVo);
            }
        }

        frontProductDetailVo.setProductCates(productCate);
        frontProductDetailVo.setProductMaterials(frontProductMaterialVos);

        return frontProductDetailVo;
    }

    private List<FrontProductCraftCateVo> getCates(List<ProductCraftCate> cates, List<ProductCraft> crafts, List<ProductCraftParams> params) {
        if (StringUtils.isEmpty(cates)) {
            return null;
        }

        List<FrontProductCraftCateVo> catesVo = new ArrayList<>();
        Map<Long, List<FrontProductCraftVo>> craftsMap = new HashMap<>();
        Map<Long, List<FrontCraftParamsVo>> paramsMap = new HashMap<>();
        if (StringUtils.isNotEmpty(params)) {
            for (ProductCraftParams productCraftParams : params) {
                FrontCraftParamsVo frontCraftParamsVo = new FrontCraftParamsVo();
                BeanUtils.copyProperties(productCraftParams, frontCraftParamsVo);

                List<FrontCraftParamsVo> paramsList = paramsMap.computeIfAbsent(frontCraftParamsVo.getCraftId(), k -> new ArrayList<>());
                paramsList.add(frontCraftParamsVo);
            }
        }
        if (StringUtils.isNotEmpty(crafts)) {

            for (ProductCraft productCraft : crafts) {
                FrontProductCraftVo craftVo = new FrontProductCraftVo();
                BeanUtils.copyProperties(productCraft, craftVo);
                craftVo.setParams(paramsMap.getOrDefault(productCraft.getId(), new ArrayList<>()));

                List<FrontProductCraftVo> craftsVo = craftsMap.computeIfAbsent(productCraft.getCateId(), k -> new ArrayList<>());
                craftsVo.add(craftVo);
            }
        }
        if (StringUtils.isNotEmpty(cates)) {

            for (ProductCraftCate productCraftCate : cates) {
                FrontProductCraftCateVo productCraftCateVo = new FrontProductCraftCateVo();
                BeanUtils.copyProperties(productCraftCate, productCraftCateVo);
                productCraftCateVo.setChildren(craftsMap.getOrDefault(productCraftCate.getId(), new ArrayList<>()));

                catesVo.add(productCraftCateVo);
            }
        }
        return catesVo;
    }

}
