package cn.newfeifan.mall.module.sale.service.shop;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.annotation.Resource;

import cn.newfeifan.mall.framework.test.core.ut.BaseDbUnitTest;

import cn.newfeifan.mall.module.sale.controller.admin.shop.vo.*;
import cn.newfeifan.mall.module.sale.dal.dataobject.shop.ShopDO;
import cn.newfeifan.mall.module.sale.dal.mysql.shop.ShopMapper;
import cn.newfeifan.mall.framework.common.pojo.PageResult;

import javax.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.newfeifan.mall.module.sale.enums.ErrorCodeConstants.*;
import static cn.newfeifan.mall.framework.test.core.util.AssertUtils.*;
import static cn.newfeifan.mall.framework.test.core.util.RandomUtils.*;
import static cn.newfeifan.mall.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.newfeifan.mall.framework.common.util.object.ObjectUtils.*;
import static cn.newfeifan.mall.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link ShopServiceImpl} 的单元测试类
 *
 * @author 非繁人
 */
@Import(ShopServiceImpl.class)
public class ShopServiceImplTest extends BaseDbUnitTest {

    @Resource
    private ShopServiceImpl shopService;

    @Resource
    private ShopMapper shopMapper;

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

        // 调用
        Long shopId = shopService.createShop(createReqVO);
        // 断言
        assertNotNull(shopId);
        // 校验记录的属性是否正确
        ShopDO shop = shopMapper.selectById(shopId);
        assertPojoEquals(createReqVO, shop, "id");
    }

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

        // 调用
        shopService.updateShop(updateReqVO);
        // 校验是否更新正确
        ShopDO shop = shopMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, shop);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> shopService.updateShop(updateReqVO), SHOP_NOT_EXISTS);
    }

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

        // 调用
        shopService.deleteShop(id);
       // 校验数据不存在了
       assertNull(shopMapper.selectById(id));
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> shopService.deleteShop(id), SHOP_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetShopPage() {
       // mock 数据
       ShopDO dbShop = randomPojo(ShopDO.class, o -> { // 等会查询到
           o.setName(null);
           o.setStatus(null);
           o.setMerchantId(null);
           o.setCreateTime(null);
           o.setLogoUrl(null);
       });
       shopMapper.insert(dbShop);
       // 测试 name 不匹配
       shopMapper.insert(cloneIgnoreId(dbShop, o -> o.setName(null)));
       // 测试 status 不匹配
       shopMapper.insert(cloneIgnoreId(dbShop, o -> o.setStatus(null)));
       // 测试 merchantId 不匹配
       shopMapper.insert(cloneIgnoreId(dbShop, o -> o.setMerchantId(null)));
       // 测试 createTime 不匹配
       shopMapper.insert(cloneIgnoreId(dbShop, o -> o.setCreateTime(null)));
       // 测试 logoUrl 不匹配
       shopMapper.insert(cloneIgnoreId(dbShop, o -> o.setLogoUrl(null)));
       // 准备参数
       ShopPageReqVO reqVO = new ShopPageReqVO();
       reqVO.setName(null);
       reqVO.setStatus(null);
       reqVO.setMerchantId(null);
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
       reqVO.setLogoUrl(null);

       // 调用
       PageResult<ShopDO> pageResult = shopService.getShopPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbShop, pageResult.getList().get(0));
    }

}