package com.shinsoft.sto.service.impl.main;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.mapper.main.MaterialMapper;
import com.shinsoft.sto.model.main.Material;
import com.shinsoft.sto.service.main.MaterialService;
import com.shinsoft.sto.model.system.ResponseCodeMsg;
import com.shinsoft.sto.model.system.ResultJSON;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MaterialServiceImp extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Override
    public ResultJSON query(int page, int rows, String material, String standard, String userId, String belongId) {
        try {
            Page<Material> pageParam = new Page<>(page, rows);
            IPage<Material> materialPage = baseMapper.selectMaterialPage(pageParam, material, standard, belongId);

            return ResultJSON.success(materialPage);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @Override
    public ResultJSON save(Material material, String userId, String belongId) {
        try {
            // 检查物料名称是否重复
            if (material.getId() == null || material.getId().isEmpty()) {
                // 新增
                List<Material> existingMaterials = baseMapper.selectByMaterialName(material.getMaterialName(), belongId);
                if (existingMaterials != null && !existingMaterials.isEmpty()) {
                    return ResultJSON.error("物料名称已存在，请勿重复添加");
                }

                // 设置新增信息
                material.setAddId(userId);
                material.setAddTime(new Date());
                material.setCancelFlag("0");
                material.setBelongId(belongId);

                boolean success = save(material);
                return success ? ResultJSON.success("新增成功") : ResultJSON.error("新增失败");
            } else {
                // 更新
                Material existingMaterial = getById(material.getId());
                if (existingMaterial == null) {
                    return ResultJSON.error("物料不存在");
                }

                // 检查除当前记录外是否有相同物料名称
                List<Material> sameNameMaterials = baseMapper.selectByMaterialName(material.getMaterialName(), belongId);
                if (sameNameMaterials != null && sameNameMaterials.stream()
                        .anyMatch(m -> !m.getId().equals(material.getId()))) {
                    return ResultJSON.error("物料名称已存在，请修改");
                }

                // 设置更新信息
                material.setModifyId(userId);
                material.setModifyTime(new Date());

                boolean success = updateById(material);
                return success ? ResultJSON.success("更新成功") : ResultJSON.error("更新失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @Override
    public ResultJSON remove(String id, String userId) {
        try {
            Material material = getById(id);
            if (material == null) {
                return ResultJSON.error("物料不存在");
            }

            // 软删除
            material.setCancelFlag("9");
            material.setCancelId(userId);
            material.setCancelTime(new Date());

            boolean success = updateById(material);
            return success ? ResultJSON.success("删除成功") : ResultJSON.error("删除失败");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @Override
    public List<com.shinsoft.sto.model.common.selectModel> getMaterialOptions(String belongId) {
        return baseMapper.selectMaterialOptions(belongId);
    }
}