package com.cpop.oam.framework.strategy.cloud;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.base.enums.SourceType;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.bo.CloudUnionCallbackBo;
import com.cpop.system.business.bo.SyncBrandAndStoreBo;
import com.cpop.system.business.entity.Brand;
import com.cpop.system.business.entity.Store;
import com.cpop.system.business.entity.StoreLicense;
import com.cpop.system.business.service.BrandService;
import com.cpop.system.business.service.StoreLicenseService;
import com.cpop.system.business.service.StoreService;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowKey;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 *
 * @author DB
 * @version 1.0.0
 * @since 2024-01-16 14:37
 */
@Component
public class CloudSyncBrandOrStoreCallbackStrategy implements CloudCallbackStrategy{

    /**
     * 回调策略
     * @author DB
     * @since 2024/1/16
     * @param callback 回调参数
     */
    @Override
    public void callback(CloudUnionCallbackBo callback) {
        SyncBrandAndStoreBo.BrandInfo brandInfo = callback.getSyncBrandAndStore().getBrandInfo();
        //查询品牌
        Row brandExtend = DbChain.table("cp_j_brand_extend").where("brand_cloud_id = ?", brandInfo.getBrandCloudId()).one();
        Brand brand;
        LocalDateTime now = LocalDateTime.now();
        LoginUser loginUser = SecurityUtils.getInstance().getLoginUser();
        BrandService brandService = SpringUtils.getBean(BrandService.class);
        if (brandExtend.isEmpty()) {
            brand = new Brand();
            brand.setBrandName(brandInfo.getBrandName()).setSourceType(SourceType.JAMBOX.toString());
            brandService.save(brand);
            //保存拓展
            RowKey brandKey = RowKey.SNOW_FLAKE_ID;
            DbChain.table("cp_j_brand_extend")
                    .setId(brandKey)
                    .set("brand_id", brand.getId())
                    .set("brand_cloud_id", brandInfo.getBrandCloudId())
                    .set("create_time", now)
                    .set("update_time", now)
                    .set("create_user_id", loginUser == null ? "1" : loginUser.getUserId())
                    .set("update_user_id", loginUser == null ? "1" : loginUser.getUserId())
                    .save();
        }else {
            brand = brandService.getById(brandExtend.getString("brandId"));
        }
        SyncBrandAndStoreBo.StoreInfo storeInfo = callback.getSyncBrandAndStore().getStoreInfo();
        Store store = BeanUtils.mapToClass(storeInfo, Store.class);
        store.setBrandId(brand.getId()).setSourceType(SourceType.JAMBOX.toString());
        SpringUtils.getBean(StoreService.class).save(store);
        //拓展
        Row row = Row.ofKey(RowKey.SNOW_FLAKE_ID);
        row.set("store_cloud_id", storeInfo.getStoreCloudId());
        row.set("store_id", store.getId());
        row.set("create_time", now);
        row.set("update_time", now);
        row.set("create_user_id", loginUser == null ? "1" : loginUser.getUserId());
        row.set("update_user_id", loginUser == null ? "1" : loginUser.getUserId());
        Db.insert("cp_j_store_extend",row);
        //营业执照
        StoreLicense storeLicense = BeanUtils.mapToClass(storeInfo, StoreLicense.class);
        storeLicense.setStoreId(store.getId());
        SpringUtils.getBean(StoreLicenseService.class).save(storeLicense);
    }
}
