package cn.iocoder.yudao.module.mes.service.producereport;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.producereport.ProduceReportDetailDO;
import cn.iocoder.yudao.module.mes.dal.mysql.producereport.ProduceReportDetailMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRODUCE_REPORT_DETAIL_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

/**
 * {@link ProduceReportDetailServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(ProduceReportDetailServiceImpl.class)
public class ProduceReportDetailServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ProduceReportDetailServiceImpl produceReportDetailService;

    @Resource
    private ProduceReportDetailMapper produceReportDetailMapper;

    @Test
    public void testCreateProduceReportDetail_success() {
        // 准备参数
        ProduceReportDetailSaveReqVO createReqVO = randomPojo(ProduceReportDetailSaveReqVO.class).setId(null);

        // 调用
        Long produceReportDetailId = produceReportDetailService.createProduceReportDetail(createReqVO);
        // 断言
        assertNotNull(produceReportDetailId);
        // 校验记录的属性是否正确
        ProduceReportDetailDO produceReportDetail = produceReportDetailMapper.selectById(produceReportDetailId);
        assertPojoEquals(createReqVO, produceReportDetail, "id");
    }

    @Test
    public void testUpdateProduceReportDetail_success() {
        // mock 数据
        ProduceReportDetailDO dbProduceReportDetail = randomPojo(ProduceReportDetailDO.class);
        produceReportDetailMapper.insert(dbProduceReportDetail);// @Sql: 先插入出一条存在的数据
        // 准备参数
        ProduceReportDetailSaveReqVO updateReqVO = randomPojo(ProduceReportDetailSaveReqVO.class, o -> {
            o.setId(dbProduceReportDetail.getId()); // 设置更新的 ID
        });

        // 调用
        produceReportDetailService.updateProduceReportDetail(updateReqVO);
        // 校验是否更新正确
        ProduceReportDetailDO produceReportDetail = produceReportDetailMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, produceReportDetail);
    }

    @Test
    public void testUpdateProduceReportDetail_notExists() {
        // 准备参数
        ProduceReportDetailSaveReqVO updateReqVO = randomPojo(ProduceReportDetailSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> produceReportDetailService.updateProduceReportDetail(updateReqVO), PRODUCE_REPORT_DETAIL_NOT_EXISTS);
    }

    @Test
    public void testDeleteProduceReportDetail_success() {
        // mock 数据
        ProduceReportDetailDO dbProduceReportDetail = randomPojo(ProduceReportDetailDO.class);
        produceReportDetailMapper.insert(dbProduceReportDetail);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbProduceReportDetail.getId();

        // 调用
        produceReportDetailService.deleteProduceReportDetail(id);
       // 校验数据不存在了
       assertNull(produceReportDetailMapper.selectById(id));
    }

    @Test
    public void testDeleteProduceReportDetail_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> produceReportDetailService.deleteProduceReportDetail(id), PRODUCE_REPORT_DETAIL_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetProduceReportDetailPage() {
       // mock 数据
       ProduceReportDetailDO dbProduceReportDetail = randomPojo(ProduceReportDetailDO.class, o -> { // 等会查询到
           o.setReportId(null);
           o.setPlanId(null);
           o.setProductId(null);
           o.setUserId(null);
           o.setOrgId(null);
           o.setQualityNumber(null);
           o.setWasteNumber(null);
           o.setTotalNumber(null);
           o.setQualityRate(null);
           o.setWasteReason(null);
           o.setReportTime(null);
           o.setTotalTime(null);
           o.setPackageNumber(null);
           o.setRemark(null);
           o.setIsEnable(null);
           o.setCreateTime(null);
       });
       produceReportDetailMapper.insert(dbProduceReportDetail);
       // 测试 reportId 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setReportId(null)));
       // 测试 planId 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setPlanId(null)));
       // 测试 productId 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setProductId(null)));
       // 测试 userId 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setUserId(null)));
       // 测试 orgId 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setOrgId(null)));
       // 测试 qualityNumber 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setQualityNumber(null)));
       // 测试 wasteNumber 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setWasteNumber(null)));
       // 测试 totalNumber 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setTotalNumber(null)));
       // 测试 qualityRate 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setQualityRate(null)));
       // 测试 wasteReason 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setWasteReason(null)));
       // 测试 reportTime 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setReportTime(null)));
       // 测试 totalTime 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setTotalTime(null)));
       // 测试 packageNumber 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setPackageNumber(null)));
       // 测试 remark 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setRemark(null)));
       // 测试 isEnable 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setIsEnable(null)));
       // 测试 createTime 不匹配
       produceReportDetailMapper.insert(cloneIgnoreId(dbProduceReportDetail, o -> o.setCreateTime(null)));
       // 准备参数
       ProduceReportDetailPageReqVO reqVO = new ProduceReportDetailPageReqVO();
       reqVO.setReportId(null);
       reqVO.setPlanId(null);
       reqVO.setProductId(null);
       reqVO.setUserId(null);
       reqVO.setOrgId(null);
       reqVO.setQualityNumber(null);
       reqVO.setWasteNumber(null);
       reqVO.setTotalNumber(null);
       reqVO.setQualityRate(null);
       reqVO.setWasteReason(null);
       reqVO.setReportTime(null);
       reqVO.setTotalTime(null);
       reqVO.setPackageNumber(null);
       reqVO.setRemark(null);
       reqVO.setIsEnable(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));

       // 调用
       PageResult<ProduceReportDetailRespVO> pageResult = produceReportDetailService.getProduceReportDetailPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbProduceReportDetail, pageResult.getList().get(0));
    }

}