package com.fjhx.service.product.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.Product;
import com.fjhx.entity.product.ProductEx;
import com.fjhx.mapper.product.ProductExMapper;
import com.fjhx.params.production.ProductWorkingProcedure;
import com.fjhx.params.production.ProductWorkingRoute;
import com.fjhx.service.ClassifyService;
import com.fjhx.service.ProductService;
import com.fjhx.service.product.ProductExService;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-14
 */
@Service
public class ProductExServiceImpl extends ServiceImpl<ProductExMapper, ProductEx> implements ProductExService {

    @Autowired
    private ProductService productService;

    @Autowired
    private ClassifyService classifyService;

    @Override
    public Page<Map<String, Object>> getPage(Map<String, String> condition) {

        QueryWrapper<?> wrapper = WrapperUtil.init(condition)
                .keyword("p.name", "p.code")
                .getWrapper();

        // 编码正序
        wrapper.orderByAsc("p.code");

        // 如果传入分类id，查询分类已经分类下级
        String classifyId = condition.get("classifyId");
        if (ObjectUtil.isNotEmpty(classifyId)) {
            List<Long> childrenIdList = classifyService.getChildrenIdList(Long.valueOf(classifyId));
            wrapper.in("p.classify_id", childrenIdList);
        }

        return baseMapper.getPage(createPage(condition), wrapper);
    }

    @Override
    public void add(Map<String, Object> map) {
        Product productVo = BeanUtil.toBean(map, Product.class);
        ProductEx productEx = BeanUtil.toBean(map, ProductEx.class);

        productService.add(productVo);

        productEx.setId(productVo.getId());
        save(productEx);
    }

    @Override
    public void edit(Map<String, Object> map) {
        Product productVo = BeanUtil.toBean(map, Product.class);
        ProductEx productEx = BeanUtil.toBean(map, ProductEx.class);

        productService.edit(productVo);
        updateById(productEx);
    }

    @Override
    public void delete(Map<String, Object> map) {
        Product productVo = BeanUtil.toBean(map, Product.class);

        productService.delete(productVo);
    }

    @Override
    public List<Map<String, Object>> getList() {
        // 编码正序
        QueryWrapper<?> wrapper = Wrappers.query().orderByAsc("p.code");

        return baseMapper.getList(wrapper);
    }

    @Override
    public ProductWorkingRoute getWorkingRoute(Long ProductId) {
        ProductWorkingRoute productWorkingRoute = baseMapper.getProductWorkingRoute(ProductId);
        List<ProductWorkingProcedure> procedureList = baseMapper.getProcedureList(productWorkingRoute.getWorkingRouteId());
        productWorkingRoute.setProductWorkingProcedureList(procedureList);
        return productWorkingRoute;
    }

}
