package vip.xiaonuo.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.production.entity.ProductionWriteRate;
import vip.xiaonuo.production.param.ProductionPageParam;
import vip.xiaonuo.quality.entity.QualityWrite;
import vip.xiaonuo.quality.mapper.QualityWriteMapper;
import vip.xiaonuo.quality.param.QualityWriteAddParam;
import vip.xiaonuo.quality.service.IQualityWriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 质量来料不合格填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-01
 */
@Service
public class QualityWriteServiceImpl extends ServiceImpl<QualityWriteMapper, QualityWrite> implements IQualityWriteService {

    @Autowired
    private QualityWriteMapper qualityWriteMapper;

    @Override
    public CommonResult add(QualityWriteAddParam param) {
        QualityWrite qualityWrite = new QualityWrite();
        BeanUtil.copyProperties(param, qualityWrite);
        qualityWriteMapper.insert(qualityWrite);
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(QualityWrite qualityWrite) {
        qualityWriteMapper.updateById(qualityWrite);
        return CommonResult.ok();
    }

    @Override
    public CommonResult delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        qualityWriteMapper.deleteById(id);
        return CommonResult.ok();
    }

    @Override
    public Page<QualityWrite> page(ProductionPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        QueryWrapper<QualityWrite> queryWrapper = new QueryWrapper();
        Page<QualityWrite> page = new Page<>(pageNum, pageSize);
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return qualityWriteMapper.selectPage(page, queryWrapper);
    }
}
