package com.gree.mall.manager.logic.admin;

import com.gree.mall.manager.constant.SysDictConstant;
import com.gree.mall.manager.logic.common.SysDictCompanyLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@Service
public class AdminCompanyWechatOtherLogic {


    private final AdminCompanyWechatOtherService adminCompanyWechatOtherService;
    private final CommonLogic commonLogic;
    private final SysDictCompanyLogic sysDictCompanyLogic;
    private final GoodsCategoryService goodsCategoryService;
    private final GoodsService goodsService;
    private final SysDictCompanyService sysDictCompanyService;
    private final BrandService brandService;
    private final GoodsNewsCategoryService goodsNewsCategoryService;
    private final GoodsNewsCategoryGoodsService goodsNewsCategoryGoodsService;
    private final OrderSmallTypeService orderSmallTypeService;
    private final CarouselMapService carouselMapService;
    /**
     * 获取小程序配置信息
     */
    public AdminCompanyWechatOther getOne(HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        String loginCompanyWechatId = adminUser.getCompanyWechatId();
        if (StringUtils.isBlank(loginCompanyWechatId)) {
            return null;
        }

        return adminCompanyWechatOtherService.lambdaQuery()
                .eq(AdminCompanyWechatOther::getCompanyWechatId, loginCompanyWechatId)
                .one();
    }

