package com.fjhx.material.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.constants.StatusConstant;
import com.fjhx.entity.material.Material;
import com.fjhx.material.mapper.MaterialSMapper;
import com.fjhx.material.service.MaterialService;
import com.fjhx.params.material.EditMaterialVo;
import com.fjhx.utils.Assert;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 物料 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-20
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialSMapper, Material> implements MaterialService {

    /**
     * 下拉列表
     *
     * @param condition
     * @return
     */
    @Override
    public List<Material> selectList(Map<String, Object> condition) {
        List<Material> list = baseMapper.selectMaList(condition);
        return list;
    }

    /**
     * 下拉用途
     *
     * @return
     */
    @Override
    public List<Material> selectPurpose(String purpose) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("Purpose").groupBy("Purpose");
        if (StringUtils.isNotEmpty(purpose)) {
            queryWrapper.like("Purpose", purpose);
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getMaterialPrice(String materialCode) {
        return baseMapper.getMaterialPrice(materialCode);
    }

    @Override
    public List<Map<String, Object>> getInRfidData(List<String> rfidList) {
        QueryWrapper<Object> wrapper = Wrappers.query()
                .eq("st.IsDelete", StatusConstant.NO)
                .in("st.rfidCode", rfidList)
                .isNull("sd.id")
                .isNull("swd.id");

        return baseMapper.getInRfidData(wrapper);
    }

    @Override
    public List<Map<String, Object>> getOutRfidData(List<String> rfidList, String stockHouseId) {
        QueryWrapper<Object> wrapper = Wrappers.query()
                .eq("sd.IsDelete", StatusConstant.NO)
                .eq("sd.StockHouseId", stockHouseId)
                .in("sd.RfidCode", rfidList);

        return baseMapper.getOutRfidData(wrapper);
    }

    @Override
    public boolean edit(EditMaterialVo editMaterialVo) {

        Material material = new Material();

        String code = editMaterialVo.getCode();
        Assert.notEmpty(code, "物料编码不能为空");

        material.setCode(editMaterialVo.getCode());
        material.setName(editMaterialVo.getName());
        material.setSpec(editMaterialVo.getSpec());
        material.setUnitweight(editMaterialVo.getUnitWeight());
        material.setWidth(editMaterialVo.getWidth());
        material.setStockunitid(editMaterialVo.getStockUnit());
        material.setCategorycode(editMaterialVo.getCategoryCode());

        Material oldMaterial = getOne(Wrappers.<Material>lambdaQuery()
                .eq(Material::getCode, editMaterialVo.getCode())
                .last("limit 1")
        );

        Date date = new Date();

        switch (editMaterialVo.getType()) {
            case 1:
                Assert.empty(oldMaterial, "新增失败，物料编码已存在");
                material.setIsdelete(0);
                material.setSafetystock(0);
                material.setCreatedTime(date);
                material.setUpdatedTime(date);
                material.setAutotasktime(date);
                material.setCheckrate(new BigDecimal("0.3"));
                material.setSafetywarnday(0);
                material.setDelayperiod(30);
                material.setPrice(BigDecimal.ZERO);
                material.setStockstandard(BigDecimal.ZERO);
                material.setUnitnum(BigDecimal.ZERO);
                material.setTechnologytype(4);

                return save(material);
            case 2:
                Assert.notEmpty(oldMaterial, "编辑失败，物料编码不存在");

                material.setId(oldMaterial.getId());
                material.setUpdatedTime(date);

                return updateById(material);
            case 3:
                Assert.notEmpty(oldMaterial, "删除失败，物料编码不存在");

                return removeById(oldMaterial.getId());
            default:
                return false;
        }
    }
}
