package cn.iocoder.yudao.module.chain.service.operatedetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.chain.controller.admin.operatedetail.vo.OperateDetailExportReqVO;
import cn.iocoder.yudao.module.chain.controller.admin.operatedetail.vo.OperateDetailPageReqVO;
import cn.iocoder.yudao.module.chain.dal.dataobject.operatedetail.OperateDetailDO;
import cn.iocoder.yudao.module.chain.dal.mysql.operatedetail.OperateDetailMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

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.chain.enums.ErrorCodeConstants.OPERATE_DETAIL_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* {@link OperateDetailServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(OperateDetailServiceImpl.class)
public class OperateDetailServiceImplTest extends BaseDbUnitTest {

    @Resource
    private OperateDetailServiceImpl operateDetailService;

    @Resource
    private OperateDetailMapper operateDetailMapper;

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

        // 调用
        operateDetailService.deleteOperateDetail(id);
       // 校验数据不存在了
       assertNull(operateDetailMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> operateDetailService.deleteOperateDetail(id), OPERATE_DETAIL_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOperateDetailPage() {
       // mock 数据
       OperateDetailDO dbOperateDetail = randomPojo(OperateDetailDO.class, o -> { // 等会查询到
           o.setChainOperateId(null);
           o.setTitle(null);
           o.setType(null);
           o.setContent(null);
           o.setOperateInfo(null);
           o.setAddTime(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       operateDetailMapper.insert(dbOperateDetail);
       // 测试 chainOperateId 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setChainOperateId(null)));
       // 测试 title 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setTitle(null)));
       // 测试 type 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setType(null)));
       // 测试 content 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setContent(null)));
       // 测试 operateInfo 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setOperateInfo(null)));
       // 测试 addTime 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setAddTime(null)));
       // 测试 belongBusinessId 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setCreateTime(null)));
       // 准备参数
       OperateDetailPageReqVO reqVO = new OperateDetailPageReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setChainOperateId(null);
       reqVO.setTitle(null);
       reqVO.setType(null);
       reqVO.setContent(null);
       reqVO.setOperateInfo(null);
       reqVO.setAddTime(dates);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       PageResult<OperateDetailDO> pageResult = operateDetailService.getOperateDetailPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbOperateDetail, pageResult.getList().get(0));
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetOperateDetailList() {
       // mock 数据
       OperateDetailDO dbOperateDetail = randomPojo(OperateDetailDO.class, o -> { // 等会查询到
           o.setChainOperateId(null);
           o.setTitle(null);
           o.setType(null);
           o.setContent(null);
           o.setOperateInfo(null);
           o.setAddTime(null);
           o.setBelongBusinessId(null);
           o.setBelongBusinessName(null);
           o.setCreateTime(null);
       });
       operateDetailMapper.insert(dbOperateDetail);
       // 测试 chainOperateId 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setChainOperateId(null)));
       // 测试 title 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setTitle(null)));
       // 测试 type 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setType(null)));
       // 测试 content 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setContent(null)));
       // 测试 operateInfo 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setOperateInfo(null)));
       // 测试 addTime 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setAddTime(null)));
       // 测试 belongBusinessId 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setBelongBusinessId(null)));
       // 测试 belongBusinessName 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setBelongBusinessName(null)));
       // 测试 createTime 不匹配
       operateDetailMapper.insert(cloneIgnoreId(dbOperateDetail, o -> o.setCreateTime(null)));
       // 准备参数
       OperateDetailExportReqVO reqVO = new OperateDetailExportReqVO();
        LocalDateTime[] localDateTimes = LocalDateTimeUtils.buildBetweenTime(2023, 2, 1, 2023, 2, 28);
        Date[] dates = new Date[localDateTimes.length];
        for(int i=0; i<localDateTimes.length; i++) {
            dates[i] = Date.from(localDateTimes[i].atZone(ZoneId.systemDefault()).toInstant());
        }
       reqVO.setChainOperateId(null);
       reqVO.setTitle(null);
       reqVO.setType(null);
       reqVO.setContent(null);
       reqVO.setOperateInfo(null);
       reqVO.setAddTime(dates);
       reqVO.setBelongBusinessId(null);
       reqVO.setBelongBusinessName(null);
       reqVO.setCreateTime(dates);

       // 调用
       List<OperateDetailDO> list = operateDetailService.getOperateDetailList(reqVO);
       // 断言
       assertEquals(1, list.size());
       assertPojoEquals(dbOperateDetail, list.get(0));
    }

}
