package vip.xiaonuo.write.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.param.ProductionPageParam;
import vip.xiaonuo.quality.entity.QualityWrite;
import vip.xiaonuo.write.entity.WriteProduct;
import vip.xiaonuo.write.mapper.WriteProductMapper;
import vip.xiaonuo.write.param.WriteProductAddParam;
import vip.xiaonuo.write.service.IWriteProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 成品检验台账填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class WriteProductServiceImpl extends ServiceImpl<WriteProductMapper, WriteProduct> implements IWriteProductService {

    @Autowired
    private WriteProductMapper productMapper;

    @Override
    public CommonResult add(WriteProductAddParam addParam) {
        WriteProduct writeProduct = new WriteProduct();
        BeanUtil.copyProperties(addParam, writeProduct);
        productMapper.insert(writeProduct);
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(WriteProduct writeProduct) {
        productMapper.updateById(writeProduct);
        return CommonResult.ok();
    }

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

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