package com.fjhx.service.material.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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.constants.StatusConstant;
import com.fjhx.entity.Material;
import com.fjhx.entity.material.MaterialEx;
import com.fjhx.mapper.material.MaterialExMapper;
import com.fjhx.service.ClassifyService;
import com.fjhx.service.MaterialService;
import com.fjhx.service.material.MaterialExService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料拓展表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-14
 */
@Service
public class MaterialExServiceImpl extends ServiceImpl<MaterialExMapper, MaterialEx> implements MaterialExService {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private ClassifyService classifyService;

    @Override
    public Page<Map<String, Object>> getPage(Map<String, String> condition) {
        QueryWrapper<?> wrapper = WrapperUtil.init(condition)
                .keyword("m.code", "m.name")
                .getWrapper();

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

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

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Map<String, Object> map) {

        Material material = BeanUtil.toBean(map, Material.class);
        MaterialEx materialEx = BeanUtil.toBean(map, MaterialEx.class);

        Integer coiled = materialEx.getCoiled();
        Assert.notEmpty(coiled, "是否卷材判断字段不能为空");

        if (ObjectUtil.isEmpty(materialEx.getSafetyStock())) {
            materialEx.setSafetyStock(BigDecimal.ZERO);
        }

        if (ObjectUtil.isEmpty(materialEx.getCycle())) {
            materialEx.setCycle(0);
        }

        if (ObjectUtil.isEmpty(materialEx.getQualityTestingRate())) {
            materialEx.setQualityTestingRate(BigDecimal.ZERO);
        }

        if (ObjectUtil.isEmpty(materialEx.getLoss()) || coiled.equals(StatusConstant.NO)) {
            materialEx.setLoss(BigDecimal.ZERO);
        }

        materialService.add(material);

        materialEx.setId(material.getId());
        save(materialEx);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(Map<String, Object> map) {
        Material material = BeanUtil.toBean(map, Material.class);
        MaterialEx materialEx = BeanUtil.toBean(map, MaterialEx.class);

        materialService.edit(material);
        updateById(materialEx);
    }

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

        materialService.delete(material);
    }

}