    /**
     * 初始化其他配置
     */
    @Transactional
    public void init(String companyId, String companyName) {
        adminCompanyWechatOtherService.lambdaUpdate()
                .eq(AdminCompanyWechatOther::getCompanyWechatId, companyId)
                .remove();

        AdminCompanyWechatOther other = new AdminCompanyWechatOther();
        other.setCompanyWechatId(companyId);
        other.setCompanyName(companyName);
        other.insert();
        log.info("初始化完成1");
        //初始化必要的渠道
        SysDictCompany orderChannelDict = sysDictCompanyLogic.dictByTypeOfValue(SysDictConstant.ORDER_CHANNEL, "商城",companyId);
        if(orderChannelDict == null){
            SysDictCompany sysDictCompany = new SysDictCompany();
            sysDictCompany.setDictType(SysDictConstant.ORDER_CHANNEL);
            sysDictCompany.setDictValue("商城");
            sysDictCompany.setCompanyWechatId(companyId);
            sysDictCompany.setCompanyName(companyName);
            sysDictCompanyLogic.saveOpenDict(sysDictCompany);
        }
        log.info("初始化完成2");
        //初始化轮播图
        List<CarouselMap> carouselMaps = carouselMapService.lambdaQuery().eq(CarouselMap::getCompanyWechatId, "1").list();
        for(CarouselMap carouselMap : carouselMaps){
            carouselMap.setId(null);
            carouselMap.setCompanyWechatId(companyId);
            carouselMap.setCompanyName(companyName);
            carouselMap.setGoodsId(companyId + carouselMap.getGoodsId());
        }
        if(CollectionUtils.isNotEmpty(carouselMaps)){
            carouselMapService.saveBatch(carouselMaps);
        }
        log.info("初始化完成3");
        //初始化工单类型
        List<OrderSmallType> orderSmallTypes = orderSmallTypeService.lambdaQuery().eq(OrderSmallType::getCompanyWechatId, "1").list();
        for(OrderSmallType orderSmallType : orderSmallTypes){
            orderSmallType.setId(companyId + orderSmallType.getId());
            orderSmallType.setCompanyWechatId(companyId);
            orderSmallType.setCompanyWechatName(companyName);
        }
        if(CollectionUtils.isNotEmpty(orderSmallTypes)) {
            orderSmallTypeService.saveBatch(orderSmallTypes);
        }
        log.info("初始化完成4");
        //初始化品牌
        List<Brand> brandList = brandService.lambdaQuery().eq(Brand::getCompanyWechatId, "1").list();
        for(Brand brand:brandList){
            brand.setId( companyId + brand.getId());
            brand.setCompanyWechatId(companyId);
            brand.setCompanyWechatName(companyName);
        }
        if(CollectionUtils.isNotEmpty(brandList)) {
            brandService.saveBatch(brandList);
        }
        log.info("初始化完成5");
        //初始化产品分类
        List<GoodsCategory> goodsCategorys = goodsCategoryService.lambdaQuery().eq(GoodsCategory::getCompanyWechatId, "1").list();
        for (GoodsCategory goodsCategory : goodsCategorys) {
            goodsCategory.setCategoryId( companyId + goodsCategory.getCategoryId());
            goodsCategory.setParentId(companyId + goodsCategory.getParentId());
            goodsCategory.setDemandTypeId(null);
            goodsCategory.setDemandSmallTypeId(null);
            goodsCategory.setCompanyWechatId(companyId);
            goodsCategory.setCompanyName(companyName);
        }
        if(CollectionUtils.isNotEmpty(goodsCategorys)) {
            goodsCategoryService.saveBatch(goodsCategorys);
        }
        log.info("初始化完成6");
        //初始化商品
//        List<Goods> goodsList = goodsService.lambdaQuery().eq(Goods::getCompanyWechatId, "1").list();
//        for(Goods goods : goodsList){
//            goods.setGoodsId(companyId + goods.getGoodsId());
//            goods.setCategoryId(companyId + goods.getCategoryId());
//            goods.setBrandId(companyId + goods.getBrandId());
//            goods.setFreightTemplateId("");
//            goods.setMarketingId(null);
//            goods.setCompanyWechatId(companyId);
//            goods.setCompanyName(companyName);
//        }
//        if(CollectionUtils.isNotEmpty(goodsList)) {
//            goodsService.saveBatch(goodsList);
//        }
        //初始化字典
        List<SysDictCompany> sysDictCompanies = sysDictCompanyService.lambdaQuery().eq(SysDictCompany::getCompanyWechatId, "1").list();
        for (SysDictCompany sysDictCompany : sysDictCompanies) {
            sysDictCompany.setSysDictId(null);
            sysDictCompany.setCompanyWechatId(companyId);
            sysDictCompany.setCompanyName(companyName);
        }
        if(CollectionUtils.isNotEmpty(sysDictCompanies)){
            sysDictCompanyService.saveBatch(sysDictCompanies);
        }
        log.info("初始化完成7");
        //新维度分类
        List<GoodsNewsCategory> goodsNewsCategories = goodsNewsCategoryService.lambdaQuery().eq(GoodsNewsCategory::getCompanyWechatId, "1").list();
        for (GoodsNewsCategory goodsNewsCategory : goodsNewsCategories) {
            goodsNewsCategory.setGoodsNewsCategoryId(companyId + goodsNewsCategory.getGoodsNewsCategoryId());
            goodsNewsCategory.setCompanyWechatId(companyId);
            goodsNewsCategory.setCompanyName(companyName);
        }
        if(CollectionUtils.isNotEmpty(goodsNewsCategories)){
            goodsNewsCategoryService.saveBatch(goodsNewsCategories);
        }
        log.info("初始化完成8");
        //新维度分类商品
        List<GoodsNewsCategoryGoods> goodsNewsCategoryGoods = goodsNewsCategoryGoodsService.lambdaQuery().eq(GoodsNewsCategoryGoods::getCompanyWechatId, "1").list();
        for (GoodsNewsCategoryGoods bean : goodsNewsCategoryGoods) {
            bean.setGoodsNewsCategoryGoodsId(companyId + bean.getGoodsNewsCategoryGoodsId());
            bean.setObjId(companyId + bean.getObjId());
            bean.setGoodsId(companyId + bean.getGoodsId());
            bean.setCompanyWechatId(companyId);
            bean.setCompanyName(companyName);
        }
        if(CollectionUtils.isNotEmpty(goodsNewsCategoryGoods)){
            goodsNewsCategoryGoodsService.saveBatch(goodsNewsCategoryGoods);
        }
        log.info("初始化完成9");
    }

    /**
     * 更新其他配置
     */
    public void update(AdminCompanyWechatOther other,HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        adminCompanyWechatOtherService.lambdaUpdate()
                .eq(AdminCompanyWechatOther::getCompanyWechatId, adminUser.getCompanyWechatId())
                .remove();
        other.setCompanyWechatId(adminUser.getCompanyWechatId());
        other.setCompanyName(adminUser.getCompanyName());
        other.setCreateTime(new Date());
        other.insert();
    }


}
