package com.ruoyi.web.v2.v1.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsMaterial;
import com.ruoyi.web.v2.v1.mapper.JsMaterialMapper;
import com.ruoyi.web.v2.v1.service.IJsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v2.0物料管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-21
 */
@Service
public class JsMaterialServiceImpl extends ServiceImpl<JsMaterialMapper, JsMaterial> implements IJsMaterialService {

    @Autowired
    private JsMaterialMapper materialMapper;

    @Override
    public AjaxResult add(JsMaterial material) {
        Integer count = materialMapper.selectCount(new QueryWrapper<JsMaterial>().eq("material_name",material.getMaterialName()));
        if (count != 0) {
            return error("物料类型已存在！");
        } else {
            JsMaterial jsMaterial = materialMapper.selectOne(new QueryWrapper<JsMaterial>().last(" limit 1").orderByDesc("id"));
            if (ObjectUtil.isEmpty(jsMaterial)) {
                String formattedNumber = String.format("%04d", 1);
                material.setMaterialNum(formattedNumber);
            } else {
                String workshopNum = jsMaterial.getMaterialNum();
                Integer integer = Integer.valueOf(workshopNum);
                String formattedNumber = String.format("%04d", integer+1);
                material.setMaterialNum(formattedNumber);
            }
            materialMapper.insert(material);
        }
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsMaterial material) {
        String materialName = material.getMaterialName();
        Integer materialType = material.getMaterialType();
        Integer count = materialMapper.selectCount(new QueryWrapper<JsMaterial>().
                eq("material_name", materialName).
                eq("material_type",materialType).ne("id",material.getId()));
        if (count != 0) {
            return error("物料已存在！");
        } else {
            materialMapper.updateById(material);
        }
        return success("修改成功");
    }

    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            materialMapper.deleteById(s);
        }
        return success("删除成功");
    }

    /**
     * 物料管理分页查询
     * @param pageNum
     * @param pageSize
     * @param materialName
     * @param materialType
     * @param needAllFlag 【是否查询全部标志，默认false表示（排除掉危险品的查询）】
     * @return
     */
    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String materialName, Integer materialType, Boolean needAllFlag) {
        QueryWrapper<JsMaterial> queryWrapper = new QueryWrapper<>();
        Page<JsMaterial> page = new Page<>(pageNum, pageSize);
        if (null != materialType) {
            queryWrapper.eq("material_type", materialType);
        }else if(ObjectUtils.isEmpty(needAllFlag) || needAllFlag.equals(Boolean.FALSE)) {
            //material_type=3表示该种物料是危险品，查询时默认排除危险品
            queryWrapper.ne("material_type", 3);
        }
        queryWrapper.like(StringUtils.isNotEmpty(materialName), "material_name", materialName);
        return success(materialMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(materialMapper.selectList(null));
    }

}
