package com.izouma.dingdong.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.domain.merchant.GoodsSpecification;
import com.izouma.dingdong.domain.merchant.MerchantClassification;
import com.izouma.dingdong.dto.AppraisalMerDTO;
import com.izouma.dingdong.enums.GoodType;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
import com.izouma.dingdong.service.merchant.GoodsService;
import com.izouma.dingdong.service.merchant.GoodsSpecificationService;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.merchant.GoodsController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class GoodsServiceTest {

    @Autowired
    private AppraisalService appraisalService;

    @Autowired
    private GoodsRepo goodsRepo;

    @Autowired
    private GoodsSpecificationRepo goodsSpecificationRepo;

    @Autowired
    private GoodsSpecificationService goodsSpecificationService;

    @Autowired
    private GoodsController goodsController;

    @Test
    public void testApp() {
        System.out.println(appraisalService.goodsAppraisals(214L));
    }

    @Test
    public void showGoods() {
        HashMap<String, Object> map = new HashMap<>();
        Goods goods = goodsRepo.findById(214L).orElseThrow(new BusinessException("无记录"));
        map.put("goods", goods);

        List<AppraisalMerDTO> appraisals = appraisalService.goodsAppraisals(214L);
        map.put("appraisal", appraisals);

        List<GoodsSpecification> specifications = goodsSpecificationRepo.findAllByGoodsId(214L);
        map.put("specification", specifications);

        System.out.println(map);
    }

    @Test
    public void goodsSpec() {
        GoodsSpecification spec1 = GoodsSpecification.builder()
                .multiple(false)
                .name("甜度")
                .goodsId(1258L)
                .build();
        spec1.setId(-1L);
        GoodsSpecification spec2 = GoodsSpecification.builder()
                .amount(BigDecimal.ZERO)
                .name("全糖")
                .goodsId(1258L)
                .parent(-1L)
                .build();
        spec2.setId(-2L);
/*        GoodsSpecification spec3 = GoodsSpecification.builder()
                .amount(BigDecimal.ZERO)
                .name("少冰")
                .goodsId(121L)
                .parent(-1L)
                .build();
        spec3.setId(-3L);*/

/*        GoodsSpecification spec4 = GoodsSpecification.builder()
                .multiple(false)
                .name("酸度")
                .goodsId(121L)
                .build();
        spec4.setId(-4L);*/
        GoodsSpecification spec5 = GoodsSpecification.builder()
                .amount(BigDecimal.ONE)
                .name("半糖")
                .goodsId(1258L)
                .parent(-1L)
                .build();
        spec5.setId(-3L);
/*         GoodsSpecification spec6 = GoodsSpecification.builder()
                .amount(BigDecimal.ZERO)
                .name("半糖")
                .goodsId(121L)
                .parent(-4L)
                .build();
        spec6.setId(-6L);
        GoodsSpecification spec7 = GoodsSpecification.builder()
                .amount(BigDecimal.ZERO)
                .name("全糖")
                .goodsId(121L)
                .parent(-4L)
                .build();
        spec7.setId(-7L);*/


        List<GoodsSpecification> specifications = new ArrayList<>(3);
        specifications.add(spec1);
        specifications.add(spec2);
//        specifications.add(spec3);
//        specifications.add(spec4);
        specifications.add(spec5);
//        specifications.add(spec6);
//        specifications.add(spec7);
        goodsSpecificationService.saveAll(specifications);
    }

    @Test
    public void t() {
        List<GoodsSpecification> spec = new ArrayList<>();
        GoodsSpecification spec5 = GoodsSpecification.builder()
                .amount(BigDecimal.ZERO)
                .name("少糖")
                .goodsId(1258L)
                .build();
        //spec5.setId(1513L);
        spec.add(spec5);
        GoodsSpecification spec6 = GoodsSpecification.builder()
                // .amount(BigDecimal.ZERO)
                .name("糖量")
                //.goodsId(1258L)
                //.multiple(false)
                // .parent(-4L)
                .children(spec)
                .build();
        spec6.setId(1512L);

        List<GoodsSpecification> spec1 = new ArrayList<>();
        spec1.add(spec6);
        String str = JSONObject.toJSONString(spec1);
        System.out.println(str);
        List<GoodsSpecification> specifications = JSONObject.parseArray(str, GoodsSpecification.class);
        System.out.println(specifications);

        specifications.forEach(s -> {
            GoodsSpecification save;
            if (s.getId() != null) {
                GoodsSpecification specification = goodsSpecificationRepo.findById(s.getId()).orElseThrow(new BusinessException("无记录"));
                ObjUtils.merge(specification, s);
                save = goodsSpecificationRepo.save(specification);
            } else {
                save = goodsSpecificationRepo.save(s);
            }

            if (CollUtil.isNotEmpty(s.getChildren())) {
                s.getChildren().forEach(c -> {
                    c.setParent(save.getId());
                    if (c.getId() != null) {
                        GoodsSpecification specification = goodsSpecificationRepo.findById(s.getId()).orElseThrow(new BusinessException("无记录"));
                        ObjUtils.merge(specification, s);
                        goodsSpecificationRepo.save(specification);
                    } else {
                        goodsSpecificationRepo.save(c);
                    }
                });
            }
        });

    }


    @Test
    public void testSave(){
        Goods goods = Goods.builder()
                //.recommend(true)
                .signboard(true)
                .build();
        goods.setId(310L);

        Goods goods1 = Goods.builder()
                .recommend(true)
                .build();
        goods1.setId(310L);

        goodsController.save(goods);
        //goodsController.save(goods1);
    }

    @Test
    public void showAllTest(){
        System.out.println(goodsController.showAll(GoodType.SIGNBOARD_FOOD, 118.734661, 31.981746));
    }
}
