package com.org.hktshop.base;

import com.org.hktshop.Interface.admin.*;
import com.org.hktshop.Interface.advert.IHktAdCategoryService;
import com.org.hktshop.Interface.advert.IHktAdDetailService;
import com.org.hktshop.Interface.brand.IHktBrandCategoryService;
import com.org.hktshop.Interface.brand.IHktBrandService;
import com.org.hktshop.Interface.product.*;
import com.org.hktshop.Interface.system.*;
import com.org.hktshop.entity.product.HktGoodsActivity;
import com.org.hktshop.service.hotking.BrandService;
import com.org.hktshop.service.hotking.SampleGoodsAttrQueryService;
import com.org.hktshop.service.user.HktUserAddressService;
import com.org.hktshop.entity.system.HktFriendLink;
import com.org.hktshop.entity.system.HktSysHelpes;
import com.org.hktshop.entity.system.HktSysHelpsCategory;
import com.org.hktshop.service.user.HktUserService;
import com.org.hktshop.utils.thesaurus.ThesaurusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;


@Component
public class ServerListener implements CommandLineRunner {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    IHktAdminResourceService resourceService;

    @Autowired
    IHktAdminRoleService roleService;


    @Autowired
    IHktAdminService adminService;

    @Autowired
    IHktSysRemindService sysRemindService;

    @Autowired
    IHktSysRemindTempleService templeService;

    @Autowired
    IHktBrandCategoryService brandCategoryService;

    @Autowired
    IHktBrandService brandService;

    @Autowired
    IHktAdminLogService logService;

    @Autowired
    IHktGoodsSeoDescService hktGoodsSeoDescService;

    @Autowired
    IHktAdDetailService adDetailService;

    @Autowired
    IHktAdCategoryService adCategoryService;

    @Autowired
    IHktNewsCategoryService newsCategoryService;

    @Autowired
    HktUserAddressService userAddressService;

    @Autowired
    IHktGoodsDeliveryService goodsDeliveryService;

    @Autowired
    IHktGoodsCategoryService goodsCategoryService;

    @Autowired
    IHktFriendLinkService friendLinkService;

    @Autowired
    IHktSysHelpsCategoryService sysHelpsCategoryService;

    @Autowired
    IHktHelperCenterService helperCenterService;

    @Autowired
    IHktNewsArticleService newsArticleService;

    @Autowired
    ThesaurusUtils thesaurusUtils;

    @Autowired
    IHktGoodsActivityService goodsActivityService;

    @Autowired
    IHktCountRegulationService hktCountRegulationService;

    @Autowired
    IHktPriceRegulationService hktPriceRegulationService;

    @Autowired
    IHktGoodsService goodsService;

    @Autowired
    private BrandService brandServiceimpl;

    @Autowired
    private SampleGoodsAttrQueryService sampleGoodsAttrQueryService;

    @Autowired
    private HktUserService hktUserService;


    @Override
    public void run(String... args) throws Exception {
        System.out.println("初始化: 开始");

        MongoHelper.InitMongoDb(mongoTemplate);
        resourceService.queryList(true);
        roleService.queryLists(true);

        adminService.queryList(true);

//        brandCategoryService.queryLists(true);
        brandService.queryLists(true);

        sysRemindService.queryLists();
        templeService.queryList();

//        priceTemplateService.InitLoad();
        thesaurusUtils.inputStringToThesaurus();
        thesaurusUtils.stopStringToThesaurus();
        hktGoodsSeoDescService.initDate();
        adDetailService.InitData();
        adCategoryService.InitData();
        adCategoryService.InitData();
        newsCategoryService.InitData();
        goodsDeliveryService.InitDate();
        goodsCategoryService.InitData(true);
        newsArticleService.InitData();
        friendLinkService.queryLists(new HktFriendLink());
        sysHelpsCategoryService.queryLists(new HktSysHelpsCategory());
        helperCenterService.queryLists(new HktSysHelpes());
        goodsActivityService.queryLists(new HktGoodsActivity());
        hktPriceRegulationService.initData();
        hktCountRegulationService.initDate();
        brandServiceimpl.init();
        sampleGoodsAttrQueryService.init();
        brandServiceimpl.initBrandGoodsAttr();

        hktUserService.initUserDate();

        System.out.println("初始化: 成功");
        createThread();
    }

    private void createThread() {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        //每10秒执行一次
                        Thread.sleep(10000);
                        logService.saveLogEvent();

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        thread.setPriority(6);
        thread.start();
    }
}
